2021-8-2 review of serialization and deserialization

Serializable and Deserialize

Serialization refers to the process of converting the state information of an object into a form that can be stored or transmitted. During serialization, the object writes its current state to the temporary or persistent storage area. Later, the object can be re created by reading or deserializing the state of the object from the storage area!

Serialization, using ObjectOutputStream, the information of the object is converted into a string of byte values in a fixed format, output and saved to disk for a long time

Deserialization: use ObjectInputStream to read the previously serialized data in the disk and restore it to an object

characteristic

  1. The files to be serialized must implement the Serializable interface to enable the serialization function
  2. Data that does not need to be serialized can be modified as static. Reason: static resources belong to class resources and are not output with the object being serialized
  3. Each serialized file has a unique id. if this id is not added, the compiler will automatically calculate and generate one according to the class definition information
  4. During deserialization, if it is inconsistent with the serialized version number, deserialization cannot be completed
  5. It is commonly used to transmit data with the server, serialize it into a file, deserialize and read data
  6. Socket streams are commonly used to pass objects between hosts
  7. Data that does not need to be serialized can also be modified as transient. It only exists in memory during program operation and will not be serialized and persisted

Serialization: ObjectOutputStream

Construction method:
ObjectOutputStream(OutputStream out)
Creates an ObjectOutputStream that writes to the specified OutputStream
General method:
writeObject(Object obj)
Writes the specified object to ObjectOutputStream
 

Deserialization: ObjectInputStream

Construction method:
ObjectInputStream(InputStream in) creates an ObjectInputStream read from the specified InputStream
General method:
readObject() reads objects from ObjectInputStream

package cn.tedu.test1;

import java.io.Serializable;

public class Student implements Serializable {
    private static final long serialVersionUID = 2964503584901399973L;
    private String name;//full name
    private int age;//Age
    private String addr;//address
    private char gender;//Gender

    public Student() {
        System.out.println("I am Student Nonparametric structure of");
    }

    public Student(String name, int age, String addr, char gender) {
        this.name = name;
        this.age = age;
        this.addr = addr;
        this.gender = gender;
        System.out.println("I am Student Parametric structure of");
    }

    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 String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

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


package cn.tedu.test1;

import java.io.*;

public class XuLie {
    public static void main(String[] args) {
        ObjectOutputStream objectOutputStream=null;
        ObjectInputStream objectInputStream=null;
        try {
            objectOutputStream= new ObjectOutputStream(new FileOutputStream("D://ready//1.txt"));
            Student student = new Student("Han Yun", 20, "Infernal", 'male');
            objectOutputStream.writeObject(student);
            objectInputStream=new ObjectInputStream(new FileInputStream("D://ready/1.txt"));
            Object o = objectInputStream.readObject();
            System.out.println(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try {
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Test report error NotSerializableException: the class of the object to be serialized does not implement the serialization interface

The test reported an error InvalidClassException: the UID used in this deserialization does not match the UID used in serialization

Added by alex57 on Mon, 03 Jan 2022 04:14:05 +0200