Does collect operation on Stream close the stream and underlying resources?
问题 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()); 回答1: 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.