gpt4 book ai didi

java - 我如何检查给定路径列表中的文件是否存在?

转载 作者:搜寻专家 更新时间:2023-10-31 20:04:47 24 4
gpt4 key购买 nike

我有一个包含 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 个文件对象?我也在用 Guava 。那里有任何实用程序可以帮助我使用批量 exists() 方法吗?

最佳答案

创建 50,000 个 File 对象几乎肯定不是瓶颈。实际的文件系统操作可能是导致速度变慢的原因。

我有两个建议:

  1. 在检查之前,按位置对路径进行排序,以充分利用文件系统缓存。
  2. 如果子目录不存在,您可以自动假定其中的所有文件和子目录也不存在。

关于java - 我如何检查给定路径列表中的文件是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11046772/

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