gpt4 book ai didi

java - FilenameFilter 产生神秘结果

转载 作者:行者123 更新时间:2023-12-01 20:04:47 24 4
gpt4 key购买 nike

这是我的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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com