当前位置: 代码网 > it编程>编程语言>Java > Java如何读取csv文件并将数据放入对象中

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

2025年04月24日 Java 我要评论
读取csv文件并封装数据为对象例如图中的一个 .csv 文件,需要读取数据封装对象进行数据持久化。public static void readcsv(string readpath, arrayli

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

例如

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

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对象来获取到需要封装对象的类名,进一步实现方法一般化:

public class readcsv {
    public static void readcsv(inputstream inputstream, arraylist<object> list, class cls){
        bufferedreader reader = null;
        try {
            reader = new bufferedreader(new inputstreamreader(inputstream));
            boolean flag = false;
            arraylist<string> 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<string,float> 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();
                }
        }
    }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com