Java object serialization (Finishing Part 1)
Read many of the online series of articles, I also wrote two now feeling these articles are not well clear of the sequences (including myself), so I will sum up their predecessors as well as their own experience with more simple understandable language to describe the mechanism, of course, there would still be a bad place, I hope you can read that, as a programmer should have a continuous exploration of knowledge and strong desire!
Summary of sequence:
In simple terms is a sequence of the target flow to deal with the mechanism, the so-called object-object is the content will flow, the concept of flow here Needless to say (that I / O), we can be the object of convection reading and writing can also be fluid in the transmission network between objects (Note: To be targeted transmission network must be carried out in the flow of)! In the flow of reading and writing to object will trigger some problems, and serialization mechanism is used to solve these problems!
Problem leads to:
As noted above, the reader will target what? For example: I object to a disk file and then write then read out what is the problem? Do not, one of the biggest problems is the object reference! For example: If I have two categories, namely, A and B, B category at the Class A contains a reference to targets, we are now conducted on the two types of examples = (A a new A (); B b = new B ();), in memory when in fact the two distribution space, a storage object a, b a storage object, then we would like to write them to disk in a file, it is in writing all documents there! Because object b contain a reference to the object, the system automatically will be a copy of a data to b, so when we recover from the document object (that is, re-loading into memory), memory allocation three of space and a target at the same time in memory in two, think about the consequences it, if I want to change a data object, it would also like to search it is not a copy of each data object to achieve consistency This is not what we want!
The following sequence mechanism solutions:
1. Saved to disk all objects are given a serial number (1, 2, 3, etc.)
2. When an object to preserve, to check whether the object was preserved.
3. Previously saved if only write "and the series has been preserved, with the object of the same x" marks, otherwise, the depositary of the object
Through the above steps sequence of mechanisms to address the question of the object reference!
The realization of sequence:
Will be required by the sequence of the class implements Serializable interface without the need to achieve the method implements Serializable only to mark the targets could be the sequence, and then use the output stream (such as: FileOutputStream) to construct an ObjectOutputStream (Object Data Stream) object, and then use ObjectOutputStream object writeObject (Object obj) method can be parameters for the object obj write (that is, to preserve the state) to restore it used input streams.
Examples:
Import java.io. *;
Public class Test
(
Public static void main (String [] args)
(
Employee harry = new Employee ( "Harry Hacker," 50000);
Manager manager1 = new Manager ( "Tony Tester," 80,000);
Manager1.setSecretary (harry);
Employee [] = new Employee staff [2];
Staff [0] = harry;
Staff [1] = manager1;
Try
(
ObjectOutputStream out new ObjectOutputStream = (
New FileOutputStream ( "employee.dat"));
Out.writeObject (staff);
Out.close ();
ObjectInputStream in new ObjectInputStream = (
New FileInputStream ( "employee.dat"));
Employee newStaff [] = ([] Employee) in.readObject ();
In.close ();
/ **
* Object to pay through harry
* Will be reflected in the secretary
* /
NewStaff [0]. RaiseSalary (10);
For (int i = 0; i <newStaff.length; i + +)
System.out.println (newStaff [i]);
)
Catch (Exception e)
(
E.printStackTrace ();
)
)
)
Class Employee implements Serializable
(
Public Employee (String n, double s)
(
Name = n;
Salary = s;
)
/ **
* Salary increase
* /
Public void raiseSalary (double byPercent)
(
Double raise = byPercent salary * / 100;
Salary + = raise;
)
Public String toString ()
(
Return getClass (). GetName ()
+ "[Name =" + name
+ ", Salary =" + salary
+ "]";
)
Private String name;
Private double salary;
)
Class Manager extends Employee
(
Public Manager (String n, double s)
(
Super (n, s);
Secretary = null;
)
/ **
* Set up CLERK
* /
Public void setSecretary (Employee s)
(
= S secretary;
)
Public String toString ()
(
Return super.toString ()
+ "[Secretary =" + secretary
+ "]";
)
/ / Secretary on behalf of the Secretary
Private Employee secretary;
)
Modify the default mechanism in the sequence:
In the process of sequence, and some data fields we do not want their sequence, for these fields we need to define it in the transient keywords, transient field serialization mechanism will not skip will be incorporated into the document, of course, can not be restored. But sometimes we want to be a sequence of the field, but it is the definition contained in the SDK is not the type of sequence, so we must also his mark as transient, but he would not resume writing and how? Fortunately serialization mechanism for this special issue contains the class provides the following definition of the method:
Private void readObject (ObjectInputStream in) throws
IOException, ClassNotFoundException;
Private void writeObject (ObjectOutputStream out) throws
IOException;
(Note: The definition of these methods must be private, because you do not show calls, serialization mechanism automatically call)
We can use the above methods manual for those of you not want to sequence can be the sequence of data fields to read and write operations.
Below is a typical example of the Point2D.Double java.awt.geom package type is not of the sequence, because these did not materialize Serializable interface, in the example I would class it as a LabeledPoint data field, and demonstration of how to sequence!
Import java.io. *;
Import java.awt.geom .*;
Public class TransientTest
(
Public static void main (String [] args)
(
LabeledPoint label = new LabeledPoint ( "Book", 5.00, 5.00);
Try
(
System.out.println (label); / / write before
ObjectOutputStream out = new ObjectOutputStream (new
FileOutputStream ( "Label.txt"));
Out.writeObject (label);
Out.close ();
System.out.println (label); / / write after
ObjectInputStream in new ObjectInputStream = (new
FileInputStream ( "Label.txt"));
LabeledPoint label1 = (LabeledPoint) in.readObject ();
In.close ();
System.out.println (label1); / / read out after the plus 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
(
/ **
* By calling defaultWriteObject () method to write
* Object can be described as well as those of the field sequence
* /
Out.defaultWriteObject ();
Out.writeDouble (point.getX ());
Out.writeDouble (point.getY ());
)
Private void readObject (ObjectInputStream in)
Throws IOException, ClassNotFoundException
(
/ **
* Call defaultReadObject () method
* /
In.defaultReadObject ();
In.readDouble double x = () + 1.0;
In.readDouble double y = () + 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;
)
Tags: object








0 Comments to “Java object serialization (Finishing Part 1)”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.