我正在使用opencsv版本4.1(也已通过4.2测试)进行CSV读取。
以下是编写的测试方法
public static void main(String[] args) throws IOException {
CSVReader soruceReader = null;
try {
long recordCount = 0;
soruceReader = new CSVReader(new FileReader("D:\\sample.csv"));
recordCount = soruceReader.getRecordsRead();
System.out.println("Records read : "+recordCount);
Iterator<String[]> sourceIterator = soruceReader.iterator();
recordCount = soruceReader.getRecordsRead();
System.out.println("Records read : "+recordCount);
boolean next = sourceIterator.hasNext();
recordCount = soruceReader.getRecordsRead();
System.out.println("Records read : "+recordCount);
if (next) {
String[] record = sourceIterator.next();
System.out.println("Record red : "+record[0]+record[1]+record[2]+record[3]+record[4]);
recordCount = soruceReader.getRecordsRead();
System.out.println("Records read : "+recordCount);
}
} finally {
if (soruceReader != null) {
soruceReader.close();
}
}
}
输出在这里-
Records read : 0
Records read : 1
Records read : 1
Record red : recordonthefirstline
Records read : 2
对于sample.csv文件-看起来像-
record,on,the,first,line
record,on,the,second,line
record,on,the,third,line
它表明iterator()方法增加了读取的记录。我没有看到任何文件暗示是否/为什么这样做,这似乎也不合逻辑。
这是opencsv csvreader的错误吗?还是我错过了什么?