• 串行化对象(深度复制)


     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、深度复制

    深度复制

    复制整个对象图。
    串行化                 //将对象转换成字节数组。数据格式。
    java.io.Serializable    //可串行化接口,标识性接口。
    ObjectOuputStream       //串行
    ObjectInputStream       //反串行
    serialVersionUID        //反串行时判断类的版本。
    transient               //临时的。
    

    压缩流

    ZipInputStream      //解压缩流
    ZipOutputStream     /压缩流
    ZipEntry            //条目
    

    串行化

    将对象转换成字节数组。
    

    java.io.Serializable

    串行化接口,标识性接口。
    

    ObjectOutputStream

    java.io.NotSerializableException: com.it18zhang.archive.Person
    

    transient

    关键在,修饰不能持久化的属性。
    串行化时不包含的。
    

    深度复制

    复制的是整个对象图。
    通过java的串行化机制实现。
    

    ByteArray输入输出流可以不关闭

     

     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;
        }
    }
  • 相关阅读:
    C#类中的字段、属性和方法
    Linux下制作Windows启动U盘的工具
    将samba共享目录映射为本地文件夹(百度网盘直接下载到samba共享目录下)
    《怎样编写研究报告》读书笔记0-0
    mcnp的重复探测器单元计数-fmesh卡的介绍
    单能X射线产生方法
    matlab学习之降噪平滑算法
    matlab学习之求解函数的根和极小值
    matlab学习之绘制参数曲线,添加辅助线以及颜色设置
    MC资源整理
  • 原文地址:https://www.cnblogs.com/yihaifutai/p/6785420.html
Copyright © 2020-2023  润新知