The Java APIs describe this topic as follows:
Streams have a BaseStream.close()
method and implement AutoCloseable
, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset))
will require closing. Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can be declared as a resource in a try-with-resources statement.)
Also note the API for Files.lines(Path, Charset)):
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.
Bottom line is: if the stream corresponds to a resource that, in normal scenarios need to be closed after use (like IO), use it in a try-with-resources statement.