Home> Java> javaTutorial> body text

How to convert Java object to byte sequence

WBOY
Release: 2023-05-05 20:22:05
forward
1434 people have browsed it

Concept

The process of converting Java objects into byte sequences.

Function

The main purpose of serialization is to ensure the integrity and deliverability of objects when transferring and saving objects. Serialization is the conversion of an object into an ordered stream of bytes for transmission over a network or for saving in a local file. Its core role is to save and reconstruct the target state.

Example

package constxiong.interview; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** * 测试序列化,反序列化 * @author ConstXiong * @date 2019-06-17 09:31:22 */ public class TestSerializable implements Serializable { private static final long serialVersionUID = 5887391604554532906L; private int id; private String name; public TestSerializable(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "TestSerializable [id=" + id + ", name=" + name + "]"; } @SuppressWarnings("resource") public static void main(String[] args) throws IOException, ClassNotFoundException { //序列化 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("TestSerializable.obj")); oos.writeObject("测试序列化"); oos.writeObject(618); TestSerializable test = new TestSerializable(1, "ConstXiong"); oos.writeObject(test); //反序列化 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("TestSerializable.obj")); System.out.println((String)ois.readObject()); System.out.println((Integer)ois.readObject()); System.out.println((TestSerializable)ois.readObject()); } }
Copy after login

The above is the detailed content of How to convert Java object to byte sequence. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!