java的Serialization 机制

[size=large]基本使用方法[/size]
Serialization是指把类或者基本的数据类型持久化(persistence)到数据流(Stream)中,包括文件、字节流、网络数据流。
JAVA中实现serialization主要靠两个类:ObjectOuputStream和ObjectInputStream。他们是JAVA IO系统里的OutputStream和InputStream的子类。既然他们是JAVA IO中的流,那么就可以像操作一般的流一样来操作他们。下面是他们使用方法:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Pair implements Serializable{

private static final long serialVersionUID = -1874850715617681161L;
private int type;
private String name;

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public Pair(int type, String name) {
super();
this.type = type;
this.name = name;
}

public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
//serialize object pair
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
Pair pair = new Pair(1, "charlie");
oos.writeObject(pair);
//deserialize object, get new object newpair
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Pair newpair = (Pair) ois.readObject();

System.out.println(newpair.getType()+":"+newpair.getName());
}
}



1. 这两个类都是decorator模式的,在创建他们的时候,都要传入一个基于字节的流,真正在底下存贮序列化数据的都是这些流。
2. 被持久化的类要实现Serializable接口,这个接口没有任何函数,只是一个标记接口。如果在一台机器上进行序列化,把得到的数据传送到另外一个机器上进行反序列化,那么这两台机器上的类应该是完全一样的,否则序列化是不会成功的。
3. 切记不要把上面代码中的bos用toString得到String,然后再从这个String中得到ByteArrayInputStream,再进行反序列化。bos是以字节存贮的,转成以字符存贮的String必然会造成数据的变化,而从String中到的byte[]也不会是之前那个byte[]了。我遇到过这个问题,是因为我想把序列化之后的数据存在xml文件中。这个问题的具体解决方法见我的另外一篇博客:[url]http://zzy1943.iteye.com/blog/634553[/url]

[size=large]java虚拟机在序列化和反序列化的时候都做了些什么?[/size]

javadoc中对这两个类的描述中对java的序列化机制进行了详细的描述:
[quote]
The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.
[/quote]
默认的序列化机制写到流中的数据有:
1、对象所属的类
2、类的签名
3、所有的非transient和非static的属性
4、对其他对象的引用也会造成对这些对象的序列化
5、如果多个引用指向一个对象,那么会使用sharing reference机制
[quote]
Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

private void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException;
private void writeObject(java.io.ObjectOutputStream stream)
throws IOException
private void readObjectNoData()
throws ObjectStreamException;

[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值