Java 8 Friday Goodies: finally rocks!

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. tweet this

Interacting with the file system has been a bit of a pain in Java. CTMMC shows us an example of how to copy a file with Java. While some issues still remain, at least, we can now use lambdas and the new Streams API to traverse the file system and list files! Here is the FileFilterGoodies example that we’ve pushed to our GitHub repository:

public class FileFilterGoodies {

    public static void main(String args[]) {
        listRecursive(new File("."));

     * This method recursively lists all
     * .txt and .java files in a directory
    private static void listRecursive(File dir) {, n) ->
                     (new File(f, n).isDirectory()
                  ||  n.endsWith(".txt")
                  ||  n.endsWith(".java"))
              .forEach(unchecked((file) -> {
                          .substring(new File(".")

                  if (file.isDirectory()) {

     * This utility simply wraps a functional
     * interface that throws a checked exception
     * into a Java 8 Consumer
    private static <T> Consumer<T>
    unchecked(CheckedConsumer<T> consumer) {
        return t -> {
            try {
            catch (Exception e) {
                throw new RuntimeException(e);

    private interface CheckedConsumer<T> {
        void accept(T t) throws Exception;

The output of the above programme is:

\out\production\jOOQ's Java 8 Goodies
\out\production\jOOQ's Java 8 Goodies\org
\out\production\jOOQ's Java 8 Goodies\org\jooq
\out\production\jOOQ's Java 8 Goodies\org\jooq\java8
\out\production\jOOQ's Java 8 Goodies\org\jooq\java8\goodies
\out\production\jOOQ's Java 8 Goodies\org\jooq\java8\goodies\io

Now, that’s really awesome, isn’t it? Let’s decompose the above listRecursive() method:

// With this method, we wrap the File[] array
// into a new Java 8 Stream, which has awesome
// new methods.

// The Java 1.2 File.listFiles() method luckily
// accepts a @FunctionalInterface, which can be
// instantiated using a lambda expression
// ...
// In this example, we'll just ignore the fact
// that listFiles can return null
              dir.listFiles((f, n) ->
             (new File(f, n).isDirectory()
          ||  n.endsWith(".txt")
          ||  n.endsWith(".java"))

// Each Stream (and also java.util.List) has this
// awesome forEach method, that accepts a Consumer

// Unfortunately, Java 8 Consumers don't allow
// throwing checked exceptions. So let's quickly
// wrap it (see details below) ...

// ... and pass another lambda expression to it,
// which prints the local path and recurses
                         (file) -> {
                  .substring(new File(".")

          if (file.isDirectory()) {

More goodies next week

Stay tuned for next week, when we show you how Java 8 improves using XML with jOOX

More on Java 8

In the mean time, have a look at Eugen Paraschiv’s awesome Java 8 resources page

Tags: , , , , , ,

6 responses to “Java 8 Friday Goodies: finally rocks!”

  1. Why Not (@YerocJr) says :

    A couple of comments:

    1. File I/O was largely fixed in Java 7 with the new NIO.2 APIs including CTMMC #35 you referenced in this post.
    2. Rather than using dir.listFiles() I’d use the new Files.newDirectoryStream() function which you can also give a lambda expression to apply filtering. The advantage here is that you’re building an Iterable rather than an Array so it scales to millions of files.

  2. joeattardi says :

    While it hasn’t been deprecated, is considered legacy as of Java 7. They recommend using java.nio.file.Path instead.

  3. ponzao says :

    Interesting post, really need to check Java 8 out.

    I think your example could be improved a lot by changing `listRecursive` to return a sequence of all the files, this would enable the caller to decide what files she is interested in (filter) and what to do with them (map, each, fold).

  4. kwerulant says :

    the filter doesnt work. your code also lists *.class files etc.

    • lukaseder says :

      Thank you very much for the hint. Fixed. The error was in checking whether f.isDirectory(), which is always true. It should be new File(f, n).isDirectory()

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 3,805 other followers

%d bloggers like this: