IO P11 对象序列化

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();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值