The public interface Serializable class enables its serialization functionality by implementing the Java.io.Serializable interface. Classes that do not implement this interface will not be able to serialize or deserialize any of their states. All the subtypes of a serializable class are themselves serializable. The serialized interface has no methods or fields and is used only to identify the semantics of serializable.
Serialization is the preservation of the state of an object (the amount of each property), which is then obtained at the appropriate time.
Serialization consists of two parts: serialization and deserialization. Serialization is the first part of this process that decomposes data into a byte stream for storage in a file or on a network. Deserialization is the opening of a byte stream and refactoring the object. Object serialization not only converts the base data type to a byte representation, but sometimes restores the data. Recovering data requires an object instance with recovery data
What are the characteristics of serialization:
If a class can be serialized, its subclasses can also be serialized.
member data declared as static and transient types cannot be serialized.
Static is shared by the class, and when an object of that class is serialized, the static variable may be changed by another object, so this determines that the static variable cannot be serialized, but if it is final, it is possible because this is a constant.
If you declare an instance variable with transient, its value does not need to be maintained when the object is stored. In other words, a member variable tagged with the transient keyword does not participate in the serialization process.
When to use serialization:
Object serialization enables the implementation of distributed objects. Main applications For example: RMI to use object serialization to run a service on a remote host, just as you would when running an object on a local machine.
Java object serialization preserves not only the data of an object, but also the data of each object referenced by the object. You can write the entire object hierarchy to a stream of bytes that can be saved in a file or passed on a network connection. Object serialization allows you to "deep copy" the object, which is to copy the object itself and the referenced object itself. Serializing an object may get the entire sequence of objects.
Java code 650) this.width=650; "class=" star "src=" Http://injavawetrust.iteye.com/images/icon_star.png "alt=" Favorites Code "style = "border:0px;"/>
import java.io.Serializable;
/**
*
* @author Injavawetrust
*
*/
Public class Person implements serializable{
Private Static Final long serialversionuid = -399858645849256406l;
Private String name;
Private String sex;
Public String GetName () {
return name;
}
Public void setName (String name) {
this. Name = name;
}
Public String Getsex () {
return sex;
}
Public void setsex (String sex) {
this. Sex = sex;
}
}
Java code 650) this.width=650; "class=" star "src=" Http://injavawetrust.iteye.com/images/icon_star.png "alt=" Favorites Code "style = "border:0px;"/> get ""
import java.io.File;
import Java.io.FileInputStream;
import java.io.FileNotFoundException;
import Java.io.FileOutputStream;
import java.io.IOException;
import Java.io.InputStream;
import Java.io.ObjectInputStream;
import Java.io.ObjectOutputStream;
import Java.io.OutputStream;
/**
* Serialization and deserialization
* @author Injavawetrust
*
*/
Public class Testperson {
Public Static void Main (string[] args) {
Person person = New person ();
Person.setname ("Java");
Person.setsex ("F");
File File = new file ("D:\\person.txt");
Try {
File.createnewfile ();
} catch (IOException e) {
E.printstacktrace ();
}
Try {
//Serialization
OutputStream OS = new fileoutputstream (file);
ObjectOutputStream Oos = new objectoutputstream (OS);
Oos.writeobject (person);
Oos.flush ();
Oos.close ();
Os.close ();
//Deserialization
InputStream is = new fileinputstream (file);
ObjectInputStream ois = new ObjectInputStream (IS);
Person per = (person) ois.readobject ();
System.out.println ("name[" + per.getname () + "]");
System.out.println ("sex[" + per.getsex () + "]");
Ois.close ();
Is.close ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}
}
Serialization and deserialization of Java