ITeye博客

java的Serialization 机制

zzy1943 · 2010-04-06 11:23 · JAVASE · 编程语言 · Markdown

基本使用方法
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文件中。这个问题的具体解决方法见我的另外一篇博客:http://zzy1943.iteye.com/blog/634553

java虚拟机在序列化和反序列化的时候都做了些什么?

javadoc中对这两个类的描述中对java的序列化机制进行了详细的描述:

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.

默认的序列化机制写到流中的数据有: 1、对象所属的类 2、类的签名 3、所有的非transient和非static的属性 4、对其他对象的引用也会造成对这些对象的序列化 5、如果多个引用指向一个对象,那么会使用sharing reference机制

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures: [code=java] private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException; private void writeObject(java.io.ObjectOutputStream stream) throws IOException private void readObjectNoData() throws ObjectStreamException; [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。