How do I search for files recursively?

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.



import java.util.Collection;

public class SearchFileRecursive {
    public static void main(String[] args) {
        File root = new File("/Users/wayan/Dev");

        try {
            String[] extensions = {"xml", "java", "dat"};

            // 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
            Collection files = FileUtils.listFiles(root, extensions, true);

            for (Object o : files) {
                File file = (File) o;
                System.out.println("File = " + file.getAbsolutePath());
        } catch (Exception e) {

Maven Dependencies

<!-- -->

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.