V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
gIrl1990
V2EX  ›  问与答

快速统计目录(文件夹)大小,有哪些好的算法?

  •  
  •   gIrl1990 · 2019-09-18 19:44:25 +08:00 · 984 次点击
    这是一个创建于 1681 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 简单暴力,穷举遍历 commons / io / FileUtils.java

    https://gitbox.apache.org/repos/asf?p=commons-io.git;a=blob;f=src/main/java/org/apache/commons/io/FileUtils.java;h=72d8f2af6910cdad49e3d50c7a06e5a28c2f37e7;hb=HEAD#l2667

        public static long sizeOfDirectory(final File directory) {
            checkDirectory(directory);
            return sizeOfDirectory0(directory);
        }
    
        private static long sizeOfDirectory0(final File directory) {
            final File[] files = directory.listFiles();
            if (files == null) {  // null if security restricted
                return 0L;
            }
            long size = 0;
    
            for (final File file : files) {
                if (!isSymlink(file)) {
                    size += sizeOf0(file); // internal method
                    if (size < 0) {
                        break;
                    }
                }
            }
    
            return size;
        }
    
        private static long sizeOf0(final File file) {
            if (file.isDirectory()) {
                return sizeOfDirectory0(file);
            }
            return file.length(); // will be 0 if file does not exist
        }
    
    • linux du 命令里做了哪些优化?

    • Q: 有木有引入线程池或者其它优化之类的?

    2 条回复    2019-10-08 16:37:29 +08:00
    gIrl1990
        1
    gIrl1990  
    OP
       2019-10-08 16:13:34 +08:00
    gIrl1990
        2
    gIrl1990  
    OP
       2019-10-08 16:37:29 +08:00
    修正问题: 有木有引入线程池或者其它优化之类的(其它统计法)?

    新增参考: https://github.com/gaul/ptools
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3175 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:36 · PVG 22:36 · LAX 07:36 · JFK 10:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.