使用序列化实现对象深拷贝

实现树节点的深拷贝
    public DefaultMutableTreeNode deepCopy(DefaultMutableTreeNode node) {
        DefaultMutableTreeNode cloneNode = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(node);

            ByteArrayInputStream bais = new ByteArrayInputStream(baos.
                    toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            cloneNode = (DefaultMutableTreeNode) ois.readObject();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            Log.errorException(this, ex);
        } catch (IOException ex) {
            ex.printStackTrace();
            Log.errorException(this, ex);
        }

        return cloneNode;
    }



附一个序列化的例子:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

//利用序列化来做深复制
//深clone
public class DeepCloneTest {

    public static Student deepCopy(Student node) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(node);

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        Student cloneNode = (Student) ois.readObject();

        return cloneNode;
    }

    public static void main(String[] args) throws Exception {
        //teacher对象将不被clone出来的Student对象共享.
        Teacher teacher = new Teacher();
        teacher.setAge(40);
        teacher.setName("Teacher zhang");

        Student student1 = new Student();
        student1.setAge(20);
        student1.setName("zhangsan");
        student1.setTeacher(teacher);

        //复制出来一个对象student2
        Student student2 = (Student) student1.deepCopy();
//        Student student2 = deepCopy(student1);
        System.out.println(student2.getAge());
        System.out.println(student2.getName());

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println(student1.getTeacher().getAge());
        System.out.println(student1.getTeacher().getName());

        //修改student2的引用对象
        student2.setName("ddddddddd");
        student2.getTeacher().setAge(50);
        student2.getTeacher().setName("Teacher Li");

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println(student1.getAge());
        System.out.println(student1.getName());
        System.out.println(student1.getTeacher().getAge());
        System.out.println(student1.getTeacher().getName());
    }
}


class Teacher implements Serializable {

    private static final long serialVersionUID = -8834559347461591191L;

    public int age;
    public String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}


class Student implements Serializable, Cloneable {

    //serialVersionUID 如果你的对象序列化后存到硬盘上面后,可是后来你却更改了类的field(增加或减少或改名),当你反序列化时,就会出现Exception的,这样就会造成不兼容性的问题。
    //但当serialVersionUID相同时,它就会将不一样的field以type的缺省值赋值(如int型的是0,String型的是null等),这个可以避开不兼容性的问题。所以最好给serialVersionUID赋值
    private static final long serialVersionUID = 7991552226614088458L;

    public int age;
    public String name;
    public Teacher teacher;
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    //序列化实现深拷贝
    public Object deepCopy() throws Exception {
        //将该对象序列化成流,因为写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        ObjectOutputStream oos = new ObjectOutputStream(bos);

        oos.writeObject(this);

        //将流序列化成对象
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

        ObjectInputStream ois = new ObjectInputStream(bis);

        return ois.readObject();
    }

    //浅拷贝
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}

你可能感兴趣的:(jvm)