Source of EOFExceptionDemo.java


  1: //EOFExceptionDemo.java

  3: import java.io.FileInputStream;
  4: import java.io.ObjectInputStream;
  5: import java.io.FileNotFoundException;
  6: import java.io.EOFException;
  7: import java.io.IOException;

  9: public class EOFExceptionDemo
 10: {
 11:     public static void main(String[] args)
 12:     {
 13:         String fileName = "out.dat";
 14:         try
 15:         {
 16:             ObjectInputStream inputStream =
 17:                 new ObjectInputStream(new FileInputStream(fileName));
 18:             System.out.println("\nReading all integers from the file "
 19:                 + fileName + ":");
 20:             try
 21:             {
 22:                 while (true)
 23:                 {
 24:                     int anInteger = inputStream.readInt();
 25:                     System.out.println(anInteger);
 26:                 }
 27:             }
 28:             catch(EOFException e)
 29:             {
 30:                 System.out.println("Finished reading from the file.");
 31:             }
 32:             inputStream.close();
 33:         }
 34:         catch(FileNotFoundException e)
 35:         {
 36:             System.out.println("Error opening the file " + fileName + ".");
 37:         }
 38:         catch(IOException e)
 39:         {
 40:             System.out.println("Error reading input from file " + fileName + ".");
 41:         }
 42:     }
 43: }