BufferedInputStream BufferedOutputStream

Measuring the Performance with BufferedInputStream BufferedOutputStream

The speed of copying or time taken for copying can be measured reading system time before and after while loop as follows.

       long beforeLoop = System.currentTimeMillis();
       while((temp = bistream.read()) != -1 )
      long afterLoop = System.currentTimeMillis();
      System.out.println("Time taken: " + (afterLoop – beforeLoop));

The static method, currentTimeMillis(), of System class returns the system time in milliseconds. Measuring the time before while loop and after while loop gives the time taken for copying. This style can be followed for any file copying program including FileToFile1.java. To have a noticeable difference, take a source file of atleast 2 lakhs bytes.

Using anonymous objects

      FileInputStream fistream = new FileInputStream("pqr.txt"); 
      BufferedInputStream bistream = new BufferedInputStream(fistream); 

Here, we use FileInputStream object only once just to open the file pqr.txt and fistream object is passed to BufferedInputStream() constructor. The above two statement can be replaced with one statement using anonymous objects.

      BufferedInputStream bistream = new BufferedInputStream(new FileInputStream("pqr.txt")); 

Similarly we can do with output streams also.

For basics of file copying, refer Semantics of File Copying.

1 thought on “BufferedInputStream BufferedOutputStream”

Leave a Comment

Your email address will not be published.