简体   繁体   English

Java文件夹大小估计

[英]Java Folder size estimate

We are building a java application that enables the user to import large datasets from a third party application. 我们正在构建一个Java应用程序,使用户能够从第三方应用程序导入大型数据集。 The data in the third party application is on the filesystem and distributed into a huge number of folders and small files (which a lot of users have on external disks). 第三方应用程序中的数据位于文件系统上,并分发到大量文件夹和小文件中(许多用户在外部磁盘上拥有这些文件)。 In order to protect the user, we want to warn him, if there is not enough disk space available to perform the import. 为了保护用户,如果没有足够的磁盘空间来执行导入,我们想警告他。 However, to do so we have to calculate the disk space used by this huge lot of small files. 但是,为此,我们必须计算大量小文件占用的磁盘空间。

I tried using Apache IO and java.nio approaches to calculate the directory size. 我尝试使用Apache IO和java.nio方法来计算目录大小。 However, both methods take about 10 minutes with about 50GB of data on a FireWire disk. 但是,两种方法都需要大约10分钟的时间,而FireWire磁盘上大约需要50GB的数据。

This is too long as this task is a pure safety measure and most of the time, we arrive at the solution that there is enough space available. 只要此任务是纯粹的安全措施,这就太久了,而且在大多数情况下,我们得出的解决方案是有足够的可用空间。

Is there some method that can produce a fast, intelligent raw estimate about the space consumed by the directory? 是否有一些方法可以对目录所消耗的空间进行快速,智能的原始估算?

I would also be interested in knowing if there is a bit that holds the size of the disk. 我也想知道是否有一点可以容纳磁盘的大小。

Meanwhile here is my solution - with a bit of fancy text processing of the output you can get the size - it takes abour 6 min for 70G again probably not what you are looking for but it could half your time 同时,这是我的解决方案-通过对输出进行一些精美的文本处理,您可以得到大小-70G大约需要6分钟,可能不是您想要的,但可能会花费一半的时间

long tm=System.currentTimeMillis();
try {
  String cmd="cmd /c dir c:\\  /s  ";
  execute(cmd, false);
}
catch (Exception ex) { }
  System.out.println((System.currentTimeMillis()-tm));


public String execute(String cmd, boolean getoutput) {
String output=null;
  try {
    Runtime rt = Runtime.getRuntime();
    Process pr=rt.exec(cmd);
    StreamGobbler errorGobbler=new StreamGobbler(pr.getErrorStream(), "ERROR", getoutput);
    errorGobbler.start();
    StreamGobbler inputGobbler=new StreamGobbler(pr.getInputStream(), "INPUT", getoutput);
    inputGobbler.start();
    int exitVal=pr.waitFor();
    System.out.println("ExitValue: " + exitVal);
    output=""+errorGobbler.output;
    output+=inputGobbler.output;
  }
  catch(Throwable t) { t.printStackTrace(); }
  return output;
}


import java.util.*;
import java.io.*;

public class StreamGobbler extends Thread {
boolean redirect=false;
InputStream is;
OutputStream os;
String type, output="";

StreamGobbler(InputStream is, String type) {
    this.is = is;
    this.type = type;
}

StreamGobbler(InputStream is, String type, boolean redirect) {
    this.is = is;
    this.type = type;
    this.redirect=redirect;
}

StreamGobbler(OutputStream os, String type) {
    this.os = os;
    this.type = type;
}

StreamGobbler(OutputStream is, String type, boolean redirect) {
    this.os = os;
    this.type = type;
    this.redirect=redirect;
}

 public void run() {
    try
    {
        if(type.equals("OUTPUT")) {
        }
        else {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line=null;
        int i=0;
        while ( (line = br.readLine()) != null) {
            if(redirect) output+=line;
            else System.out.println("line "+i+" "+type + ">" + line);
            i++;
        }
        }
        } catch (IOException ioe)
          {
            ioe.printStackTrace();
          }
}   

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM