1 对象序列化
对象序列化:
● 使用到的流是对象字节输出流:ObjectOutputStreamd
● 作用:以内存为基准,把内存中的对象存储到磁盘文件中去,称为对象序列化
IO体系结构图
对象字节输出流:ObjectOutputStream
构造器 | 说明 |
public ObjectOutputStream(OutputStram out) | 将低级字节输出流包装成高级的对象字节输出流 |
ObjectOutputStream序列化方法
方法名称 | 说明 |
public final void writeObject(Object obj) | 将对象写出到对象序列化流的文件中 |
【示例】Student类
查看代码
public class Student implements Serializable {
private String name;
private String longinName;
private transient String password;
private int age;
public Student() {
}
public Student(String name, String longinName, String password, int age) {
this.name = name;
this.longinName = longinName;
this.password = password;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLonginName() {
return longinName;
}
public void setLonginName(String longinName) {
this.longinName = longinName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", longinName='" + longinName + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}
【示例】序列化对象
public class ObjectOutputStreamDemo01 {
public static void main(String[] args) throws Exception {
//1.创建学生对象
Student s = new Student("叶子","yezi","123123",21);
//2.对象序列化:使用对象字节输出流包装字节输出流管道
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("io-app2/src/obj.txt"));
//3.直接调用序列化方法
oos.writeObject(s);
//4.释放资源
oos.close();
System.out.println();
System.out.println("序列化完成了~");
}
}
2 对象反序列化
对象反序列化
● 使用到的流是对象字节输入流:ObjectInputStream
● 作用:以内存为基准,把存储到磁盘文件中的对象数据恢复成内存中的对象,称为对象反序列化。
构造器 | 说明 |
public ObjectInputStream() | 将低级字节输入流包装成高级的对象字节输入流 |
ObjectInputStream序列化方法
方法名称 | 说明 |
public Object readObject( ) | 将存储到磁盘文件中的对象数据恢复成内存中的对象返回 |
【示例】
package com.itheima.d5_serializable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
public class ObjectInputStramDemo02 {
public static void main(String[] args) throws Exception {
//1. 创建对象字节输入流管道包装低级的字节输入流管道
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("io-app2/src/obj.txt"));
//2. 调用对象字节输入流的反序列化方法
Student s = (Student) ois.readObject();
System.out.println(s);
}
}
运行结果:
Student{name='叶子', longinName='yezi', password='null', age=21}
补充:
● transient修饰的成员变量不参与序列化
● 声明序列化的版本号号
● 序列化的版本号与反序列化的版本号必须一致才不会出错
【示例】
查看代码
/**
对象如果要序列化,必须实现Serializable序列化接口。
*/
public class Student implements Serializable {
// 申明序列化的版本号码
// 序列化的版本号与反序列化的版本号必须一致才不会出错!
private static final long serialVersionUID = 1;
private String name;
private String loginName;
// transient修饰的成员变量不参与序列化了
private transient String passWord;
private int age ;
public Student(){
}
public Student(String name, String loginName, String passWord, int age) {
this.name = name;
this.loginName = loginName;
this.passWord = passWord;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", loginName='" + loginName + '\'' +
", passWord='" + passWord + '\'' +
", age=" + age +
'}';
}
}