【发布时间】:2023-03-29 02:32:01
【问题描述】:
我正在使用 IntelliJ IDEA 在 Mac 上编程,并且正在编写一个程序来使用递归查找大文件 (1GB)。这是我目前写的代码。
public class Exercise05 {
public static MyFilter myFilter = new MyFilter();
public static int count = 0;
public static void main(String[] args) throws FileNotFoundException {
File file = new File("/");
long startTime = System.currentTimeMillis();
findBigFile(file);
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
System.out.println(count);
}
public static void findBigFile(File file) throws FileNotFoundException {
if (file.isFile()) {
if (myFilter.bigFile(file)) {
System.out.println(file.getAbsolutePath());
System.out.println(file.length());
count++;
}
} else {
try {
if (file.listFiles().length > 0) {
File[] files = file.listFiles();
for (File file1 : files) {
findBigFile(file1);
}
}
} catch (NullPointerException ex) {
System.out.println(file.getAbsolutePath());
}
}
}
}
class MyFilter {
public boolean bigFile(File file) {
if (file.length() > (1024 * 1024 * 1024)) {
return true;
} else
return false;
}
}
这是我的结果示例
/.DocumentRevisions-V100
/.fseventsd
/.Spotlight-V100
/.Trashes
/Applications/.Wineskin2
/Applications/AliWangwang.app/Contents/Frameworks/Sparkle.framework/Resources/fr.lproj/fr.lproj
/Applications/AliWangwang.app/Contents/Frameworks/Sparkle.framework/Resources/fr_CA.lproj
/Applications/AliWangwang.app/Contents/Frameworks/Sparkle.framework/Versions/A/Resources/fr.lproj/fr.lproj
/Applications/AliWangwang.app/Contents/Frameworks/Sparkle.framework/Versions/A/Resources/fr_CA.lproj
/Applications/AliWangwang.app/Contents/Frameworks/Sparkle.framework/Versions/Current/Resources/fr.lproj/fr.lproj
/Applications/AliWangwang.app/Contents/Frameworks/Sparkle.framework/Versions/Current/Resources/fr_CA.lproj
/Applications/leanote.app/Contents/Frameworks/Electron Framework.framework/Frameworks
/Applications/leanote.app/Contents/Frameworks/Electron Framework.framework/Libraries/Libraries
我调试了程序,发现在评估File.isFile()时,有些文件返回了false,这很奇怪。它们是文件而不是文件夹,这会导致程序执行 else 语句。为什么要这样做?
【问题讨论】:
-
“执行了 else 语句”是什么意思?哪些文件?如果您真的只是在问为什么
File.isFile有时会意外返回false,那么如果您只是提出这个问题并提供所涉及文件的具体示例,将会有所帮助。 -
我只是在问为什么
File.isFile会返回false。 -
见@Burkhard 的回答。当这些文件“异常”时,您需要进行调查
标签: java macos recursion intellij-idea