Resources:
1, http://blog.csdn.net/u012554102/article/details/51902697
2, "in-depth analysis of Java Web Technology Insider " Xilingpo
3, http://blog.csdn.net/summer_sy/article/details/70255421
First, serialization
The process of transforming the state information of an object into a form that can be stored or transmitted (a binary byte array).
Second, the Java serialization mode
1. Implement serializable interface, ObjectOutputStream writeobject () method
2, implement Externalizable interface, rewrite the Writeexternal () method in JavaBean, ObjectOutputStream's WriteObject () method
3. Implement serializable interface, use third party jar package, convert object to JSON data format
III. Special attention
1. Serialversionuid in serialization and deserialization are tokens that differentiate whether the same object is the same. If the serializable class does not explicitly declare Serialversionuid, the serialization runtime calculates the default of the class based on the various aspects of the class
The Serialversionuid value, calculated by Serialversionuid, has a high sensitivity to the details of the class, and varies depending on the compiler implementation, which may cause unexpected
Invalidclassexception. Therefore, to ensure consistency of serialversionuid values across different Java compilers, the serialization class must declare an explicit SERIALVERSIONUID value.
2, the parent class inherits the Serializable interface, then the subclass can be serialized
3, the subclass implements the Serializable interface, but the parent class is not, the property in the parent class cannot be serialized (without error, but the data is lost), the attributes in the subclass can participate in the serialization normally
4, the serialized attribute is the object, then this object also must implement the Serializable interface, otherwise the error
5, if the original object display declaration Serialversionuid, when deserializing, if the original object's properties are modified or truncated, then the modified part of the property will be lost, but will not error
6, if the original object does not show the declaration Serialversionuid or Serialversionuid is modified, the deserialization will fail
7, through the ObjectOutputStream WriteObject () method serialization, in a multi-lingual environment, it is difficult to restore the results in other languages. We recommend using JSON, XML data structures
8, serialization and JavaBean whether there is a set, get method Independent
9. Interface Externalizable inherits interface Serializable, overrides writeexternal of interface serializable (defines which properties can be serialized), Readexternal ( based on sequence order sequence-by-read)
10, the keyword static, transient modified variables, do not participate in serialization.
Iv. code example for implementing the Serializable interface
1. Serialization
Newnew ObjectOutputStream (BOS); Os.writeobject (SRC); Os.flush (); Os.close (); byte [] B = Bos.tobytearray (); Bos.close ();
2. Deserialization
New FileInputStream ("Test.obj"new= (Uservo) ois.readobject (); Ois.close (); Fis.close ();
V. Code Examples for implementing the Externalizable interface
overriding methods in JavaBean, others consistent with implementing serializable interfaces
/*** Extended classes for serialization operations*/@Override Public voidWriteexternal (ObjectOutput out)throwsIOException {//Add a new objectDate date=NewDate (); Out.writeobject (UserName); Out.writeobject (password); Out.writeobject (date); } /*** Deserialization Extension class*/@Override Public voidReadexternal (ObjectInput in)throwsIOException, ClassNotFoundException {//Note that there is a limit to the order of acceptance here, otherwise it will go wrong.//For example, the first write is a object, then the following will be accepted must be a object ...Username=(String) in.readobject (); Password=(String) in.readobject (); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Date=(Date) in.readobject (); System.out.println ("The date after deserialization is:" +Sdf.format (date)); }
You can encrypt data in persistent use by overriding the WriteObject (), ReadObject () method.
Serialization and deserialization of Java