Why is Files.lines (and similar Streams) not automatically closed?

Yes, this was a deliberate decision. We considered both alternatives.

The operating design principle here is “whoever acquires the resource should release the resource”. Files don’t auto-close when you read to EOF; we expect files to be closed explicitly by whoever opened them. Streams that are backed by IO resources are the same.

Fortunately, the language provides a mechanism for automating this for you: try-with-resources. Because Stream implements AutoCloseable, you can do:

try (Stream<String> s = Files.lines(...)) {
    s.forEach(...);
}

The argument that “it would be really convenient to auto-close so I could write it as a one-liner” is nice, but would mostly be the tail wagging the dog. If you opened a file or other resource, you should also be prepared to close it. Effective and consistent resource management trumps “I want to write this in one line”, and we chose not to distort the design just to preserve the one-line-ness.

Leave a Comment