我正在使用 java.io.File
中的方法 File.listFiles()
获取文件列表,但它返回一些系统文件,如 (.sys
和 etc
).. 我需要在返回列表时排除所有系统相关文件(Windows、Linux、Mac)。谁能解决我的问题?
最佳答案
我将实现一个简单的 FileFilter
,它具有确定文件是否为系统文件的逻辑,并以 AlexR showed in his answer 的方式使用它的实例。 .像这样(规则 a 仅用于演示目的!):
public class IgnoreSystemFileFilter implements FileFilter {
Set<String> systemFileNames = new HashSet<String>(Arrays.asList("sys", "etc"));
@Override
public boolean accept(File aFile) {
// in my scenario: each hidden file starting with a dot is a "system file"
if (aFile.getName().startsWith(".") && aFile.isHidden()) {
return false;
}
// exclude known system files
if (systemFileNames.contains(aFile.getName()) {
return false;
}
// more rules / other rules
// no rule matched, so this is not a system file
return true;
}
关于java - 在 java 中排除 file.lists() 中的系统文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12897841/