2012-09-19 52 views
0

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

    } 

} 
+0

[你有什么尝试?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

+0

'System.nanotime()'? – Nishant

+0

@ratchetfreak - 你假设这是一个小文件,不是吗? –

回答

2

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

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

+0

+1并采取差异。 –

相关问题