Java基础巩固系列 对象流

本文介绍Java中对象的序列化与反序列化过程,包括如何将内存中的对象转换为二进制流存储到硬盘文件中,以及如何从硬盘文件中读取并转换回对象。展示了Person和Pet类的具体实现。

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

代码示例:

public class testObjectIntOutputStream {
    //对象的反序列化过程:将硬盘中的文件通过ObjectInputStream转换为相应的对象
    @Test
    public void testObjectInputStream() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("person.txt"));
            Person p1 = (Person) ois.readObject();
            System.out.println(p1);
            Person p2 = (Person) ois.readObject();
            System.out.println(p2);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois !=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //对象的序列化过程,将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中
    @Test
    public void testObjectOutputStream() {
        Person p1 = new Person("小明", 23,new Pet("花花"));
        Person p2 = new Person("小红", 21,new Pet("石头"));

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("person.txt"));

            oos.writeObject(p1);
            oos.flush();
            oos.writeObject(p2);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

/*
 * 要实现序列化的类:1.要求此类是可序列化的:实现Serializable接口
 * 2.要求类的属性同样的要实现Serializable接口
 * 3.提供一个版本号:public static final long serialVersionUID
 * 4.使用static或transient修饰的属性,不可实序列化
 * */
class Person implements Serializable {
    public static final long serialVersionUID = 2332423423442131L;
    static String name;
    transient Integer age;
    Pet pet;

    public Person(String name, Integer age,Pet pet) {
        this.name = name;
        this.age = age;
        this.pet = pet;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", pet=" + pet +
                '}';
    }
}

class Pet implements Serializable{
    String name;

    public Pet(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                '}';
    }
}public class testObjectIntOutputStream {
    //对象的反序列化过程:将硬盘中的文件通过ObjectInputStream转换为相应的对象
    @Test
    public void testObjectInputStream() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("person.txt"));
            Person p1 = (Person) ois.readObject();
            System.out.println(p1);
            Person p2 = (Person) ois.readObject();
            System.out.println(p2);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois !=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //对象的序列化过程,将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中
    @Test
    public void testObjectOutputStream() {
        Person p1 = new Person("小明", 23,new Pet("花花"));
        Person p2 = new Person("小红", 21,new Pet("石头"));

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("person.txt"));

            oos.writeObject(p1);
            oos.flush();
            oos.writeObject(p2);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

/*
 * 要实现序列化的类:1.要求此类是可序列化的:实现Serializable接口
 * 2.要求类的属性同样的要实现Serializable接口
 * 3.提供一个版本号:public static final long serialVersionUID
 * 4.使用static或transient修饰的属性,不可实序列化
 * */
class Person implements Serializable {
    public static final long serialVersionUID = 2332423423442131L;
    static String name;
    transient Integer age;
    Pet pet;

    public Person(String name, Integer age,Pet pet) {
        this.name = name;
        this.age = age;
        this.pet = pet;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", pet=" + pet +
                '}';
    }
}

class Pet implements Serializable{
    String name;

    public Pet(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                '}';
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值