序列化与反序列化
序列化:将对象转换为一个字节流的过程称为序列化。
反序列化:将字节流恢复为对象的过程称为对象的反序列化。
序列化的条件
一个类的对象若想序列化,该类必须实现java.io.Serializable接口或者实现Externalizable,该接口是一个标识接口,接口中没有任何方法或者属性。仅仅是表明实现该接口的类可以被序列化。
序列化的作用
在Java中,平时通过new创建的对象,只要对象没有被回收,且JVM进程没有结束,我们可以一直访问,但一旦JVM进程结束,再想使用这么一个对象就不能了。
而通过序列化我们可以将该对象持久化保存下来。用的时候再通过反序列化就可以重新生成一个对象。
序列化步骤
- 首先要有一个待序列化的对象,且该对象所处的类实现了Serializable接口。
- 创建一个对象输出流ObjectOutputStream类的对象
- 然后用该类的writeObject方法将对象写入外部设备中。
- 序列化完毕
反序列化步骤
- 创建一个对象输出流ObjectInputStream类的对象。
- 然后调用该类的readObject方法将字节流文件读进来。该方法返回值为Object类型然后强转为目标类型
- 反序列化完毕。
对象序列化与反序列练习
创建一个待序列化的类
// 实现Serializable接口
public class Student implements Serializable {
private String name;
private Integer age;
private static final String work = "student";
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "name:" + this.name + " " + "age:" + this.age + " " + "work:" + Student.work;
}
序列化方法:参数为带序列化的对象,和序列化后存放字节流数据的文件
public static void serializable(File fIle,Student stu){
try( ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fIle));){
oos.writeObject(stu);
}catch(IOException e){
e.printStackTrace();
}
}
反序列化方法,参数为保存字节流的文件,返回实例对象
public static Student UnSerializable(File file){
Student stu = null;
try( ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));){
stu = (Student) ois.readObject();
}catch(IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return stu;
}
序列化后的文件内容:
测试代码:
public static void main(String[] args) {
Student stu = new Student("Jack",15);
String path = "student.txt";
File fIle = new File(path);
serializable(fIle,stu);
Student sturet = UnSerializable(fIle);
System.out.println(sturet);
System.out.println(stu == sturet); // false
}
运行结果:
最后用==判断这两个对象是否相等,结果是false说明,序列前后不是同一个对象,只是对象的内容相同而已。属于深拷贝的一种。
transient关键字
继承Serializable 接口,默认会序列化对象的所有属性值,但有时候我们并不希望对象的某些属性值被他人得到,因此引入transient关键字,被该关键字修饰的属性不参与序列化,到时候只能得到一个相应属性的默认值。
代码演示:
public class Student implements Serializable {
private String name;
private transient Integer age; // 被transient修饰
private static String work = "student";
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "name:" + this.name + " " + "age:" + this.age + " " + "work:" + Student.work;
}
public static void main(String[] args) {
Student stu = new Student("Jack",15);
String path = "student.txt";
File fIle = new File(path);
serializable(fIle,stu);
Student sturet = UnSerializable(fIle);
System.out.println(sturet);
System.out.println(stu == sturet);
}
// 序列化
public static void serializable(File fIle,Student stu){
// JDK1.7提供的自动关闭
try( ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fIle));){
oos.writeObject(stu);
}catch(IOException e){
e.printStackTrace();
}
}
// 反序列化
public static Student UnSerializable(File file){
Student stu = null;
try( ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));){
stu = (Student) ois.readObject();
}catch(IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return stu;
}
}
程序运行结果:
可以看出被transient修饰的属性不参与序列化,最终在文件中保存的是该属性的默认值。
同一个对象多次序列化
当一个对象被多次系列化后,序列化生成的文件,并不会翻倍,而是增加五个字节,因此可以推断,JVM是对序列化做了一定的优化的。