This example demonstrate how we can use the FileUtils class listFiles() method to search for a file specified by their extensions. We can also define to find the file recursively deep down into the subdirectories.
package org.kodejava.example.commons.io;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
public class SearchFileRecursive {
public static void main(String[] args) {
File root = new File("/home/foobar/Personal/Examples");
try {
String[] extensions = {"xml", "java", "dat"};
boolean recursive = true;
//
// Finds files within a root directory and optionally its
// subdirectories which match an array of extensions. When the
// extensions is null all files will be returned.
//
// This method will returns matched file as java.io.File
//
Collection files = FileUtils.listFiles(root, extensions, recursive);
for (Iterator iterator = files.iterator(); iterator.hasNext();) {
File file = (File) iterator.next();
System.out.println("File = " + file.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}