Java Core Learning (17) IO Framework---object serialization

Source: Internet
Author: User
Tags object serialization

First, the use of object flow to achieve serialization.

Define a serializable object first

    

 PackageCom.shy.IO;Importjava.io.Serializable; Public classPersonImplementsserializable{ Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }    Private intAge ; PrivateString name;  PublicPerson (String name,intAge ) {         This. Age =Age ;  This. Name =name; }}

The instance of the Persen class is then serialized using the object flow

PackageCom.shy.IO;ImportJava.io.FileOutputStream;ImportJava.io.IOException;import Java.io.objectoutputstream; Public class writeobject { Public static void main ( String[] args) {try (ObjectOutputStream outputstream = Span style= "COLOR: #0000ff" >new objectoutputstream (new fileoutputstream ("Object.txt" Span style= "color: #000000")); {Person person = new person ("YuYu", 22catch (IOException IoE) {ioe.printstacktrace ();}}}   

and finally deserialization.

PackageCom.shy.IO;ImportJava.io.FileInputStream;ImportJava.io.IOException;ImportJava.io.ObjectInputStream;PublicClassReadObject {public static void main (string[] args) {try ( ObjectInputStream InputStream = new objectinputstream ( New FileInputStream ("Object.txt"  ( person) Inputstream.readobject (); System.out.println (Person.getname () + "of the Age is" +person.getage ());} catch (IOException IoE) {ioe.printstacktrace ();} catch (classnotfoundexception e) {e.printstacktrace ();} finally {}}}        

As can be seen from the above code, deserialization must be downcasting, and the deserialization system area is only the data of the Java object, and the object constructor is not used, indicating that the deserialization mechanism does not need to be constructed to initialize the Java object.

Ii. Introduction to the contents of the Java Object serialization algorithm

1. All objects saved to disk have a serialized number.

2, when the program attempts to serialize an object, the program will first check whether the object has been serialized, only the object has never (in this virtual machine) is serialized, the system will convert the object into a sequence of bytes and output.

3. If an object is already serialized, the program will simply output a serialized number instead of re-serializing the object again.

Iii. Custom Serialization

For objects that implement the Serializable interface, you can use the following three ways to customize serialization and deserialization.

In the first way, if you use the transient keyword modifier in front of an instance member variable, you can specify that the instance variable is not ignored by Java serialization;

The second way, in a class that requires serialization, is to provide a method of the following special signature, in which the member variables of the Java object are accessed using the Out.writeobject () method, respectively

  

Private void writeobject (java.io.ObjectOutputStream out)throws  IOException;     Private void readobject (java.io.ObjectInputStream in)throws  ioexception,classnotfoundexception;     Private void Readobjectnodata ()throws objectstreamexception;

The third Way, when serialized, is to replace the object with another object, the following method should be provided for serialization, the serialization mechanism will serialize the object returned by the method.

    throws Objectstreamexception;

As described above, it is possible to know that the Java serialization mechanism calls the WriteObject method of the serialized object before it is written to the file, and then calls the Writereplace method, which corresponds to the last time the object is read from the file. Calls the ReadObject method and then calls the Readresolve method, which, like the Writereplace method, can convert the source serialized object to another object to return, which can be used to implement the whole object of the White tiger's happiness.

Iv. another serialization mechanism to implement the Externalizable interface

Unlike serializable, which is a declarative interface, the Externalizable interface defines two methods writeexternal () and Readexternal () two methods, These two methods are the same except that the names are different from writeobject and ReadObject, and the use is to force custom serialization.

V. Scope of serialization

The class name and instance variables of the object are serialized, methods, class variables, and transient variables are not serialized.

Liu, Serialversionuid

It is best to manually define the version of the serialized object when defining the serialized object, that is, to define a static member variable in the serialized object, private static fianl long serialversionuid = 512L; Used to ensure that the class file can be correctly deserialized after it is changed.

Java Core Learning (17) IO Framework---object serialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.