Reputation: 2312
In the doc it says
Read all lines from a file as a
Stream
.
Does that necessary mean that it's loading the entire file? For example:
try (Stream<String> stream = Files.lines(Paths.get("myfilename.txt"))) {
stream.forEach(x -> {
if myfilename
is 100GB , will Files.lines
load the entire 100GB?
Upvotes: 13
Views: 4825
Reputation: 2843
There is an issue with the Spliterator if Files.lines is used with a parallel stream. Than all processed lines will be stored in the Spliterator which makes it unuseable for large files.
Upvotes: 0
Reputation: 21
A new method lines() has been added since 1.8, it lets BufferedReader returns content as Stream.this method does not read all lines into a List, but instead populates lazily as the stream is consumed.when you call for-each,then a new line is reload. hope can help you!
Upvotes: 2
Reputation: 2276
Well, the link you provided states it already:
Unlike readAllLines, this method does not read all lines into a List, but instead populates lazily as the stream is consumed.
So for every time your for-each
block is called, a new line is read.
Upvotes: 9
Reputation: 50716
No, it doesn't load the entire file into memory. It internally uses a BufferedReader
with the default buffer size, repeatedly calling readLine()
.
Upvotes: 6