I have been using apache.commons.CSVParser for a while without any issues. I know this sounds silly like the dog ate my homework but, the parser doesn’t yield any records suddenly for the past two days now. Records size shows up with a non zero value in the debugger but the list returned from CSVParser.getRecords() is zero. Elsewhere I had read the correct way to access the list is to call the size() method on the getRecords(), which I have done. To reemphasize, the code was working fine. I was using commons:commons-csv:jar:1.5 for a while, it stopped working and I tried upgrading to 1.9.0 and the behavior is still the same
CSVParser records = CSVFormat.DEFAULT.withDelimiter(DELIMITER).withHeader(blah).
withFirstRecordAsHeader().
parse(fileReader);
log.info(“Size:” records.getRecords()); //shows up as 125, say
List<CSVRecord> recordList = records.getRecords();//0 size!
I have written some test code to validate my sanity, and this code works fine
FileReader fReader = new FileReader("/tmp/tempexport.csv");
CSVParser records =
CSVFormat.DEFAULT.withDelimiter(DELIMITER).
withHeader("").withFirstRecordAsHeader().parse(fReader);
File file = new File("/tmp/tempexport.csv");
System.out.println(Files.lines(file.toPath()).count());
for (CSVRecord record: records.getRecords()) {
System.out.println(record.get(0)
"::" record.get(1));
CodePudding user response:
Naming is a bit misleading
(Both: your variables and their method-name)
A parser (even of class CSVParser) is a parser.
The records are a collection of records, that can be obtained using parser.getRecords() - as a List or Iterable.
Actually, the method is more than an idempotent getter. So getRecords() should be renamed to nextRecords() to be semantically exact
