JAVA序列化(二) 自定义序列化

简介:

在某些情况下,可能需要改变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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的序列和反序列是将对象转换为字节流以便存储或传输,并在需要时重新构造对象的过程。 要自定义序列和反序列,需要实现`Serializable`接口。这个接口是一个标记接口,没有任何方法需要实现。只要一个类实现了`Serializable`接口,它就可以被序列和反序列。 下面是一个示例代码,展示了如何自定义序列和反序列一个`Person`类: ```java import java.io.*; public class Person implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; // 构造函数 public Person(String name, int age) { this.name = name; this.age = age; } // 序列方法 private void writeObject(ObjectOutputStream out) throws IOException { out.writeObject(name); out.writeInt(age); } // 反序列方法 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { name = (String) in.readObject(); age = in.readInt(); } // 其他方法和属性... } ``` 在上面的示例中,`Person`类实现了`Serializable`接口,并定义了私有的`writeObject`和`readObject`方法。这些方法在进行序列和反序列时会被调用。 当使用`ObjectOutputStream`进行序列时,会调用`writeObject`方法,将对象的属性写入输出流。当使用`ObjectInputStream`进行反序列时,会调用`readObject`方法,从输入流中读取属性并重新构造对象。 使用自定义序列和反序列方法可以灵活地控制对象的序列过程,可以选择性地保存或恢复对象的某些属性。 希望这个示例能帮助你理解如何在Java中自定义序列和反序列

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值