序列化与反序列化Serializable,Externalizable
发布日期:2021-10-12 20:08:31 浏览次数:11 分类:技术文章

本文共 4124 字,大约阅读时间需要 13 分钟。

1.代码

Demo3测试类

/* * zt * 2020/8/8 * 15:34 *序列化:把Person对象写入硬盘中 要求:1.序列化的类必须是Serializable自动或者Externalizable手动序列化接口      2.序列化的类要添加一个私有的long类型静态常量serialVersionUID,保证序列化的类反序列化的类是同一个类。      (1)使用transient修饰的属性,不可以序列化      (2)静态属性不能序列化      面试题:使用transient的属性一定不能序列化吗?  能 , 使用 Externalizable 实现。      @Override    public void writeExternal(ObjectOutput out) throws IOException {        out.writeObject(name);        out.writeObject(age);    }    @Override    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {        name=(String)in.readObject();        age=(int)in.readObject();    } */import java.io.*;import java.util.ArrayList;public class Demo3 {    public static void main(String[] args) throws Exception {//        writeObject();        readObject();    }    //序列化    public static void writeObject() throws Exception {        //创建ObjectOutoutStream        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e:\\aaa.txt"));        //创建对象        Person zhuxi = new Person("aa",20);        Person xiongfei = new Person("bb",20);        Person.country="china";        ArrayList
list = new ArrayList<>(); list.add(zhuxi); list.add(xiongfei); oos.writeObject(list); oos.close(); System.out.println("序列化成功"); } //反序列化 public static void readObject() throws Exception { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:\\aaa.txt")); ArrayList
list = (ArrayList
) ois.readObject(); for (Person person : list) { System.out.println(person.toString()); } System.out.println(Person.country); ois.close(); }}

Person类

import java.io.Externalizable;import java.io.IOException;import java.io.ObjectInput;import java.io.ObjectOutput;public class Person implements Externalizable,Cloneable {    //serialVersionUID    private static final long serialVersionUID=1000L;    private String name;    private transient int age;    public static String country="中国";    private Address add;    public Person(String name, int age) {        this.name = name;        this.age = age;        System.out.println("带参构造执行了");    }    public Person() {        System.out.println("无参构造执行了");    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public Address getAdd() {        return add;    }    public void setAdd(Address add) {        this.add = add;    }    @Override    public String toString() {        return "Person{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }    @Override    public void writeExternal(ObjectOutput out) throws IOException {        out.writeObject(name);        out.writeObject(age);    }    @Override    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {        name=(String)in.readObject();        age=(int)in.readObject();    }    @Override    public Object clone() throws CloneNotSupportedException {        Person p=(Person)super.clone();        p.setAdd((Address) add.clone());        return p;    }}

Address类

public class Address implements Cloneable{    private String city;    private String area;    public Address(String city, String area) {        this.city = city;        this.area = area;    }    public Address() {    }    public String getCity() {        return city;    }    public void setCity(String city) {        this.city = city;    }    public String getArea() {        return area;    }    public void setArea(String area) {        this.area = area;    }    @Override    public String toString() {        return "Address{" +                "city='" + city + '\'' +                ", area='" + area + '\'' +                '}';    }    @Override    protected Object clone() throws CloneNotSupportedException {        return super.clone();    }}

2.运行结果

无参构造执行了无参构造执行了Person{name='aa', age=20}Person{name='bb', age=20}中国

转载地址:https://blog.csdn.net/qq_39773004/article/details/107881367 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java实现客户端服务端互发消息并接收
下一篇:将一个txt文件,复制到另一个txt文件中(缓冲字节流(BufferedInputStream,BufferedOutputStream))

发表评论

最新留言

不错!
[***.144.177.141]2024年04月07日 18时52分04秒