Question

I want to use Apache Commons IO's FileUtils.listFiles to find all files in a directory but do not want to perform the search recursively.

I can list all files recursively like so:

FileUtils.listFiles(directory,
                    new RegexFileFilter(regex),
                    DirectoryFileFilter.DIRECTORY);

Here DirectoryFileFilter.DIRECTORY accepts all directories, thus recursing completely. Looking at the JavaDoc for DirectoryFileFilter I don't see any provided implementations that rejects all directories. I can implement it myself like so..

public class NonRecursiveDirectoryFileFilter extends DirectoryFileFilter {

    @Override
    public boolean accept(File file) {
        return false;
    }

}

I figure if Commons IO includes a convenient DirectoryFileFilter.DIRECTORY for recursive searching, there should be an equally convenient implementation for non-recursive searching, no? The JavaDocs suggest there are no extensions of DirectoryFileFilter provided in Commons IO. Am I overlooking something?

Was it helpful?

Solution 2

I wanted to give credit here to @MrLore who noted that File.listFiles does exactly what I wanted. As he put it, before redacting his comment, the CommonsIO's FileUtils.listFiles is intended to offer more functionality and the use case I described doesn't need these extra features.

OTHER TIPS

Doi, found it..

FalseFileFilter

A file filter that always returns false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top