Java serialization: Serializable

When it comes to serialization, most people probably haven't used it in their actual work. Although it hasn't been used, it doesn't mean it doesn't matter. What is JAVA serialization?

Java serialization can also be called object serialization mechanism: it allows Java objects in memory to be converted into platform independent binary streams, thus allowing such binary streams to be persisted on disk or transmitted to another network node through the network. The advantage of serialization is that it can convert any object that implements the Serializable interface into byte data, so that it can be restored when it is saved and transferred. Serialization is a mechanism that must be implemented for parameters and return values of RMI (Remote Method Invoke) procedures, and RMI is the foundation of Java EE. So serialization mechanism is the foundation of Java EE platform.

Now let's practice and see how to realize object serialization through code

First create a JAVA class:

public class TestBean implements Serializable{

    public static final long serialVersionUID = 475463534532L;

    private String name;
    private int age;
    private int id;
    private Dog dog;

    public TestBean(String name, int age, int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public TestBean(String name, int age, int id, Dog dog) {
        this.name = name;
        this.age = age;
        this.id = id;
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "TestBean{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                ", dog=" + dog +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 TestBean(String name, int age) {

        this.name = name;
        this.age = age;
    }

    public TestBean() {

    }
}

class Dog implements Serializable{
    public static final long serialVersionUID = 4754534532L;
    private String dogName;

    @Override
    public String toString() {
        return "Dog{" +
                "dogName=" + dogName +
                '}';
    }

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public Dog(String dogName) {

        this.dogName = dogName;
    }
}

Another test class:

public class SerializableTest {

    
    @Test
    public void testObjectOutputStream(){
        ObjectOutputStream oos = null;

        try {

            oos = new ObjectOutputStream(new FileOutputStream("object.data"));

            oos.writeObject(new String("Chongqing refueling"));
            oos.flush();//Refresh operation

            oos.writeObject(new TestBean("Ha ha ha",15));
            oos.flush();

            oos.writeObject(new TestBean("Italy",44,5000,new Dog("Prosperous wealth")));
            oos.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

   
    @Test
    public void testObjectInputStream(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("object.data"));

            Object obj = ois.readObject();
            String str = (String) obj;

            TestBean p = (TestBean) ois.readObject();
            TestBean p1 = (TestBean) ois.readObject();

            System.out.println(str);
            System.out.println(p);
            System.out.println(p1);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}

Little friend, although not commonly used, we still need to understand its usage and use.

 

Welcome to the group exchange, the author of the official account of the wormhole stack is sitting in this group. We have the opportunity to face the God with great force.

Published 4 original articles, won praise 0, visited 27
Private letter follow

Tags: Java network

Posted on Fri, 13 Mar 2020 07:49:54 -0700 by abid786