我使用IntelliJ IDEA在Mac上进行编程,并且正在编写一个程序来使用递归来查找大文件(1GB)。这是我迄今编写的代码。在Mac中使用递归查找大文件
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
我调试的程序,发现一些文件返回false
时File.isFile()
评价,这是奇怪的一个例子。它们是文件而不是文件夹,这会导致程序执行else语句。它为什么这样做?
你是什么意思“执行else语句”?哪些文件?如果你真的只是问为什么'File.isFile'有时候会意外地返回'false',那么如果你只是提出这个问题,并且涉及到这些文件的具体例子,这将会有所帮助。 –
我只是问为什么'File.isFile'返回'false'。 – HelloSilence
查看@Burkhard的回答。你需要调查这些文件是否“异常” – Craig