When two processes perform remote communication, they can send different types of data to each other. Regardless of the type of data, it is transmitted on the network in the form of binary sequence. The sender needs to convert the Java object into a byte sequence before it can be transmitted over the network. The receiver needs to restore the byte sequence to a Java object.
The process of converting a Java object to a byte sequence is called object serialization.
The process of restoring a byte sequence to a Java object is called object deserialization.
Object serialization has two main purposes:
1) the object's byte sequence is permanently stored on the hard disk, usually stored in a file;
2) transmits the object's byte sequence over the network.
Java. Io. objectoutputstream represents the object output stream. Its writeobject (Object OBJ) method can serialize the OBJ object specified by the parameter and write the obtained byte sequence to a target output stream.
Java. Io. objectinputstream indicates the object input stream. Its readobject () method reads byte sequences from a source input stream, deserializes them into an object, and returns it. ,
Only objects of classes that implement the serializable and externalizable interfaces can be serialized. The externalizable interface inherits from the serializable interface. classes that implement the externalizable interface completely control the serialization behavior. classes that only implement the serializable interface can adopt the default serialization method.
Object serialization includes the following steps:
1) create an object output stream, which can wrap a target output stream of another type, such as a file output stream;
2) write an object using the writeobject () method of the object output stream.
The steps for object deserialization are as follows:
1) create an object input stream, which can wrap a source input stream of another type, such as a file input stream;
2) read the object through the readobject () method of the object input stream.
For example:
Import java. Io .*;
Public class objectdemo
{
Public static void main (string [] ARGs)
{
Try
{
Objectoutputstream objectout = new objectoutputstream (New bufferedoutputstream (New fileoutputstream ("object. bin ")));
Customer Cus = new customer ("Wang", "0001", "pddd", 3000 );
Objectout. writeobject (CUs );
Objectout. Close ();
Objectinputstream objectin = new objectinputstream (New bufferedinputstream (New fileinputstream ("object. bin ")));
Cus = (customer) objectin. readobject ();
System. Out. println ("name:" + Cus. getname ());
System. Out. println ("ID:" + Cus. GETID ());
System. Out. println ("Password:" + Cus. GetPassword ());
System. Out. println ("Salary:" + Cus. getsalary ());
}
Catch (notserializableexceptionE)
{
System. Out. println (E. getmessage ());
}
Catch (classnotfoundexception E)
{
System. Out. println (E. getmessage ());
}
Catch (ioexception E)
{
System. Out. println (E. getmessage ());
}
}
}
Class Customer implements serializable
{
Private string name, ID;
Transient private string password;
Private float salary;
Public customer (string name, string ID, string password, float salary)
{
This. Name = Name;
This. ID = ID;
This. Password = password;
This. Salary = salary;
}
Public String getname ()
{
Return name;
}
Public String GETID ()
{
Return ID;
}
Public String GetPassword ()
{
Return password;
}
Public float getsalary ()
{
Return salary;
}
};