IO P11 对象序列化

这篇博客详细介绍了Java中对象序列化的过程,包括如何将对象写入文件以及从文件中读取。文章还讨论了引用类型的序列化需求,并展示了如何使用transient关键字来排除字段不被序列化。此外,讲解了如何处理类升级问题,通过设置serialVersionUID确保序列化兼容性。
摘要由CSDN通过智能技术生成
package file;

import java.io.*;

// 对象序列化
public class TestWriteObject {
    // 实现 Serializable接口
    private static class Person implements Serializable {
        private String name;
        private int age;
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public int getAge() {
            return age;
        }
    }

    public static void writeObject() {
        Person person = new Person("hike", 27);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
            oos.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void readObject() {
        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"))) {
            Person person = (Person) ois.readObject();
            System.out.println(person.getName() + "-->" + person.getAge());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        writeObject();
        readObject();
    }
}

扩展:带有引用变量的对象序列化

引用类型也必须是可以序列化的

package file;

import java.io.*;

// 对象序列化
public class TestWriteObject {
    // 实现 Serializable接口
    private static class Person implements Serializable {
        private String name;
        private int age;
        // Job 是引用类型,需要把这个引用的类 也序列化才可以
        private Job job;
        public Person(String name, int age, Job job) {
            this.name = name;
            this.age = age;
            this.job = job;
        }
        public String getName() {
            return name;
        }
        public int getAge() {
            return age;
        }
        public Job getJob() {
            return job;
        }
    }
    private static class Job implements Serializable{
        private String job;
        public Job(String job) {
            this.job = job;
        }
        public String getJob() {
            return job;
        }
    }

    public static void writeObject() {
        Person person = new Person("hike", 27, new Job("giser"));
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
            oos.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void readObject() {
        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"))) {
            Person person = (Person) ois.readObject();
            System.out.println(person.getName() + "-->" + person.getAge() + "->" + person.getJob().getJob());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        writeObject();
        readObject();
    }
}
transient 修饰词  和  类的升级
package file;

import java.io.*;

// 对象序列化
public class TestWriteObject {
    // 实现 Serializable接口
    private static class Person implements Serializable {
        // 解决类升级问题
        private static final long serialVersionUID = 2788713780681967735L;
        private String name;
        // 给字段 使用 transient 修饰词 可以不被序列化, 序列化的时候,获取该字段的时候,获取的就是默认值,比如 int 字段 的默认值 是 0
        private transient int age;
        // Job 是引用类型,需要把这个引用的类 也序列化才可以
        private Job job;
        public Person(String name, int age, Job job) {
            this.name = name;
            this.age = age;
            this.job = job;
        }
        public String getName() {
            return name;
        }
        public int getAge() {
            return age;
        }
        public Job getJob() {
            return job;
        }
    }
    private static class Job implements Serializable{
        private String job;
        public Job(String job) {
            this.job = job;
        }
        public String getJob() {
            return job;
        }
    }

    public static void writeObject() {
        Person person = new Person("hike", 27, new Job("giser"));
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
            oos.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void readObject() {
        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"))) {
            Person person = (Person) ois.readObject();
            System.out.println(person.getName() + "-->" + person.getAge() + "->" + person.getJob().getJob());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        writeObject();
        readObject();
    }
}

transient 修饰词 和 类的升级

package file;

import java.io.*;

// 对象序列化
public class TestWriteObject {
    // 实现 Serializable接口
    private static class Person implements Serializable {
        // 解决类升级问题
        private static final long serialVersionUID = 2788713780681967735L;
        private String name;
        // 给字段 使用 transient 修饰词 可以不被序列化, 序列化的时候,获取该字段的时候,获取的就是默认值,比如 int 字段 的默认值 是 0
        private transient int age;
        // Job 是引用类型,需要把这个引用的类 也序列化才可以
        private Job job;
        public Person(String name, int age, Job job) {
            this.name = name;
            this.age = age;
            this.job = job;
        }
        public String getName() {
            return name;
        }
        public int getAge() {
            return age;
        }
        public Job getJob() {
            return job;
        }
    }
    private static class Job implements Serializable{
        private String job;
        public Job(String job) {
            this.job = job;
        }
        public String getJob() {
            return job;
        }
    }

    public static void writeObject() {
        Person person = new Person("hike", 27, new Job("giser"));
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
            oos.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void readObject() {
        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"))) {
            Person person = (Person) ois.readObject();
            System.out.println(person.getName() + "-->" + person.getAge() + "->" + person.getJob().getJob());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        writeObject();
        readObject();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值