//BinaryInputDemo.java

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.FileNotFoundException;
import java.io.EOFException;
import java.io.IOException;
import java.util.Scanner;

public class BinaryInputDemo
{
  public static void main(String[] args)
  {
     String fileName = "numbers.dat";
     try
     {
         ObjectInputStream inputStream = 
             new ObjectInputStream(new FileInputStream(fileName));
         System.out.println("\nReading all nonnegative integers"
             + "from the file " + fileName + ",\nup to "
             + "first negative sentinel value:");
         int anInteger = inputStream.readInt();
         while (anInteger >= 0)
         {
             System.out.println(anInteger);
             anInteger = inputStream.readInt();
         }
         System.out.println("Finished reading from the file.");
         inputStream.close();
     }
     catch(FileNotFoundException e)
     {
         System.out.println("Error opening the file " + fileName + ".");
     }
     catch(EOFException e)
     {
         System.out.println("Error reading the file " + fileName + ".");
         System.out.println("Encountered the end of the file.");
     }
     catch(IOException e)
     {
         System.out.println("Error reading input from file " + fileName + ".");
     }
  }
}

