【问题标题】:How to implement directory deph in java program that dispaly directory sizes?如何在显示目录大小的java程序中实现目录深度?
【发布时间】:2012-12-20 09:21:41
【问题描述】:

代码:

public class DirSize
{
    public static void main(String[] args)
    {
        DirSize ds = new DirSize();
        System.out.println(ds.getDirSizeInMegabytes(new File("c:/temp")));
    }

    long getDirSize(File dir)
    {
        long size = 0;

        if (dir.isFile())
        {
            size = dir.length();
        }
        else
        {
            File[] subFiles = dir.listFiles();

            for (File file : subFiles)
            {
                if (file.isFile())
                {
                    size += file.length();
                }
                else
                {
                    size += this.getDirSize(file);
                    System.out.println("Directory " + file.getName()
                                       + " size = " + size / 1021 / 1024);
                }
            }
        }
        return size;
    }

    long getDirSizeInMegabytes(File dir)
    {
        return this.getDirSize(dir) / 1024 / 1024;
    }
}

我想只打印目录的大小,例如从最初的第 2 级:

c:\temp1\temp2

但如果还有temp3:

c:\temp1\temp2\temp3 its size shouldn't be printed.

喜欢:

 c:\temp1\temp2 size = 10M
 c:\temp1\temp21 size = 15M
 ....

如何做到这一点?谢谢。

【问题讨论】:

    标签: java recursion directory


    【解决方案1】:

    您需要在递归方法中添加递归深度,以便能够限制打印的内容:

    long getDirSize(File dir, int depth, int printDepth) 
    

    那么你需要这样的递归调用:

    size += this.getDirSize(file, depth+1, printDepth);
    

    如果你的意思是你只想打印最大深度的尺寸,那么你需要添加测试

    if (depth == printDepth) { // or depth <= printDepth maybe
        // ...do printing only for these
    }
    

    将整个东西包装在一个类中可能是有意义的,因此您可以将 printDepth 设为成员变量,并将递归方法设为私有,例如:

    class DirSizePrinter {
        int printDepth;
        File root;
        public DirSizePrinter(int printDepth, File root) {
            this.printDepth = printDepth;
            this.root = root;
        }
    
        public long printSize() {
            return printSizeRecursive(0);
        }
    
        private long printSizeRecursive(int depth) {
            // ... code from question with depth added, and using printDepth and root
        }
    }
    

    用法:

        new DirSizePrinter(3, "C:/temp").printSize();
    

    或者这个的一些变体,取决于你的所有要求。

    【讨论】:

      【解决方案2】:
      void getDirSize(File dir,depth) {
          long size = 0;
      
          if (dir.isFile()) {
              size = dir.length();
          } else {
              depth++;
              File[] subFiles = dir.listFiles();
      
              for (File file : subFiles) {
                  if (file.isFile()) {
                      size += file.length();
                  } else {
                      size += this.getDirSize(file,depth);
                      if(depth==1) {
                      System.out.println("Directory " + file.getName()
                              + " size = " + size / 1021 / 1024);
      }
                  }
      
              }
          }
      
      }
      

      然后调用

      getDirSize(new File("c:/temp"),0)
      

      【讨论】:

        猜你喜欢
        • 2013-06-26
        • 1970-01-01
        • 1970-01-01
        • 2015-08-21
        • 1970-01-01
        • 2011-09-06
        • 1970-01-01
        • 1970-01-01
        • 2011-04-22
        相关资源
        最近更新 更多