search
HomeJavaJavaBaseHow to implement serialization in java

How to implement serialization in java

Dec 26, 2019 am 11:51 AM
javaSerialization

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方法}

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());
    }}

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 {
    }}

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment