[英]Java Folder size estimate
我们正在构建一个Java应用程序,使用户能够从第三方应用程序导入大型数据集。 第三方应用程序中的数据位于文件系统上,并分发到大量文件夹和小文件中(许多用户在外部磁盘上拥有这些文件)。 为了保护用户,如果没有足够的磁盘空间来执行导入,我们想警告他。 但是,为此,我们必须计算大量小文件占用的磁盘空间。
我尝试使用Apache IO和java.nio方法来计算目录大小。 但是,两种方法都需要大约10分钟的时间,而FireWire磁盘上大约需要50GB的数据。
只要此任务是纯粹的安全措施,这就太久了,而且在大多数情况下,我们得出的解决方案是有足够的可用空间。
是否有一些方法可以对目录所消耗的空间进行快速,智能的原始估算?
我也想知道是否有一点可以容纳磁盘的大小。
同时,这是我的解决方案-通过对输出进行一些精美的文本处理,您可以得到大小-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.