FilesLinesLeak
The stream returned by Files.lines should be closed using try-with-resources

Category
Severity
ERROR
Maturity

The problem

The problem is described in the javadoc for Files.lines:

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.

To ensure the stream is closed, always use try-with-resources with Files.lines:

String input;
try (Stream<String> stream = Files.lines(path)) {
  input = stream.collect(Collectors.joining(", "));
}

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("FilesLinesLeak") to the enclosing element.