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:         try
 14:         {
 15:             ObjectInputStream inputStream =
 16:               new ObjectInputStream(new FileInputStream("numbers.dat"));
 17:             int n;
 18: 
 19:             System.out.println("Reading ALL the integers");
 20:             System.out.println("in the file numbers.dat.");
 21:             try
 22:             {
 23:                 while (true)
 24:                 {
 25:                     n = inputStream.readInt( );
 26:                     System.out.println(n);
 27:                 }
 28:             }
 29:             catch(EOFException e)
 30:             {
 31:                 System.out.println("End of reading from file.");
 32:             }
 33:             inputStream.close( );
 34:         }
 35:         catch(FileNotFoundException e)
 36:         {
 37:             System.out.println("Cannot find file numbers.dat.");
 38:         }
 39:         catch(IOException e)
 40:         {
 41:             System.out.println("Problem with input from file numbers.dat.");
 42:         }
 43:     }
 44: }