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

    序列化的存在是为了让他们离开内存空间,存入磁盘,以便长期保存。

    反序列化就是把磁盘中的文件拿到内存中来。

    以内存为基准,想内存中放数据就使用ObjectInputStream   

      从内存中取出数据到磁盘就使用ObjectOutputStream


    只有实现了 Serializable或 Externalizable接口的类的对象才能被序列化。
    实现Externalizable接口的类完全由自身来控制序列化的行为,而仅实现Serializable接口的类可以 采用默认的序列化方式 。

    序列化使用的是 

      ObjectOutputStream对象   和  writeObject方法

    反序列化使用的是ObjectInputStream   和readObject方法

    1.创建一个Person类实现了Serializable接口

    package cn.happy.Serialization;
    
    import java.io.Serializable;
    
    public class Person implements Serializable {
    
         private static final long serialVersionUID = -5809782578272943999L;//序列化版本标识符
         private int age;
         private String name;
         private String sex;
    
    
        public static long getSerialVersionUID() {
            return serialVersionUID;
        }
    
        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 String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    }

    2.实现序列化和反序列化

    package cn.happy.Serialization;
    
    import java.io.*;
    import java.text.MessageFormat;
    
    public class TestObjSerializeAndDeserialize {
    
        public static void main(String[] args) throws Exception {
                    //序列化方法
                    SerializePerson();
                    //反序列化方法
            Person p = DeserializePerson();
            System.out.println(MessageFormat.format("name={0},age={1},sex={2}",p.getName(),p.getAge(),p.getSex()));
        }
        //序列化
        private static void SerializePerson() throws Exception{//记得声明异常,不然new FileOutputStream(new File("D:/序列化输入地址.txt"))会报错。
            Person person=new Person();
            person.setAge(12);
            person.setName("小明");
            person.setSex("男");
            //ObjectOutputStream将内存中的内容输出到磁盘中。
            ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream(new File("D:/序列化输入地址.txt")));
            outputStream.writeObject(person);
            System.out.println("序列化成功");
            outputStream.close();
        }
        //反序列化
        private static Person DeserializePerson()throws Exception{
            //使用ObjectInputStream写到内存
            ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream(new File("D:/序列化输入地址.txt")));
            Person ps= (Person) objectInputStream.readObject();
            System.out.println("反序列化成功");
            return ps;
        }
    }

    结果:

  • 相关阅读:
    git push 出现 you are not allowed to upload merges 错误提示
    构建React-app应用时create-react-app卡住超慢的解决办法<转>
    防抖与节流函数<转>
    this全面解析<转>
    正确的安装和使用nvm(mac)<转>
    TypeScript如何添加自定义d.ts文件(转)
    为什么angular library的build不能将assets静态资源打包进去(转)
    Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
    linux命令新建文件
    mac系统终端sudo免输入密码技能get
  • 原文地址:https://www.cnblogs.com/java-263/p/10171176.html
Copyright © 2011-2022 走看看