代码之家  ›  专栏  ›  技术社区  ›  XtremeBaumer

FilenameFilter产生神秘结果

  •  0
  • XtremeBaumer  · 技术社区  · 8 年前

    这是我的 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;
        }
    })
    

    我做错了什么,它接受每个文件?

    1 回复  |  直到 8 年前
        1
  •  3
  •   Jiri Tousek    8 年前

    dir 总是一个目录,就这么简单。

    Javadocs :

    Parameters:
        dir - the directory in which the file was found.
        name - the name of the file.
    

    你的意思可能是:

    new File(dir, name).isDirectory()