Source of EOFExceptionDemo.java


  1: import java.io.FileInputStream;
  2: import java.io.ObjectInputStream;
  3: import java.io.EOFException;
  4: import java.io.FileNotFoundException;
  5: import java.io.IOException;

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