ZetCode

Java Files.walk

last modified January 27, 2024

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.

Source

Java Basic I/O - tutorial

In this article we have used Files.walk to walk 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 all Java tutorials.