IT俱乐部 Java Java如何读取csv文件并将数据放入对象中

Java如何读取csv文件并将数据放入对象中

读取csv文件并封装数据为对象

例如

图中的一个 .csv 文件,需要读取数据封装对象进行数据持久化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public static void readCSV(String readpath, ArrayList list)
{
    File inFile = new File(readpath);
    try
    {
        BufferedReader reader = new BufferedReader(new FileReader(inFile));
        boolean sign = false;       //用来跳过第一行的名称
        while(reader.ready())
        {
            String line = reader.readLine();
            StringTokenizer st = new StringTokenizer(line, ",");
            int date, time, num_transaction, response_time;
            double sucRate;
 
            if (st.hasMoreTokens() && sign)
            {
                date = Integer.valueOf(st.nextToken().trim());
                time = Integer.valueOf(st.nextToken().trim());
                num_transaction = Integer.valueOf(st.nextToken().trim());
                sucRate = Double.valueOf(st.nextToken().trim());
                response_time = Integer.valueOf(st.nextToken().trim());
 
                Sample sample = new Sample(date, time, num_transaction, sucRate, response_time);
                list.add(sample);
            }
            else
            {
                sign = true;
            }
        }
        reader.close();
 
    }
    catch (FileNotFoundException e)
    {
 
        e.printStackTrace();
    }
    catch (IOException e)
    {
 
        e.printStackTrace();
    }
}

当有多个对象时

可以传入一个 Class对象来获取到需要封装对象的类名,进一步实现方法一般化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class ReadCSV {
    public static void readCSV(InputStream inputStream, ArrayList<object data-origwidth="" data-origheight="" style="width: 1264px;"> list, Class cls){
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream));
            boolean flag = false;
            ArrayList headerList = new ArrayList();
            while(reader.ready()){
                String line = reader.readLine();
                StringTokenizer st = new StringTokenizer(line,",");
                        //处理当前行数据
                if(st.hasMoreTokens() && flag){
                    String typeName = cls.getSimpleName();
                    //如果文件中存储的是 EnergyProvince类信息
                    if(typeName.equals("EnergyProvice")){
                        String provinceName = st.nextToken();
//                        Float year2019 = Float.valueOf(st.nextToken());
//                        Float year2018 = Float.valueOf(st.nextToken());
                        Float year2017 = Float.valueOf(st.nextToken());
                        Float year2016 = Float.valueOf(st.nextToken());
                        Float year2015 = Float.valueOf(st.nextToken());
                        Float year2014 = Float.valueOf(st.nextToken());
                        Float year2013 = Float.valueOf(st.nextToken());
                        Float year2012 = Float.valueOf(st.nextToken());
                        Float year2011 = Float.valueOf(st.nextToken());
                        Map dataMap = new HashMap();
//                        dataMap.put(headerList.get(1),year2019);
//                        dataMap.put(headerList.get(2),year2018);
                        dataMap.put(headerList.get(1),year2017);
                        dataMap.put(headerList.get(2),year2016);
                        dataMap.put(headerList.get(3),year2015);
                        dataMap.put(headerList.get(4),year2014);
                        dataMap.put(headerList.get(5),year2013);
                        dataMap.put(headerList.get(6),year2012);
                        dataMap.put(headerList.get(7),year2011);
                        list.add(new EnergyProvice(provinceName,dataMap));
                    }
                }
                else{   //添加表头到 List 集合
                    while(st.hasMoreTokens()){
                        headerList.add(st.nextToken());
                    }
                    flag=true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(reader!=null)
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }</object>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/java/15261.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部