通过序列化复制整个对象

Posted on 2016-06-09 19:28  上善其若水,厚德载物  阅读(148)  评论(0编辑  收藏  举报

用到的所有的类都要实现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);
    
    }
}

Copyright © 2024 上善其若水,厚德载物
Powered by .NET 8.0 on Kubernetes