Java se之序列化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Kevin_King1992/article/details/80045524

参考:

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]

    }

}

猜你喜欢

转载自blog.csdn.net/Kevin_King1992/article/details/80045524
今日推荐