如何轻松将CSV文件处理为List< MyClass> [英] How to easily process CSV file to List<MyClass>

查看:122
本文介绍了如何轻松将CSV文件处理为List< MyClass>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用了许多CSV文件,我必须阅读这些文件并根据它们建立一个列表.我想找到一种简单的方法来做到这一点.您知道不使用任何数量的配置文件等简单框架吗?

In my application I use a lot of CSV files which I have to read and build a lists based on them. I'd like to discover an easy way to do this. Do you know any easy framework which does it without using number of config files etc?

例如,我有一个Person类:

For instance, I have got a class Person:

public class Person {
    String name;
    String surname;

    double shoeSize;
    boolean sex; // true: male, false:female

    public Person() {
    }

    public String getName() {
            return name;
    }

    public void setName(String name) {
            this.name = name;
    }

    public String getSurname() {
            return surname;
    }

    public void setSurname(String surname) {
            this.surname = surname;
    }

    public double getShoeSize() {
            return shoeSize;
    }

    public void setShoeSize(double shoeSize) {
            this.shoeSize = shoeSize;
    }

    public boolean isSe) {
            return sex;
    }

    public void setSeboolean sex) {
            this.sex = sex;
    }

}

为此课程,我准备了CSV文件:

For this class, I have prepared CSV file:

name,surname,shoesize,sex
Tom,Tommy,32,true
Anna,Anny,27,false

我如何轻松地做到这一点?

How can I do it easily?

推荐答案

有很多用Java编写的好的框架来解析CSV文件并形成对象列表. OpenCSV JSefa & jCSV 仅举几例.

There are lot of good frameworks written in Java to parse a CSV file and form a List of Objects. OpenCSV, JSefa & jCSV are to name a few of them.

根据您的要求,我相信 jCSV 最合适.下面是jCSV的示例代码,您可以轻松使用它们.

For your requirement, I believe jCSV suits the best. Below is the sample code from jCSV which you can make use of easily.

Reader reader = new FileReader("persons.csv");

CSVReader<Person> csvPersonReader = ...;

// read all entries at once
List<Person> persons = csvPersonReader.readAll();

// read each entry individually
Iterator<Person> it = csvPersonReader.iterator();
while (it.hasNext()) {
  Person p = it.next();
  // ...
}

此外,解析CSV文件并将其转换为列表没什么大不了的,它可以不使用任何框架来实现,如下所示.

Moreover, parsing a CSV file and converting it to a List isn't a big deal and it can be achieved without using any framework, as shown below.

br = new BufferedReader(new FileReader(csvFileToRead));  
List<Person> personList = new ArrayList<>();
while ((line = br.readLine()) != null) {  
       // split on comma(',')  
       String[] personCsv = line.split(splitBy);  

       // create car object to store values  
       Person personObj = new Person();  

       // add values from csv to car object  
       personObj.setName(personCsv[0]);  
       personObj.setSurname(personCsv[1]);  
       personObj.setShoeSize(personCsv[2]);  
       personObj.setGender(personCsv[3]); 

       // adding car objects to a list  
       personList.add(personObj);         
} 

如果在实际情况下CSV列到bean对象的映射是复杂的,重复的或大的,则可以使用 DozerBeanMapper .

If the mapping of CSV columns to bean object is complex, repetitive or large in real case scenario, then it can be done easily by using DozerBeanMapper.

希望这会对您有所帮助.

Hope this will help you.

Shishir

这篇关于如何轻松将CSV文件处理为List&lt; MyClass&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆