类的序列化和反序列化

本文介绍了一个Java实现的序列化与反序列化的例子,通过实现Serializable接口并使用transient关键字来排除特定字段的序列化过程。示例中创建了一个Employee类,演示了如何序列化对象到文件以及如何从文件中反序列化读取对象。

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

要使一个类能进行序列化操作,并实现Serializable接口
如果类中的某些成员变量不想实现序列化,则需要在前面添加关键字transient

实体类

package cn.io;

import java.io.Serializable;

/**
 * @author Duoduo
 * @version 1.0
 * @date 2017/4/16 22:43
 */
public class Employee implements Serializable {

    private transient String name;// 不需要序列化
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

测试类

package cn.io;

import java.io.*;

/**
 * 只有类实现了 Serializable 才能进行序列化操作
 * @author Duoduo
 * @version 1.0
 * @date 2017/4/16 22:43
 */
public class Test3 {

    public static void main(String[] args) {
        writeObject();
        readObject();
    }

    /**
     * 反序列化
     */
    public  static void readObject(){
        File file = new File("/Users/Pengjinjin/Desktop/employee.txt");
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));

           Object object = objectInputStream.readObject();
           if( object instanceof  Employee){
               Employee employee =(Employee)object;
               System.out.println(employee.getSalary());
           }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(objectInputStream!=null){
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 序列化
     */
    public static void writeObject() {
        File file = new File("/Users/Pengjinjin/Desktop/employee.txt");
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

            Employee employee = new Employee("employee",1000);
            objectOutputStream.writeObject(employee);
            objectOutputStream.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(objectOutputStream!=null){
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

最后运行结果:获取到Employee的salary 为 1000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值