简介:
在某些情况下,可能需要改变Serializable类的默认序列化过程,或者需要对一个没有实现Serializable的属性进行序列化,此时就需要自定义序列化流程。
实现:
1)目标类实现Serializable,上述两种情况中的属性设为transient,通过增加下列两个方法来自定义该属性的序列化过程
private void writeObject(java.io.ObjectOutputStream out) throws IOException;
private void readObject(java.io.ObjectInputStream in) throws IOException,ClassNotFoundException;
实例
Student.java
package com.siyuan.serializable;
import java.io.IOException;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = -8218974228671917830L;
private int id;
private String stuNo;
private String name;
private transient Teacher teacher;
public Student(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("Student[");
result.append("id=").append(this.id);
result.append(",stuNo=").append(this.stuNo);
result.append(",name=").append(this.name);
result.append(",teacher=").append(this.teacher);
result.append("]");
return result.toString();
}
private void writeObject(java.io.ObjectOutputStream out)
throws IOException {
System.out.println("writeObject ... ");
out.defaultWriteObject();
out.writeInt(this.teacher.getId());
out.writeObject(this.teacher.getTeachNo());
out.writeObject(this.teacher.getName());
}
private void readObject(java.io.ObjectInputStream in)
throws IOException,ClassNotFoundException {
System.out.println("readObject ... ");
in.defaultReadObject();
this.teacher = new Teacher();
this.teacher.setId(in.readInt());
this.teacher.setTeachNo((String) in.readObject());
this.teacher.setName((String) in.readObject());
}
}
Teacher
package com.siyuan.serializable;
public class Teacher {
private int id;
private String teachNo;
private String name;
public Teacher() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTeachNo() {
return teachNo;
}
public void setTeachNo(String teachNo) {
this.teachNo = teachNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("Teacher[");
result.append("id=").append(this.id);
result.append(",teachNo=").append(this.teachNo);
result.append(",name=").append(this.name);
result.append("]");
return result.toString();
}
}
StudentSerializableTest
package com.siyuan.serializable.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.siyuan.serializable.Student;
import com.siyuan.serializable.Teacher;
public class StudentSerializableTest {
/**
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
Student stu = new Student();
stu.setId(1);
stu.setStuNo("001");
stu.setName("siyuan");
Teacher teacher = new Teacher();
teacher.setId(101);
teacher.setTeachNo("101");
teacher.setName("carro");
stu.setTeacher(teacher);
System.out.println(stu);
System.out.println("Serialized begin...");
ByteArrayOutputStream output = new ByteArrayOutputStream();
ObjectOutputStream objOutput = new ObjectOutputStream(output);
objOutput.writeObject(stu);
System.out.println("Serialized end...");
System.out.println("Deserialized begin...");
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
ObjectInputStream objInput = new ObjectInputStream(input);
Student stuDe = (Student) objInput.readObject();
System.out.println("Deserialized end...");
System.out.println(stuDe);
}
}
运行结果:
Student[id=1,stuNo=001,name=siyuan,teacher=Teacher[id=101,teachNo=101,name=carro]]
Serialized begin...
writeObject ...
Serialized end...
Deserialized begin...
readObject ...
Deserialized end...
Student[id=1,stuNo=001,name=siyuan,teacher=Teacher[id=101,teachNo=101,name=carro]]
2)通过实现Externalizable来完全控制目标类的序列化过程
实例
StudentCp.java
package com.siyuan.serializable;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class StudentCp implements Externalizable{
private static final long serialVersionUID = -8218974228671917830L;
private int id;
private String stuNo;
private String name;
private Teacher teacher;
public StudentCp(){
}
public void copyFrom(StudentCp cp) {
this.id = cp.id;
this.stuNo = cp.stuNo;
this.name = cp.name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("Student[");
result.append("id=").append(this.id);
result.append(",stuNo=").append(this.stuNo);
result.append(",name=").append(this.name);
result.append(",teacher=").append(this.teacher);
result.append("]");
return result.toString();
}
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
System.out.println("readExternal ... ");
this.setId(in.readInt());
this.setStuNo((String) in.readObject());
this.setName((String) in.readObject());
this.teacher = new Teacher();
this.teacher.setId(in.readInt());
this.teacher.setTeachNo((String) in.readObject());
this.teacher.setName((String) in.readObject());
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("writeExternal ... ");
out.writeInt(this.id);
out.writeObject(this.stuNo);
out.writeObject(this.name);
out.writeInt(this.teacher.getId());
out.writeObject(this.teacher.getTeachNo());
out.writeObject(this.teacher.getName());
}
}
StudentCpSerializableTest
package com.siyuan.serializable.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.siyuan.serializable.StudentCp;
import com.siyuan.serializable.Teacher;
public class StudentCpSerializableTest {
/**
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
StudentCp stu = new StudentCp();
stu.setId(1);
stu.setStuNo("001");
stu.setName("siyuan");
Teacher teacher = new Teacher();
teacher.setId(101);
teacher.setTeachNo("101");
teacher.setName("carro");
stu.setTeacher(teacher);
System.out.println(stu);
System.out.println("Serialized begin...");
ByteArrayOutputStream output = new ByteArrayOutputStream();
ObjectOutputStream objOutput = new ObjectOutputStream(output);
objOutput.writeObject(stu);
System.out.println("Serialized end...");
System.out.println("Deserialized begin...");
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
ObjectInputStream objInput = new ObjectInputStream(input);
StudentCp stuDe = (StudentCp) objInput.readObject();
System.out.println("Deserialized end...");
System.out.println(stuDe);
}
}
运行结果:
Student[id=1,stuNo=001,name=siyuan,teacher=Teacher[id=101,teachNo=101,name=carro]]
Serialized begin...
writeExternal ...
Serialized end...
Deserialized begin...
readExternal ...
Deserialized end...
Student[id=1,stuNo=001,name=siyuan,teacher=Teacher[id=101,teachNo=101,name=carro]]
参考资料:
http://www.javapractices.com/home/HomeAction.do