Performance gain of a BufferedInputStream over an InputStream

IO happens a lot faster if you wrap your original InputStream using BufferedInputStream. Internally, it uses a default buffer size of 2048 bytes. For example, wrapping a FileInputStream in a BufferedInputStream will result in a significant performance gain.

Main.java:

import java.io.*;
  
public class Main {
   public static void main(String args[]) {
      if (args.length != 1) {
         System.err.println("Usage: java Main <file.txt>");
         System.exit(1);
      }
 
      long start1 = System.currentTimeMillis();
      readFileIS(args[0]);
      long end1 = System.currentTimeMillis();
      System.out.println("Reading using FileInputStream: " + (end1 - start1) + " ms.");
  
      long start2 = System.currentTimeMillis();
      readFileBIS(args[0]);
      long end2 = System.currentTimeMillis();
      System.out.println("Reading using BufferedInputStream: " + (end2 - start2) + " ms.");
   }  
 
   public static void readFileIS(String filename) {
      try {
         FileInputStream fis = new FileInputStream(filename);
         while (fis.read() > 0) ;
      }
      catch(Exception e) {
         System.err.println(e);
         System.exit(1);
      }
   }
 
   public static void readFileBIS(String filename) {
      try {
         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
         while (bis.read() > 0) ;
      }
      catch(Exception e) {
         System.err.println(e);
         System.exit(1);
      }
   }
}