Java List All Subdirectories From a Directory Example

In this source code example, we show you how to list all the subdirectories from a directory in Java.

We use Files.list() method to list all the subdirectories from a directory in Java.

Java List All Subdirectories From a Directory Example

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

public class FilesListDirectories {

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

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

        Files.list(new File(homeDir).toPath())
                .filter(path -> path.toFile().isDirectory())
                .forEach(System.out::println);
    }
}

References


Comments