1、对象的序列化和反序列化
- 对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化
- 序列化流(ObjectOutputStream),是字节过滤流---writerObject
- 反序列化流(ObjectInPutStream),---readObject
- 序列化接口( Serializable)
- 对象必须实现序列化接口,才能进行序列化,否则将出现异常,这个接口没有任何方法,只是一个标准。
2、对象序列化,代码示例:
package com.lord.quick;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 6780974907389412343L;
private String number;
private String username;
private int age;
public Student() {
}
public Student(String number, String username, int age) {
super();
this.number = number;
this.username = username;
this.age = age;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [number=" + number + ", username=" + username + ", age=" + age + "]";
}
}
public static void main(String[] args) throws Exception {
String file = "demo/obj.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
Student stu = new Student("10001", "张三", 20);
oos.writeObject(stu);
oos.flush();
oos.close();
}
3、对象反序列化,代码示例:
public static void main(String[] args) throws Exception {
String file = "demo/obj.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Student stu = (Student)ois.readObject();
System.out.println(stu);
ois.close();
}
4、transient关键字
package com.lord.quick;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 6780974907389412343L;
private String number;
private String username;
private transient int age; // 该元素不会进行jvm默认的序列化,也可以自己完成序列化
public Student() {
}
public Student(String number, String username, int age) {
super();
this.number = number;
this.username = username;
this.age = age;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [number=" + number + ", username=" + username + ", age=" + age + "]";
}
}
5、自主完成序列化,代码示例
- 此处参考ArrayList类源码序列化和反序列化的实现
- 自主实现序列化和反序列化可提供程序性能
package com.lord.quick;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 6780974907389412343L;
private String number;
private String username;
private transient int age;
public Student() {
}
public Student(String number, String username, int age) {
super();
this.number = number;
this.username = username;
this.age = age;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [number=" + number + ", username=" + username + ", age=" + age + "]";
}
// 序列化
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();// 把jvm能默认序列化的元素进行序列化
s.writeInt(age);
}
// 反序列化
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();// 把jvm能默认反序列化的元素进行反序列化
this.age = s.readInt();
}
}
6、序列化中子父类构造函数问题
- 一个类实现了序列化接口,其子类都可以进行序列化
- 创建对象时,递归调用父类的构造函数
- 对子类对象进行反序列化操作时,如果其父类没有实现序列化接口,那么其父类的构造函数会被显示调用
网友评论