Source of EOFExceptionDemo.java


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