Buffering usually appropriate

Unbuffered input/output classes operate only on one byte at a time, while buffered streams interact with the disk in larger-sized chunks. Buffering input/output is usually recommended, since it's generally faster, and it should likely be the default style.

With small files, buffering usually makes little or no difference. However, it can be hard to predict if a file will always remain small.

You can run the following demo class on your host, to look for differences between buffered and unbuffered input. With large files (~1 Meg), you might see a difference of 2-3 times. (In Java Platform Performance by Wilson and Kesselman, an example using a 370K JPEG file has a gain in execution speed of 83x!)


import java.io.*;

/** JDK 6- */
public final class BufferDemo {

  public static void main (String... aArguments) {
    File file = new File("C:\\Temp\\blah.txt");
    verifyFile( file );

    Stopwatch stopwatch = new Stopwatch();

    stopwatch.start();
    readWithBuffer(file);
    stopwatch.stop();
    System.out.println("With buffering: " + stopwatch);

    stopwatch.start();
    readWithoutBuffer(file);
    stopwatch.stop();
    System.out.println("Without buffering: " + stopwatch);
  }

  /**
  * @param aFile is a file which already exists and can be read.
  */
  static public void readWithBuffer(File aFile) {
    try { 
      //use buffering, with default buffer size of 8K
      Reader input =  new BufferedReader(new FileReader(aFile));
      try {
        int data = 0;
        while ((data = input.read()) != -1){
          //do nothing
        }
      } 
      finally {
         input.close();
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }
  }

  /**
  * @param aFile is an existing file which can be read.
  */
  static public void readWithoutBuffer(File aFile) {
    try {
      Reader input =  new FileReader(aFile);
      try {
        //do not use buffering
        int data = 0;
        while ((data = input.read()) != -1){
          //do nothing
        }
      }
      finally {
        input.close();
      }
   }
   catch (IOException ex){
     ex.printStackTrace();
   }
 }

  private static void verifyFile(File aFile) {
    if (aFile == null) {
      throw new IllegalArgumentException("File should not be null.");
    }
    if (!aFile.exists()) {
      throw new IllegalArgumentException ("File does not exist: " + aFile);
    }
    if (!aFile.isFile()) {
      throw new IllegalArgumentException("Should not be a directory: " + aFile);
    }
    if (!aFile.canWrite()) {
      throw new IllegalArgumentException("File cannot be written: " + aFile);
    }
  }
} 



See Also :
Time execution speed
Would you use this technique?
Yes   No   Undecided   
© 2014 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -