Ebooks

Java Files.walk tutorial

Java Files.walk tutorial shows how to walk files in Java with Files.walk.

Files.walk returns a stream that is lazily populated with Path by recursively walking the file tree rooted at a given starting file. The file tree is traversed depth-first. There are two overloaded Files.walk methods; one of them takes the maxDepth parameter, which sets the maximum number of levels of directories to visit.

By default, symbolic links are not automatically followed by this method. If the options parameter contains the FOLLOW_LINKS option then symbolic links are followed.

Files.walk regular files

The first example shows regular files in the specified directory.

FilesWalkRegularFilesEx.java
package com.zetcode;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class FilesWalkRegularFilesEx {

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

        var dirName = "C:/Users/Jano/Downloads";

        try (Stream<Path> paths = Files.walk(Paths.get(dirName), 2)) {
            paths.filter(Files::isRegularFile)
                    .forEach(System.out::println);
        }
    }
}

The program walks the directory for two levels. We apply a filter with Files.isRegular() predicate.

Files.walk directories

The following example shows directories in the specified directory.

FilesWalkDirectoriesEx.java
package com.zetcode;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class FilesWalkDirectoriesEx {

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

        var dirName = "C:/Users/Jano/Downloads";

        try (Stream<Path> paths = Files.walk(Paths.get(dirName))) {
            paths.filter(Files::isDirectory)
                    .forEach(System.out::println);
        }
    }
}

To output directories, we apply the Files.isDirectory() predicate. This time there is not limit for recursive walking.

Files.walk by file extensions

The next program lists all PDF files in the specified direcory and its subdirectories for two levels.

FilesWalkFileExtensionEx.java
package com.zetcode;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class FilesWalkFileExtensionEx {

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

        var dirName = "C:/Users/Jano/Downloads";

        try (Stream<Path> paths = Files.walk(Paths.get(dirName), 2)) {
            paths.map(path -> path.toString()).filter(f -> f.endsWith(".pdf"))
                    .forEach(System.out::println);
        }
    }
}

The program lists PDF files in the Downloads directory. The path object is converted to a string and we call endsWith() on the string to check if it ends with pdf extension.

In this tutorial, we have used Files.walk to walk the directory contents. You might also be interested in the related tutorials: Java Files.list tutorial, Java Files.walk tutorial, Java DirectoryStream tutorial, Java file tutorial, Java create directory, Copying file in Java, Java ProcessBuilder tutorial, Creating file in Java, and Java tutorial.