What is object serialization?

Asked 2 years ago, Updated 2 years ago, 122 views

Please explain what object serialization is. It's better to have an example.

java serialization object-serialization

2022-09-22 22:20

1 Answers

Serialization of an object means converting the contents of the object in bytes to enable stream (transmission and reception) through a file or network. Java I/O processing only supported integer, string, and byte processing.Therefore, in order to store/restore or transfer the contents of a complex object to the network, each content of the object's member variable had to be transmitted in a certain format (called a packet).

Object serialization provides a function for Java I/O to automatically convert the contents of objects (specifically the contents of member variables) into bytes, store/restore, or transmit them to the network.

In other words, from the developer's point of view, no matter how complex the object is, when object serialization is used, Java I/O automatically converts the content of the object in bytes and stores or transmits it.

▼ FileOutputStream class is required to save files first. Take the location where the file will be stored as an argument, create an object, and then select the ObjectOutputStream class for serialization I used it. The function that creates the serialization is writeObject(). Pass the user object inherited from the serializable interface as an argument.

// perform marshalling, serialize 
public void doSerializable() throws IOException {
    FileOutputStream fos = new FileOutputStream(filePath);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(user);
    oos.close();
}

FileInputStream and ObjectInputStream classes were used to reread serialized files. The function to read is readObject(), and you need to cast it with that object.

// perform unmarshalling, reverse serialization
public Object undoSerializable() throws IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream(filePath);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object user = ois.readObject();
    ois.close();

    return user;
}

▼ Set data on serialization objects in the init() function, then serialize and save as a file using the doSerializable() function. And again, it's about releasing serialization using the undoSerializable() function and printing the value on the console.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Main {

    private String filePath;
    private User user;
    public static void main(String[] args){

        Main main = new Main();
        try {
            main.init();
            main.doSerializable();
            User user = (User) main.undoSerializable();

            System.out.println(user.getName());
            System.out.println(user.getAge());
            System.out.println(user.getPhoneNumber());

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

    public void init() {

        filePath = "C:\\user.ser";

        user = new User();
        user.setName("kim dong");
        user.setAge(50);
        user.setPhoneNumber("010-9858-9985");
    }

    // marshalling, serializing 
    public void doSerializable() throws IOException {

        FileOutputStream fos = new FileOutputStream(filePath);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(user);
        oos.close();
    }

    // Perform unmarshalling, deserialize
    public Object undoSerializable() throws IOException, ClassNotFoundException {

        FileInputStream fis = new FileInputStream(filePath);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object user = (User) ois.readObject();
        ois.close();

        return user;
    }
}

// Serialized Objects 
class User implements Serializable {
    private static final long serialVersionUID = 2L;
    private String name;
    private int age;
    private String phoneNumber;

    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 getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String socialNumber) {
        this.phoneNumber = socialNumber;
    }
}
// Results
kim dong
50
010-9858-9985


2022-09-22 22:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.