Java Basics-Serialization

Source: Internet
Author: User

    • The " object serialization " of Java translates an object that implements the Serialiable interface (the markup interface, without any method) into a set of byte, so that when this object is used later, the byte data can be recovered. and reconstruct that object accordingly . This is true in cross-network environments, which means that the serialization mechanism can automatically compensate for differences in operating systems. That is, you can create an object on a Windows machine, serialize it, then upload it over the network to the UNIX machine, and then rebuild the object, without worrying about how the data is represented on different platforms, what the byte order is, or what the details are.
    • To serialize an object, you must first create a outputstream and then embed it in ObjectOutputStream, and you can write the object to OutputStream with the WriteObject () method. You need to embed the intputstream into the objectintputstream when you read it, and then call the ReadObject () method. However, this is just a reference of an object, so you have to upload it before you use it.

See: http://www.cnblogs.com/xudong-bupt/archive/2013/05/19/3086493.html

When two processes are communicating remotely, each other can send various types of data. Regardless of the type of data, it is transmitted over the network in the form of a binary sequence . The sender needs to convert the Java object to a sequence of bytes to be transmitted over the network, and the receiver needs to revert the byte sequence back to the Java object.

Only objects that support the Java.io.Serializable interface can be written to the stream. The classes for each serializable object are encoded, including the class name and class signature, the object's field value and the array value, and the closure of all other objects referenced from the original object.

1. Concept

  serialization : The process of converting a Java object to a sequence of bytes.
  deserialization : The process of reverting a sequence of bytes to a Java object.

2. Use

There are two main uses for serialization of objects:
1) The object's byte sequence is permanently saved to the hard disk, usually stored in a file;
2) A sequence of bytes that transmits an object over the network.

3. Serialization of objects

3.1 Serialization API

Java.io.ObjectOutputStream represents an object output stream, and its writeobject (object obj) method serializes the Obj object specified by the parameter and writes the resulting sequence of bytes to a target output stream. Only objects of classes that implement the serializable and Externalizable interfaces can be serialized.

Java.io.ObjectInputStream represents an object input stream, and its readobject () method reads a sequence of bytes from a source input stream, deserializes them into an object, and returns them.

3.2 Code Examples

1 ImportJava.io.*;2 Importjava.util.Date;3 4  Public classObjectsaver {5      Public Static voidMain (string[] args)throwsException {6         /*where the d:\\objectfile.obj represents the file that holds the serialized object*/7 8         9         //Serializing ObjectsTenObjectOutputStream out =NewObjectOutputStream (NewFileOutputStream ("D:\\objectfile.obj")); OneCustomer customer =NewCustomer ("Pock", 24);  AOut.writeobject ("Hello!");//write literal literals constant -Out.writeobject (NewDate ());//Write anonymous Date object -Out.writeobject (customer);//write to the Customer object the out.close (); -  -          -         //deserializing Objects +ObjectInputStream in =NewObjectInputStream (NewFileInputStream ("D:\\objectfile.obj")); -System.out.println ("obj1" + (String) in.readobject ());//read literal constants +System.out.println ("Obj2" + (Date) in.readobject ());//Read Anonymous Date object ACustomer OBJ3 = (customer) in.readobject ();//read the Customer object atSystem.out.println ("Obj3" +obj3); - in.close (); -     } - } -  - classCustomerImplementsSerializable { in     PrivateString name; -     Private intAge ; to      PublicCustomer (String name,intAge ) { +          This. Name =name; -          This. Age =Age ; the     } *  $      PublicString toString () {Panax Notoginseng         return"Name=" + name + ", age=" +Age ; -     } the}

3.3 Execution Results

4. Description

  The order in which objects are read is consistent with the order in which they are written .

  The default serialization mechanism for an object is written to the object's class, the class signature, and the values of non-transient and non-static fields .

Java Basics-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.