Thinking in java-39 序列化 Serialization

本文详细介绍了Java语言中的对象序列化机制,包括序列化的意义、常用类和方法,并通过一个具体的序列化与反序列化Demo演示了如何实现对象的持久化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 对象序列化的意义

本文详细内容参考自java_T_point && tutorialspoint.
这里写图片描述
Java语言中的序列化:将对象状态写入字节流中的机制。
对象可以用一组包含该对象数据和信息(包含对象的类型、存储在对象中的数据)的字节所表示。在序列化对象写入到文件之后,该对象可以从文件中读出,并进行反向的解序列化过程。也就是说,类型信息、代表该对象的字节信息及相关数据可以用来在内存中重建该对象数据。

2. 对象序列化常用的类和方法

//Methods used in Serialization
java.io.FileOutputStream.FileOutputStream(String name) throws FileNotFoundException
java.io.ObjectOutputStream.ObjectOutputStream(OutputStream out) throws IOException
void java.io.ObjectOutputStream.writeObject(Object obj) throws IOException

//Methods used in Deserialization
java.io.FileInputStream.FileInputStream(String name) throws FileNotFoundException
java.io.ObjectInputStream.ObjectInputStream(InputStream in) throws IOException
Object java.io.ObjectInputStream.readObject() throws IOException, ClassNotFoundException

3. 序列化和解序列化Demo

对于要持久化的对象,必须实现Serializable interface.

package com.fqy.serial;

import java.io.Serializable;

public class Student implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private transient double weight;
    private String name;

    public Student(int id, String name, double weight) {
        this.id = id;
        this.name = name;
        this.weight = weight;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getWeight() {
        return weight;
    }
}

package com.fqy.serial;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class Persist {

    public static void main(String[] args) {
        Student stu = new Student(985, "fqy", 62.0);
        // Create a fileOutputStream to write to the file with a specified name
        try {
            FileOutputStream fout = new FileOutputStream("fout.txt");
            // Create an ObjectOutstream that writes to the specified
            // OutputStream
            ObjectOutputStream oos = new ObjectOutputStream(fout);

            // Write the specified object to the ObjectOutputStream
            oos.writeObject(stu);
            oos.flush();
            oos.close();
            System.out.println("success");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

package com.fqy.serial;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Depersist {

    public static void main(String[] args) {
        Student stu = null;
        try {
            FileInputStream fis = new FileInputStream("fout.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            stu = (Student) ois.readObject();

            ois.close();
        } catch (IOException i) {
            i.printStackTrace();
            return;
        } catch (ClassNotFoundException c) {
            System.out.println("Student class not found.");
            c.printStackTrace();
            return;
        }

        /*
         * The value of the weight field was 62.0 when the object was
         * serialized, but because the field is transient, this value was not
         * sent to the output stream. The weight field of the deserialized
         * Student object is 0.
         */
        System.out.println(stu.getName() + " " + stu.getId() + " " + stu.getWeight());
    }
}
//Running result:
fqy 985 0.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值