前言
要使得类可以序列化和反序列化,只需要实现java.io.Serializable 接口或java.io.Externalizable 接口即可。序列化和反序列化可以提供对Java的远程方法调用(Remote Method Invocation RMI)的支持。
Java序列化的方式
Java序列化有三种方式:
- 实现
java.io.Serializable 接口 - 实现
java.io.Externalizable 接口、重写writeExternal() 和readExternal() 方法 - 实现
java.io.Serializable 接口、严格添加private void writeObject(ObjectOutputStream stream) throws IOException{} 和private void readObject(ObjectInputStream stream) throws IOException,ClassNotFoundException{} 方法
其中3可以看作是2的替代
区别
- 实现Serializable接口的类所有属性都可以被序列化和反序列化;Externalizable接口可以使用方法指定部分属性被序列化、反序列化。
- Serializable序列化时不会调用默认构造器;Externalizable会调用默认构造器。
Serializable和Externalizable接口
java.io.Serializable
此接口是类支持序列化的标志,需要序列化/反序列化的类要实现它。这个接口是一个空接口,仅仅作为标志使用。 一个类必须实现Serializable接口才能进行序列化!
java.io.Externalizable
继承自java.io.Serializable ,声明了两个方法writeExternal() 和readExternal()
ObjectStream对象流
序列化和反序列化是一个概念,具体实现是Object实例和ObjectStream流的互换,操作均在java.io
import java.io.*;
ObjectOutputStream序列化
序列化一般使用ObjectOutputStream类输出对象生成对象流,然后传入文件流写入文件
Person p = new Person();
p.setName("zhangsan");
p.setId("12345");
p.setSex("Male");
ObjectOutputStream oos = new ObjectOutStream(new FileOutputStream(new File("tmp.out")))
oos.writeObject(p);
查看序列化串
ObjectIntputStream反序列化
反序列化一般使用ObjectInputStream类读取文件输入文件流,然后传入对象流生成实例
ObjectInputStream oos = new ObjectInputStream(new FileInputStream(new File("tmp.out")))
Person p = (Person) ois.readObject();
反序列化的结果是Object,需要类型转换
同一对象流有多个序列化对象时:反序列化的顺序和序列化一致,按顺序read即可
Person p1 = (Person) ois.readObject();
Person p2 = (Person) ois.readObject();
Person p3 = (Person) ois.readObject();
如何序列化与反序列化?
Serializable接口实现demo
- Serializable接口方便简洁,常用
- 序列化全部属性
- 反序列化不调用(默认)构造器
import java.io.*;
public class SerializationTest {
public static void main(String[] args) throws Exception {
File f = File.createTempFile("serialize",".out");
System.out.println("[+]创建对象");
PersonSerialize p = new PersonSerialize("zhangsan",12321);
System.out.println("[.]序列化");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(p);
System.out.println("[.]反序列化");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
PersonSerialize pp = (PersonSerialize) ois.readObject();
System.out.println(pp);
f.deleteOnExit();
}
}
class PersonSerialize implements Serializable {
private static final long serialVersionUID = -2818606485066001460L;
public String name;
private int id;
public PersonSerialize() {
System.out.println("无参构造器");
}
public PersonSerialize(String name, int id) {
System.out.println("有参构造器");
this.name = name;
this.id = id;
}
}
输出:
[+]创建对象
有参构造器
[.]序列化
[.]反序列化
PersonSerialize@6f539caf
Externalizable接口实现demo
- Externalizable接口用于编程特殊需求
- 指定属性序列化
- 反序列化调用无参构造器
- 需要重写
readExternal 和readExternal 方法
import java.io.*;
public class ExternalSerializationTest {
public static void main(String[] args) throws Exception {
File f = File.createTempFile("externalserialize",".out");
System.out.println("[+]创建对象");
System.out.print("p1 ");
PersonExternalSerialize p1 = new PersonExternalSerialize();
System.out.print("p2 ");
PersonExternalSerialize p2 = new PersonExternalSerialize("zhangsan","Male",12321);
System.out.println("[.]序列化");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
System.out.print("p1 ");
oos.writeObject(p1);
System.out.print("p2 ");
oos.writeObject(p2);
System.out.println("[.]反序列化");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
System.out.print("p1 ");
PersonExternalSerialize pp = (PersonExternalSerialize) ois.readObject();
System.out.print("p2 ");
PersonExternalSerialize ppp = (PersonExternalSerialize) ois.readObject();
System.out.println(pp);
System.out.println(ppp);
System.out.println(ppp.sex);
f.deleteOnExit();
}
}
class PersonExternalSerialize implements Externalizable{
private static final long serialVersionUID = 4184813134939827841L;
public String name;
public String sex;
private int id;
@Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("writeExternal");
out.writeObject(name);
out.writeObject(id);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("readExternal");
this.name = (String) in.readObject();
this.id = (int) in.readObject();
}
public PersonExternalSerialize() {
System.out.println("无参构造器");
}
public PersonExternalSerialize(String name, String sex,int id) {
System.out.println("有参构造器");
this.name = name;
this.sex = sex;
this.id = id;
}
}
输出:
[+]创建对象
p1 无参构造器
p2 有参构造器
[.]序列化
p1 writeExternal
p2 writeExternal
[.]反序列化
p1 无参构造器
readExternal
p2 无参构造器
readExternal
PersonExternalSerialize@378fd1ac
PersonExternalSerialize@49097b5d
null
可以观察到sex属性没有进行序列化,因此其为null;反序列化先调用了无参对构造器
这里还演示了多个实例的反序列化,依次取出即可
完
欢迎在评论区留言,欢迎关注我的CSDN @Ho1aAs
|