home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / serialization / examples / externsuper / Savesuper.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  2.7 KB  |  94 lines

  1. /*
  2.  * @(#)Savesuper.java    1.1 98/10/03        
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * 
  20.  */
  21.  
  22. import java.io.*;
  23.  
  24. /** 
  25.  * When using the Externalizable Interface, an externalizable object
  26.  * must implement a writeExternal method to save the state of the object.
  27.  * It must also explicitly coordinate with its supertype to save its state.
  28.  
  29.  * This simple example shows how to do this for an object whose supertype
  30.  * is also externalizable.
  31.  *
  32.  *  How to Run: 
  33.  *             Compile the file:  javac Savesuper.java 
  34.  *           Then run: java Savesuper.java
  35.  *
  36.  * This should print out a book object before and after serialization.
  37.  * 
  38.  * Tested and compiled on JDK 1.1 & JDK 1.2
  39.  */
  40.  
  41. public class Savesuper {
  42.  
  43.     /**
  44.      * Create an Book (subclass of reading material) object, serialize it, 
  45.      * deserialize it and see that they are the same. So, basically test that
  46.      * this Externalizable example's works
  47.      */
  48.     public static void main(String args[]) {
  49.  
  50.     // create a Book object 
  51.     Book bookorg = new Book(100, "How to Serialize", true, "R.R", "Serialization", 97);
  52.     Book booknew = null;
  53.     
  54.     //serialize the book
  55.     try {
  56.         FileOutputStream fo = new FileOutputStream("tmp");
  57.         ObjectOutputStream so = new ObjectOutputStream(fo);
  58.         so.writeObject(bookorg);
  59.         so.flush();
  60.     } catch (Exception e) {
  61.         System.out.println(e);
  62.         System.exit(1);
  63.     }
  64.  
  65.     // de-serialize the Book
  66.     try {
  67.         FileInputStream fi = new FileInputStream("tmp");
  68.         ObjectInputStream si = new ObjectInputStream(fi);          
  69.         booknew = (Book) si.readObject();
  70.     }
  71.     catch (Exception e) {
  72.         System.out.println(e);
  73.         System.exit(1);
  74.     }
  75.  
  76.     /* 
  77.      * Print out the original and new book information
  78.      * It should be the same if we did everything correctly!
  79.      */
  80.     System.out.println();
  81.     System.out.println("Printing original book...");
  82.     System.out.println(bookorg);
  83.     System.out.println("Printing new book... ");
  84.     System.out.println(booknew);    
  85.     System.out.println("Both original and new should be the same!");
  86.     System.out.println();
  87.     }
  88. }
  89.  
  90.  
  91.  
  92.  
  93.   
  94.