0

I have an log file named abcd.log in my C: drive that I am reading it through a java program , I want to measure How much time the program had taken to completely read the log file please advise how to achieve this..!!

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

I have come up with this..

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

    }

}
raam86
  • 6,785
  • 2
  • 31
  • 46

1 Answers1

2

Just use System.currentTimeMillis() to record the before/after time in milliseconds.

System.nanoTime() is another option but it's worth reading this answer to understand differences and usages.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440