我在
java.io.File中使用方法File.listFiles()获取文件列表,但它返回一些系统文件,如(.sys等)..我需要排除所有系统相关文件(
Windows,Linux,Mac)返回列表时.任何人都可以解决我的问题吗?
解决方法
我将实现一个简单的FileFilter,其逻辑用于确定文件是否为系统文件,并以
AlexR showed in his answer的方式使用它的实例.这样的事情(规则a仅用于演示目的!):
@H_403_6@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;
}