[英]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.