Consider the following code:
Path directory = Paths.get(/* some directory */); Files.list(directory).forEach(System.out::println);
Does a terminal operation (like forEach
) close the underlying file that has been opened?
Refer to the relevant parts of the javadoc of Files.list:
The returned stream encapsulates a DirectoryStream. 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.
If it doesn't call Stream.close()
, what would then be the best alternative to call it while producing maintainable code?
A terminal stream operation is an operation that starts the internal iteration of the elements, calls all the listeners, and returns a result. The call to the map() method of the Stream interface is a non-terminal operation. It merely sets a lambda expression on the stream which converts each element to lowercase.
The Java Stream API's terminal operations return the final result of iterating through all the elements in the stream, and providing the non-terminal and terminal operations to the elements. The result of the terminal operation is returned after the last element in the stream has been processed.
A stream can be composed of multiple functions that create a pipeline that data that flows through. This data cannot be mutated. That is to say the original data structure doesn't change. However the data can be transformed and later stored in another data structure or perhaps consumed by another operation.
Terminal operators do NOT close the stream automatically. Consider this code:
Stream<Path> list = Files.list(directory).onClose(() -> System.out.println("Closed")); list.forEach(System.out::println);
This does NOT print "Closed".
However, the following does print "Closed":
try (Stream<Path> list = Files.list(directory).onClose(() -> System.out.println("Closed"))) { list.forEach(System.out::println); }
So the best way to do it is to use the try-with-resources mechanism.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With