用一个FileNameExtensionFilter
final FileNameExtensionFilter fnef = new FileNameExtensionFilter("", "ext1", "ext2", "ext3");
final List<File> files = new LinkedList<>();
for(final File f : new File(directoryPath).listFiles()) {
if(fnef.accept(f)) files.add(f);
}
这来自 Swing,但它完全符合您的要求,并且它对 EDT 没有任何依赖关系或任何奇怪的东西。
你甚至可以将它包裹FileNameExtensionFilter
在一个FileFilter
立面中并使其成为单线:
final List<File> filesList = new LinkedList<>(Arrays.asList(new File(directoryPath).listFiles(new FileFilter() {
final FileNameExtensionFilter fnef = new FileNameExtensionFilter("", "ext1", "ext2", "ext3");
@Override
public boolean accept(File pathname) {
return fnef.accept(pathname);
}
})));