这是我的FilenameFilter
,它应该只允许以.docx
结尾的目录和文件。然而,由于某种原因,它现在允许每个文件,无论其结尾如何或是否是目录。一旦我删除 || dir.isDirectory()
其按预期工作。
new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.toLowerCase().endsWith(".docx") || dir.isDirectory()) {
return true;
}
return false;
}
})
我做错了什么,它接受每个文件?
最佳答案
dir
始终是一个目录,就这么简单。
Javadocs :
Parameters: dir - the directory in which the file was found. name - the name of the file.
What you probably meant was:
new File(dir, name).isDirectory()
关于java - FilenameFilter 产生神秘结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47597048/