ZetCode

Ebooks

Java DirectoryStream

last modified July 12, 2024

In this article we how 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.

DirectoryStream listing home directory

The first example lists the user's home directory.

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

void main() throws IOException {

    var homeDir = Paths.get(System.getProperty("user.home"));

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

        paths.forEach(System.out::println);
    }
}

The example lists contents of the user's home directory.

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.

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

void main() throws IOException {

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

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

        paths.forEach(System.out::println);
    }
}

The example shows all PDF files in the specified directory.

DirectoryStream filter example

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

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

void main() throws IOException {

    DirectoryStream.Filter<Path> filter = file ->
            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(System.out::println);
    }
}

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

DirectoryStream recursive walking

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

Main.java
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;

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

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;
}

void main() throws IOException {

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

    paths.forEach(System.out::println);
}

The example walks over the directory recursively.

Source

Java DirectoryStream - language reference

In this article we have used Files.newDirectoryStream to list the directory contents.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List Main.java tutorials.