Java 8 BufferedReader lines() 方法打印不同的计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23564103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:59:13  来源:igfitidea点击:

Java 8 BufferedReader lines() method prints different count number

javalambdabufferedreaderjava-8

提问by Masudul

I have faced a problem to count line number using lines() method of BufferedReader. Following is the content of test.txtfile.

我遇到了使用BufferedReader. 以下是test.txt文件内容。

1 Career
2 Filmography
3 Awards
4 References
5 External

Here is the source code to count line number twice.

这是两次计算行号的源代码。

  BufferedReader br=new BufferedReader(new FileReader(new File("test.txt")));
  long lineNo=br.lines().count();
  long lineNo2=br.lines().count();

  System.out.println(lineNo); // 5
  System.out.println(lineNo2);// 0

Here, I have question why second line of lineNo2print 0instead of 5? Thanks in advance.

在这里,我有疑问为什么要lineNo2打印第二行0而不是5? 提前致谢。

采纳答案by Mark Rotteveel

The BufferedReader.lines()method returns a stream. Accessing the stream (eg when you perform a count()on it), will read lines from the buffer, moving the current position in the BufferedReaderforward.

BufferedReader.lines()方法返回一个流。访问流(例如,当您对其执行 acount()时),将从缓冲区读取行,向前移动当前位置BufferedReader

When you do a count(), the entire stream is read, so the BufferedReader()will - probably - be at the end. A second invocation of lines()will return a stream that will read no lines, because the reader is already at the end of its data.

当您执行 a 时count(),将读取整个流,因此BufferedReader()将 - 可能 - 位于末尾。第二次调用lines()将返回一个不读取任何行的流,因为读取器已经在其数据的末尾。

The javadoc of BufferedReader.lines()specifies:

的 javadocBufferedReader.lines()指定:

After execution of the terminal stream operation there are no guarantees that the reader will be at a specific position from which to read the next character or line.

执行终端流操作后,无法保证读取器将位于特定位置,从该位置读取下一个字符或行。

I read this to mean that there is no guarantee that it is immediately after the last line returned from the stream, but as a count consumes all lines, I am pretty sure it is at the end. Going back to the beginning of a reader is (usually) not possible.

我读到这意味着不能保证它紧跟在从流返回的最后一行之后,但是由于计数消耗了所有行,我很确定它在最后。回到读者的开头(通常)是不可能的。

If you need to do multiple actions with the data from the BufferedReader.lines()you either need to process to stream once, or you need to collect the data into temporary storage. But note that executing a terminal operation like a count of lines (or a collect) might never complete (eg if the BufferedReaderis fed from an infinite source).

如果您需要对来自 的数据执行多项操作,BufferedReader.lines()您要么需要处理一次流,要么需要将数据收集到临时存储中。但请注意,执行像行计数(或收集)这样的终端操作可能永远不会完成(例如,如果BufferedReader从无限源馈送)。

回答by Puce

From the Javadoc:

Javadoc

After execution of the terminal stream operation there are no guarantees that the reader will be at a specific position from which to read the next character or line.

执行终端流操作后,无法保证读取器将位于特定位置,从该位置读取下一个字符或行。

count()is a terminal operation. Thus the position of the reader is unspecified after the first count()-call.

count()是终端操作。因此,在第一次count()调用之后读取器的位置是未指定的。

回答by Thirumalai Parthasarathi

The java 8 API specifies herethat

在Java API 8指定在这里

After execution of the terminal stream operation there are no guarantees that the reader will be at a specific position from which to read the next character or line.

执行终端流操作后,无法保证读取器将位于特定位置,从该位置读取下一个字符或行。

So after you execute the br.lines().count()statement there is no guarantee on the pointer's location.

因此,在执行该br.lines().count()语句后,无法保证指针的位置。

The lines().count()invocation consumes the data from the file and when invoked again without closing the stream. It cannot consume the same data again by invoking br.lines().count().

lines().count()调用从文件占用的数据,并没有关闭流再次调用时。它不能通过调用再次使用相同的数据br.lines().count()