Does collect operation on Stream close the stream and underlying resources?
Asked Answered
A

2

27

Does below code need to be wrapped in try-with-resources to make sure underlying file is closed?

List<String> rows = Files.lines(inputFilePath).collect(Collectors.toList());
Anecdotist answered 1/7, 2015 at 20:54 Comment(1)
The design of Stream and close() was very controversial. To be safe, always close a Stream if you are unsure. And even that may not be enough, see #20319917Lotta
C
18

As the javadoc of the overloaded Files#lines(Path, Charset) method states

The returned stream encapsulates a Reader. If timely disposal of file system resources is required, the try-with-resources construct should be used to ensure that the stream's close method is invoked after the stream operations are completed.

So yes, wrap the Stream returned by lines in a try-with-resources statement. (Or close it appropriately.)

Contrabass answered 1/7, 2015 at 21:3 Comment(0)
P
35

There is a trick to make the Stream implementation calling close() after the terminal operation:

List<String> rows = Stream.of(Files.lines(inputFilePath)).flatMap(s->s)
                   .collect(Collectors.toList());

It simply creates a stream encapsulating the stream of lines as a single item and uses flatMap with an identity function (Function.identity() would work as well) to turn it into a stream of lines again.

The interesting point is a property of Stream.flatMap(…):

Each mapped stream is closed after its contents have been placed into this stream.

So the code above will close the stream of lines. While it looks more concise it has the disadvantage over try with resources that current implementation of flatMap lacks lazy evaluation which is not relevant here as you are collecting all lines into a list anyway. But it’s something to keep in mind when using this trick in other scenarios.


For the question’s code as-is there is an even simpler solution:

List<String> rows = Files.readAllLines(inputFilePath);

Reads all lines and closes all resources…

Propertius answered 2/7, 2015 at 8:45 Comment(2)
I tried your solution, and it works. I also assumed that it would be equivalent to Files.lines(inputFilePath).flatMap(Stream::of) but to my surprise it was not. The latter did not actually close the stream. (on jdk1.8.0_221)Tonkin
@Tonkin .flatMap(Stream::of) creates a new single element stream for each element and flattens them back to an equivalent stream as before the flatMap. Those single element streams will be closed, but that has no effect.Propertius
C
18

As the javadoc of the overloaded Files#lines(Path, Charset) method states

The returned stream encapsulates a Reader. If timely disposal of file system resources is required, the try-with-resources construct should be used to ensure that the stream's close method is invoked after the stream operations are completed.

So yes, wrap the Stream returned by lines in a try-with-resources statement. (Or close it appropriately.)

Contrabass answered 1/7, 2015 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.