The serialization and deserialization of Java basics

Source: Internet
Author: User

Serialization and deserialization:

The process of converting an object into a sequence of bytes is called serialization;

The process of reverting a sequence of bytes to an object is called deserialization of the object;

Method:

The Java.io.ObjectOutputStream represents the output stream of the object, and the WriteObject (object obj) method serializes the Parameter object and writes the resulting byte sequence to a target output stream.

Java.io.ObjectInputStream represents an input stream for an object, and the ReadObject () method can read a sequence of bytes from a source input stream and convert it to an object, returning it.

Only objects of classes that implement the serializable and Externalizable interfaces can be serialized.

The Externalizable interface inherits from the serializable interface, and the class that implements the Externalizable interface controls the serialization behavior entirely by itself, whereas classes that implement the serializable interface can take the default serialization method.
Object serialization consists of the following steps:
1) Create an object output stream, which can wrap a different type of target output stream, such as a file output stream;
2) writes an object through the WriteObject () method of the object output stream.
The steps for deserializing an object are as follows:
1) Create an object input stream, which can wrap a different type of source input stream, such as a file input stream;
2) The object is read through the ReadObject () method of the object input stream.

Instance:

1 ImportJava.io.File;2 ImportJava.io.FileInputStream;3 Importjava.io.FileNotFoundException;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.ObjectInputStream;7 ImportJava.io.ObjectOutputStream;8 Importjava.io.Serializable;9 ImportJava.text.MessageFormat;Ten  One  A classPersonImplementsserializable{ -     Private intAge ; -      Public intGetage () { the         returnAge ; -     } -      Public voidSetage (intAge ) { -          This. Age =Age ; +     } -      PublicString GetName () { +         returnname; A     } at      Public voidsetName (String name) { -          This. Name =name; -     } -     PrivateString name; - } -  Public classTest { in      Public Static voidMain (string[] args)throwsFileNotFoundException, IOException, ClassNotFoundException -     { to Serialiableperson (); +Person p=Deserializeperson (); -System.out.println (Messageformat.format ("Name={0},age={1}", P.getname (), P.getage ()));  the     }
The process of deserialization, which is read out from a byte sequence file and returned
* Public StaticPerson Deserializeperson ()throwsFileNotFoundException, IOException, classnotfoundexception{ $ObjectInputStream in=NewObjectInputStream (NewFileInputStream (NewFile ("E://person.txt")));Panax NotoginsengPerson p=(person) in.readobject (); - returnp; the }
serialization process, saving bytes to E://person.txt
+ Public Static voidSerialiableperson ()throwsFileNotFoundException, IOException { APerson p=NewPerson (); theP.setage (15); +P.setname ("Last Name"); - $ObjectOutputStream out=NewObjectOutputStream (NewFileOutputStream (NewFile ("E://person.txt"))); $ Out.writeobject (p); - out.close (); - } the -}

The serialization and deserialization of Java basics

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.