【发布时间】:2017-03-02 09:28:23
【问题描述】:
我有以下代码:
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部分是什么意思? -
我认为他的意思是回车
-
为什么 bytesRead 是双精度的?它不应该是一个整数值(int 或者 long)
-
这对我来说没有意义。批处理文件的输出与其文件长度有什么关系?它自己打印吗?顺便说一句:如果文件不存在,您应该跳过其余部分。否则你会遇到异常。
标签: java batch-file io