# How to list all files in a directory in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 8, 2019, updated Aug 21, 2022. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-list-all-files-in-a-directory

> A quick tutorial to learn how to list all files and folders in a directory using Java.

In Java, there are many ways to list all files and folders in a directory. You can use either the `Files.walk()`, `Files.list()`, or `File.listFiles()` method to iterate over all the files available in a certain directory.

## `Files.walk()` Method

The `Files.walk()` is another static method from the [NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api) to list all files and sub-directories in a directory. This method throws a `NoSuchFileException` exception if the folder doesn't exist.

Here is an example that lists all files and sub-directories in a directory called `~/java-runner`:

```java
try (Stream<Path> paths = Files.walk(Paths.get("~/java-runner"))) {
    // print all files and folders
    paths.forEach(System.out::println);
} catch (IOException ex) {
    ex.printStackTrace();
}
```

Since it returns a `Stream` object, you can filter out nested directories and only list regular files like below:

```java
try (Stream<Path> paths = Files.walk(Paths.get("~/java-runner"))) {
    // filer out sub-directories
    List<String> files = paths.filter(x -> Files.isRegularFile(x))
            .map(Path::toString)
            .collect(Collectors.toList());

    // print all files
    files.forEach(System.out::println);

} catch (IOException ex) {
    ex.printStackTrace();
}
```

Similarly, you can also list only sub-directories by filtering out all regular files:

```java
try (Stream<Path> paths = Files.walk(Paths.get("~/java-runner"))) {
    // filer out regular files
    List<String> folders = paths.filter(x -> Files.isDirectory(x))
            .map(Path::toString)
            .collect(Collectors.toList());

    // print all folders
    folders.forEach(System.out::println);

} catch (IOException ex) {
    ex.printStackTrace();
}
```

Here is another example that lists all `.java` files using `Files.walk()`:

```java
try (Stream<Path> paths = Files.walk(Paths.get("~/java-runner"))) {
    // keep only `.java` files
    List<String> javaFiles = paths.map(Path::toString)
            .filter(x -> x.endsWith(".java"))
            .collect(Collectors.toList());

    // print all files
    javaFiles.forEach(System.out::println);

} catch (IOException ex) {
    ex.printStackTrace();
}
```

By default, the `Stream` object returned by `Files.walk()` recursively walks through the file tree up to an n-level (all nested files and folders). However, you can pass another parameter to `Files.walk()` to limit the maximum number of directory levels to visit.

Here is an example that restricts the directory level to a top-level folder only (level 1):

```java
try (Stream<Path> paths = Files.walk(Paths.get("~/java-runner"), 1)) {
    // print all files and folders in the current folder
    paths.forEach(System.out::println);
} catch (IOException ex) {
    ex.printStackTrace();
}
```

In the above code, the second parameter of `Fils.walk()` is the maximum number of levels of directories to visit. A value of 0 means that only the starting file is visited, unless denied by the security manager. A value of `Integer.MAX_VALUE` indicatea that all levels should be traversed.

## `Files.list()` Method

The `Files.list()` static method from NIO API provides the simplest way to list the names of all files and folders in a given directory:

```java
try (Stream<Path> paths = Files.list(Paths.get("~/java-runner"))) {
    // print all files and folders
    paths.forEach(System.out::println);
} catch (IOException ex) {
    ex.printStackTrace();
}
```

The `Files.list()` method returns a lazily populated `Stream` of entries in the directory. So you can apply all the above filters for `Files.walk()` on the stream.

## `File.listFiles()` Method

In old Java versions (JDK 6 and below), the `File.listFiles()` method is available to list all files and nested folders in a directory. 

Here is an example that uses `File.listFiles()` to print all files and folders in the given directory:

```java
File folder = new File("~/java-runner");

// list all files
for (File file : folder.listFiles()) {
    System.out.println(file);
}
```

To list only regular files, do the following:

```java
File folder = new File("~/java-runner");

// list all regular files
for (File file : folder.listFiles()) {
    if (file.isFile()) {
        System.out.println(file);
    }
}
```

Similarly, to list only sub-folders, you can use the below code:

```java
File folder = new File("~/java-runner");

// list all sub-directories
for (File file : folder.listFiles()) {
    if (file.isDirectory()) {
        System.out.println(file);
    }
}
```

To recursively list all files and folders, we can write a recursive function as follows:

```java
public void listFilesRecursive(File folder) {
    for (final File file : folder.listFiles()) {
        if (file.isDirectory()) {
            // uncomment this to list folders too
            // System.out.println(file);
            listFilesRecursive(file);
        } else {
            System.out.println(file);
        }
    }
}

// list files recursively
File folder = new File("~/java-runner");
listFilesRecursive(folder);
```

## Further Reading

You may be interested in other Java I/O articles:

* [Reading and Writing Files in Java](https://attacomsian.com/blog/java-read-write-files)
* [How to Read and Write Text Files in Java](https://attacomsian.com/blog/java-read-write-text-files)
* [How to Read and Write Binary Files in Java](https://attacomsian.com/blog/java-read-write-binary-files)
* [Reading and Writing Files using Java NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api)
* [How to read a file line by line in Java](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java)
