我有下面這段代碼:如何批文件的處理轉換爲百分比
File file = new File("//path/to/BatchFile");
long fileLength = 0 ;
if(file.exists()){
fileLength = file.length();
}
Process process = Runtime.getRuntime().exec("cmd /c " + "//path/to/batchfile");
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String s;
long bytesRead=0;
while ((s = br.readLine()) != null) {
bytesRead = bytesRead + s.getBytes().length + 2; // forget about +2
int progress = (int) ((bytesRead/ fileLenth)*100);
System.out.println("Progress : "+ progress);
}
我所試圖做的是運行一個批處理文件,並將其轉變成的BufferedReader來計算進度條的百分比。但我的比例超過100.
幫助我糾正我做錯了什麼或讓我知道如何將'過程'部分轉換爲百分比。
「+ 2」部分是什麼意思? – Berger
我認爲他的意思是回車 – Andremoniy
爲什麼bytesRead是雙精度的?它不應該是一個整數值(int或可能很長) – Matt