英文:
Reading Large Text Files
问题
在Java中,是否有一种方法可以仅读取每日添加到文本文件中的新行,而无需每次运行Java应用程序时都从开头读取所有行呢?
英文:
I have a large text file consisting of thousands of lines. I keep adding new lines to the text file on a daily basis. These lines are parsed and added into the database. In Java, is there a way I can read only the new lines being added to the text file rather than reading all the lines right from the start every time I run the java application?
答案1
得分: 0
你可以存储上次读取的行数。然后,以流的形式打开文件并跳过已处理的行数。
// offset = 已处理的行数
Stream<String> lines = Files.lines(Paths.get(fileLocation));
lines.skip(offset);
lines.forEach(line -> {
// 处理
// offset++;
});
英文:
You can store the last read line count. And then, open the file as a stream and skip the count that's processed already.
//offset = already processed line count
Stream<String> lines = Files.lines(Paths.get(fileLocation));
lines.skip(offset);
lines.forEach(line -> {
// process
// offset ++;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论