读取一个csv文件并将其转换为java对象,每次n行

f5emj3cl  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(748)

我正在尝试读取一个csv文件并将其Map到javapojo,使用 OpenCSV . 下面是我的示例csv类,以及 Student.java 是我的pojo文件
学生数据.csv

name, rollno, department, result, cgpa
amar, 42, cse, pass, 8.6
rohini, 21, ece, fail, 3.2
aman, 23, cse, pass, 8.9
rahul, 45, ee, fail, 4.6
pratik, 65, cse, pass, 7.2
raunak, 23, me, pass, 9.1
suvam, 68, me, pass, 8.2

readfile()函数

public List<Student> readFile() 
    { 

        // Hashmap to map CSV data to  
        // Bean attributes. 
        Map<String, String> mapping = new 
                      HashMap<String, String>(); 
        mapping.put("name", "Name"); 
        mapping.put("rollno", "RollNo"); 
        mapping.put("department", "Department"); 
        mapping.put("result", "Result"); 
        mapping.put("cgpa", "Pointer"); 

        // HeaderColumnNameTranslateMappingStrategy 
        // for Student class 
        HeaderColumnNameTranslateMappingStrategy<Student> strategy = 
             new HeaderColumnNameTranslateMappingStrategy<Student>(); 
        strategy.setType(Student.class); 
        strategy.setColumnMapping(mapping); 

        // Create castobaen and csvreader object 
        CSVReader csvReader = null; 
        try { 
            csvReader = new CSVReader(new FileReader 
            ("D:\\EclipseWorkSpace\\CSVOperations\\StudentData.csv")); 
        } 
        catch (FileNotFoundException e) { 

            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        CsvToBean csvToBean = new CsvToBean(); 

        // call the parse method of CsvToBean 
        // pass strategy, csvReader to parse method 
        List<Student> list = csvToBean.parse(strategy, csvReader); 

        return list; // I want to read and return 10 at a time
    }

调用方法

public void foo(){

    List<Student> students;
    do{
       students = readFile(); // requirement: should return a  list of 10
       sysout(students);
    }while(!students.isEmpty)
    }

但是我想读csv文件 n 并将其Map到对象列表(假设我的文件有100条记录,我想一次读取10行到一个列表中)

bgibtngc

bgibtngc1#

你可以这样做:

String[] line; // this will contain all columns of a single line in the csv file
int counter = 0;
List<String[]> chunk = new ArrayList<>();
while ((line = csvReader.readNext()) != null) {
  counter++;

  // do something with the line, for example:
  chunk.add(line);

  if(counter == 10) {
    counter = 0; // reset counter

    // do something when counter reached 10, for example:
    System.out.println(chunk);
    chunk.clear();
  }
}

if(counter > 0) {
  // do something if counter could not be reset, for example:
  System.out.println(chunk); // remaining items
}

最后 if(counter > 0) 语句可能很有用,例如,如果您希望计数器计数为10,但csv文件中有15行。所以你可以为剩下的行运行一些代码。

hjqgdpho

hjqgdpho2#

我用你的代码做了这个解决方案。
我编写了一个过滤器来检查要跳过的行数和要返回的行数。

class LineNumberFilter implements CsvToBeanFilter {

private final int skip;

private final int lines;

private int counter;

public LineNumberFilter(int skip, int lines) {
    this.skip = skip;
    this.lines = lines;
    counter = 0;
}

public boolean allowLine(String[] line) {
    counter++;

    return counter >= skip && counter < (skip + lines);
}

要使用它,必须按如下所示传递参数以跳过2行并返回2行。

return csvToBean.parse(strategy, csvReader, new LineNumberFilter(2,2));
mrzz3bfm

mrzz3bfm3#

您可以尝试使用univocity的csv解析器。它支持通过注解Map到javapojo。下面是univocity与其他csv解析器的性能比较

相关问题