首页 > 代码库 > 串行化对象(深度复制)

串行化对象(深度复制)

 1 public class TestSeq {
 2     /**
 3      * 
 4      */
 5     @Test
 6     public void testClass(){
 7         Jing8 jing8 = new Jing8();
 8         Class clazz = jing8.getClass();
 9         //
10         System.out.println(clazz == Dog.class);
11         //
12         System.out.println(jing8 instanceof Dog);
13     }
14     
15     class Dog{
16         
17     }
18     class Jing8 extends Dog{
19         
20     }
21     
22     /**
23      * 测试序列
24      */
25     @Test
26     public void testSerialize() throws Exception{
27         Integer i = 100 ;
28         FileOutputStream fos = new FileOutputStream("d:/tmp.dat");
29         ObjectOutputStream oos = new ObjectOutputStream(fos);
30         oos.writeObject(i);
31         oos.close();
32         fos.close();
33         
34     }
35     /**
36      * 测试反序列
37      */
38     @Test
39     public void testDeSerialize() throws Exception{
40         Integer i = 100 ;
41         FileInputStream fis = new FileInputStream("d:/tmp.dat");
42         ObjectInputStream ois = new ObjectInputStream(fis);
43         Integer ii = (Integer)ois.readObject();
44         System.out.println(ii);
45         ois.close();
46         fis.close();
47     }
48 }

2、深度复制

 1 public class TestDogSerialize {
 2 
 3     /**
 4      * 串行化dog对象
 5      */
 6     @Test
 7     public void testSer() throws Exception{
 8         FileOutputStream fos = new FileOutputStream("d:/dog.dat");
 9         ObjectOutputStream oos = new ObjectOutputStream(fos);
10         Dog dog = new Dog("大黄", 3);
11         oos.writeObject(dog);
12         oos.close();
13         fos.close();
14     }
15     
16     /**
17      * 串行化dog对象
18      */
19     @Test
20     public void testDeSer() throws Exception{
21         FileInputStream fis = new FileInputStream("d:/dog.dat");
22         ObjectInputStream ois = new ObjectInputStream(fis);
23         Dog d = (Dog)ois.readObject();
24         System.out.println(d.getName());
25         System.out.println(d.getAge());
26         System.out.println(d.getColor());
27         ois.close();
28         fis.close();
29     }
30     
31 
32     /**
33      * 串行化dog对象
34      * 实现深度复制
35      */
36     @Test
37     public void testDogSer2() throws Exception{
38         //串行化
39         ByteArrayOutputStream baos = new ByteArrayOutputStream();
40         ObjectOutputStream oos = new ObjectOutputStream(baos);
41         
42         Dog dog = new Dog("大黄", 3);
43         Person p = new Person();
44         Cat cat = new Cat("加肥");
45         //设置关联关系
46         dog.setOwner(p);
47         dog.setPartner(cat);
48         cat.setOwner(p);
49         //
50         oos.writeObject(dog);
51         oos.writeObject(dog);
52         oos.close();
53         baos.close();
54         //
55         byte[] bytes = baos.toByteArray();
56         System.out.println();
57         
58         //反串行化
59         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
60         ObjectInputStream ois = new ObjectInputStream(bais);
61         Dog copyDog = (Dog) ois.readObject();
62         Dog copyDog1 = (Dog) ois.readObject();
63         ois.close();
64         bais.close();
65         System.out.println();
66     }
67 }

3、对象类

/**
 * Person
 */
public class Person implements Serializable{
    private static final long serialVersionUID = -7629715871683829614L;
    private String name ;

    public String getName() {
        return name;
    }

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


//cat
public class Cat implements Serializable{
    private static final long serialVersionUID = 718380935795444836L;
    private String name;
    
    private Person owner ;
    
    public Person getOwner() {
        return owner;
    }

    public void setOwner(Person owner) {
        this.owner = owner;
    }

    public Cat(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

//dog
public class Dog implements Serializable{
    private static final long serialVersionUID = 130874992691377104L;
    private String name;
    private int age;
    
    private String color ;
    
    //伙伴
    private Cat partner ;
    
    //临时的
    private transient Person owner ;
    
    public Person getOwner() {
        return owner;
    }

    public void setOwner(Person owner) {
        this.owner = owner;
    }

    public Cat getPartner() {
        return partner;
    }

    public void setPartner(Cat partner) {
        this.partner = partner;
    }

    public Dog(String name, int age) {
        super();
        this.name = name;
        this.age = age;
        System.out.println("kkkk");
    }

    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 String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

 

串行化对象(深度复制)