简体   繁体   English

将CSV文件读入java对象

[英]Read CSV file into java Object

I'm trying to read the CSV file content into a java object.我正在尝试将 CSV 文件内容读入 java 对象。 I found online resources that explains the two ways to read CSV ie., BufferReader/OpenCSV.我找到了解释读取 CSV 的两种方法的在线资源,即 BufferReader/OpenCSV。 But most of them are about reading row wise(I mean all the row data as one), the problem with my implementation is my CSV has the data in column wise like below:但他们中的大多数是关于阅读行明智(我的意思是所有的行数据为一个),我的实现问题是我的 CSV 有列明智的数据,如下所示:

UPDATE:更新:

Name,A,B,C,D
JoinDate,1/1/2019,1/1/2018,06/01/2018,1/1/2019
Math_Marks,80,50,65,55
Social_Marks,80,50,86,95
Science_Marks,70,50,59,85
FirstLang_Marks,60,50,98,45
SecondLang_Marks,90,97,50

As you see the marks value are not mandatory, in above file person D has no marks listed for "SecondLang_Marks"如您所见,标记值不是强制性的,在上面的文件中,D 没有为“SecondLang_Marks”列出标记

and my class object is below:我的类对象如下:

public class StudentVO {

private String name;
private Calendar joinDate;
private int math_Marks;
private int social_Marks;
private int science_Marks;
private int FirstLang_Marks;
private int secondLang_Marks;

// All get and set methods for class variables    
}

can anyone please help me to read the above csv vertically based on vertical headers and load the values into class object.任何人都可以帮助我根据垂直标题垂直读取上面的csv并将值加载到类对象中。

If possible can you please give both examples using BufferReader and OpenCSV.如果可能,请您使用 BufferReader 和 OpenCSV 给出两个示例。

Thanks谢谢

As far as i know, you can only read the data from file in row wise, there is not any mechanism to read the file vertically.据我所知,您只能按行从文件中读取数据,没有任何垂直读取文件的机制。 But i have a solution for it, read the whole file, You can create a array of students and initialize it with default constructor and then set the data while reading downwards row wise.但是我有一个解决方案,读取整个文件,您可以创建一个学生数组并使用默认构造函数初始化它,然后在向下读取行时设置数据。

try {
        BufferedReader reader = new BufferedReader(new FileReader("file.csv"));

        // Reading first line..
        String[] names = reader.readLine().split(",");
        // Execpt 'names' there are total 4 students, A,B,C,D.
        int totalStudents = names.length - 1;
        StudentVO[] array = new StudentVO[totalStudents];
        // Initialize all students with default constructor.
        for(int i = 0; i < array.length; i++) {
            array[i] = new StudentVO();
        }

        //////////////
        // Start reading other data and setting up on objects..
        // Line 2..
        String[] joinDates = reader.readLine().split(",");
        // i = 0 gives us the string 'joinDates' which is in the first column.
        // so we have to skip it and start it from i = 1
        for(int i = 1; i < joinDates.length; i++) {
            // setting the objects data..
            array[i - 1].setJoinDate(joinDates[i]); 
        }

        // And keep on doing this until SecondLang_Marks..

        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

This is the best way to do it for this solution according to the me.根据我的说法,这是针对此解决方案执行此操作的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM