Serializable parsing in Java serialization

Source: Internet
Author: User

Serializable parsing in Java serialization
Introduction

We know that everything in java is an object. If we wantStorageObject orTransferWhat should I do when it comes to objects? The object is not a byte or character,Not directlyRead and Write Data Using an input/output stream. Serialization is used.

Concept

Wikipedia defines:

For synchronization control, a single access is forced at the same time.
The data storage and transmission part refers to the process of storing an object to a storage medium, such as a file or a buffer containing hundreds of millions of objects, or encoding when data is transmitted over the network, it can be in byte or XML format. The byte or XML encoding format can restore completely equal objects. This program is used to transfer objects between different applications, and the server stores objects to files or databases. The opposite process is also called deserialization.

This blog focuses on the definition of data storage and transmission. We know that everything in java is an object. What should we do if we want to store objects or transfer objects? The object is not a byte or character and cannot be read or written directly using an input/output stream.

Then, the operation on the object should put the objectConvertThe byte sequence, and then the input and output streams are used for operations. For example, it is stored on a hard disk or transmitted over the network to transfer objects.

After you extract the byte sequence from the hard disk or receive it in the network, convert it to an object, that isDeserialization.

Implementation

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

The writeObject () method in the ObjectOutputStream class is used to write serialized objects. Read the object using the readObject () method of ObjectInputStream.

Import java. io. *; public class Main {public static void main (String [] args) throws Exception {Person person Person = new person (); Person. setName ("James"); person. setAge (19); person. setSex ("male"); ObjectOutputStream oo = new ObjectOutputStream (new FileOutputStream (new File ("/Users/lixingyu/object.txt"); oo. writeObject (person); oo. close (); System. out. print ("write OK! \ N "); ObjectInputStream oi = new ObjectInputStream (new FileInputStream (new File ("/Users/lixingyu/object.txt "); Person person1 = (Person) oi. readObject (); System. out. print (person1.getName () + "\ n"); System. out. print ("read OK! \ N ");} static class Person implements Serializable {private static final long serialVersionUID =-5809782578272943999L; public String name; public int age; public String sex; public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex ;}}}
S? E? R? I? A? L? V? E? R? S? I? O? N? U? I? D

Note that the above Code has a s? E? R? I? A? L? V? E? R? S? I? O? N? U? I? D, and there is only one initialization. The following code does not use this variable, so what does it do?
If we modify the preceding internal class, delete the variable, first store the object to the hard disk, and then add the variable "friend" to the Person class, what will be the read result? (Run twice, save only once, and read only after adding the variable.

Import java. io. *; public class Main {public static void main (String [] args) throws Exception {Person person Person = new person (); Person. setName ("James"); person. setAge (19); person. setSex ("male"); // ObjectOutputStream oo = new ObjectOutputStream (new FileOutputStream (new File ("/Users/lixingyu/object.txt"); // oo. writeObject (person); // oo. close (); System. out. print ("write OK! \ N "); ObjectInputStream oi = new ObjectInputStream (new FileInputStream (new File ("/Users/lixingyu/object.txt "); Person person1 = (Person) oi. readObject (); System. out. print (person1.getName () + "\ n"); System. out. print ("read OK! \ N ");} static class Person implements Serializable {// private static final long serialVersionUID =-5809782578272943999L; public String name; public int age; public String sex; public String friends; public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex ;}}}

You will see the following error message:

Exception in thread "main" java.io.InvalidClassException: Main$Person; local class incompatible: stream classdesc serialVersionUID = -3488593190474644338, local class serialVersionUID = 1566115588131499685    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:616)    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623)    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)    at Main.main(Main.java:15)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:497)    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

In fact, if you do not add serializableUID to the class, the compiler will automatically add and produce an ID. When we modify the source code file of the interface class that implements serialization, even if only one space is added, the compiler will also generate a different ID, so that when reading the serialized object previously stored on the hard disk, the compiler will report an error and reject loading.

Therefore, serializableUID is used to differentiate serialized objects and must be added. Otherwise, errors are easy.

Purpose

For example, the above method is used to access objects from hard disks. When Intent is used in android development to transfer objects between activities, there is a way that this object must be serialized; objects are also transmitted through the network.

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.