zoukankan      html  css  js  c++  java
  • 序列化

    原生序列化:

    https://blog.csdn.net/jtf8525140/article/details/81094222

    MessagePack

    Protocol Buffers

    Dubbo kryo

    序列化可以系统的通用性、强壮性、优化性能,同时让系统更易于调试和扩展

    1.序列化不保存静态变量的状态

    2. transient 修饰的参数 不参与序列化

    3.父类对象属性进行序列化的时候,要实现Serializable 接口

     Peron

    public class Person implements Serializable{
    
    
        private static final long serialVersionUID = -2140242550063332020L;
    
        // transient 修饰的参数 不参与序列化
        private transient int age;
    
        private  String name;
    
        public static int gender = 1;
    
        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;
        }
    }
    View Code

    序列化对象

    public class SerializeDemo {
        public static void main(String[] args) {
            serializePerson();
            //序列化不保存静态变量的状态
            Person.gender = 2;
            deSerializePerson();
        }
    
    
        private static void serializePerson() {
            try {
                ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(new File("person")));
                Person person = new Person();
                person.setAge(18);
                person.setName("mic");
                oo.writeObject(person);
                oo.flush();
                oo.writeObject(person);
                oo.flush();
                System.out.println("序列化成功"+new File("person").length());
                //-----------------------
                // 重复保存对象的时候,保存的是对象的引用
                ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("person")));
                Person person1 = (Person) inputStream.readObject();
                Person person2 = (Person) inputStream.readObject();
                System.out.println(person1==person2);
                //---------------------------
                oo.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    
    
        private static void deSerializePerson() {
            try {
                ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("person")));
                Object o = inputStream.readObject();
                if (o instanceof Person) {
                    Person person = (Person) o;
                    System.out.println(person.getAge()+","+ person.gender);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

     子类对象继承父类对象属性序列化的时候,需要父类也要实现序列化

    /**
     * 父类对象属性进行序列化的时候,要实现Serializable 接口
     */
    public class SuperPerson implements Serializable{
    
        private int age;
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    User

    public class User extends SuperPerson implements Serializable{
    
    
        private static final long serialVersionUID = 1362833492159178974L;
    
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

     demo:

    public class SuperPersonDemo {
    
        public static void main(String[] args) {
            serializePerson();
            //序列化不保存静态变量的状态
            Person.gender = 2;
            deSerializePerson();
        }
    
    
        private static void serializePerson() {
            try {
                ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(new File("person")));
                User person = new User();
                person.setAge(18);
                person.setName("mic");
                oo.writeObject(person);
                System.out.println("序列化成功");
                oo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        private static void deSerializePerson() {
            try {
                ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("person")));
                Object o = inputStream.readObject();
                if (o instanceof User) {
                    User person = (User) o;
                    System.out.println(person.getAge()+","+person.getName());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    经典51懒人5步速成法
    C语言数组元素的查询
    C语言二维数组
    C语言数组的概念
    C语言基础之水仙花数
    说说M451例程讲解之定时器
    位运算符、按位与、按位或、按位非、左移、右移、原码、反码、补码
    语音中的关于语音识别的一些知识
    caffe的db_lmdb.hpp文件
    lmdb存储的一些库相关函数
  • 原文地址:https://www.cnblogs.com/newlangwen/p/10384116.html
Copyright © 2011-2022 走看看