2014-03-31 106 views
0

我想讀一個CSV文件,這樣的:閱讀和操縱CSV文件

DATE = 2014年3月8日; ID = 01; AVG = 10

public void readInputStream(InputStream in) throws IOException { 
    BufferedReader br = null; 
    String line = ""; 
    String cvsSplitBy = ";"; 

    try { 


     br = new BufferedReader(new InputStreamReader(in, "UTF-8")); 
     while ((line = br.readLine()) != null) { 
      String[] date= line.split(cvsSplitBy); 
      if (line.contains("DATE")){ 
      System.out.println(date[0]); 
     }} 
     br.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

我想按日期和ID來計算平均值?任何幫助

+0

您需要提供更多信息。什麼是問題,你的問題是什麼? –

+0

我想獲取日期值'2014-03-08' – mariposa

+0

你會得到什麼問題?提供的代碼是否有效?如果不是什麼結果?你真的需要提供更多信息。 –

回答

1
import java.io.*; 
import java.util.*; 

public class CsvSum{ 

static Map<String, Integer> map = new HashMap<String, Integer>(); 

public static void main(String args[]) throws Exception{ 
    File file = new File("test.csv"); 
    Scanner scanner = new Scanner(file); 
    while(scanner.hasNext()){ 
     String line = scanner.next(); 
     String[] columns = line.split(";"); 

     String date = columns[0].replace("DATE=",""); 
     String id = columns[1].replace("ID=",""); 
     int avg = Integer.parseInt(columns[2].replace("AVG=","")); 

     String key = date + "_" +id; 
     if(!map.containsKey(key)){ 
      map.put(key,avg); 
     }else{ 
      Integer existing = map.get(key); 
      map.put(key, existing + avg); 
     } 
    } 

    System.out.println(map); 
} 
} 
+1

你的解決方案比我的更簡單,就像它一樣。 – Marboni

+0

不錯,謝謝 – mariposa

0
import java.io.*; 
import java.util.HashMap; 
import java.util.Map; 
public class Some { 
    public static final String FIELDS_DELIMITER = ";"; 
    public static final String KEY_VALUE_DELIMITER = "="; 

    public static void main(String[] args) throws IOException { 
     readInputStream(new FileInputStream("test.csv")); 
    } 

    public static void readInputStream(InputStream in) throws IOException { 
     BufferedReader br; 
     String line; 
     Map<GroupKey, Integer> groupedValues = new HashMap<GroupKey, Integer>(); 

     try { 
      br = new BufferedReader(new InputStreamReader(in, "UTF-8")); 
      while ((line = br.readLine()) != null) { 
       String[] fields = line.split(FIELDS_DELIMITER); 

       HashMap<String, String> fieldsValues = new HashMap<String, String>(); 
       for (String field : fields) { 
        String[] keyAndValue = field.split(KEY_VALUE_DELIMITER); 
        fieldsValues.put(keyAndValue[0], keyAndValue[1]); 
       } 
       GroupKey gk = new GroupKey(); 
       gk.id = fieldsValues.get("ID"); 
       gk.date = fieldsValues.get("DATE"); 

       if (!groupedValues.containsKey(gk)) { 
        groupedValues.put(gk, 0); 
       } 
       Integer fieldAvg = Integer.valueOf(fieldsValues.get("AVG")); 
       groupedValues.put(gk, groupedValues.get(gk) + fieldAvg); 
      } 
      br.close(); 

      for (Map.Entry<GroupKey, Integer> groupKeyAndSum : groupedValues.entrySet()) { 
       GroupKey gk = groupKeyAndSum.getKey(); 
       Integer sum = groupKeyAndSum.getValue(); 
       System.out.println("ID " + gk.id + ", date " + gk.date + ": " + sum); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static class GroupKey { 
     private String date; 
     private String id; 

     @Override 
     public boolean equals(Object o) { 
      if (this == o) return true; 
      if (o == null || getClass() != o.getClass()) return false; 

      GroupKey groupKey = (GroupKey) o; 

      return !(date != null ? !date.equals(groupKey.date) : groupKey.date != null) && 
        !(id != null ? !id.equals(groupKey.id) : groupKey.id != null); 

     } 

     @Override 
     public int hashCode() { 
      int result = date != null ? date.hashCode() : 0; 
      result = 31 * result + (id != null ? id.hashCode() : 0); 
      return result; 
     } 
    } 
} 
+0

非常感謝:) – mariposa

+0

我收到java.lang.NumberFormatException:null,在整數fieldAvg = Integer.valueOf(fieldsValues.get(「AVG」)); – mariposa

+0

@mariposa,你能否提供你閱讀的CSV文件? – Marboni