阅读大型文本文件

huangapple go评论66阅读模式
英文:

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&lt;String&gt; lines = Files.lines(Paths.get(fileLocation));
	lines.skip(offset);
	lines.forEach(line -&gt; {
		// process
		// offset ++;
	});

huangapple
  • 本文由 发表于 2020年9月24日 17:00:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64043028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定