Home > Java > JavaBase > body text

How to implement serialization in java

Release: 2019-12-26 11:51:09
Original
5308 people have browsed it

How to implement serialization in java

1. Implement serialization:

1) Use the Serializable interface to implement serialization

First we define an object class User

public class User implements Serializable {
    //序列化ID
    private static final long serialVersionUID = 1L;
    private int age;
    private String name;
    //getter和setter方法、
    //toString方法}
Copy after login

Next, implement serialization and deserialization in the Test class.

public class Test {
    public static void main(String[] args) throws Exception, IOException {
        //SerializeUser();
        DeSerializeUser();
    }
    //序列化方法
    private static void SerializeUser() throws FileNotFoundException, IOException {
        User user = new User();
        user.setName("Java的架构师技术栈");
        user.setAge(24);
        //序列化对象到文件中
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("G://Test/template"));
        oos.writeObject(user);
        oos.close();
        System.out.println("序列化对象成功");
    }
    //反序列化方法
    private static void DeSerializeUser() throws FileNotFoundException, IOException{
        File file = new File("G://Test/template");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        User newUser = (User)ois.readObject();
        System.out.println("反序列化对象成功"+newUser.toString());
    }}
Copy after login

2) Use the Externalizable interface to implement serialization

First, define a User1 class

public class User1 implements Externalizable{
    private int age;
    private String name;
    //getter、setter
    //toString方法
    
    public User1() {}
    
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
    }
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    }}
Copy after login

The difference between Externalizable and Serializable interfaces:

1) Externalizable inherits from the Serializable interface

2) We need to override the writeExternal() and readExternal() methods

3) The class that implements the Externalizable interface must provide a public Parameter constructor.

2. Function:

1) Serialization is a mechanism used to process object streams. The so-called object stream is to stream the content of the object, and the streamed object can be To perform read and write operations, you can also transfer the streamed objects to the network;

2) In order to solve the problems that may arise during object stream read and write operations (if serialization is not performed, there may be data Out-of-order problem)

3) In addition to achieving object persistence, serialization can also be used for deep cloning of objects

3. Serialization usage scenarios

1) Permanently save the object and save the object's byte sequence to a local file or database;

2) Transmit and receive the object in the form of a byte stream through serialization;

3) Transfer objects between processes through serialization;

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of How to implement serialization in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template