List Files and Subdirectories from a Directory in Java

A quick tutorial with examples covers various ways to iterate and list files from or directory and list subdirectories of a directory or folder in Java.

List Files using Java Streams

The Files class in Java provides many static methods for performing File IO operations. In order to list all files from a directory, we can use the list method of this class. The list method returns a Java Stream of file paths of given directory.

Java Stream are lazy in nature, hence we get lazily populated list of files.

private void printAllFilesInDirectory(String path) throws IOException {
    try (Stream<Path> stream = Files.list(Path.of(path))) {
        stream
                .filter(not(Files::isDirectory))
                .forEach(System.out::println);
    }
}Code language: Java (java)

Note that we are filter all the files which are not directories.

List Subdirectories using Java Stream

Similarly, we can just revert the filter class from above to list subdirectories of given directory.

private void printSubdirectories(String path) throws IOException {
    try (Stream<Path> stream = Files.list(Path.of(path))) {
        stream
                 .filter(Files::isDirectory)
                 .forEach(System.out::println);
    }
}Code language: Java (java)

Walk through Directory Tree

We have seen and example of listing the name of files and folders inside a directory. However, by walking through the directory tree we can list down nested subdirectories and files up to a specified depth.

private void printSubdirectories(String path, int depth) throws IOException {
    try (Stream<Path> stream = Files.walk(Path.of(path), depth)) {
        stream
                .forEach(System.out::println);
    }
}Code language: Java (java)

List Files and Directories using DirectoryStream

The Files class provides newDirectoryStream method, which is a faster way to list files and subdirectories. The method returns a stream of Path for all of the files and folders contained in the specified directory.

private void listContents(String path) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(Path.of(path))) {
        stream
                .forEach(System.out::println);
    }
}Code language: Java (java)

List using Traditional Way

We can also use File class to list the contents of a directory. We need to instantiate file and call listFiles method. This method returns an array of File objects.

private void listContents(String path) throws IOException {
    File file = new File(path);
    Stream.of(Objects.requireNonNull(file.listFiles()))
            .forEach(System.out::println);
}Code language: Java (java)

Summary

In this short tutorial we learned a various ways of listing files and folders under a given directory in Java. We found that the Java Stream way is lazy and easiest of all. We have also seen how can we use the File object to list down directory contents. For more on Java Tutorials please visit: Java Tutorials.