Do terminal operations close the stream?

No forEach does not close the stream (created by Files.list or Files.lines). It is documented in the javadoc, for example for Files.list:

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.

Leave a Comment