2015-09-04 52 views
-1

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

我調試的程序,發現一些文件返回falseFile.isFile()評價,這是奇怪的一個例子。它們是文件而不是文件夾,這會導致程序執行else語句。它爲什麼這樣做?

+1

你是什麼意思「執行else語句」?哪些文件?如果你真的只是問爲什麼'File.isFile'有時候會意外地返回'false',那麼如果你只是提出這個問題,並且涉及到這些文件的具體例子,這將會有所幫助。 –

+0

我只是問爲什麼'File.isFile'返回'false'。 – HelloSilence

+0

查看@Burkhard的回答。你需要調查這些文件是否「異常」 – Craig

回答

1

的Javadoc指出:

public boolean isFile() 

Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file. 

Where it is required to distinguish an I/O exception from the case that the file is not a normal file, or where several attributes of the same file are required at the same time, then the Files.readAttributes method may be used. 

Returns: 
    true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise 

所以你的情況,該文件可能不是一個正常的文件

編輯:
由於isDirectory()只返回true,如果它是一個目錄,您應該反轉if else並使用file.isDirectory()來代替。

+0

那麼如何定義一個普通的文件呢? – HelloSilence

+0

它與系統有關。使用isDirectory()可能更安全。 – Burkhard

+0

我試過你的建議,它變得好多了。有些目錄會拋出一個異常。我想這是訪問權限的原因。 – HelloSilence

1

轉換您的文件大小,然後比較以獲取文件夾中的實際大文件。

double bytes = file.length(); 
double kilobytes = (bytes/1024); 
+0

小心一點。如果它是一個目錄。 「如果此路徑名稱表示目錄,則返回值未指定。」 – Burkhard

+0

然後只需執行一次交叉檢查,如'file.isDirectory()',然後繼續。 – SaviNuclear

0

您可以試着File.isDirectory()代替,看看如果返回了類似的結果。