一、序列化:ObjectOutputStream
直接按照下面的方式写的话会报异常,原因是被序列化的类需要实现 **Serializable**
这个接口
类可以被序列化的条件:
1. **实现 **`**Serializable**`** 这个接口**
2. **属性中需要包含 **_**serialVersionUID **_
3. **内部属性也要都是可序列化的**
补充:
1. **不可以序列化 **`static` 或者 `transient` **修饰的成员变量**
package test20;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
* Created By Intellij IDEA
*
* @author Xinrui Yu
* @date 2021/12/5 17:17 星期日
*/
public class ObjectOutputStreamTest {
@Test
public void objectOutputStreamTest(){
ObjectOutputStream objectOutputStream = null;
FileOutputStream fileOutputStream = null;
Student student = new Student(1, "yxr", "男");
try{
fileOutputStream = new FileOutputStream("objectOutputStreamTest.txt");
objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(student);
}catch (Exception e){
e.printStackTrace();
}finally {
if(objectOutputStream != null){
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class Student{
private Integer id;
private String name;
private String gender;
public Student() {
}
public Student(Integer id, String name, String gender) {
this.id = id;
this.name = name;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
修改完成之后,运行:
可以得到一个看不懂的二进制文件
可以从这个二进制文件,使用反序列化方法来得到我们存进去的对象
二、反序列化:ObjectInputStream
可以通过反序列化,将上面存储的二进制文件变成一个程序中的一个对象
package test20;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
/**
* Created By Intellij IDEA
*
* @author Xinrui Yu
* @date 2021/12/5 17:57 星期日
*/
public class ObjectInputStreamTest {
@Test
public void objectInputStreamTest(){
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
try{
fileInputStream = new FileInputStream("objectOutputStreamTest.txt");
objectInputStream = new ObjectInputStream(fileInputStream);
Object object = objectInputStream.readObject();
if(object instanceof Student){
Student student = (Student) object;
System.out.println("id:" + student.getId());
System.out.println("name:" + student.getName());
System.out.println("gender:" + student.getGender());
}else{
throw new RuntimeException("反序列化失败");
}
}catch (IOException | ClassNotFoundException e){
e.printStackTrace();
} finally {
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行,可以看到成功反序列化出来了一个 Student 对象<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/21436600/1638698774611-a547412c-930c-49c8-b0e1-e39698ab1952.png#clientId=u1bab84b0-9cd8-4&from=paste&id=udddd1809&originHeight=321&originWidth=1167&originalType=binary&ratio=1&size=372545&status=done&style=none&taskId=u48a04bc0-9e63-4158-804c-7de4afb1db6)