Source of EOFExceptionDemo.java


  1: //EOFExceptionDemo.java
  2: 
  3: import java.io.FileInputStream;
  4: import java.io.ObjectInputStream;
  5: import java.io.EOFException;
  6: import java.io.FileNotFoundException;
  7: import java.io.IOException;
  8: 
  9: public class EOFExceptionDemo
 10: {
 11:     public static void main(String[] args)
 12:     {
 13:         String fileName = "numbers.dat";
 14:         try
 15:         {
 16:             ObjectInputStream inputStream =
 17:                  new ObjectInputStream(new FileInputStream(fileName));
 18:             System.out.println("Reading ALL the integers");
 19:             System.out.println("in the file " + 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("End of reading from file.");
 31:             }
 32:             inputStream.close();
 33:         }
 34:         catch (FileNotFoundException e)
 35:         {
 36:             System.out.println("Cannot find file " + fileName);
 37:         }
 38:         catch (IOException e)
 39:         {
 40:             System.out.println("Problem with input from file " + fileName);
 41:         }
 42:     }
 43: }