高级流
    ObjectOutputStream:序列化流(对象输出流)

    public final void writeObject(Object obj) - 禁止重写

    ObjectInputStream:反序列化流(对象输入流)
    public final Object readObject()

    序列化

    1. package Test21_Demo.Demo04;/*
    2. @create 2020--12--14--10:33
    3. */
    4. import java.io.FileNotFoundException;
    5. import java.io.FileOutputStream;
    6. import java.io.IOException;
    7. import java.io.ObjectOutputStream;
    8. public class SerializableDemo {
    9. public static void main(String[] args) throws IOException {
    10. //准备被序列化的对象
    11. Person person = new Person("Jack.Ma", 18);
    12. //进行序列化操作
    13. FileOutputStream fos = new FileOutputStream("person.txt");
    14. ObjectOutputStream oos = new ObjectOutputStream(fos);
    15. //开始序列化
    16. oos.writeObject(person);
    17. //关闭流
    18. oos.close();
    19. }
    20. }

    反序列化

    1. package Test21_Demo.Demo04;/*
    2. @create 2020--12--14--10:33
    3. */
    4. import java.io.*;
    5. public class SerializableDemo2 {
    6. public static void main(String[] args) throws IOException, ClassNotFoundException {
    7. //进行反序列化操作
    8. FileInputStream fis = new FileInputStream("person.txt");
    9. ObjectInputStream ois = new ObjectInputStream(fis);
    10. //开始反序列化
    11. Person person = (Person) ois.readObject();
    12. System.out.println(person.getName() + " " + person.getAge());
    13. System.out.println(person);
    14. //关闭流
    15. ois.close();
    16. }
    17. }