java - 关于计算读取文件所花费的总时间

标签 java time io

我的 C: 驱动器中有一个名为 abcd.log 的日志文件,我正在通过 java 程序读取它,我想测量程序完全读取日志文件花费了多少时间,请告知如何实现此目的..!!

public class Readdemo {



    public static void main(String[] args) {

        File file = new File("C://abcd.log");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);

            System.out.println("Total file size to read (in bytes) : "
                    + fis.available());

            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                System.out.print((char) content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

我想出了这个..

public class BufferedRedeem {


    public static void main(String[] args) {

        BufferedReader br = null;
        long startTime = System.currentTimeMillis();

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("C://abcd.log"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }
            long elapsedTime = System.currentTimeMillis() - startTime;

            System.out.println("Total execution time taken in millis: "
                    + elapsedTime);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

}

最佳答案

只需使用System.currentTimeMillis()来记录之前/之后的时间(以毫秒为单位)。

System.nanoTime() 是另一种选择,但值得一读 this answer了解差异和用法。

关于java - 关于计算读取文件所花费的总时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12497546/

相关文章:

java - 如何从客户端创建远程 session EJB

ruby-on-rails - 获取特定工作日的最接近日期

linux - Linux/Unix I/O 类型

python - Python:从给定时间播放音频文件

java - 从文本文件读取时如何维护 EOL 字符?

java - BufferedReader 读取文件时会阻塞吗?

java - 媒体播放器在 R.raw 上失败 - Android

java - 使用 JProfiler 优化 JVM

java - 如何获取JTable中最后一个单元格的值?

sql - 如何使用时间戳减去特定小时数进行 SQL Server 选择