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?