首页 > 解决方案 > 如何知道我在 CSVReader 中有文件的最后一行

问题描述

我使用 java 中的 CSVReader 将那些大文件导入数据库中。

如果我想知道我有文件的最后一行然后文件结束,我应该使用什么方法?

我的代码是:

String pathFile = "D:\\";
String ifrsFileName = "test.csv";
int countToInput = 1000;
CSVReader ifrsReader = new CSVReader(new FileReader(pathFile + ifrsFileName));
String[] nextLine;
long counter = 0;
while ((nextLine = ifrsReader.readNext()) != null) {
            deposit = process.getEntityObject(nextLine);
            deposits.add(deposit);

            if (counter == countToInput - 1 ) {
                
                repository.insertToDB(deposits, connection, pstmt);
                
                counter = 0;
                deposits.clear();
                
            }

            counter++;
}

我的问题是最后读取 1000 条记录后,我的程序结束,如果剩下的记录少于 1000 条,它们将不会被导入。多谢

标签: javalarge-filescsvreader

解决方案


推荐阅读