Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use.
一般来说,并不需要手动调用Stream的close方法, 只有背后是I/O相关的流才需要手动关闭。
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.)
/** * Read all lines from a file as a {@code Stream}. Unlike {@link * #readAllLines(Path, Charset) readAllLines}, this method does not read * all lines into a {@code List}, but instead populates lazily as the stream * is consumed. * * <p> Bytes from the file are decoded into characters using the specified * charset and the same line terminators as specified by {@code * readAllLines} are supported. * * <p> After this method returns, then any subsequent I/O exception that * occurs while reading from the file or when a malformed or unmappable byte * sequence is read, is wrapped in an {@link UncheckedIOException} that will * be thrown from the * {@link java.util.stream.Stream} method that caused the read to take * place. In case an {@code IOException} is thrown when closing the file, * it is also wrapped as an {@code UncheckedIOException}. * * <p> The returned stream encapsulates a {@link Reader}. If timely * disposal of file system resources is required, the try-with-resources * construct should be used to ensure that the stream's * {@link Stream#close close} method is invoked after the stream operations * are completed. * * * @param path * the path to the file * @param cs * the charset to use for decoding * * @return the lines from the file as a {@code Stream} * * @throws IOException * if an I/O error occurs opening the file * @throws SecurityException * In the case of the default provider, and a security manager is * installed, the {@link SecurityManager#checkRead(String) checkRead} * method is invoked to check read access to the file. * * @see #readAllLines(Path, Charset) * @see #newBufferedReader(Path, Charset) * @see java.io.BufferedReader#lines() * @since 1.8 */ publicstatic Stream<String> lines(Path path, Charset cs)throws IOException { BufferedReader br = Files.newBufferedReader(path, cs); try { return br.lines().onClose(asUncheckedRunnable(br)); } catch (Error|RuntimeException e) { try { br.close(); } catch (IOException ex) { try { e.addSuppressed(ex); } catch (Throwable ignore) {} } throw e; } }
重点看这几句,
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 Stream#close method is invoked after the stream operations are completed.
/** * Read all lines from a file. This method ensures that the file is * closed when all bytes have been read or an I/O error, or other runtime * exception, is thrown. Bytes from the file are decoded into characters * using the specified charset. **/