1

我想显示任务完成的百分比。但是由于任务的长度取决于要从文件中读取的数据量,所以我不能pb3.setValue(...)像现在这样对值进行硬编码。我知道我可以setIntermediate(true)用来显示任务正在进行中,但是有没有办法根据文件的大小确定确切的任务进度。

这是我要测量的操作,我通过 JFileChooser 选择fileArray[2]文本文件在哪里。

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());

    }

提前感谢您的任何帮助或建议。

4

1 回答 1

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();
于 2013-10-14T11:55:20.973 回答