Source of ClassObjectIODemo.java


  1: //ClassObjectIODemo.java
  2: 
  3: import java.io.FileInputStream;
  4: import java.io.FileOutputStream;
  5: import java.io.ObjectInputStream;
  6: import java.io.ObjectOutputStream;
  7: import java.io.IOException;
  8: 
  9: public class ClassObjectIODemo
 10: {
 11:     public static void main(String[] args)
 12:     {
 13:         //Create an empty binary file
 14:         ObjectOutputStream outputStream = null;
 15:         String fileName = "species.records";
 16:         try
 17:         {
 18:             outputStream =
 19:                 new ObjectOutputStream(new FileOutputStream(fileName));
 20:         }
 21:         catch(IOException e)
 22:         {
 23:             System.out.println("Error opening output file " + fileName + ".");
 24:             System.exit(0);
 25:         }
 26: 
 27:         //Write two Species objects to the binary file
 28:         Species califCondor = new Species("California Condor", 27, 0.02);
 29:         Species blackRhino = new Species("Black Rhino", 100, 1.0);
 30:         try
 31:         {
 32:             outputStream.writeObject(califCondor);
 33:             outputStream.writeObject(blackRhino);
 34:             outputStream.close();
 35:         }
 36:         catch(IOException e)
 37:         {
 38:             System.out.println("Error writing to file " + fileName + ".");
 39:             System.exit(0);
 40:         }
 41:         System.out.println("\nTwo species records were sent to the file "
 42:             + fileName + ".");
 43: 
 44:         //Reopen the file, then read and display the two species objects
 45:         //First, reopen the file
 46:         System.out.println( "Now let's reopen the file and echo those records.");
 47:         ObjectInputStream inputStream = null;
 48:         try
 49:         {
 50:             inputStream =
 51:                 new ObjectInputStream(new FileInputStream("species.records"));
 52:         }
 53:         catch(IOException e)
 54:         {
 55:             System.out.println("Error opening the input file " + fileName + ".");
 56:             System.exit(0);
 57:         }
 58: 
 59: 
 60:         //Read the two species objects (note the necessary casting)
 61:         Species firstSpecies = null, secondSpecies = null;
 62:         try
 63:         {
 64:             firstSpecies = (Species)inputStream.readObject();
 65:             secondSpecies = (Species)inputStream.readObject();
 66:             inputStream.close( );
 67:         }
 68:         catch(Exception e)
 69:         {
 70:             System.out.println("Error reading from file " +
 71:                                 fileName + ".");
 72:             System.exit(0);
 73:         }
 74: 
 75:         //Display the two species objects read from the file
 76:         System.out.println("\nThe following were read from the file "
 77:             + fileName + ":");
 78:         System.out.println(firstSpecies);
 79:         System.out.println();
 80:         System.out.println(secondSpecies);
 81:         System.out.println("\nEnd of program.");
 82:     }
 83: }                                                        
 84: