Java object serialization (finishing)

zhaozj2021-02-16  61

I have read a lot of sequential articles on the Internet. I also wrote two articles. Now I feel that these articles are very good to clear the serialization (including myself), so I will summarize the former and Your own experience, using a more light and easy-to-understand language to describe the mechanism, of course, there will still be a bad place, I hope that you can point out later that as a programmer should have a spiritual and strong idea of ​​knowledge!

Serialization overview:

Simply serialization is a mechanism for handling object flow. The so-called target stream is to fluidize the contents of the object, the concept of stream is not much more (that is, I / O), we can have a fluidized object Ready-write operations can also be transmitted between the streaming objects (Note: To transfers the object to the network must be fluidized)! Some problems will cause some problems when reading and writing for object flow, and serialization mechanism is used to solve these problems!

The extraction of the problem:

As mentioned above, what is the problem with reading and writing? For example: I want to write objects to a disk file and what is wrong after reading it? Don't worry, one of the biggest issues is object reference! For example, if I have two classes, it is a and b, and the class B contains a reference to the A class object. Now we instantiate {a a = new a (); b, now B = new b ();}, at this time, two spaces actually allocated, a storage object A, a storage object B, then we want to write them into a file of the disk, just There is a problem when writing to the file! Because object B contains reference to object A, the system automatically copies a data into B, which is allocated when we recover objects from the file (which is reloaded into memory), memory allocation Three spaces, while the object A is in two in memory. If I want to think about it, if I want to modify the data of the object A, it is not to search for each copy to achieve the consistency of object data. This is not what we hope!

Solutions for the following serialization mechanisms:

1. All objects saved to the disk get a serial number (1, 2, 3, etc.)

2. When you want to save an object, check if the object is saved.

3. If you have previously saved, you only need to write a mark that is the same as the object that has been saved. Otherwise, save the object.

The problem of the object reference is solved by the above step serialization mechanism!

Serialization implementation:

The serialization interface will be required to be serializable interface, which does not need to be implemented. IMPLEMENTS Serializable is just to be serialized, then use an output stream (such as a fileoutputstream) to construct an ObjectOutputStream. Object, then, use the WriteObject (Object Obj) method of the ObjectOutputStream object to write the object of the parameter OBJ (ie, to save its status), if you want to recover, you can use the input stream.

example:

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", 80000); Manager1.setseTary (Harry); Employee [] staff = new Employee [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 (); / ** * Take the Harry object to salary * will reflect * / news on Secretary * / news (10); for (int i = 0; i

Class Manager Extends Employee {Public Manager (String N, Double S) {Super (n, s); Secretary = NULL;} / ** * Settings Secretary * / Public Void SetSecretary (Employee S) {Secretary = S;} Public String Tostring () {Return Super.Tostring () "[Secretary =" Secretary "]";} // Secretary Represents Secretary Private Employe Secretary;} Modify the default serialization mechanism:

In the serialization process, some data fields we don't want to serialize them. For such fields, we only need to give it to the transient keyword when defined, and the serialization mechanism for the Transient field will be skipped. Write files, of course, can not be recovered. But sometimes we want to serialize a field, but it is a type in which the definition in the SDK is indiscriminate. In this way, we must also mark him as transient, but can't write, how to recover? It provides the following ways to define the following methods for the sequence mechanism to include this special problem:

Private Void ReadObject (ObjectInputStream in) THROWS

IOEXCEPTION, CLASSNOTFOUNDEXCEPTION;

Private Void WriteObject (ObjectOutputStream Out) Throws

IOEXCEPTION;

(Note: These methods must be private, because you don't need to display calls, serialization mechanisms will be called automatically)

User Method We can manually write and read operations for those data fields you want to serialize and can be serialized.

Below is a typical example, the Point2D.Double class in the java.awt.geom package is inquirable because the class does not implement the serializable interface, which will treat it as a data field in the LabledPoint class in my example. And demonstrate how to serialize it!

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); // before writing ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ( "Label.txt")); out.writeObject (label); out.close (); System.out.println (label); // write the ObjectInputStream in = new ObjectInputStream (new FileInputStream ( " Label.txt "); labeledpoint label1 = (labeledpoint) in.readObject (); in .close (); system.out.println (label1); // Read and add 1.0 after Catch (Exception E) {E .printstacktrace ();}}}

转载请注明原文地址:https://www.9cbs.com/read-14459.html

New Post(0)