An object has some corresponding attributes. The process of saving this object on a hard disk is called "persistence ".
Extend the lifecycle of objects in the heap memory, store them to the hard disk, and perform persistence operations. when we need this object again next time, we don't need new, just read it from the hard disk. (It is a file stored on the hard disk. It does not need to be parsed. If you use NotePad to open the file for resolution, garbled characters will appear. The resolution should be in a specific way, so we don't need to worry about it. we only need to read ).
Store the object to a file on the hard disk. The standard extension of this file is (. object ).
This will happen in many frameworks. because many objects are not created at the end of an object, it is too difficult to create them and can be read directly. You do not know the values of some objects. The framework is stored in. in the object file, you can directly read the value in this file. You do not need to upload this value.
1 import java. Io. serializable; 2/* 3 * serializable: Used to add an ID number to the serialized class. 4 * determines whether the class and object are of the same version. 5 */6 public class person implements serializable/* mark interface */{7/** 8 * transient: non-static data does not want to be serialized. You can use this keyword to modify it. 9 */10 Private Static final long serialversionuid = 9527l; 11 // Private transient string name; 12 private string name; 13 // Private Static int age; 14 private int age; 15 16 public person (string name, int age) {17 super (); 18 this. name = Name; 19 this. age = age; 20} 21 Public String getname () {22 return name; 23} 24 public void setname (string name) {25 this. name = Name; 26} 27 public int getage () {28 return age; 29} 30 public void setage (INT age) {31 This. age = age; 32} 33}
1 public class objectstreamdemo {2/** 3 * @ Param ARGs 4 * @ throws ioexception 5 * @ throws classnotfoundexception 6 */7 public static void main (string [] ARGs) throws ioexception, classnotfoundexception {8 // writeobj (); 9 readobj (); 10} 11 public static void readobj () throws ioexception, classnotfoundexception {12 objectinputstream OIS = new objectinputstream (New fileinputstream ("obj. object "); 13 // Deserialization of objects. 14 person P = (person) Ois. readobject (); 15 system. out. println (P. getname () + ":" + P. getage (); 16 Ois. close (); 17} 18 19 public static void writeobj () throws ioexception, ioexception {20 21 objectoutputstream OOS = new objectoutputstream (New fileoutputstream ("obj. object "); 22 // Object serialization. The serialized object must implement the serializable interface. 23 OOS. writeobject (new person ("Xiaoqiang", 30); 24 OOS. Close (); 25} 26}
Serial number: for example, when you first serialize the person class, the private type is used for serialization. However, before deserialization, changed this private to public. in this way, an exception is reported during deserialization and reading.
Exception in thread "Main" Java. Io. invalidclassexception: CN. itcast. serializable. person; local class incompatible: stream classdesc serialversionuid = 9527, local class serialversionuid = 7915096815468332737
The version numbers of the person class are inconsistent. If a version number is added, the above modification can also be deserialized.
@ ThrowsClassnotfoundexception
If only obj. object can be used to retrieve the object person, because any object created in heap memory must depend on the class file (class file) to which the object belongs. object, which contains the byte code of the person object. However, when you retrieve the object, there is no person in your memory. class file, no, so it cannot be obtained, so there must be obj. object File and person. class file. (so there is a classnotfound exception)
Class objectinputstream
Objectinputstream deserializes the basic data and objects previously written using objectoutputstream.
This sentence means that objectinputstream can only read objectoutputstream.
Objectoutputstream and objectinputstream can be used with fileoutputstream and fileinputstream respectively to provide persistent storage for object graphics for applications.
Serialization is arranged in order.
Serializable is a mark (this mark must be available to implement serialization)
The default serialization mechanism of an object writes the following content: the Object Class, Class signature, and values of non-transient and non-static fields. (Because static objects are in the method area .)
When using: For objects with certain characteristics, such as database connection objects and objects that store specific data, you do not want to create them and want to store them to prolong their lifecycle, they can be placed in the hard disk. every time the system is started. the object reads the object and the data in the object. In this case, they can be serialized.
Only store objects from the heap memory to the hard disk.
Interface serializable
During serialization, a version number called serialversionuid is used to associate with each serializable class. in the deserialization process, this serial number is used to verify whether the sender and receiver of the serialized object have loaded classes compatible with serialization for this object. (When an object is serialized, an ID number is assigned to the class. this ID can be used for verification. are the objects in the file storage you gave me the same version as the class you gave me ....)
This ID number is calculated based on the characteristics of the class and the class signature. Why is the ID number so long.
Therefore, serializable adds an ID to the class. It is used to determine whether the class and object are of the same version.
If the serialversionuid is not explicitly declared for the serializable class, the default serialversionuid value of the class is calculated based on all aspects of the class during the serialization runtime. the reason is that the calculation of the default serialversionuid is highly sensitive to the detailed information of the class. The differences may vary according to the compiler implementation, which may lead to exceptions in the deserialization process.InvalidClassException.
When learning the Web, some classes need to implement the serialization interface, because the server will temporarily store your objects locally. it is afraid that your sessions will disappear after the server crashes. therefore, if you store the data on the hard disk and restart the server, the previous sessions and objects will be restored.
Understanding of serialization Interfaces