Home » Java » Core Java » Java 8 Friday Goodies: The New New I/O APIs

About Lukas Eder

Lukas is a Java and SQL enthusiast developer. He created the Data Geekery GmbH. He is the creator of jOOQ, a comprehensive SQL library for Java, and he is blogging mostly about these three topics: Java, SQL and jOOQ.

Java 8 Friday Goodies: The New New I/O APIs

At Data Geekery, we love Java. And as we’re really into jOOQ’s fluent API and query DSL, we’re absolutely thrilled about what Java 8 will bring to our ecosystem. We have blogged a couple of times about some nice Java 8 goodies, and now we feel it’s time to start a new blog series, the…

Java 8 Friday

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”:

@lukaseder @brunoborges @ponzao +1 for “Enterprise IO” (in this case not expected to work of course) — Franz van Betteraey (@FrVaBe) January 15, 2014

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:

Files.list(new File(".").toPath())

The output I get is this:


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:

Files.list(new File(".").toPath())
     .filter(p -> !p.getFileName()

The new output I get is this one:


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:

Files.walk(new File(".").toPath())
     .filter(p -> !p.getFileName()

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:


But listed:

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:

Files.walk(new File(".").toPath())
     .filter(p -> !p.toString()
                    .contains(File.separator + "."))

This now yields the expected


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:

Files.lines(new File("pom.xml").toPath())
     .map(s -> s.trim())
     .filter(s -> !s.isEmpty())

The above yields:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


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!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments