ZetCode

Java DirectoryStream tutorial

last modified July 6, 2020

Java DirectoryStream tutorial shows how to iterate over directories with DirectoryStream.

DirectoryStream is object to iterate over the entries in a directory. A directory stream allows for the convenient use of the for-each construct to iterate over a directory.

Files.newDirectoryStream opens a directory, returning a DirectoryStream to iterate over all entries in the directory.

Java DirectoryStream example

The first example lists the current directory.

DirectoryStreamEx.java
package com.zetcode;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class DirectoryStreamEx {

    public static void main(String[] args) throws IOException {

        var dirName = Paths.get("C:/Users/Jano/Downloads");

        try (var paths = Files.newDirectoryStream(dirName)) {

            paths.forEach(path -> System.out.println(path));
        }
    }
}

The example lists contents of the specified directory.

Java DirectoryStream globbing example

We can apply simple globbing operation on a stream of content. The second parameter of the Files.newDirectoryStream() is the glob pattern.

DirectoryStreamGlobEx.java
package com.zetcode;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class DirectoryStreamGlobEx {

    public static void main(String[] args) throws IOException {

        var dirName = Paths.get("C:/Users/Jano/Downloads");

        try (var paths = Files.newDirectoryStream(dirName, "*.pdf")) {

            paths.forEach(path -> System.out.println(path));
        }
    }
}

The example shows all PDF files in the specified directory.

Java DirectoryStream filter example

More complex filtering operations can be applied with DirectoryStream.Filter.

DirectoryStreamFilterEx.java
package com.zetcode;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class DirectoryStreamFilterEx {

    public static void main(String[] args) throws IOException {

        DirectoryStream.Filter<Path> filter = file -> {
            return Files.size(file) < 100_000L && file.toString().endsWith(".jpg");
        };

        var dirName = Paths.get("C:/Users/Jano/Downloads");

        try (var paths = Files.newDirectoryStream(dirName, filter)) {

            paths.forEach(path -> System.out.println(path));
        }
    }
}

The example shows all JPEG images that are smaller than 100 KB.

Java DirectoryStream recursive walking

In the following example, we show how to traverse recursively a directory with DirectoryStream.

DirectoryStreamRecursiveEx.java
package com.zetcode;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class DirectoryStreamRecursiveEx {

    private static List<Path> paths = new ArrayList<>();

    private static List<Path> walk(Path path) throws IOException {

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {

            for (Path entry : stream) {
                if (Files.isDirectory(entry)) {
                    walk(entry);
                }
                paths.add(entry);
            }
        }

        return paths;
    }

    public static void main(String[] args) throws IOException {

        var myPath = Paths.get("C:/Users/Jano/Downloads");

        var paths = walk(myPath);

        paths.forEach(path -> System.out.println(path));
    }
}

The example walks over the directory recursively.

In this tutorial, we have used Files.newDirectoryStream() to list the directory contents.

List all Java tutorials.