在我的 android 应用程序中,我需要查找目录中是否存在某种格式的文件。我编写了代码,它运行良好,但是如果目录有太多目录,每个目录都有很多文件和目录,它会变得有点慢。
注意:我也在计算目录中的总 txt 文件
这是我的代码
int count = 0;
private boolean containsTXT(File file) {
boolean result = false;
String fList[] = file.list();
if (fList == null)
return false;
else {
for (int i = 0; i < fList.length; i++) {
File file = new File(file, fList[i]);
if (file.isFile() && (file.getName().endsWith("txt"))) {
result = true;
count++; // This counts total txt files in the dir
} else if (file.isDirectory()) {
result = containsTXT(file);
}
}
}
return result;
}
我基本上遵循最通用的方法,但有些应用程序与我在我的应用程序中尝试做的工作相同,而且速度更快。有人知道这项任务的更好方法或算法吗?谢谢 !!