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



我正在尝试读取CSV文件并将其映射到java POJO,使用OpenCSV。下面是我的样例csv类,Student.java是我的POJO文件

StudentData.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行,并将其映射到对象列表。(假设我的文件有100条记录,我想一次读取10行到列表中)

方法如下:

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行。因此,您可以为剩余的行运行一些代码。

我使用您的代码作为起点来解决这个问题。

我写了一个过滤器来检查要跳过的行数和要返回的行数。

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);
}

要使用它,必须像下面这样传递参数以跳过两行并返回两行。

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

您可以尝试使用univocity的java csv解析器。它支持通过注释映射到java pojo。下面是唯一性与其他csv解析器的性能比较

相关内容

  • 没有找到相关文章

最新更新