]在《java 序列化》中已经对序列化做过简单的介绍,今天继续说一下序列化
前瞻
首先对上一篇文章中的序列化进行总结
- 实现
java.io.Serializable
接口即可对对象进行实例化- 实现Serializable接口的同时,手动指定
serialVersionUID
,这样即使后面对类进行的更改,在反序列化的时候不会报错- 父类没有实现该接口,子类实现了,那么父类中的属性不会序列化。解决方案就是在父类实现Serializable接口
- static静态属性是不支持序列化的
transient
如果某个属性不需要序列化可以使用该关键字进行修饰- 虚拟机在默认的情况下回调用对象的writeObject和readObject方法进行自定义的序列化和反序列化,如果没有则调用
ObjectInputStream
里面的defaultReadObject和ObjectOutputStream
里的defaultWriteObject方法进行- 如果没有实现Serializable接口,父类必须含有无惨构造函数
java.io.Externalizable
正常情况下使用java.io.Serializable
基本已经足够,但是还是有必要介绍一下java.io.Externalizable
,对于自定义序列化内容,有性能极高的需求时候还是可以采用的。
下面先看一下Externalizable的基础用法
注:部分代码里面可能会掺杂一些Lombok的注解,如有需要可以自己去配置
实验一:有默认构造函数
public class Animal implements Externalizable {
@Getter@Setter
transient String name;
@Getter@Setter
int age;
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
out.writeObject(age);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = (String) in.readObject();
age = (int) in.readObject();
}
@Override
public String toString() {
return "{\r\n Animal:\r\n -- name = " + name + "\r\n -- age = " + age + " \r\n}";
}
}
public class AnimalSerializableDemo {
public static void main(String[] args) throws Exception {
String serializableFile = "./animal.ser";
FileOutputStream fos = null;
ObjectOutputStream oos = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
// 为了节省博文篇幅,直接简单粗暴的处理,不必纠结细节
fos = new FileOutputStream(new File(serializableFile));
oos = new ObjectOutputStream(fos);
Animal animal = new Animal();
animal.age = 1;
animal.name = "emo";
oos.writeObject(animal);
oos.close();
fis = new FileInputStream(new File(serializableFile));
ois = new ObjectInputStream(fis);
Animal animal2 = (Animal) ois.readObject();
System.out.println(animal2);
fis.close();
}
}
// 输出结果
{
Animal:
-- name = emo
-- age = 1
}
实验二:无默认构造函数
public class Animal2 implements Externalizable {
@Getter@Setter
transient String name;
@Getter@Setter
int age;
public Animal2(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
out.writeObject(age);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = (String) in.readObject();
age = (int) in.readObject();
}
@Override
public String toString() {
return "{\r\n Animal:\r\n -- name = " + name + "\r\n -- age = " + age + " \r\n}";
}
}
public class Animal2SerializableDemo {
public static void main(String[] args) throws Exception {
String serializableFile = "./animal2.ser";
FileOutputStream fos = null;
ObjectOutputStream oos = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
// 为了节省博文篇幅,直接简单粗暴的处理,不必纠结细节
fos = new FileOutputStream(new File(serializableFile));
oos = new ObjectOutputStream(fos);
Animal2 animal = new Animal2("emo2", 2);
oos.writeObject(animal);
oos.close();
fis = new FileInputStream(new File(serializableFile));
ois = new ObjectInputStream(fis);
Animal2 animal2 = (Animal2) ois.readObject();
System.out.println(animal2);
fis.close();
}
}
// 输出结果
Exception in thread "main" java.io.InvalidClassException: com.smxknife.java2.externalizable.Animal2; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:150)
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:790)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2001)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at com.smxknife.java2.externalizable.Animal2SerializableDemo.main(Animal2SerializableDemo.java:27)
实验三:Externalizable默认实现
public class AnimalSample implements Externalizable {
@Getter@Setter
transient String name;
@Getter@Setter
int age;
@Override
public void writeExternal(ObjectOutput out) throws IOException {
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
@Override
public String toString() {
return "{\r\n Animal:\r\n -- name = " + name + "\r\n -- age = " + age + " \r\n}";
}
}
public class AnimalSimpleSerializableDemo {
public static void main(String[] args) throws Exception {
String serializableFile = "./animal.ser";
FileOutputStream fos = null;
ObjectOutputStream oos = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
// 为了节省博文篇幅,直接简单粗暴的处理,不必纠结细节
fos = new FileOutputStream(new File(serializableFile));
oos = new ObjectOutputStream(fos);
AnimalSample animal = new AnimalSample();
animal.age = 1;
animal.name = "emo";
oos.writeObject(animal);
oos.close();
fis = new FileInputStream(new File(serializableFile));
ois = new ObjectInputStream(fis);
AnimalSample animal2 = (AnimalSample) ois.readObject();
System.out.println(animal2);
fis.close();
}
}
// 输出结果
{
Animal:
-- name = null
-- age = 0
}
一二三小结
从目前来看,用法基本与Serializable类似,有以下几点不同
- 默认情况下,Serializable不会调用构造函数,但是从实验一和实验二可以看出Externalizable必须要有构造函数,否则会报异常错误(除此之外还有一点,这个构造函数必须是public)
- 使用Serializable简单一点,实现接口即可序列化,但是从实验三来看,Externalizable没有默认实现,必须手动实现序列化内容
- 在上面的三个实验中一直存在一个特殊的的存在–关键字
transiant
,仔细看会发现这个关键字在Externalizable里面不起作用,所以,如果使用transient还是和Serializable一起使用吧