Java 关于计算读取文件所需的总时间

Java 关于计算读取文件所需的总时间,java,time,io,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 {

在我的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()
是另一个选项,但了解不同之处和用法值得一读。

@ratchetfreak-你认为这是一个小文件,不是吗?