Retrieving all filenames from hierarchical directories using Apache Commons IO
Listing of file names in hierarchical directories can be done recursively as demonstrated in the previous recipe. However, this can be done in a much easier and convenient way and with less coding using the Apache Commons IO library.
Getting ready
In order to perform this recipe, we will require the following:
- In this recipe, we will be using a Java library from Apache named Commons IO. Throughout the book, we will be using version 2.5. Download the JAR file of your choice from here: https://commons.apache.org/proper/commons-io/download_io.cgi
- Include the JAR file in your project an external JAR in Eclipse.
How to do it...
- Create a method that takes the root directory in the hierarchy of directories as input:
public void listFiles(String rootDir){
- Create a file object with the root directory name:
File dir = new File(rootDir);
- The
FileUtils
class of the Apache Commons library contains a method namedlistFiles()
. Use this method to retrieve all the file names, and put the names in a list variable with<File>
generics. UseTrueFileFilter.INSTANCE
to match all directories:List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
- The file names can be displayed on the standard output as follows. As we now have the names in a list, we have a means to process the data in these files further:
for (File file : files) { System.out.println("file: " + file.getAbsolutePath()); }
- Close the method:
}
The method in this recipe, the class for it, and the driver method to run it are as follows:
import java.io.File; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.TrueFileFilter; public class FileListing{ public static void main (String[] args){ FileListing fileListing = new FileListing(); fileListing.listFiles("Path for the root directory here"); } public void listFiles(String rootDir){ File dir = new File(rootDir); List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); for (File file : files) { System.out.println("file: " + file.getAbsolutePath()); } }
Tip
If you want to list files with some particular extensions, there is a method in Apache Commons library called listFiles
, too. However, the parameters are different; the method takes three parameters, namely, file directory, String[]
extensions, boolean recursive. Another interesting method in this library is listFilesAndDirs (File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) if someone is interested in listing not only files but also directories. Detailed information can be found at https://commons.apache.org/proper/commons-io/javadocs/.