阅读和操作CSV文件

时间:2014-03-31 08:37:30

标签: java 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();
    }

}

我想按日期和身份计算平均值?任何帮助

2 个答案:

答案 0 :(得分:2)

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 :(得分: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;
        }
    }
}