Source of DeserializeObjects.java


  1: //DeserializeObjects.java
  2: 
  3: import java.io.*;
  4: 
  5: class DeserializeObjects
  6: {
  7:   public static void main(String args[])
  8:   {
  9:     ObjectInputStream objectIn = null;  //Stores the stream reference
 10:     int objectCount = 0;                //Number of objects read
 11:       Junk object = null;               //Stores an object reference
 12:     try
 13:     {
 14: 
 15:       objectIn =  new ObjectInputStream(
 16:                   new BufferedInputStream(
 17:                   new FileInputStream("JunkObjects.bin")));
 18: 
 19:       //Read from the stream until we hit the end
 20:       while(true)
 21:       {
 22:         object = (Junk)objectIn.readObject();  //Read an object
 23:         objectCount++;                         //Increment the count
 24:         System.out.println(object);            //Output the object
 25:       }
 26:     }
 27:     catch(ClassNotFoundException e)
 28:     {
 29:       e.printStackTrace(System.err);
 30:       System.exit(1);
 31: 
 32:     }
 33:     catch(EOFException e)
 34:     {             //This will execute when we reach EOF
 35:       System.out.println("EOF reached. "+ objectCount + " objects read.");
 36: 
 37:     }
 38:     catch(IOException e)
 39:     {//This is for other I/O errors
 40:       e.printStackTrace(System.err);
 41:       System.exit(1);
 42:     }
 43: 
 44:     //Close the stream
 45:     try
 46:     {
 47:       objectIn.close(); //Close the input stream
 48: 
 49:     }
 50:     catch(IOException e)
 51:     {
 52:       e.printStackTrace(System.err);
 53:       System.exit(1);
 54:     }
 55:   }
 56: }