序列化可以将对象写入字节流,他也可以从字节流中读取对象。将对象状态转换成字节流之后,可以用java.io包中的各种字节流类将其保存到文件中,管道到另一线程中或通过网络连接将对象数据发送到另一主机。对象序列化功能非常简单、强大,在RMI、Socket、JMS、EJB都有应用。java核心类的许多类都实现了java.io.Serializable接口
   而且,java的序列化比较简单,通常不需要编写保存和恢复对象状态的定制代码。实现java.io.Serializable接口的类对象可以转换成字节流或从字节流恢复,不需要在类中增加任何代码。

解决方案 »

  1.   

    高峰写得比较清楚了我觉得:
    一、serialization是java的一个强大的功能,serialization仅是一个接口,是java的一个标识接口,中文意思为“串行化”,相应的也就有“反串行化”的概念了,某类实现此接口时(即加上implement serialization),即意味着此类可以串行化,串行化的意思即是可以将自己的状态保存起来(以字节流或字符流的形式),可以保存在文件或某种流在网上传递,日后可以反串行化恢复此对象。二、我觉得java的序列化不一定仅将对象写入字节流,还可以将对象写成字符流,如rmi,串行化的对象即是以字符流在网络上传递的。三、使用serialization时要小心,并不是每种类都支持serialization得很好!而且有时要实现一些特殊的方法。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. 
      

  2.   

    要说明一下
    java中的java.io.Serializable接口是个标志接口
    也就是说其功能又虚拟机实现了
    不须你操心
      

  3.   

    rushfly(天圆地方) 帖出来的那段e文很说明问题。
     举个例子,比如说java.sql.Connection的实例就是一种不支持序列化的对象......