2015-12-15 75 views
1

我们正在构建一个Java应用程序,使用户能够从第三方应用程序导入大型数据集。第三方应用程序中的数据位于文件系统中,并分发到大量文件夹和小文件(许多用户在外部磁盘上)。 为了保护用户,我们要警告他,如果没有足够的磁盘空间可用于执行导入。但是,要这样做,我们必须计算大量小文件使用的磁盘空间。Java文件夹大小估计

我尝试使用Apache IO和java.nio方法来计算目录大小。但是,这两种方法大约需要10分钟,FireWire磁盘上的数据量约为50GB。

这太长了,因为这项任务是一项纯粹的安全措施,而且大多数情况下,我们都会提供有足够空间的解决方案。

是否有某种方法可以对目录所占用的空间进行快速,智能的原始估计?

+0

我希望这会帮助你http://stackoverflow.com/questions/116574/java-get-file-size-efficiently – Rocky

回答

0

我也有兴趣知道是否有一点可以容纳磁盘的大小。

同时这里是我的解决方案 - 用位输出的花哨的文字处理,你可以得到大小 - 它需要华南简介6分钟为70G又可能不是你要找的,但它可能有一半的时间

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(); 
      } 
} 

}