zoukankan      html  css  js  c++  java
  • Serializable的理解和使用 -----转载

    1.定义

    这是一个接口,当一个类实现这个接口后,这个类就变成了一个可序列化的类,它就可以被写入流,保存起来,然后也可以用流读取,反序列化。

    一般情况下,一个对象会随着程序的执行完成而消失,而有时我们需要保存下来一个对象的状态,这个时候就可以把它序列化。
    2.具体实现:

    先定义一个Person类

        class Person implements Serializable{   
            private static final long serialVersionUID = 1L; //一会就说这个是做什么的
            String name;
            int age;
            public Person(String name,int age){
                this.name = name;
                this.age = age;
            }   
            public String toString(){
                return "name:"+name+" age:"+age;
            }
        }

    测试类Test

        public class Test {
            public static void main(String[] args) {
                File file = new File("D:" + File.separator + "test.txt");
         
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                    ObjectOutputStream oos = null;
                    try {
                        oos = new ObjectOutputStream(fos);
                        Person person = new Person("tom", 22);
                        System.out.println(person);
                        oos.writeObject(person); // 写入对象
                        oos.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            oos.close();
                        } catch (IOException e) {
                            System.out.println("oos关闭失败:" + e.getMessage());
                        }
                    }
                } catch (FileNotFoundException e) {
                    System.out.println("找不到文件:" + e.getMessage());
                } finally {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        System.out.println("fos关闭失败:" + e.getMessage());
                    }
                }
         
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(file);
                    ObjectInputStream ois = null;
                    try {
                        ois = new ObjectInputStream(fis);
                        try {
                            Person person = (Person) ois.readObject(); // 读出对象
                            System.out.println(person);
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            ois.close();
                        } catch (IOException e) {
                            System.out.println("ois关闭失败:" + e.getMessage());
                        }
                    }
                } catch (FileNotFoundException e) {
                    System.out.println("找不到文件:" + e.getMessage());
                } finally {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        System.out.println("fis关闭失败:" + e.getMessage());
                    }
                }
            }
        }

    输出结果:

    name:tom    age:22
    name:tom    age:22

    如果Person类没有实现Serializable类,程序就会报如下错

    java.io.NotSerializableException: com.souter.SerializableTest.Person

    我们可以看到在Person类中定义了一个serialVersionUID,这个字段可以是1L,或者随机生成一个long类型的数据。

    这个字段是这个序列化的对象的唯一标识,如果想上面的例子,序列化和反序列化都在一起,那么不会影响,如果是网络传输,那就不需保证序列化和反序列化的ID保持一致,否则会反序列化失败
    3.有关静态变量的序列化

    如果类的一个变量是静态的,那么它不会被序列化到流中,反序列化后这个字段会为空或者0。因为本质上序列化是针对对象的序列化,而静态变量实际上是类的属性,不是对象的属性。
    4.序列化的继承

    如果父类实现了Serializable接口,那么子类不需要继承,自动成为可序列化;
    如果子类实现了Serializable,那么父类也实现Serializable才能被序列化
    ————————————————
    版权声明:本文为CSDN博主「南泽rolix」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/yufeng1397/article/details/83512586

  • 相关阅读:
    golang手动导入github net(https://github.com/golang/net)库到本地(Windows)使用
    lstm例子generate_movies函数分析
    python使用pylab一幅图片画多个图
    python数组array的transpose方法
    python Parallel delayed 示例
    json&pickle数据序列化模块
    html的标签分类————body内标签系列
    html的标签分类————可以上传的数据篇
    不可将布尔值直接与true或者1进行比较
    mock
  • 原文地址:https://www.cnblogs.com/bedfly/p/12088655.html
Copyright © 2011-2022 走看看