List all files in a directory in Java using Guava and Apache Commons IO

In this post, we will see how to list all files in a directory and all its sub-directories in Java with Guava and Apache Commons IO library.


 

1. 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

 

2. 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.

 

1. iterateFiles(directory, fileFilter, dirFilter)

It returns an iterator for the matching files in 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 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 a 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.

 

2. listFiles(directory, fileFilter, dirFilter)

It returns a Collection of the matching files in given root directory (and optionally its subdirectories). It takes 3 parameters – the directory to search in, the filter to apply when finding files and filter to apply when finding subdirectories. If we pass 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 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 a 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. File Filters provided by Apache Commons IO

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

 
See Also:

Traverse given directory in Java 7 and Java 8

Traverse the given directory using BFS and DFS in Java

 
References:

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

 
Thanks for reading.




 
Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz