java 对象序列化

/*对象序列化:
 * 将那些实现了Serializable接口的对象转换成一个字节序列,并能够将这个字节完全恢复为原来的对象。*/
class Data implements Serializable{
	private int n;
	public Data(int n){this.n=n;}
	public String toString(){return Integer.toString(n);}
}
public class Worm implements Serializable{
   private static Random rand=new Random(47);
   private Data[] d={
		   new Data(rand.nextInt(10)),
		   new Data(rand.nextInt(10)),
		   new Data(rand.nextInt(10))
   };
   private Worm next;
   private char c;
   public Worm(int i,char x){
	   print("Worm constructor: "+i);
	   c=x;
	   if(--i>0)
		   next=new Worm(i,(char)(x+1));
   }
   public Worm(){
	   print("Default constructor");
   }
   public String toString(){
	   StringBuilder result=new StringBuilder(":");
	   result.append(c);
	   result.append("(");
	   for(Data dat:d)
		   result.append(dat);
	   result.append(")");
	   if(next!=null)
		   result.append(next);
	   return result.toString();
   }
   public static void main(String[]args)throws ClassNotFoundException,IOException{
	   Worm w=new Worm(6,'a');
	   print("w= "+w);
	   ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("worm.out"));
	   out.writeObject("Worm storage\n");
	   out.writeObject(w);
	   out.close();
	   ObjectInputStream in=new ObjectInputStream(new FileInputStream("worm.out"));
	   String s=(String)in.readObject();
	   Worm w2=(Worm)in.readObject();
	   print(s+"w2= "+w2);
	   ByteArrayOutputStream bout=new ByteArrayOutputStream();
	   ObjectOutputStream out2=new ObjectOutputStream(bout);
	   out2.writeObject("Worm storage\n");
	   out2.writeObject(w);
	   out2.flush();
	   ObjectInputStream in2=new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
	   s=(String)in2.readObject();
	   Worm w3=(Worm)in2.readObject();
	   print(s+"w3 = "+w3);
   }
   
}

序列化的控制:

例如不希望对象的某一部分被序列化,或者一个对象被还原后,某个子对象需要重新创建,从而不必将子对象序列化。

/*序列化控制。实现Externalizable接口,增加两个方法,这两个方法会在序列化还原的过程中被自动调用。
 * 默认构造器必须是public,对于一个Externalizable对象,所有普通的默认构造器都会被调用(包括在字段定义时的初始化),
 * 然后调用readExternal()。*/
public class Blip3 implements Externalizable{
   private int i;
   private String s;
   public Blip3(){
	   print("Blip3 Constructor");
   }
   public Blip3(String x,int a){
	   print("Blip3(String x,int a)");
	   s=x;
	   i=a;
   }
   public String toString(){return s+i;}
   public void writeExternal(ObjectOutput out)throws IOException{
	   print("Blip3.writeExternal");
	   out.writeObject(s);
	   out.writeInt(i);
   }
   public void readExternal(ObjectInput in)throws IOException,ClassNotFoundException{
	   print("Blip3.readExternal");
	   s=(String)in.readObject();
	   i=in.readInt();
   }
   public static void main(String[]args)throws IOException,ClassNotFoundException{
	   print("Constructing objects:");
	   Blip3 b3=new Blip3("A String ",47);
	   print(b3);
	   
	   ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream("Blip3.out"));
	   print("Saving object:");
	   o.writeObject(b3);
	   o.close();
	   ObjectInputStream in=new ObjectInputStream(new FileInputStream("Blip3.out"));
	   print("Recovering b3:");
	   b3=(Blip3)in.readObject();
	   print(b3);
   }
}
transient(关键字)
/*transient(瞬时) 关键字
 * 如果正在操作的是一个Serializable对象,那么所有的序列化操作都会自动进行,为了能够控制,
 * 可以用transient(瞬时)关键字逐个字段地关闭序列化,他的意思是"不用麻烦你保存或恢复数据——我们会处理"*/
public class Logon implements Serializable{
   private Date date=new Date();
   private String username;
   private transient String password;
   public Logon(String name,String pwd){
	   username=name;
	   password=pwd;
   }
   public String toString(){
	   return "logon info: \n username: "+username+"\n date: "+date+"\n password: "+password;
   }
   public static void main(String[]args)throws Exception{
	   Logon a=new Logon("Mulk","MyLittlePony");
	   print("logon a= "+a);
	   ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream("Logon.out"));
	   o.writeObject(a);
	   o.close();
	   TimeUnit.SECONDS.sleep(1);
	   ObjectInputStream in=new ObjectInputStream(new FileInputStream("Logon.out"));
	   print("Recovering object at "+new Date());
	   a=(Logon)in.readObject();
	   print("logon a= "+a);
   }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值