Every Friday, we’re showing you a couple of nice new tutorial-style Java 8 features, which take advantage of lambda expressions, extension methods, and other great stuff. You’ll find the source code on GitHub.
Java 8 Goodie: The New New I/O APIs
In a previous blog post from this series, we have shown how Java 8’s lambda expressions improve on the existing (yet outdated) JDK 1.2 I/O API, mainly by helping you express java.io.FileFilter instances as lambda expressions.
Many readers have rightfully pointed out that much of the java.io API has been superseded by Java 7’s java.nio API, where “N” stands for “New” (I know. New. Old. Old-2. Old-2-FIXME. Old-2-TODO…). But things get even better with Java 8. We’re calling it the New New I/O APIs (NNIO), though jOOQ community members have suggested to call it “Enterprise IO”:
Back to more constructive blogging. Let’s have a short walk (pun intended, see Files.walk()) around the improved Java 8 NIO features. Let’s first have a look at the new methods in java.nio.Files. It’s actually quite awesome that we can finally just list contents of a Path! In Java 8 we would use the newly introduced Files.list(), which returns a lazy Stream of files:
Remember that forEach() is a “terminal method”, i.e. a method that consumes the stream. You mustn’t call any further methods on such a Stream.
We could also skip all hidden files and list only the first three “regular” files like this:
Now, that’s already pretty awesome. Can it get better? Yes it can. You can also “walk” a whole file hierarchy by descending into directories using the new Files.walk() method. Here’s how:
Unfortunately, the above will create a Stream of Paths excluding all the hidden files and directories, but their descendants are still listed. So we get:
Omitted:
.\.git
But listed:
.\.git\COMMIT_EDITMSG
.\.git\config
.\.git\description
[...]
It is easy to understand why this happens. Files.walk() returns a (lazy) Stream of all descendant files. The call to .filter() will remove the ones that are hidden from the Stream, but this has no influence on any recursive algorithm that might apply in the implementation of walk(). Frankly, this is a bit disappointing. We cannot leverage Java 7’s Files.walkFileTree() method, because the receiving FileVisitor type is not a @FunctionalInterface
We can, however, inefficiently work around this limitation with the following trivial logic:
Good news, however, is the new Files.lines() method. The following example shows how we can easily read line by line from a file, trimming each line (removing indentation) and filtering out the empty ones:
Clearly, the notion of lazy evaluation will produce a big amount of confusion in the community, similar to the fact that a Stream can be consumed only once. We are placing a bet that the Java 8 Streams API will be the single biggest source of new Stack Overflow questions.
Nonetheless, the Streams API will be awesome, and next week on the Java 8 Friday series, we’ll see how we can leverage lambda expressions and Streams to sort things, before we’ll see how Java 8 will improve our database interactions!