读取的行的输出百分比

时间:2013-05-13 12:05:06

标签: java

我有一个方法可以读取文本文件中的所有行,并且我想输出已读取的文件的当前百分比。我已设法做到这一点,但是,每次我读取一个新行时它都输出百分比,所以输出如下:

0.0% read.
0.0% read.
0.0% read.
1.0% read.
1.0% read.
1.0% read.

依旧...... 我希望它每次从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++;
}

3 个答案:

答案 0 :(得分:6)

存储先前的值

在下面的代码片段中,我假设一个浮点变量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%

答案 1 :(得分: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 ");
    }
}

答案 2 :(得分: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++;   
}