import java.io.*;

public class FileAccess {
  
  /** read each line of a file and display the contents 
   * on the screen
   * @throws IOException
   */
  public static void displayFile(String filename) 
    throws IOException {
    /* Note that we indicate that this method throws an IOException.
     * Whenever we use a java class method that throws an exception in one of our
     * methods, we must indicate the that our method also could throw an exception.
     * And, if some method uses our method, it must also state that it "throws"
     * an exception ... and so on.
     * Try removing the throws IOException.  We are using the FileReader
     * constructor which throws an IOException. More specifically, if you look
     * at the API, you will see that it throws a FileNotFoundException which is a 
     * subclass of an IOException - therefor we must also indicate that our method
     * throws an IOException or a FileNotFoundException.
     */
    // uncomment the following line to force an exception to happen in execution
    // int i = 5/0;  // should cause a divide by zero error - execution fails
    FileReader f = new FileReader(filename);
    // a FileReader only reads a character at a time
    /* the buffered reader saves (buffers) all the characters
       until it reaches an end of line/file character.  It
       then returns the whole line at once each time readLine
       is called .
    */
    BufferedReader br = new BufferedReader(f);
    String temp = br.readLine();
    
    while(temp != null) {
      System.out.println(temp);
      temp = br.readLine();
    }
    /* don't forget to close up before you leave! */
    f.close();
    br.close();
  }
}

