List all files in a directory in Java using Guava and Apache Commons IO
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.
|
1 2 3 4 5 6 7 8 9 10 |
// Method to list all files in a directory using Guava public static void listFiles(File rootDir) { for (File file: Files.fileTreeTraverser().breadthFirstTraversal(rootDir)) { if (file.isFile()) { System.out.println(file); } } } |
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.
|
1 2 3 4 5 6 7 8 9 10 |
// Method to list all files in a directory with Apache Commons IO public static void listFiles(File rootDir) { Iterator<File> files = FileUtils.iterateFiles(rootDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); while (files.hasNext()) { System.out.println(files.next()); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 |
// Method to list all files in a directory with Apache Commons IO public static void listFiles(File rootDir) { Iterator<File> files = FileUtils.iterateFilesAndDirs(rootDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); while (files.hasNext()) { System.out.println(files.next()); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 |
// Method to list all files in a directory with Apache Commons IO public static void listFiles(File rootDir) { // pass extension array as null to return all file extensions Iterator<File> files = FileUtils.iterateFiles(rootDir, null, true); while (files.hasNext()) { System.out.println(files.next()); } } |
⮚ 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.
|
1 2 3 4 5 6 7 8 9 10 11 |
// Method to list all files in a directory with Apache Commons IO public static void listFiles(File rootDir) { Collection<File> files = FileUtils.listFiles(rootDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); for (File file: files) { System.out.println(file); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 |
// Method to list all files in a directory with Apache Commons IO public static void listFiles(File rootDir) { Collection<File> files = FileUtils.listFilesAndDirs(rootDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); for (File file: files) { System.out.println(file); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 |
// Method to list all files in a directory with Apache Commons IO public static void listFiles(File rootDir) { // pass extention array as null to return all file extentions Collection<File> files = FileUtils.listFiles(rootDir, null, true); for (File file: files) { System.out.println(file); } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Recursive method to list all files in a directory with Apache Commons IO public static void listFiles(File rootDir) { // FileFilterUtils.fileFileFilter() returns a file filter that accepts // only files and not directories File[] files = rootDir.listFiles((FileFilter) FileFilterUtils.fileFileFilter()); for (File file: files) { System.out.println(file); } // FileFilterUtils.directoryFileFilter() returns a file filter that accepts // only directories and not files File[] dirs = rootDir.listFiles((FileFilter) FileFilterUtils.directoryFileFilter()); for (File dir: dirs) { listFiles(dir); } } |
That’s all about listing all files in a directory in Java using Guava and Apache Commons IO.
Also See:
References:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)