zoukankan      html  css  js  c++  java
  • Serializable 序列化为文件

    package test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    public class Run {
    
        public static void main(String[] args) throws Exception {
    
            String personName = "Xiaoming";
            String fileName = "person_file";
    
            Person person = new Person();
            person.setName(personName);
    
            se2File(person, fileName); // here1
            // deFromFile(fileName); // here2
        }
    
        public static void se2File(Person person, String fileName)
                throws FileNotFoundException, IOException {
            System.out.println(new File(fileName).exists());
            ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(
                    fileName));
            oo.writeObject(person);
            oo.close();
            System.out.println("serialized.");
            System.out.println(new File(fileName).exists());
        }
    
        public static void deFromFile(String fileName)
                throws FileNotFoundException, IOException, ClassNotFoundException {
            ObjectInputStream oi = new ObjectInputStream(new FileInputStream(
                    fileName));
            Person person = (Person) oi.readObject();
            oi.close();
            System.out.println("deserialized.");
            System.out.println("Hi, " + person.getName());
        }
    }
    
    class Person implements Serializable {
    
        // private static final long serialVersionUID = 1L; //here4
        private String name;
    
        // private int age; // here3
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    第一步:只保留here1 ,运行。

    false
    serialized.
    true

    第二步:只保留here2 ,运行。

    deserialized.
    Hi, Xiaoming

    第三步:只保留here2、here3 ,运行。

    Exception in thread "main" java.io.InvalidClassException: test.Person; local class incompatible: stream classdesc serialVersionUID = -4720721859021305407, local class serialVersionUID = -6592546504901093862
        at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
        at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
        at java.io.ObjectInputStream.readClassDesc(Unknown Source)
        at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.readObject(Unknown Source)
        at test.Run.deFromFile(Run.java:41)
        at test.Run.main(Run.java:23)

    第4步:只保留here1(here4 ),运行。

    true
    serialized.
    true

    第5步:只保留here2(here4 ),运行。

    deserialized.
    Hi, Xiaoming

    第6步:只保留here2、here3(here4 ),运行。

    deserialized.
    Hi, Xiaoming
  • 相关阅读:
    单表清除重复数据
    调用webApi封装
    简单写入本地日志,日志文件位置与主程序exe位置相同
    APPConfig.XML获取配置文件(主程序和Dll各自的)
    获取当前运行程序上一级目录指定文件夹,没有就创建文件夹
    shell脚本中的单引号和双引号以及反引号详解
    Linux shell中反引号(`)的应用
    关于网页 硬解 软解 H264 HEVC 和你电脑起飞了那点事
    浏览器支持H.265解码总结
    微软、谷歌、亚马逊、Facebook等硅谷大厂91个开源软件盘点(附下载地址)
  • 原文地址:https://www.cnblogs.com/zno2/p/4699974.html
Copyright © 2011-2022 走看看