March 2018
Intermediate to advanced
208 pages
4h 52m
English
| | class LogBooks { |
| | |
| | static List<LogBook> getAll() throws IOException { |
| » | return Files.walk(Paths.get("/var/log")) |
| | .filter(Files::isRegularFile) |
| | .filter(LogBook::isLogbook) |
| | .map(path -> { |
| | try { |
| | return new LogBook(path); |
| | } catch (IOException e) { |
| » | throw new UncheckedIOException(e); |
| | } |
| | }) |
| | .collect(Collectors.toList()); |
| | } |
| | } |
As you’ve seen in Chapter 5, Prepare for Things Going Wrong, you should be prepared for dealing with exceptions. Unfortunately, this can be hard with lambda expressions.
In the code above, we use the Java NIO API to iterate over the filesystem with a stream. Files.walk() opens a Stream<Path> that starts from a given Path and contains all files and directories ...