Serialization and deserialization instances

Source: Internet
Author: User

Public class Foo implements serializable {
Private int width;
Private int height;
Public int getwidth (){
Return width;
}
Public void setwidth (INT width ){
This. width = width;
}
Public int getheight (){
Return height;
}
Public void setheight (INT height ){
This. Height = height;
}

}


Public class test_serialize extends testcase {

// Serialization: fileoutputstream stores the path file. If it is a relative path, it is stored in the root directory of the project. If it is an absolute path, it is stored in the absolute path.
Public void tests () throws ioexception {
Foo A = new Foo ();
A. setheight (200 );
A. setwidth (300 );
Foo B = new Foo ();
B. setheight (300 );
B. setwidth (400 );
Try {
Fileoutputstream FS = new fileoutputstream ("D:/Foo. Ser ");
Objectoutputstream OS = new objectoutputstream (FS );
OS. writeobject ();
OS. writeobject (B );
OS. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}

// Deserialization
Public void testgets () throws ioexception, classnotfoundexception {
Try {
Fileinputstream FS = new fileinputstream ("D:/Foo. Ser ");
Objectinputstream OIS = new objectinputstream (FS );
Foo A = (FOO) Ois. readobject ();
Foo B = (FOO) Ois. readobject ();
System. Out. println (A. getheight ());
System. Out. println (B. getheight ());

} Catch (filenotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}



Precautions
A) During serialization, only the object state is saved, regardless of the object method;
B) when a parent class is serialized, the Child class is automatically serialized without explicitly implementing the serializable interface;
C) when the instance variable of an object references other objects, the referenced object is also serialized when the object is serialized;
D) Not all objects can be serialized. There are many reasons for this. For example:
1. security reasons: for example, an object has fields such as private and public. For an object to be transmitted, such as writing to a file, or performing RMI transmission, during serialization and transmission, the private and other fields of this object are not protected.
2. the reason for resource allocation, such as socket and thread classes, cannot be re-allocated if they can be serialized, transmitted, or saved. Moreover, there is no need to implement this.


The default serialization has requirements on the class structure. Generally, the server and the customer version are different when the version is upgraded in the future. Alternatively, you can use the new version to read a serialized object written to the disk from the old version, these two versions are required to be compatible. To meet this requirement, at least the following fields are included, and the values of the classes of the new and old versions should be the same, you can also use other rules in special cases. For example, your structure has changed. In the past, name/phone was a field of A. Later, an internal class named "Contact" was created in the new version, which called "name" and "phone ".
If the two fields are moved to the internal class contact, Java can also provide the custom readobject/writeobject process in the new version to ensure compatibility with the old version.
Private Static final long serialversionuid; // The values must be the same in multiple versions of a class. For classes without the same class, their values must not be unique.
Http://java.sun.com/developer/technicalArticles/Programming/serialization/

SerialversionuidPurpose: To maintain version compatibility during serialization, that is, during version upgrade, deserialization still maintains the uniqueness of objects. There are two generation methods: one is the default 1l, for example: Private Static final long se...

Serialversionuid:
To maintain version compatibility during serialization, that is, during version upgrade, deserialization still maintains the uniqueness of objects.
There are two generation methods:
One is the default 1l, for example, Private Static final long serialversionuid = 1l;
One is to generate a 64-bit hash field based on the class name, Interface Name, member method, and attribute. For example:
Private Static final long serialversionuid = xxxxl;
When a class implements the serializable interface, if serialversionuid is not defined, eclipse will provide this
The prompt function tells you to define it. Click the "warning" icon in the class in eclipse, and eclipse will
Two automatic generation methods are provided. If you do not want to define it, it is also set in eclipse.
You can disable it as follows:
Window ==> preferences ==> Java ==> compiler ==> error/warnings ==>
Potential Programming Problems
Change the value of serializable class without serialversionuid to ignore.
If you do not consider compatibility issues, turn it off, but this function is good. If serializable is implemented in any category, if serialversionuid is not added, eclipse will give you a warning that this serialversionuid is backward compatible for this type of serializable.
If your class serialized is stored on the hard disk, but then you change the field of the category (increase, decrease, or rename), when you deserialize, an exception will occur, this will cause incompatibility issues.
However, when the serialversionuid is the same, it will deserialize different fields with the default value of type to avoid incompatibility issues.


serialization and deserialization

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.