首先声明:我这里只是简单说明一下,如果想深入研究Serialization,请读 Thinking in Java(www.mindview)的I/O章节,以及 Serialization Specification (http://java.sun.com/j2se/1.4.2/docs/guide/serialization/spec/serialTOC.html)Java Object序列化允许你: 在你需要序列话的类实现接口Serializable后,将这个类的实例转换成byte类型的序列。这样的话,在以后你可以重新产生你原始的Object. 这样的话,你就可以通过网络(Java的序列化可以在不同的操作系统之间实现,你可以在windows操作系统上生成一个Object,并且序列化它,而后将序列话以后的Object通过网络传输到Unix操作系统的机器上,你也一样可以正确的重新构建它,你不需要考虑在不同的机器上你的Object的不同表现形式(byte类型的排序或者其他))序列化允许你轻量级的保存(lightweight persistence),轻量级的保存:一个Object的生命不在于程序是否在执行,而在于程序的调用之间。通过序列化一个Object,并且将他写入硬盘(或其他存储介质),你可以重新构键它(通过程序读入你刚刚写入的文件)下面是一个简单的例子:import java.io.*;
import java.util.*;class Data implements Serializable { // 实现序列话接口
  private int n;
  public Data(int n) { this.n = n; }
  public String toString() { return Integer.toString(n); }
}public class Worm implements Serializable {
  public static void main(String[] args)
  throws ClassNotFoundException, IOException { // 序列话读入和写入Object可能会有这两个异常
      // 将你要序列化的object,保留到一个文件中
      Random rand = new Random();
      Data d = new Data(rand.nextInt(10)); //构建你需要序列话的Object
      System.out.println("d = " + d);
      ObjectOutputStream out = new ObjectOutputStream(
        new FileOutputStream("worm.out")); // 准备写入的文件
      out.writeObject(d);
      out.flush();  
      out.close(); // 执行到这里你可以看见worm.out这个文件,
      // 以下的代码读出你刚刚写入Object
      ObjectInputStream in = new ObjectInputStream(
        new FileInputStream("worm.out")); // 读你刚刚写入的文件
      Data d2 = (Data)in.readObject(); // 重新构建你刚刚写入的Object
      System.out.println("d2 = " + d2);  }
} 这只是一个简单的例子,下面简单说一下什么时间用:
1、Java的RMI(Remote method Invocation) 技术: Object生存在其他的机器上就象在你自己的机器上一样,当发送消息到远程的Object, 传输的参数和结果就需要序列化
2、JavaBeans技术 当一个Bean用到时,他的状态信息在设计的时间产生,并且需要将状态信息存储起来,以便以后重新构建。

解决方案 »

  1.   

    以上的说明可能会让大家笑话的(My English is poor!),研究一个东西最好先用搜索看看网上有没有自己需要的信息,如果没有再在论坛上提问(建议而已)在Serializable接口的API上, sun就有详细说明,如:Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable. To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime. During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream. When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object. Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures: 
     private void writeObject(java.io.ObjectOutputStream out)
         throws IOException
     private void readObject(java.io.ObjectInputStream in)
         throws IOException, ClassNotFoundException;
     
    The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The default mechanism for saving the Object's fields can be invoked by calling out.defaultWriteObject. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput. The readObject method is responsible for reading from the stream and restoring the classes fields. It may call in.defaultReadObject to invoke the default mechanism for restoring the object's non-static and non-transient fields. The defaultReadObject method uses information in the stream to assign the fields of the object saved in the stream with the correspondingly named fields in the current object. This handles the case when the class has evolved to add new fields. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput. Serializable classes that need to designate an alternative object to be used when writing an object to the stream should implement this special method with the exact signature: 
     ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
     
    This writeReplace method is invoked by serialization if the method exists and it would be accessible from a method defined within the class of the object being serialized. Thus, the method can have private, protected and package-private access. Subclass access to this method follows java accessibility rules. Classes that need to designate a replacement when an instance of it is read from the stream should implement this special method with the exact signature.
     ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
     
    This readResolve method follows the same invocation rules and accessibility rules as writeReplace.