Java io Detailed (vi)------serialization and deserialization (object flow)

Source: Internet
Author: User
Tags serialization server memory

Description of the File class: Http://www.cnblogs.com/ysocean/p/6851878.html

Java IO streams are categorized: http://www.cnblogs.com/ysocean/p/6854098.html

Java IO byte input/output stream: http://www.cnblogs.com/ysocean/p/6854541.html

Java IO character input/output stream: https://i.cnblogs.com/EditPosts.aspx?postid=6859242

Java IO Wrapper Flow: http://www.cnblogs.com/ysocean/p/6864080.html

1. What is serialization and deserialization?

  Serialization: Refers to the Java object data in the heap memory, in some way the object is stored in a disk file or passed to other network nodes (transmission over the network). This process is called serialization. In popular terms, the process of converting a data structure or object into a binary string

Deserialization: The process of restoring object data from a disk file or object data on a network node to a Java object model. The process of converting a binary string generated during serialization into a data structure or object

2, why do you want to do serialization?

①, in the distributed system, when the object needs to be transferred on the network, it is necessary to convert the object data into binary form, and the JavaBean object that needs to share the data must be serialized.

②, Server passivation: If the server discovers that some objects are inactive for a long time, the server persists the objects in the local disk file (the Java object is converted to a binary file), and if the server discovers that some objects need to be active, look in memory first Unable to find and then de-serialize our object data in the disk file and revert to the Java object. This will save server memory.

3, how to serialize Java?

①, the class that needs to serialize the object, must implement the serialization interface: Java.lang.Serializable interface (this is a flag interface, there is no abstract method), most classes in Java implement the interface, such as: String,integer

②, the bottom will judge, if the current object is Serializable instance, only allow serialization, Java object instanceof Serializable to judge.

③, using object flow in Java to complete serialization and deserialization

    ObjectOutputStream: Using the WriteObject () method to perform serialization operations

    ObjectInputStream: Using the ReadObject () method to deserialize the operation

    

First step: Create a JavaBean object

public class Person implements serializable{private string Name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} @Overridepublic String toString () {return "person [name=" + name + ", age=" + Age + "]";} Public person (String name, int.) {super (); this.name = Name;this.age = Age;}}

Step two: Using ObjectOutputStream objects for serialization

Create a new Io folder in the root directory outputstream op = new FileOutputStream ("io" +file.separator+ "a.txt"); ObjectOutputStream Ops = new ObjectOutputStream (OP); Ops.writeobject (New Person ("Vae", 1)); Ops.close ();

We open the A.txt file, found inside the content garbled, note that this does not need us to understand, this is a binary file, the computer can read the line.

Error One: If the new Person object does not implement the Serializable interface, the above operation will error:

    

Step three: Use the ObjectInputStream object for deserialization

The deserialized object must provide a bytecode file for the object. Class

InputStream in = new FileInputStream ("io" +file.separator+ "A.txt"), ObjectInputStream OS = new ObjectInputStream (in); byte[] buffer = new Byte[10];int len =-1; Person P = (person) os.readobject (); SYSTEM.OUT.PRINTLN (p);  person [Name=vae, Age=1]os.close ();

  

Question 1: If some data does not need to be serialized, such as the password, such as the above age?

Workaround: Add transient in front of the field

Private String name;//requires serialization transient private int age;//does not need serialization

So when we are deserializing, we print out the person [name=vae, age=0], the integer data default value is 0

Issue 2: Serialization version problem, after the serialization operation, due to the upgrade or modification of the project, we may modify the serialized object, such as adding a field, then we will be in the deserialization of error:

Workaround: Add a serialversionuid field to the JavaBean object to fix this version, no matter how we modify it, the version is consistent and can be deserialized

Private static final long serialversionuid = 8656128222714547171L;

  

Java io Detailed (vi)------serialization and deserialization (object flow)

Related Article

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.