Java IO stream - object serialization and object deserialization

serialized object

insert image description here

object serialization

Object serialization concept :

Function: Based on memory, storing objects in memory to disk files is called object serialization.

The stream used is the object byte output stream: ObjectOutputStream

ObjectOutputStream constructor :

constructor illustrate
ObjectOutputStream(OutputStream out) Wrap the low-level byte output stream into a high-level object byte output stream

ObjectOutputStream serialization method :

method name illustrate
writeObject(Object obj) Write the object out to the file of the object serialization stream

Demo code :

For example, we have the following Student object

Note: If the object is to be serialized, it must implement the Serializable interface

package com.chenyq.d4_serializable;

import java.io.Serializable;

// 对象要序列化必须实现Serializable接口
public class Student implements Serializable {
    
    
    private String name;
    private int age;
    private double height;

    public Student() {
    
    
    }

    public Student(String name, int age, double height) {
    
    
        this.name = name;
        this.age = age;
        this.height = height;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public double getHeight() {
    
    
        return height;
    }

    public void setHeight(double height) {
    
    
        this.height = height;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                '}';
    }
}

Object serialization using object bytes output stream

public static void main(String[] args) throws Exception {
    
    
    // 1.创建学生对象
    Student stu1 = new Student("aaa", 19, 1.88);
    System.out.println(stu1); // Student{name='aaa', age=19, height=1.88}
    Student stu2 = new Student("bbb", 32, 1.66);
    System.out.println(stu2); // Student{name='bbb', age=32, height=1.66}

    // 2.使用对象字节输出流包装字节输出流
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Users/chenyq/Documents/test.txt"));

    // 3.调用序列化方法, 把对象写到文件当中去
    oos.writeObject(stu1);
    oos.writeObject(stu2);

    // 4.释放资源
    oos.close();
}

Note: The serialization result as follows means that the serialization is successful, because the serialization is only temporarily stored, not for our developers to see

Serialization result: ��sr"com.chenyq.d4_serializable.Student��P����IageDheightLnametLjava/lang/String;xp?�z�G�taaasq~ ?��(� tbbb

object deserialization

Object deserialization concept :

The stream used is the object byte input stream: ObjectInputStream

Function: Based on the memory, the object data stored in the disk file is restored to the object in the memory, which is called object deserialization.

Object deserialization constructor :

constructor illustrate
ObjectInputStream(InputStream out) Wrap low-level byte input streams into high-level object byte input streams

Object deserialization method :

method name illustrate
readObject() Restore the object data stored in the disk file to the object in the memory and return

Demo code :

Deserialize the just-serialized file back to the object in the original Java memory

public static void main(String[] args) throws Exception {
    
    
    // 1.创建对象字节输入流并包装原始字节输入流
    ObjectInputStream oos = new ObjectInputStream(new FileInputStream("/Users/chenyq/Documents/test.txt"));

    // 2.调用对象字节输入流反序列化方法
    Student stu1 = (Student) oos.readObject();
    Student stu2 = (Student) oos.readObject();

    System.out.println(stu1); // Student{name='aaa', age=19, height=1.88}
    System.out.println(stu2); // Student{name='aaa', age=19, height=1.88}
}

Guess you like

Origin blog.csdn.net/m0_71485750/article/details/127606884