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

  1. /*
  2.  * @(#)NonSerialSuperExample.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. import java.io.*;
  22.  
  23. /** This example shows how to serialize a subclass whose superclass is not
  24.  * serializable.
  25.  
  26.  * When a superclass of a particular is not serializable, the subclass is
  27.  * responsible for saving the state of its supertype (in its writeObject)
  28.  *
  29.  * Compiled and Tested on JDK1.1 & JDK1.2
  30.  *
  31.  * How to run this example:
  32.  *                         Compile this file: javac NonSerialSuperExample.java
  33.  *                         Then run:          java NonSerialSuperExample
  34.  *
  35.  * This will print out a book object before and after serialization.
  36.  */
  37. public class NonSerialSuperExample {
  38.  
  39.     /**
  40.      * Creates a book object, serializes it, deserializes it and then prints 
  41.      * out to test that the serialization did work.
  42.      */
  43.     public static void main(String args[]) {
  44.  
  45.     // create a Book object 
  46.     Book bookorg = new Book(100, "How to Serialize", true, "R.R", "Serialization", 1997);
  47.     Book booknew = null;
  48.     
  49.     // serialize the Book
  50.     try {
  51.         FileOutputStream fo = new FileOutputStream("tmp");
  52.         ObjectOutputStream so = new ObjectOutputStream(fo);
  53.         so.writeObject(bookorg);
  54.         so.flush();
  55.     } catch (Exception e) {
  56.         System.out.println(e);
  57.         System.exit(1);
  58.     }
  59.     
  60.     // deserialize the Book
  61.     try {
  62.         FileInputStream fi = new FileInputStream("tmp");
  63.         ObjectInputStream si = new ObjectInputStream(fi);  
  64.         booknew = (Book) si.readObject();
  65.     }catch (Exception e) {
  66.         System.out.println(e);
  67.         System.exit(1);
  68.     }
  69.     
  70.     // The books should be the same if we did everything correctly!
  71.     System.out.println();
  72.     System.out.println("Printing original book...");
  73.     System.out.println(bookorg);
  74.     System.out.println("Printing new book... ");
  75.     System.out.println(booknew);
  76.     System.out.println("The original and new should be the same!");
  77.     System.out.println();
  78.     }  
  79. }
  80.  
  81.  
  82.  
  83.   
  84.  
  85.  
  86.