两种对象流的使用 - 图1

一、序列化:ObjectOutputStream

直接按照下面的方式写的话会报异常,原因是被序列化的类需要实现 **Serializable** 这个接口

类可以被序列化的条件:

  1. 1. **实现 **`**Serializable**`** 这个接口**
  2. 2. **属性中需要包含 **_**serialVersionUID **_
  3. 3. **内部属性也要都是可序列化的**

补充:

  1. 1. **不可以序列化 **`static` 或者 `transient` **修饰的成员变量**
  1. package test20;
  2. import jdk.nashorn.internal.objects.annotations.Getter;
  3. import org.junit.Test;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectOutputStream;
  7. /**
  8. * Created By Intellij IDEA
  9. *
  10. * @author Xinrui Yu
  11. * @date 2021/12/5 17:17 星期日
  12. */
  13. public class ObjectOutputStreamTest {
  14. @Test
  15. public void objectOutputStreamTest(){
  16. ObjectOutputStream objectOutputStream = null;
  17. FileOutputStream fileOutputStream = null;
  18. Student student = new Student(1, "yxr", "男");
  19. try{
  20. fileOutputStream = new FileOutputStream("objectOutputStreamTest.txt");
  21. objectOutputStream = new ObjectOutputStream(fileOutputStream);
  22. objectOutputStream.writeObject(student);
  23. }catch (Exception e){
  24. e.printStackTrace();
  25. }finally {
  26. if(objectOutputStream != null){
  27. try {
  28. objectOutputStream.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. }
  35. }
  36. class Student{
  37. private Integer id;
  38. private String name;
  39. private String gender;
  40. public Student() {
  41. }
  42. public Student(Integer id, String name, String gender) {
  43. this.id = id;
  44. this.name = name;
  45. this.gender = gender;
  46. }
  47. public Integer getId() {
  48. return id;
  49. }
  50. public void setId(Integer id) {
  51. this.id = id;
  52. }
  53. public String getName() {
  54. return name;
  55. }
  56. public void setName(String name) {
  57. this.name = name;
  58. }
  59. public String getGender() {
  60. return gender;
  61. }
  62. public void setGender(String gender) {
  63. this.gender = gender;
  64. }
  65. @Override
  66. public String toString() {
  67. return "Student{" +
  68. "id=" + id +
  69. ", name='" + name + '\'' +
  70. ", gender='" + gender + '\'' +
  71. '}';
  72. }
  73. }

修改完成之后,运行:
可以得到一个看不懂的二进制文件
可以从这个二进制文件,使用反序列化方法来得到我们存进去的对象image.png

二、反序列化:ObjectInputStream

可以通过反序列化,将上面存储的二进制文件变成一个程序中的一个对象

  1. package test20;
  2. import org.junit.Test;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. /**
  7. * Created By Intellij IDEA
  8. *
  9. * @author Xinrui Yu
  10. * @date 2021/12/5 17:57 星期日
  11. */
  12. public class ObjectInputStreamTest {
  13. @Test
  14. public void objectInputStreamTest(){
  15. FileInputStream fileInputStream = null;
  16. ObjectInputStream objectInputStream = null;
  17. try{
  18. fileInputStream = new FileInputStream("objectOutputStreamTest.txt");
  19. objectInputStream = new ObjectInputStream(fileInputStream);
  20. Object object = objectInputStream.readObject();
  21. if(object instanceof Student){
  22. Student student = (Student) object;
  23. System.out.println("id:" + student.getId());
  24. System.out.println("name:" + student.getName());
  25. System.out.println("gender:" + student.getGender());
  26. }else{
  27. throw new RuntimeException("反序列化失败");
  28. }
  29. }catch (IOException | ClassNotFoundException e){
  30. e.printStackTrace();
  31. } finally {
  32. if (objectInputStream != null) {
  33. try {
  34. objectInputStream.close();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }
  41. }
  1. 运行,可以看到成功反序列化出来了一个 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)