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

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