/*对象序列化:
* 将那些实现了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);
}
}