Use org.apache.commons.io.FileFilter, there's a good example of how to use it in the JavaDoc.
For example I have a directory structure like:
/tmp/testFilter/A
/tmp/testFilter/B
/tmp/testFilter/C/c1
/tmp/testFilter/C/c2
/tmp/testFilter/D/d1
Then I could list only the files under C and D with the following code:
public class FileLister {
public static void main(String args[]) {
File dir = new File("/tmp/testFilter");
String[] files = dir.list(
new NotFileFilter(
new OrFileFilter(
new PrefixFileFilter("A"),
new PrefixFileFilter("B")
)
)
);
listFiles(dir, files);
}
private static void listFiles(File rootDir, String[] files) {
for (String fileName: files) {
File fileOrDir = new File(rootDir, fileName);
if (fileOrDir.isDirectory()) {
listFiles(fileOrDir, fileOrDir.list());
} else {
System.out.println(fileOrDir);
}
}
}
}