Reprint http://www.cnblogs.com/DreamDrive/p/4005966.html
An object has some corresponding properties, and the process of saving the object on the hard disk is called "persistence".
Extend the life cycle of objects in the heap memory to the hard disk and make persistent operations. When you need this object again, we don't have to new, just read it from the hard drive. (Storage to the hard disk is a file, do not need us to parse, if you open the parsing with Notepad will appear garbled, parsing to be in a specific way, without our tube. We only need to read).
The object is stored in a file on the hard disk, and the standard extension of this file is (. Object).
In many frameworks there will be this. Object end of the file, because many objects are not created, created too cumbersome, directly read, and some object values you do not know, the framework is sealed in the. Object file, directly read this file in the value of the line, do not need to pass this value.
1 Import java.io.Serializable; 2/* 3 * Serializable: Used to add an ID number to the serialized class. 4 * is used to determine if the class and object are of the same version. 5 * /6 public class person implements serializable/* tag interface */{7 /** 8 * Transient: Non-static data does not want to be serialized can use this keyword adornment. 9 */10 private static final long serialversionuid = 9527l;// private transient String name;12 p Rivate string name;13// private static int age;14 private int age;15 (string name, I NT age) {+ super (); this.name = name;19 this.age = age;20 }21 public String getName () {22< C20/>return name;23 }24 public void SetName (String name) {+ this.name = name;26 }27 Public int Getage () { age;29}30 public void Setage (int.) { this.age = age;32 }33}
1 public class Objectstreamdemo {2 /** 3 * @param args 4 * @throws ioexception 5 * @throws CLASSNOTFO Undexception 6 */7 public static void Main (string[] args) throws IOException, ClassNotFoundException {8
//writeobj (); 9 readobj (); }11 public static void Readobj () throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream (New FileInputStream ("Obj.object")), and the deserialization of the object. The person p = (person) ois.readobject (); System.out.println (P.getname () + ":" +p.getage ()); Ois.close (); }18 public static void Writeobj () throws IOException, IOException { ObjectOutputStream oos = new ObjectOutputStream (New FileOutputStream ("Obj.object")), and/or object serialization. the serialized object must implement the Serializable interface. Oos.writeobject (New Person ("Xiao Qiang"); oos.close (); }26}
The function of the serial number: for example, when you first serialize this person class with the private type serialized, but before you deserialize it, you change the private to public. This causes the exception to be reported when deserializing the read.
Exception in thread "main" Java.io.InvalidClassException:cn.itcast.serializable.Person; Local class Incompatible:stream Classdesc serialversionuid = 9527, local class Serialversionuid = 7915096815468332737
It's about the number of the person class is not uniform. If you add a version number, the above modification can be deserialized.
@throws ClassNotFoundException
If only obj.object this file can be removed from the object person, because any object created in heap memory must depend on the class file that the object belongs to (class file), if only to Obj.object, which has the person object's byte code, But when you take out the memory is not person.class file, no, so do not get out, so you must have obj.object files and person.class files. (So there is a ClassNotFound exception)
Class ObjectInputStream
ObjectInputStream the basic data and objects previously written using ObjectOutputStream are deserialized.
The meaning of this sentence is that ObjectInputStream can only read objectoutputstream.
When ObjectOutputStream and ObjectInputStream are used with FileOutputStream and FileInputStream respectively, you can provide the application with persistent storage of object graphics.
Serialization is arranged in the order of
The default serialization mechanism for an object is written to the object's class, the class signature, and the values of non-transient and non-static fields. (because statically static things are in the method area.)
Use time: For certain characteristics of objects, such as database connection objects, storage of specific data objects, so that objects you do not want to create them, want to store, so that their life cycle, you can put them on the hard disk. Every time the system starts, it reads the object and the data inside it. At this point, they can be serialized to complete.
It simply stores the object from the heap memory on the hard disk.
The Serializableid number is calculated based on the class's characteristics and the class's signature. Why the ID number is so long is because to avoid duplication. So serializable is used to add ID to the class. Used to determine if a class and an object are the same version.
If the serializable class does not explicitly declare Serialversionuid, the serialization runtime calculates the default Serialversionuid value for the class based on the various aspects of the class. The reason is that calculating the default Serialversionuid has high sensitivity to the details of the class , depending on the compiler implementation, it can vary widely, which can cause unexpected results during deserialization InvalidClassException
.
When it comes to web development, some classes need to implement a serialization interface, because the server will temporarily store your objects locally. It's afraid that after the server has collapsed, your session is gone. So stored on the hard disk, you restart the server will restore the previous session, restore the object, you run before the things are in.
//====================================================================
Specific examples can be seen in my blog post (reprint + summary):
Http://www.cnblogs.com/DreamDrive/p/5412931.html
Summary of the functions of the Java serialization interface serializable interface