Read all lines from a file as a Stream. Unlike readAllLines, this method does not read
all lines into a List, but instead populates lazily as the stream
is consumed.
Bytes from the file are decoded into characters using the specified
charset and the same line terminators as specified by
readAllLines are supported.
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 UncheckedIOException that will
be thrown from the
java.util.stream.Stream method that caused the read to take
place. In case an IOException is thrown when closing the file,
it is also wrapped as an UncheckedIOException.
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.
path
| the path to the file | |
cs
| the charset to use for decoding |
Stream
IOException
| if an I/O error occurs opening the file | |
SecurityException
| In the case of the default provider, and a security manager is
installed, the checkRead
method is invoked to check read access to the file. |
readAllLines(Path, Charset), newBufferedReader(Path, Charset), java.io.BufferedReader.lines()
Diagram: Files