我有一个包含 50,000 个路径的列表,我需要检查每个路径是否存在文件。现在,我正在像这样独立验证每条路径:
public static List<String> filesExist(String baseDirectory, Iterable<String> paths) throws FileNotFoundException{
File directory = new File(baseDirectory);
if(!directory.exists()){
throw new FileNotFoundException("No Directory found: " + baseDirectory );
}else{
if(!directory.isDirectory())
throw new FileNotFoundException(baseDirectory + " is not a directory!");
}
List<String> filesNotFound = new ArrayList<String>();
for (String path : paths) {
if(!new File(baseDirectory + path).isFile())
filesNotFound.add(path);
}
return filesNotFound;
}
有没有办法改进它,这样我就不会创建 50,000 个文件对象?我也在用番石榴。那里有什么实用程序可以帮助我使用批量exists()
方法吗?