【问题标题】:find big files with recursion in mac在mac中递归查找大文件
【发布时间】: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


【解决方案1】:

转换您的文件大小,然后比较以获得文件夹中的实际大文件。

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

【讨论】:

  • 要小心。如果是目录。 "如果此路径名表示目录,则返回值未指定。"
  • 然后像file.isDirectory()那样做一个交叉检查然后继续。
【解决方案2】:

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()。

【讨论】:

  • 那么如何定义一个普通的文件呢?
  • 依赖于系统。使用 isDirectory() 可能更安全。
  • 我试过你的建议,效果好多了。有些目录抛出异常。我猜是访问权限的原因。
【解决方案3】:

你可以试试File.isDirectory(),看看它是否返回类似的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-19
    • 2014-06-21
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多