gIrl1990
V2EX  ›  问与答

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

  •  
  •   gIrl1990 · Sep 18, 2019 · 1334 views
    This topic created in 2436 days ago, the information mentioned may be changed or developed.
    • 简单暴力,穷举遍历 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 replies    2019-10-08 16:37:29 +08:00
    gIrl1990
        1
    gIrl1990  
    OP
       Oct 8, 2019
    gIrl1990
        2
    gIrl1990  
    OP
       Oct 8, 2019
    修正问题: 有木有引入线程池或者其它优化之类的(其它统计法)?

    新增参考: https://github.com/gaul/ptools
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1697 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 16:24 · PVG 00:24 · LAX 09:24 · JFK 12:24
    ♥ Do have faith in what you're doing.