Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to serialize and deserialize objects in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to serialize and deserialize objects in Java. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

Introduction:

Serialization is the process of converting the state information of an object into a form that can be stored or transmitted. During serialization, the object writes its past state to a temporary or holding storage area, and deserialization is the process of recreating an object. this object comes from a temporary or persistent storage area.

The purpose of serialization:

Just like storing data to the database and persisting some data to the database, and sometimes we need to persist the object, although there are many ways to persist the object state, java provides us with a very convenient way, that is serialization, serialization can achieve the direct conversion between objects to files, and the implementation details are hidden from us.

Three specific uses:

Persist the state information of the object to the hard disk

Transfer object information over the network

Deep cloning (that is, serialization and then deserialization)

Method 1: implement the Serializable interface by serializing the stream

Implement the Serializable interface to serialize and deserialize objects through ObjectOutputStream and ObjectInputStream.

Import java.io.*;public class User implements Serializable {private static final long serialVersionUID = 1L; private String name; private int age; public User (String name, int age) {this.name = name; this.age = age } @ Override public String toString () {return "User {" + "name='" + name +'\'+ ", age=" + age +'}';} public static void main (String [] args) throws IOException, ClassNotFoundException {/ / User user = new User ("gol", 22); / / ByteArrayOutputStream bo = new ByteArrayOutputStream () / / ObjectOutputStream oo = new ObjectOutputStream (bo); / / oo.writeObject (user); / / serialize. User writes to the byte array stream / / ByteArrayInputStream bi = new ByteArrayInputStream (bo.toByteArray ()); / / ObjectInputStream oi = new ObjectInputStream (bi); / / User userSer = (User) oi.readObject (); / / deserialize / / System.out.println (userSer) User user = new User ("gol", 22); FileOutputStream fos = new FileOutputStream ("a.txt"); ObjectOutputStream oo = new ObjectOutputStream (fos); oo.writeObject (user); / / serialize. User writes FileInputStream fis = new FileInputStream ("a.txt") to the file; ObjectInputStream oi = new ObjectInputStream (fis); User userSer = (User) oi.readObject () / / deserialize System.out.println (userSer); oi.close (); fis.close (); oo.close (); fos.close ();} Mode 2: implement Externalizable interface, override writeExternal and readExternal methods

The Externalizable interface inherits the Serializable interface and encapsulates two methods for us, one for serialization and one for deserialization. This way is to serialize the property, note that the transient modifier will not work in this way, that is, the property modified by transient will be serialized as long as you serialize the property in the writeExternal method.

Import java.io.*;public class User implements Externalizable {private static final long serialVersionUID = 1L; private String name; private int age; public User () {} public User (String name, int age) {this.name = name; this.age = age } @ Override public String toString () {return "User {" + "name='" + name +'\'+ ", age=" + age +'}';} @ Override public void writeExternal (ObjectOutput out) throws IOException {out.writeObject (this.name); / / serialize out.writeObject (this.age) respectively } @ Override public void readExternal (ObjectInput in) throws IOException, ClassNotFoundException {this.name= (String) in.readObject (); / / deserialization attribute this.age= (int) in.readObject ();} public static void main (String [] args) throws IOException, ClassNotFoundException {FileOutputStream fos = new FileOutputStream ("a.txt"); ObjectOutputStream oo = new ObjectOutputStream (fos); FileInputStream fis = new FileInputStream ("a.txt") ObjectInputStream oi = new ObjectInputStream (fis); User user = new User ("gol", 19); user.writeExternal (oo); / / serialization User userEnr = new User (); userEnr.readExternal (oi); / / deserialization System.out.println (userEnr); oi.close (); fis.close (); oo.close (); fos.close () }} Summary:

Note the following three points:

The Serializable interface is a tagged interface, an empty interface that identifies that the class can be serialized.

Transient is a property modifier, and the property modified by it will not be serialized, but if you use method 2, you can also serialize by explicitly stating that the property serialization.

The serialVersionUID property is the serialization identifier ID of the class, and an error will be reported if the serialized object and the deserialized object have different serialVersionUID properties.

Thank you for reading! On how to achieve object serialization and deserialization in Java to share here, I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report