1

我有一个读取文本文件中所有行的方法,我想输出已读取文件的当前百分比。我已经设法做到了,但是,每次读取新行时它都会输出百分比,因此输出如下所示:

0.0% 读取。
0.0% 读取。
0.0% 读取。
1.0% 阅读。
1.0% 阅读。
1.0% 阅读。

依此类推...我希望它每次从 0 变为 1、从 1 变为 2 或类似时输出。这是相关的代码片段:

BufferedReader reader = new BufferedReader(new InputStreamReader(
        new FileInputStream(file), "UTF-8"));
String line = reader.readLine();
// Counts total number of lines
BufferedReader reader2 = new BufferedReader(new InputStreamReader(
        new FileInputStream(file), "UTF-8"));
int totalLines = 0;
while (reader2.readLine() != null) {
    totalLines++;
}
// read all lines from file
int lineNumber = 1;
while (line != null) {
    float percent = lineNumber * 100f / totalLines;
    double x = Math.round(percent);
    System.out.println(x + "% read.");
    line = reader.readLine();
    lineNumber++;
}
4

3 回答 3

7

存储上一个值

在下面的代码片段中,我假设一个浮点变量previousPercent. 您可以在循环顶部声明它。previousPercent在每个循环上使用新值进行更新。当循环中两个值不同时,输出该值,否则简单地更新previousPercent

float percent = (float)Math.floor(lineNumber * 100f / totalLines);
// Compute your percentage.
if(percent != previousPercent)
{
    // Output if different from the last time.
    System.out.println(percent + "% read");
}
// Update the percentage.
previousPercent = percent;

关于您的百分比的注释

如评论中所述,最好将百分比四舍五入。这是因为最微小的变化都会导致输出。使用Math.floor()而不是的原因Math.round()是因为,假设您已经阅读6.7%了一个文件。你可以证明说6% read但你没有读过7%

于 2013-05-13T12:07:07.370 回答
2

使其工作,您必须将当前百分比与上一次迭代中的百分比进行比较;为了让它更好地工作,你应该计算字节而不是的百分比;

使用字节,您将:

  • 防止两次加载/读取文件,这几乎使您的执行时间加倍;
  • 通过使用每行读取的实际字节数提供更准确的百分比(一个大行可能算作 100 个小行等);

在一个 100MB 的文件上运行它,并检查执行时间;

(注意第一种方法也是进行JVM预热)

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Date;

public class FilePercentageTest {

    public static void main(String[] args) throws java.lang.Exception {
        File file = new File("D:/test.txt");

        showPercentageByBytes(file);
        showPercentageByLines(file);

    }

    private static void showPercentageByBytes(File file) throws Exception{
        long start = new Date().getTime();          
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), "UTF-8"));               
        long totalBytes = file.length();                    
        long bytesRead = 0;
        int previousPercent = 0;
        String line = reader.readLine();
        while (line != null) {          
            bytesRead+=line.length();
            int percent = (int)(bytesRead * 100 / totalBytes);          
            if (previousPercent<percent){
                System.out.println(percent + "% read.");            
                previousPercent = percent;
            }
            line = reader.readLine();
        }       
        long end = new Date().getTime();            
        System.out.println(end - start + " milliseconds ");
    }


    private static void showPercentageByLines(File file) throws Exception{
        long start = new Date().getTime();          
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), "UTF-8"));
        String line = reader.readLine();
        BufferedReader reader2 = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), "UTF-8"));
        int totalLines = 0;
        int previousPercent = 0;
        while (reader2.readLine() != null) {
            totalLines++;
        }
        int lineNumber = 1;
        while (line != null) {
            int percent = lineNumber * 100 / totalLines;
            if (previousPercent<percent){
                System.out.println(percent + "% read.");            
                previousPercent = percent;
            }
            line = reader.readLine();
            lineNumber++;
        }       
        long end = new Date().getTime();            
        System.out.println(end - start + " milliseconds ");
    }
}
于 2013-05-13T12:50:18.013 回答
0
double x = 0;
while(line != null) {

    float percent = lineNumber * 100f / totalLines;
    if (Math.round(percent) != x) {
         System.out.println(x + "% read.");
         x = Math.round(percent);
    }
    line = reader.readLine();

    lineNumber++;   
}
于 2013-05-13T12:08:15.900 回答