2015-09-04 60 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()代替,看看如果返回了类似的结果。