我使用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