Java I/O Stream operations (IV)-Object serialization

Source: Internet
Author: User
First, the serializable interface must be implemented for Object serialization. Only in this way can the object be persisted to the hard disk to obtain other places.  The object serialization operation requires two objects: objectoutputstream and objectinputstream. Objectoutputstream(Outputstream out)
Create an objectoutputstream to write the object to the outputstream output stream

Objectoutputstream is also a subclass of the byte output stream:
java.lang.Object  java.io.OutputStream      java.io.ObjectOutputStream
Objectinputstream is a subclass of the byte input stream:
java.lang.Object  java.io.InputStream      java.io.ObjectInputStream
Objectoutputstream is tested through a simple program through writeobject (Object OBJ) objectinputstream through readobject:

Publicstaticvoid objetcseri () throws exception
{

Objectoutputstream OOS = new objectoutputstream (New fileoutputstream ("person. object "); // write the person object OOS. writeobject (new person ("Johnny", 21, "China"); OOS. close ();

}

 

The following error occurs on the console:
Exception in thread "main" java.io.NotSerializableException: com.huaxia.day21.Personat java.io.ObjectOutputStream.writeObject0(Unknown Source)at java.io.ObjectOutputStream.writeObject(Unknown Source)at com.huaxia.day21.ObjectSerializable.objetcSeri(ObjectSerializable.java:15)at com.huaxia.day21.ObjectSerializable.main(ObjectSerializable.java:9) 
This indicates that the class "person" is not implemented.Serializable Interface
After the error is corrected, A person. obejct file is added to the hard disk.
The content contains some characters that we cannot understand:

 

So can we read the object content through objectinputstream?

public static void readObject() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.object"));
Person person = (Person)ois.readObject();
System.out.println(person);
} 
Console output result: Name: Johnny age: 21 country: China
Let's take a look at the serializable API

 

 

This means that if the class implementing the serialization interface does not display the DeclarationSerialversionuid variable. During serialization, the value of a default serialversionuid is calculated as the class-based variable value.
We strongly recommend that all classes implementing the serialization interface display the Declaration serialversionuid variable, because the default serialversionuid variable value is very sensitive to class modification, because its value is based on
The class member signature is generated instead of randomly generated by the system. If we serialize Class A, we can deserialize the information of Class, if we modify Class,
So we will encounter a java. Io. invalidclassexception exception when deserializing again, because its ID is a value when class A is compiled for the first time,
After you modify Class A, the value of the ID for re-compilation has changed. Therefore, to ensure the value consistency of serialversionuid variables in different compilers,
Therefore, we recommend that you define this variable as a private constant.
Simulate this situation as follows: Now I modify the person class as follows:

 

 

Deserialization once:
 public static void readObject() throws Exception {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.object"));Person person = (Person)ois.readObject();System.out.println(person);} 
Console print shop exception: exception in thread "Main" Java. Io. invalidclassexception: COM. Huaxia. day21.person; local class incompatible:
stream classdesc serialVersionUID = -379036032685453711, 
local class serialVersionUID = 1208026685571330753 
 
 
 
Conclusion: The serializable interface must be implemented for Object persistence and the serialversionuid constant must be declared.
 
Reprinted please indicate the source: http://blog.csdn.net/johnny901114/article/details/8710341
 
 

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.