我想显示已完成任务的百分比。但由于任务的长度取决于要从文件中读取的数据量,因此我无法对pb3.setValue(...)
的值进行硬编码,就像我现在正在做的那样。我知道我可以使用setIntermediate(true)
来显示任务正在进行中,但有没有办法根据文件大小确定确切的任务进度。
这是我要测量的操作,其中fileArray[2]
是文本文件我通过JFileChooser选择。
BufferedReader br3 = new BufferedReader(new FileReader(fileArray[2]));
String line;
while ((line = br3.readLine()) != null) {
StringBuffer sb3 = new StringBuffer(br3.readLine());
progress += 1;
Thread.sleep(10);
pb3.setValue(Math.min(progress, 100));
sb3.reverse();
bwr3.write(sb3.toString());
txt3.append(sb3.toString());
}
提前感谢您的任何帮助或建议。
答案 0 :(得分:1)
试试这个:
BufferedReader br1 = new BufferedReader(
new FileReader(fileArray[1]));
while ((line = br1.readLine()) != null) {
StringBuffer sb1 = new StringBuffer(
br1.readLine());
sb1.reverse();
String lineReversed = sb1.toString();
byte[] bytesRead = lineReversed.getBytes();
for (int i = 0; i < bytesRead.length; i++) {
totalBytesRead = totalBytesRead
+ bytesRead[i];
}
}
BufferedReader br1n = new BufferedReader(
new FileReader(fileArray[1]));
int totalBytesReadNow = 0;
int progress = 0;
while ((line = br1n.readLine()) != null) {
StringBuffer sb1n = new StringBuffer(
br1n.readLine());
sb1n.reverse();
String lineReversed = sb1n.toString();
byte[] bytesReadNow = lineReversed.getBytes();
for (int i = 0; i < bytesReadNow.length; i++) {
totalBytesReadNow = totalBytesReadNow
+ bytesReadNow[i];
if (totalBytesReadNow >= totalBytesRead * 0.01) {
progress = progress + 1;
totalBytesReadNow = 0;
}
pb2.setValue(Math.min(progress, 100));
}
bwr1.write(lineReversed + "\n");
txt2.append(lineReversed);
Thread.sleep(0, 1);
}
bwr1.flush();
bwr1.close();
br1n.close();
br1.close();