我从 BorisTheSpider 获得了以下代码(谢谢他,循环运行完美):
final FileNameExtensionFilter filter = new FileNameExtensionFilter("pdf");
final File path = new File (pathToFiles);
for (final File child: path.listFiles()) {
if (filter.accept(child)) {
claimArrayList.add(child.getName());
System.out.println(child.getName());
}
}
但我得到以下 FileNameExtensionFilter 异常
Exception in thread "main" java.lang.IllegalArgumentException: Extensions must be non-null and not empty
然后我尝试了
i tried final FileNameExtensionFilter filter = new FileNameExtensionFilter(null, "pdf");
虽然它过滤 pdf 文件,但它也允许子文件夹
当我尝试添加其他参数时,过滤器允许所有文件
final FileNameExtensionFilter filter = new FileNameExtensionFilter("csv", "pdf");