Java IO---3---IO serialization ObjectOutputStream class and ObjectInputStream class

Overview

Java provides a mechanism for object serialization. An object can be represented by a sequence of bytes that contains information such as the data of the object, the type of the object, and the properties stored in the object. After the byte sequence is written out to the file, it is equivalent to persisting the information of an object in the file.

Conversely, the byte sequence can also be read back from the file, reconstruct the object, and deserialize it. The data of the object, the type of the object, and the data information stored in the object can all be used to create the object in memory. See the diagram to understand serialization:

ObjectOutputStream class

The java.io.ObjectOutputStream class writes the primitive data types of Java objects to a file to implement persistent storage of objects.

Construction method

  • public ObjectOutputStream(OutputStream out): Creates an ObjectOutputStream that specifies an OutputStream.

serialization operation

  1. For an object to be serialized, two conditions must be met:
  • The class must implement the java.io.Serializable interface. Serializable is a marker interface. A class that does not implement this interface will not serialize or deserialize any state, and will throw a NotSerializableException.
  • All properties of the class must be serializable. If there is a property that does not need to be serializable, the property must be marked as transient and modified with the transient keyword.

2. Write the object method

  • public final void writeObject (Object obj) : Write the specified object out.

ObjectInputStream class

ObjectInputStream deserializes the stream, restoring the original data that was previously serialized with ObjectOutputStream as an object.

Construction method

  • public ObjectInputStream(InputStream in): Creates an ObjectInputStream of the specified InputStream.

deserialization operation 1

If we can find the class file of an object, we can perform the deserialization operation and call the method of ObjectInputStream to read the object:

  • public final Object readObject () : Read an object.

For the JVM to deserialize an object, it must be a class that can find the class file. If the class file for the class is not found, a ClassNotFoundException is thrown.

deserialization operation 2

In addition, when the JVM deserializes the object, it can find the class file, but if the class file is modified after the object is serialized, the deserialization operation will also fail and an InvalidClassException exception will be thrown. The reason for this exception is as follows:

  • The serial version number of the class does not match the version number of the class descriptor read from the stream
  • The class contains an unknown data type
  • This class has no accessible parameterless constructor

Serialization implementation

Serialized class: To be serialized, you need to implement the Serializable interface. If there are properties that do not need to be serialized, you need to add the transient keyword transient keyword

public class serialized class  implements Serializable {
    public String love;
    public transient String name;
    public int age;

    public void show(){
        System.out.println("mylove="+love+"---myname="+name+"---myage="+age);
    }
}

Make serialization:

public class serialize {
    public static void main(String[] args) {

        serialized class u=new serialized class();
        u.name="Zhang San";
        u.love="fishing";
        u.age=89;

        try {
            ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("D:\\summer note message\\picture\\winter vacation pictures\\experimental folder\\IO\\sequence file.txt"));
            out.writeObject(u);
            out.close();

            System.out.println("object except name´╝îEverything else is serialized");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Deserialize:

 public class deserialize {
    public static void main(String[] args) {
        try {
            ObjectInputStream in=new ObjectInputStream(new FileInputStream("D:\\summer note message\\picture\\winter vacation pictures\\experimental folder\\IO\\sequence file.txt"));
            serialized class u=(serialized class) in.readObject();

            u.show();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

Tags: Java io

Posted by DarkSuperHero on Fri, 03 Jun 2022 04:38:35 +0530