Java Basic Tutorial - Serialization

Keywords: PHP Java network git REST

serialize

Serialization: Serialize, which writes Java objects to IO streams (which can be stored in databases, files, etc.)
Deserialize, which reads and restores Java objects from an IO stream.

So to understand: serialization is to seal the object, and deserialization is to unblock.

Objects contain not only characters, but more non-characters, so byte streams are needed.

All classes of objects that can be transmitted over the network should be serializable, otherwise the program will be abnormal.

Implementing Serializable Interface

There's nothing in this interface. It's a markup interface, indicating that an object can be serialized.

public interface Serializable {
}

Transient: transient keyword
| transient variables - - do not participate in serialization (some variables may be sensitive information, such as bank account passwords, etc.)

transient: Short.

import java.io.*;
public class Serialization 1 {
    public static void main(String[] args) throws Exception {
        int r_w = 3;
        if (r_w == 1) {
            on serialize();
        } else if (r_w == 2) {
            on Deserialize();
        } else {
            on serialize();
            on Deserialize();
        }
    }
    private static void on serialize() throws Exception {
        Dog _d = new Dog("Dog", "Golden hair");
        File f = new File("myObj.txt");
        System.out.println("Serialization:" + f.getAbsolutePath());
        FileOutputStream fos = new FileOutputStream(f);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(_d);
        oos.close();
        fos.close();
    }
    private static void on Deserialize() throws Exception {
        // Prerequisites: 1. Classes must be serializable, 2. Class files must be corresponding
        File f = new File("myObj.txt");
        FileInputStream fis = new FileInputStream(f);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Dog d = (Dog) ois.readObject();
        System.out.println("Deserialization:" + d + ": name = " + d.name);
        ois.close();
        fis.close();
    }
}
class Dog implements Serializable {
    private static final long serialVersionUID = 1L;
    public Dog(String name, String type) {
        super();
        this.name = name;
        this.type = type;
    }
    String name;
    transient String type;
    @Override
    public String toString() {
        System.out.println("type:Not involved in serialization");
        return "[Doggy:name = " + name + ", type = " + type + "]";
    }
}

Operation results:

Serialization: C: Users Administrator git Ah01Jsp JavaHtml myObj. TXT
 type: Not involved in serialization
 Deserialization: [Dog: name = dog, type = null]: name = dog

Serial number: Serial Version UID
| - Modify the definition of a class to generate a new serial number
| -- | - Change Constructor: Change
| Change fields: Change (including transient fields)
| The implementation in the system of change will remain unchanged.

If the serial number is different between serialization and deserialization, the exception java.io.InvalidClassException will be thrown.
You can display and declare a fixed serial number. Such as:

private static final long serialVersionUID = 1L;

* Another Serialization Method: Implementing Externalizable Interface

Externalizable interface can replace Serializable interface.

Hands are required to provide a parametric construction method, otherwise an exception will be thrown when deserialization occurs.

java.io.InvalidClassException: ......; no valid constructor

At the same time, two methods (writeExternal() and readExternal()) need to be defined to control the fields to be serialized.

For the following example, only the implementation of the Dog class is different from the previous example, and the rest of the code can be the same.

package ah.externalizable;
import java.io.*;
public class Serialization 2 {
    public static void main(String[] args) throws Exception {
        on serialize();
        on Deserialize();
    }
    private static void on serialize() throws Exception {
        Dog _d = new Dog("Dog", "Golden hair");
        File f = new File("myObj.txt");
        System.out.println(f.getAbsolutePath());
        FileOutputStream fos = new FileOutputStream(f);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(_d);
        oos.close();
        fos.close();
    }
    private static void on Deserialize() throws Exception {
        File f = new File("myObj.txt");
        FileInputStream fis = new FileInputStream(f);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Dog d = (Dog) ois.readObject();
        System.out.println(d + ": name = " + d.name);
        ois.close();
        fis.close();
    }
}
class Dog implements Externalizable {
    public Dog() {
        // If you don't write empty structures, you can't read them.
    }
    public Dog(String name, String type) {
        super();
        this.name = name;
        this.type = type;
    }
    String name;
    String type;
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        // Field s that are not written are not serialized
        out.writeUTF(name);
        out.writeUTF(type);
    }
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        // Read in the same order as write
        name = in.readUTF();
        type = in.readUTF();
    }
    @Override
    public String toString() {
        return "[Doggy:" + name + ", It's one." + type + "]";
    }
}

Posted by Justin98TransAm on Fri, 12 Jul 2019 16:38:04 -0700