Home > Java > javaTutorial > body text

How do Java I/O streams handle serialized objects?

WBOY
Release: 2024-04-13 13:18:01
Original
979 people have browsed it

Java I/O stream can serialize and deserialize objects for transmission or storage. The specific steps are as follows: make the object implement the Serializable interface; use ObjectOutputStream to serialize the object into the output stream; read from the input stream Get a byte stream; use ObjectInputStream to deserialize the byte stream into an object.

Java I/O流如何处理序列化对象?

Java I/O Stream Processing Serialized Object

Introduction
Serialization is a The process of converting an object into a stream of bytes so that it can be transmitted or stored over a network or storage device. Deserialization is the reverse process, reconstructing an object from a stream of bytes. In Java, serialization and deserialization are done through I/O streams.

Serializing objects
To serialize an object, we need to:

  1. Make the object implement the Serializable interface.
  2. Use ObjectOutputStream to write objects to the output stream.
// 序列化一个对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.ser"));
oos.writeObject(object);
oos.close();
Copy after login

Deserializing an object
To deserialize an object, we need to:

  1. Read bytes from the input stream flow.
  2. Use ObjectInputStream to deserialize the byte stream into an object.
// 反序列化一个对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.ser"));
Object object = ois.readObject();
ois.close();
Copy after login

Practical case
Let us create a Student class, make it serializable and demonstrate the serialization and deserialization process:

import java.io.Serializable;

public class Student implements Serializable {
    private int id;
    private String name;

    // 构造函数和 getter/setter 略...
}

public class Main {
    public static void main(String[] args) {
        // 创建一个 Student 对象
        Student student = new Student(1, "John Doe");

        // 序列化该对象
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.ser"))) {
            oos.writeObject(student);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 反序列化该对象
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.ser"))) {
            Student deserializedStudent = (Student) ois.readObject();
            System.out.println(deserializedStudent.getId() + " " + deserializedStudent.getName());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

Running this code will output: 1 John Doe, which indicates that the object was successfully serialized and deserialized.

The above is the detailed content of How do Java I/O streams handle serialized objects?. 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