JAVA中对象的序列化
什么是序列化?
JAVA在运行时环境用来支持用户定义类型的流化的机制。它是将对象实例的状态存储到存储媒体的过程。在此过程中,先将对象的公共字段和私有字段以及类的名称(包括类所在的程序集)转换为字节流,然后再把字节流写入数据流。在随后对对象进行反序列化时,将创建出与原对象完全相同的副本。
序列化的目的:
1、以某种存储形式使自定义对象持久化;
2、将对象从一个地方传递到另一个地方。
只要我们的class 实现了java.io.Serializable接口,就可以利用ObjectOutputStream的writeObject()方法将一个对象序列化到文件或字符流;同时利用ObjectInputStream的readObject()方法,就可以从文件或字符流里读出的object对象。Serializable接口不需要我们实现任何方法。
下面是一个比较简单的把对象序列化到文件保存,再从文件反序列化读出并调用的例子。让我们来看一下整个过程:
Staffer实现了java.io.Serializable接口,成为一个可序列化的类。我们首先构造一个Staffer的对象Staffer1然后将其保存(序列化)在一个文件中,而后再将其读出(反序列化),并调用其toString方法打印内容。
package test.code;
import java.io.*;
public class Staffer implements Serializable {
int id;
String addr;
String name;
boolean isOnJob;
public Staffer() {
}
public Staffer(int id, String name, String addr, boolean isOnJob) {
this.id = id;
this.name = name;
this.addr = addr;
this.isOnJob = isOnJob;
}
/**
* deserialize
*/
public static Staffer deserialize(ObjectInputStream objIn) throws Exception {
Staffer s = (Staffer) objIn.readObject();
return s;
}
public String toString() {
return "DATA: id " + this.id +
" name " + this.name +
" address " + this.addr +
" isOnJob " + this.isOnJob;
}
/**
* serialize
*/
public void serialize(ObjectOutputStream oout) throws Exception {
oout.writeObject(this);
}
public static void main(String[] args) {
Staffer staffer1 = new Staffer(752, "linlan",
"dayer street #5 building 02-287", false);//构造一个新的对象
FileInputStream fIn = null;
FileOutputStream fOut = null;
ObjectInputStream objIn = null;
ObjectOutputStream objOut = null;
String fName = "d:\\Staffer.txt";
try {
fOut = new FileOutputStream(fName);
objOut = new ObjectOutputStream(fOut);
staffer1.serialize(objOut);//序列化
objOut.close();
objOut = null;
fIn = new FileInputStream("d:\\Staffer.txt");
objIn = new ObjectInputStream(fIn);
Staffer staffer2 = Staffer.deserialize(objIn);//反序列化
System.out.println(staffer2);//打印结果
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fIn != null) {
fIn.close();
}
if (objIn != null) {
objIn.close();
}
if (fOut != null) {
fOut.close();
}
if (objOut != null) {
objOut.close();
}
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
}
import java.io.*;
public class Staffer implements Serializable {
int id;
String addr;
String name;
boolean isOnJob;
public Staffer() {
}
public Staffer(int id, String name, String addr, boolean isOnJob) {
this.id = id;
this.name = name;
this.addr = addr;
this.isOnJob = isOnJob;
}
/**
* deserialize
*/
public static Staffer deserialize(ObjectInputStream objIn) throws Exception {
Staffer s = (Staffer) objIn.readObject();
return s;
}
public String toString() {
return "DATA: id " + this.id +
" name " + this.name +
" address " + this.addr +
" isOnJob " + this.isOnJob;
}
/**
* serialize
*/
public void serialize(ObjectOutputStream oout) throws Exception {
oout.writeObject(this);
}
public static void main(String[] args) {
Staffer staffer1 = new Staffer(752, "linlan",
"dayer street #5 building 02-287", false);//构造一个新的对象
FileInputStream fIn = null;
FileOutputStream fOut = null;
ObjectInputStream objIn = null;
ObjectOutputStream objOut = null;
String fName = "d:\\Staffer.txt";
try {
fOut = new FileOutputStream(fName);
objOut = new ObjectOutputStream(fOut);
staffer1.serialize(objOut);//序列化
objOut.close();
objOut = null;
fIn = new FileInputStream("d:\\Staffer.txt");
objIn = new ObjectInputStream(fIn);
Staffer staffer2 = Staffer.deserialize(objIn);//反序列化
System.out.println(staffer2);//打印结果
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fIn != null) {
fIn.close();
}
if (objIn != null) {
objIn.close();
}
if (fOut != null) {
fOut.close();
}
if (objOut != null) {
objOut.close();
}
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
}
posted on 2006-11-24 17:26 changhai-xuri 阅读(387) 评论(0) 编辑 收藏 举报