Source of BinaryInputDemo.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;
  6: import java.util.Scanner;

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