zoukankan      html  css  js  c++  java
  • Java se之序列化

    参考:

    Java序列化示例教程: http://www.importnew.com/14465.html
    深入学习 Java 序列化: http://www.importnew.com/24490.html
    浅析若干Java序列化工具:http://www.importnew.com/20125.html
    深入分析Java的序列化与反序列化:http://www.importnew.com/18024.html
    Java序列化漏洞对数百万应用造成威胁: http://www.importnew.com/19963.html

           经过上述一些列文章的阅读,你会重新认识什么是序列化,jdk序列化与反序列化的用处,怎么实现序列化以及原理,以及原生序列化的方式和其他开源实现的优劣等。
           序列化是持久化对象为二进制字节码,反序列化则是相反过程。首先它的用处是持久化指定对象,或者如rpc时进行网络传输以二进制形式,传输前序列化,接收端则反序列化为对象使用!
           序列化使用过程以及如何做到防漏洞,以及源码的解读,请参考上文这里不赘述。

    简单的例子:

    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    public class Dog implements Serializable{
        private static final long serialVersionUID = 6632289998332231262L;
        private String name;
        private int age;
        private String type;
    
        transient String color = "red";
        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 getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        @Override
        public String toString() {
            return "Dog [name=" + name + ", age=" + age + ", type=" + type + ",color="+ color+ "]";
        }
    
        /**
         * transient修饰的变量是不会被持久化的,而是将其变量读取为初始值,但是如果仍然像要持久化的话,只能在序列化对象中
         *  定义此下两个方法,他们会在序列化或反序列化过程中被调用。
         * 
         */
        private void writeObject(ObjectOutputStream os) throws IOException {
            //调用默认读取方法,获取常规属性
            os.defaultWriteObject();
            //需要特别对待 transient 修饰的变量
            os.writeObject(color);
        }
    
        private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
            //调用默认读取方法,获取常规属性
            is.defaultReadObject();
            //需要特别对待 transient 修饰的变量
            color = (String) is.readObject();
    
        }
    
    }
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutput;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    
    /**
     * @author Kevin
     * 
     * 
     * 序列化  需要實現serializable接口。
     * 
     * ObjectOutputStream 、 ObjectInputStream 进行序列化和反序列化。分别是使用writeObject、readObject方法
     * 
     * transient修饰的变量不会被序列化,需要在序列化对象中writeObject readObject 重新修改一番。(可以
     * 探寻ArrayList中transient Object[] elementData;但是为什么仍然会在反序列化获取到数组元素?)
     *
     */
    public class SerializDemo {
    
        public static void main(String[] args) throws IOException, ClassNotFoundException {
            Dog dog = new Dog();
            dog.setAge(20);
            dog.setName("Tom");
            dog.setType("泰迪");
    //      Dog.setColor("yellow");
    
    
            OutputStream opm = new FileOutputStream("D:\test\dog.out");
            ObjectOutput oop = new ObjectOutputStream(opm);
            oop.writeObject(dog);
            oop.close();
    
    
            FileInputStream fis = new FileInputStream("D:\test\dog.out");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Object obj = ois.readObject();
            ois.close();
            System.out.println(obj);//Dog [name=Tom, age=20, type=泰迪,color=red]
    
        }
    
    }
  • 相关阅读:
    css 控制溢出文本显示省略号效果
    限制input 文本框复制粘贴
    charCodeAt()方法
    背景全透明 background: transparent
    jQuery 双击事件(dblclick)时,不触发单击事件(click)
    html textarea(文本域)自动换行
    input 文本框无法输入 有光标
    双击鼠标滚动屏幕的代码
    ABP 日志审计 返回值开启 循环引用序列化异常 Self referencing loop detected
    浏览器相关知识点一(前端面试准备)
  • 原文地址:https://www.cnblogs.com/Kevin-1992/p/12608363.html
Copyright © 2011-2022 走看看