Thinking in java-39 序列化 Serialization

来源:互联网 发布:js获取json中的数据 编辑:程序博客网 时间:2024/05/01 05:20

1. 对象序列化的意义

本文详细内容参考自java_T_point && tutorialspoint.
这里写图片描述
Java语言中的序列化:将对象状态写入字节流中的机制。
对象可以用一组包含该对象数据和信息(包含对象的类型、存储在对象中的数据)的字节所表示。在序列化对象写入到文件之后,该对象可以从文件中读出,并进行反向的解序列化过程。也就是说,类型信息、代表该对象的字节信息及相关数据可以用来在内存中重建该对象数据。

2. 对象序列化常用的类和方法

//Methods used in Serializationjava.io.FileOutputStream.FileOutputStream(String name) throws FileNotFoundExceptionjava.io.ObjectOutputStream.ObjectOutputStream(OutputStream out) throws IOExceptionvoid java.io.ObjectOutputStream.writeObject(Object obj) throws IOException//Methods used in Deserializationjava.io.FileInputStream.FileInputStream(String name) throws FileNotFoundExceptionjava.io.ObjectInputStream.ObjectInputStream(InputStream in) throws IOExceptionObject java.io.ObjectInputStream.readObject() throws IOException, ClassNotFoundException

3. 序列化和解序列化Demo

对于要持久化的对象,必须实现Serializable interface.

package com.fqy.serial;import java.io.Serializable;public class Student implements Serializable {    private static final long serialVersionUID = 1L;    private int id;    private transient double weight;    private String name;    public Student(int id, String name, double weight) {        this.id = id;        this.name = name;        this.weight = weight;    }    public int getId() {        return id;    }    public String getName() {        return name;    }    public double getWeight() {        return weight;    }}package com.fqy.serial;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;public class Persist {    public static void main(String[] args) {        Student stu = new Student(985, "fqy", 62.0);        // Create a fileOutputStream to write to the file with a specified name        try {            FileOutputStream fout = new FileOutputStream("fout.txt");            // Create an ObjectOutstream that writes to the specified            // OutputStream            ObjectOutputStream oos = new ObjectOutputStream(fout);            // Write the specified object to the ObjectOutputStream            oos.writeObject(stu);            oos.flush();            oos.close();            System.out.println("success");        } catch (IOException ex) {            ex.printStackTrace();        }    }}package com.fqy.serial;import java.io.FileInputStream;import java.io.IOException;import java.io.ObjectInputStream;public class Depersist {    public static void main(String[] args) {        Student stu = null;        try {            FileInputStream fis = new FileInputStream("fout.txt");            ObjectInputStream ois = new ObjectInputStream(fis);            stu = (Student) ois.readObject();            ois.close();        } catch (IOException i) {            i.printStackTrace();            return;        } catch (ClassNotFoundException c) {            System.out.println("Student class not found.");            c.printStackTrace();            return;        }        /*         * The value of the weight field was 62.0 when the object was         * serialized, but because the field is transient, this value was not         * sent to the output stream. The weight field of the deserialized         * Student object is 0.         */        System.out.println(stu.getName() + " " + stu.getId() + " " + stu.getWeight());    }}//Running result:fqy 985 0.0
原创粉丝点击