java serialization (rpm)

Object simply serialization mechanism for an object is a kind of a process stream, i.e. a so-called object stream content object is fluidized Needless to say this concept of flow (that is, I / O), we can fluidized after read and write operations, the object can also be transmitted between the fluidized network (NOTE: to have the network object transport fluidized)! When an object stream read and write operations can cause some problems, but it is serialization mechanism to solve these problems!
Leads to the question:
As mentioned above, the object to read and write what the problem? For example: I want to write to a disk file objects and then read it out and then there will be a problem? Do not worry, one of the biggest problem is the object reference! For example: if I have two classes, namely, A and B, Type B contains a pointer to the class object reference A, and now we instantiate {A a = new A () on two classes; B b = new B ();} , this time in two memory actually allocated space, a storage object a, a storage object B, then we want to write them to a file on disk, in There was a problem writing to the file! Because the object b contains the object a reference, the system will automatically copy a data a into b, so when we recover objects from the file (that is reloaded into memory), memory is allocated three spaces, while at the same time there is a duplicate objects in memory, think about the consequences of it, if I want to modify a target of data, then it's not even search every copy of the object to achieve the consistency of the data, this is not what we want!
The following mechanisms of sequence Solution:
1. Save to disk all objects are given a sequence number (1, 2, 3, etc.)
2. When you want to save an object, first check whether the object is saved.
3. If you have previously saved, just write "with the object that has the serial number has been stored in the same x" marks, otherwise save the object
sequence of steps of the above mechanisms solve the problem of object references!
Achieve serialization
Will need to be serialized class implements Serializable interface, method interface does not need to achieve, the implements Serializable only to label the object is serialized, then an output stream (eg: FileOutputStream) to construct a the ObjectOutputStream (object stream ) object, then the object using an ObjectOutputStream writeObject (object obj) method can be used to write the object obj parameters (i.e., to save its state), to be restored if the input stream.
In the serialization process, some data fields that we do not want to serialize it, we just need to add to it in the transient keyword can be defined for these fields, serialization mechanism for transient field will not be skipped write to the file, of course, it can not be restored. But sometimes we want to serialize a field, but it is defined in the SDK is serialized type is not available, so we have to put him labeled as transient, but can not write and how to restore it? Fortunately serialization mechanism to provide this particular type comprising a definition of the problem is as follows:
Private void the readObject (the ObjectInputStream in) throws
IOException, a ClassNotFoundException;
Private void the writeObject (the ObjectOutputStream OUT) throws
IOException;
(NOTE: These methods must be defined is private, because you do not need to show) called serialization mechanism automatically invoked
using the above method we can manually those you want can not be serialized serialized data fields write and read operations.
The following is a typical example, in java.awt.geom package Point2D.Double class is not serializable because the class does not implement Serializable interface, in my case treats it as a class data field LabeledPoint and demonstrates how to serialize it!
the java.io. * Import;
Import in java.awt.geom *;.
public class TransientTest
{
         public static void main (String [] args)
         {
                 LabeledPoint new new label = LabeledPoint ( "Book", 5.00, 5.00);
                 the try
                 {
                         the System. out.println (label); // write before
                         the ObjectOutputStream the ObjectOutputStream new new OUT = (new new
                         a FileOutputStream ( "Label.txt"));
                         out.writeObject (label);
                         the out.close ();
                         System.out.println(label);// 写入后
                         ObjectInputStream in = new ObjectInputStream(new
                         FileInputStream("Label.txt"));
                         LabeledPoint label1 = (LabeledPoint) in.readObject();
                         in.close();
                         System.out.println(label1);// 读出并加1.0后
                 }
                 catch (Exception e)
                 {
                         e.printStackTrace();
                 }
         }
}
class LabeledPoint implements Serializable
{
         public LabeledPoint(String str, double x, double y)
         {
                 label = str;
                 point = new Point2D.Double(x, y);
         }
         private void writeObject(ObjectOutputStream out) throws IOException
         {
                
                 out.defaultWriteObject();
                 out.writeDouble(point.getX());
                 out.writeDouble(point.getY());
         }
         private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
         {
                
                 in.defaultReadObject();
                 double x = in.readDouble() + 1.0;
                 double y = in.readDouble() + 1.0;
                 point = new Point2D.Double(x, y);
         }
         public String toString()
         {
                 return getClass().getName()+ "[label = " + label+ ", point.getX() = " + point.getX()+ ", point.getY() = " + point.getY()+ "]";
         }
         private String label;
         transient private Point2D.Double point;
}

Original: http: //blog.163.com/benbenfafa_88/blog/static/64930162201152373158142/

Guess you like

Origin www.cnblogs.com/lychee-wang/p/12441249.html