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

发布时间:2011-10-20 01:40:54   来源:文档文库   
字号:

为什么需要序列化和反序列化:

当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要把这个Java对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为Java对象。

Java对象转换为字节序列的过程称为对象的序列化字节序列恢复为Java对象的过程称为对象的反序列化

对象的序列化主要有两种用途:

1) 持久化: 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;比如:休眠的实现。以后服务器session管理,hibernate将对象持久化实现。

2) 网络通信:在网络上传送对象的字节序列。比如:服务器之间的数据通信,对象传递

序列化涉及的类和接口:

java.io.ObjectOutputStream代表对象输出流,它的writeObject(Object obj)方法可对参数指定的obj对象进行序列化,把得到的字节序列写到一个目标输出流中。

java.io.ObjectInputStream代表对象输入流,它的readObject()方法从一个源输入流中读取字节序列,再把它们反序列化为一个对象,并将其返回。

只有实现了Serializable接口的类的对象才能被序列化。

序列化/反序列化的步骤和简单测试:

1. 将类Person的实例进行序列化和反序列化

a) Person类实现Serializable接口:

class Person implements Serializable {

int age;

boolean isMan;

String name;

public Person(int age, boolean isMan, String name) {

super();

this.age = age;

this.isMan = isMan;

this.name = name;

}

}

b) 通过ObjectOutputStream将Person对象的数据写入到文件中,即序列化。

Person person = new Person(20,true,"aa");

FileOutputStream fos = null;;

ObjectOutputStream oos =null;

//序列化

fos = new FileOutputStream("d:/c.txt");

oos = new ObjectOutputStream(fos);

oos.writeObject(person);

oos.flush();

oos.close();

fos.close();

c) 通过ObjectInputStream将文件中二进制数据反序列化成Person对象:

ObjectInputStream ois = null;

FileInputStream fis = null;

//反序列化

fis = new FileInputStream("d:/c.txt");

ois = new ObjectInputStream(fis);

Person p = (Person) ois.readObject();

System.out.println(p.name);

综合的练习:

class Person implements Serializable {

int age;

boolean isMan;

String name;

public Person(int age, boolean isMan, String name) {

super();

this.age = age;

this.isMan = isMan;

this.name = name;

}

}

public class TestDataStream {

public static void main(String[] args) {

ObjectInputStream ois = null;

FileInputStream fis = null;

Person person = new Person(20,true,"aa");

FileOutputStream fos = null;;

ObjectOutputStream oos =null;

try {

//序列化

fos = new FileOutputStream("d:/c.txt");

oos = new ObjectOutputStream(fos);

oos.writeObject(person);

oos.writeObject(new String("cccc"));

oos.writeObject(new Date());

oos.writeInt(300);

oos.writeBoolean(true);

oos.flush();

//反序列化

fis = new FileInputStream("d:/c.txt");

ois = new ObjectInputStream(fis);

Person p = (Person) ois.readObject();

String s = (String) ois.readObject();

Date date = (Date) ois.readObject();

int i = ois.readInt();

boolean b = ois.readBoolean();

System.out.println(p.name);

System.out.println(s);

System.out.println(date);

System.out.println(i);

System.out.println(b);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

ois.close();

} catch (IOException e) {

e.printStackTrace();

}

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

本文来源:https://www.2haoxitong.net/k/doc/5450a7553c1ec5da50e2705b.html

《java对象的序列化和反序列化.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式