1.JAVA API 1.6.0文档对JAVA序列化的解释:
类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。
要允许不可序列化类的子类型序列化,可以假定该子类型负责保存和恢复超类型的公用 (public)、受保护的 (protected) 和(如果可访问)包 (package) 字段的状态。仅在子类型扩展的类有一个可访问的无参数构造方法来初始化该类的状态时,才可以假定子类型有此职责。如果不是这种情况,则声明一个类为可序列化类是错误的。该错误将在运行时检测到。
在反序列化过程中,将使用该类的公用或受保护的无参数构造方法初始化不可序列化类的字段。可序列化的子类必须能够访问无参数构造方法。可序列化子类的字段将从该流中恢复。
当遍历一个图形时,可能会遇到不支持 Serializable 接口的对象。在此情况下,将抛出 NotSerializableException,并将标识不可序列化对象的类。
2.笔者对JAVA序列化的简单实现:
a.首先定义一个将被序列化的Student类

package com.neusoft.test1;  

import java.io.Serializable;  

class Student implements Serializable {// 定义一个可以被序列化的类  

    String name;  

    int age;  

    String num;  

    double score;  

    public Student() {  

    }  

    public Student(String name, int age, String num, double score) {  

        this.name = name;  

        this.age = age;  

        this.num = num;  

        this.score = score;  

    }  

    public String toString() {  

        return name + "\t" + age + "\t" + num + "\t" + score;  

    }  

} 

 b.定义一个用于将Student对象序列化的类 

package com.neusoft.test1;  

import java.io.File;  

import java.io.FileOutputStream;  

import java.io.IOException;  

import java.io.ObjectOutputStream;  

public class SerializableTest {  

    public static void main(String[] args) {  

        Student stu_1 = new Student("wjl", 25, "47843847", 89);// 实例化两个可以被序列化的student对象  

        Student stu_2 = new Student("yxm", 23, "47856547", 99);  

        File f = new File("c:/demo/456.txt");// 保存两个对象的文件对象  

        try {  

            FileOutputStream fos = new FileOutputStream(f);  

            ObjectOutputStream oos = new ObjectOutputStream(fos);  

            System.out.println("没有被序列化时的对象如下:");  

            System.out.println(stu_1);  

            System.out.println(stu_2);  

            oos.writeObject(stu_1);  

            oos.writeObject(stu_2);  

            System.out.println("序列化成功!!");  

            oos.flush();  

            fos.close();  

            oos.close();  

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }  

}  

c.定义一个用于将Student对象反序列化的类 

package com.neusoft.test1;  

import java.io.File;  

import java.io.FileInputStream;  

import java.io.IOException;  

import java.io.ObjectInputStream;  

public class TurnSerializableTest {  

    public static void main(String[] args) {  

        File f = new File("c:/demo/456.txt");  

        try {  

            FileInputStream fis = new FileInputStream(f);  

            ObjectInputStream ois = new ObjectInputStream(fis);  

            Student stu_1;  

            stu_1 = (Student) ois.readObject();  

            System.out.println(stu_1);  

            Student stu_2 = (Student) ois.readObject();  

            System.out.println(stu_2);  

            fis.close();  

            ois.close();  

        } catch (ClassNotFoundException e) {  

            e.printStackTrace();  

        } catch (IOException e) {  

            e.printStackTrace();  

        }  

    }  

}


d.将这三个类放在一个包下,首先执行SerializableTest类将Student的两个对象序列化,并保存到c:/demo/456.txt文件中,然后再执行TurnSerializableTest类将c:/demo/456.txt文件中保存的两个Student对象反序列化回来。