Source of BinaryInputDemo.java


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