Java对对象的序列化和反序列化

发表于:2011-11-01 09:20

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:a352193394    来源:51Testing软件测试网采编

分享:

  下面是第二种方式

  1. package com.bird.thinking;  
  2.   
  3. import java.io.Externalizable;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.ObjectInput;  
  9. import java.io.ObjectInputStream;  
  10. import java.io.ObjectOutput;  
  11. import java.io.ObjectOutputStream;  
  12. /** 
  13.  * @use 第二种可控制序列化变量个数的方式 
  14.  * @author Bird 
  15.  * 
  16.  */  
  17. public class Blip3 implements Externalizable {  
  18.     private int i;  
  19.     private String s;//没有实例化   
  20.     public Blip3(){  
  21.         System.out.println("Blip3 Constructor!!");  
  22.     }  
  23.     //注意  这里的s没有实例化   
  24.       
  25.     public Blip3(String x, int a){  
  26.         System.out.println("Blip3(String x, int a)");  
  27.         s = x;  
  28.         i = a;  
  29.         //s  和 i实例化在非默认构造函数中   
  30.     }  
  31.       
  32.     public String toString(){  
  33.         return s + i;  
  34.     }  
  35.       
  36.     public void writeExternal(ObjectOutput out){//可选择写入变量   
  37.         System.out.println("Blip3.writeExternal");  
  38.         try {  
  39.             out.writeObject(s);  
  40.             out.writeInt(i);  
  41.         } catch (IOException e) {  
  42.             throw new RuntimeException(e);  
  43.         }//必须进行这两项,否则就一个变量也不出初始化   
  44.     }  
  45.       
  46.     public void readExternal(ObjectInput in){//可选择读入数据   
  47.         System.out.println("Blip3.readExternal");  
  48.         try {  
  49.             s = (String)in.readObject();  
  50.             i = in.readInt();  
  51.         } catch (ClassNotFoundException e) {  
  52.               
  53.         } catch (IOException e) {  
  54.         throw new RuntimeException(e);  
  55.         }  
  56.           
  57.     }  
  58.       
  59.     public void read() throws FileNotFoundException, IOException, ClassNotFoundException{//读取序列化的类   
  60.         ObjectInputStream in = new ObjectInputStream(new FileInputStream("d://Blip3.out"));  
  61.         System.out.println("Revovering  b3");  
  62.         Blip3 b3 = (Blip3)in.readObject();  
  63.         System.out.println(b3);  
  64.     }  
  65.       
  66.     public void write() throws Exception{//写入对象   
  67.         Blip3 b3 = new Blip3("A String"47);  
  68.         System.out.println(b3);  
  69.         ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("d://Blip3.out"));  
  70.         System.out.println("Saving Object");  
  71.         o.writeObject(b3);  
  72.         o.close();  
  73.     }  
  74.       
  75.     public static void main(String[] args) throws Exception{  
  76.         Blip3 b = new Blip3();  
  77.         //  b.write();   
  78.         b.read();  
  79.     }  
  80. }

  输出结果为

  1. Blip3 Constructor!!  
  2. Blip3(String x, int a)  
  3. A String47  
  4. Saving Object  
  5. Blip3.writeExternal

  1. Blip3 Constructor!!  
  2. Revovering  b3  
  3. Blip3 Constructor!!  
  4. Blip3.readExternal  
  5. A String47

22/2<12
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号