This post will discuss how to list all files in a directory and all its subdirectories in Java with Guava and Apache Commons IO library.

1. Using Guava Library

Since Guava version 15, we can use Files.fileTreeTraverser() that returns a TreeTraverser instance for File trees.

We can choose between breadthFirstTraversal(), postOrderTraversal() and preOrderTraversal() iteration methods that internally uses breadth-first, post-order and pre-order traversal, respectively.

Download Code

2. Using Apache Commons IO

org.apache.commons.io.FileUtils provides two static utility methods, iterateFiles(…) and listFiles(…), which can be used to list all files in a directory and optionally its subdirectories.

iterateFiles(directory, fileFilter, dirFilter)

It returns an iterator for the matching files in the given root directory (and optionally its subdirectories). It takes 3 parameters – the directory to search in, a file filter to apply when finding files, and a directory filter to apply when finding subdirectories. If we pass the directory filter as null, subdirectories will not be included in the search. We can use TrueFileFilter.INSTANCE to match all directories with no filtering.

 
Please note that iterateFiles() returns an iterator over the matching files, but it doesn’t include the subdirectories themselves. We can use iterateFilesAndDirs() instead to include both files and directories.

 
There is an overloaded version of FileUtils.iterateFiles() that takes the directory to search in, a string array of permissible file extensions, and a boolean value to indicate if all subdirectories are to searched as well.

listFiles(directory, fileFilter, dirFilter)

It returns a Collection of the matching files in the given root directory (and optionally its subdirectories). It takes 3 parameters – the directory to search in, the filter to apply when finding files, and the filter to apply when finding subdirectories. If we pass the directory filter as null, subdirectories will not be included in the search. We can use TrueFileFilter.INSTANCE to match all directories with no filtering.

 
Please note that listFiles() returns a collection of matching files, but it doesn’t include the subdirectories themselves. We can use listFilesAndDirs() instead to include both files and directories.

 
There is an overloaded version of FileUtils.listFiles() that takes the directory to search in, a string array of permissible file extensions to include in the Collection, and a boolean value to indicate if files in subdirectories are to be included as well.

3. Using Apache Commons IO

The java.io.File.listFiles() method returns an array of all files and directories present in the root that satisfy the specified filter. We can pass filters provided by Apache Common IO, as shown below:

That’s all about listing all files in a directory in Java using Guava and Apache Commons IO.

 
Also See:

Traverse a directory and list all files in Java

Traverse a given directory using BFS and DFS in Java

 
References:

1. Files (Guava 19.0 API)
2. FileUtils (Commons IO 2.4 API)