Source of BinaryInputDemo.java


  1: //BinaryInputFile.java
  2: 
  3: import java.io.FileInputStream;
  4: import java.io.ObjectInputStream;
  5: import java.io.IOException;
  6: import java.io.FileNotFoundException;
  7: 
  8: 
  9: public class BinaryInputDemo
 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:             System.out.println("Reading the nonnegative integers");
 19:             System.out.println("in the file numbers.dat.");
 20:             n = inputStream.readInt( );
 21:             while (n >= 0)
 22:             {
 23:                 System.out.println(n);
 24:                 n = inputStream.readInt( );
 25:             }
 26:             
 27:             System.out.println("End of reading from file.");
 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: }
 40: