- BufferedReader.lines()
- Files.list(Path)
- Files.walk(Path, int, FileVisitOption...)
- Files.walk(Path, FileVisitOption...)
- Files.find(Path, int, BiPredicate,
FileVisitOption...)
- Files.lines(Path, Charset)
- DirectoryStream.stream()
- UncheckedIOException - thrown when an
IO error occurs, but since IOException isn't allowed to be in the
signature of Iterator/Stream, it has to be smuggled through with an
unchecked exception.
Most of these additions give you ways
to obtain java.util.stream.Stream from files and InputStreams. They're a bit
different from the streams you obtain from regular collections though. For one,
they may throw UncheckedIOException. Also, they are instances of streams where
using the stream.close() method is necessary.
Streams implement AutoCloseable and can therefore be used in
try-with-resources statements. Streams also have an onClose(Runnable) intermediate operation
that I didn't list in the earlier section about streams. It allows you to
attach handlers to a stream that execute when it is closed. Here is an example:
// Print the lines in a file, then
"done"
try (Stream lines = Files.lines(path, UTF_8)) {
lines.onClose(() -> System.out.println("done"))
.forEach(System.out::println);
}
ReplyDeleteI have read your blog its very attractive and impressive. Nice information. It helped me alot.
law college|law college in Jaipur|Best law college in Jaipur|Law Course In Jaipur|Top College Of law In Jaipur|Vidyasthali Law College|Best Law College|Jaipur Law College