通过序列化复制整个对象

Posted 寂寞的小丑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过序列化复制整个对象相关的知识,希望对你有一定的参考价值。

用到的所有的类都要实现Serializable接口

public class Address implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String state;
    private String province;
    private String city;
    public Address(String s,String p,String c){
        this.state = s;
        this.province = p;
        this.city = c;
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("地区:"+state+"\\n");
        sb.append("省份:"+province+"\\n");
        sb.append("城市:"+city+"\\n");
        return sb.toString();
    }

    /**
     * @return the state
     */
    public String getState() {
        return state;
    }

    /**
     * @param state the state to set
     */
    public void setState(String state) {
        this.state = state;
    }

    /**
     * @return the province
     */
    public String getProvince() {
        return province;
    }

    /**
     * @param province the province to set
     */
    public void setProvince(String province) {
        this.province = province;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }
    
    
}
public class Employee implements Serializable{

    private String name;
    private int age;
    private Address address;
    public Employee(String n,int a,Address ad){
        this.name = n;
        this.age = a;
        this.address = ad;
        
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    /**
     * @return the address
     */
    public Address getAddress() {
        return address;
    }
    /**
     * @param address the address to set
     */
    public void setAddress(Address address) {
        this.address = address;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("姓名:"+name);
        sb.append("年龄:"+age);
        sb.append("地址:"+"\\n"+address+"\\n");
        return sb.toString();
    }
    
}

复制整个对象

    public static void main(String[] args) {
        System.out.println("序列化前");
        Address address = new Address("中国","吉林","长春");
        Employee employee1 = new Employee("张XX",30,address);
        System.out.println(employee1);
        System.out.println("序列化后:");
        ObjectOutputStream out = null;
        ObjectInputStream  in = null;
        Employee employee2 = null;
        try {
            out = new ObjectOutputStream(new FileOutputStream("emploooo.dat"));
            out.writeObject(employee1);
            in = new ObjectInputStream(new FileInputStream("emploooo.dat"));
            employee2 = (Employee)in.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally{
            //toDo释放资源
        }
        System.out.println(employee2);
    
    }
}

以上是关于通过序列化复制整个对象的主要内容,如果未能解决你的问题,请参考以下文章

serialVersionUID的作用

JavaScript对象复制

如何将数据从活动传输到片段? [复制]

如何将库类或对象传递给片段?

如何通过C#中的特定片段从句子中提取整个单词?

设计模式三 原型模式