Java serialization (2)

Source: Internet
Author: User

 
In the previous article, a simple Name object is serialized and deserialized. Next we will look at a slightly complicated situation where the Name class is composite with other classes.
Name. java:

Import java. io. serializable;/*** Serializable class, which must implement the Serializable interface * @ author Shuang **/public class Name implements Serializable {private static final long serialVersionUID = 1L; private String firstname; private String lastname; private Nickname nickname; public Name () {} public Name (String firstname, String lastname) {this. firstname = firstname; this. lastname = lastname;} public Name (String firstname, String lastname, Nickname nickname) {this. firstname = firstname; this. lastname = lastname; this. nickname = nickname;} public String getFirstname () {return firstname;} public void setFirstname (String firstname) {this. firstname = firstname;} public String getLastname () {return lastname;} public void setLastname (String lastname) {this. lastname = lastname;} public Nickname getNickname () {return nickname;} public void setNickname (Nickname nickname) {this. nickname = nickname ;}@ Overridepublic String toString () {return my name is + firstname +, + lastname + my nickname is + nickname ;}}
Nickname. java:
Import java. io. serializable;/*** Nickname class ** @ author awesome **/public class Nickname implements Serializable {private static final long serialVersionUID = 1L; private String name; public Nickname () {} public Nickname (String name) {this. name = name;} public String getName () {return name;} public void setName (String name) {this. name = name ;}@ Overridepublic String toString () {return name ;}}
WriteObject. java:
Import java. io. IOException; import com. runqianapp. test. bean. name; import com. runqianapp. test. bean. nickname; public class WriteObject {public static void main (String [] args) throws IOException {Nickname nickname = new Nickname (Black Mamba); Name name = new Name (Kobe, Bryant, nickname); Serializations. serialize (args [0], name );}}
After running, the corresponding file is generated in the specified directory, and ReadObject. java is run again, the following output information is obtained:
My name is Kobe and Bryant. My nickname is Mamaba.
During Object serialization, not only will the current object itself be serialized, but other objects referenced by this object will also be serialized. Similarly, other objects referenced by these other objects will also be serialized, and so on. During the serialization process, objects that do not support serializable interfaces may be encountered. In this case, NotSerializableException will be thrown and classes that identify non-serializable objects. If you remove the Serializable interface from Nickname. java and run WriteObject. java again, the following exception is thrown:
Exception in thread main java.io.NotSerializableException: Nicknameat java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)at Serializations.serialize(Serializations.java:26)at WriteObject.main(WriteObject.java:13)

We can useTransientTo modify the nickname attribute, so that the class can be serialized normally, but the attribute in the nickname cannot be serialized, so how can we enable the name attribute in the class NickName that cannot be serialized to be serialized or deserialized? Classes that require special processing during serialization and deserialization must use the following accurate signatures to implement special methods:

private void writeObject(java.io.ObjectOutputStream out)    throws IOException;private void readObject(java.io.ObjectInputStream in)    throws IOException, ClassNotFoundException;
The writeObject method is called during Object serialization. The readObject method is called during object deserialization. The writeObject method is used to write the state of the object of a specific class so that the corresponding readObject method can restore it. You can call out. defaultWriteObject to call the default mechanism for saving the fields of the Object. The readObject method reads and restores class fields from the stream. It can call in. defaultReadObject to call the default mechanism to restore non-static and non-transient fields of the object. Therefore, we can add the following method to the Name class:
Private void writeObject (java. io. objectOutputStream out) throws IOException {// default serialization mechanism out. defaultWriteObject (); // serialize the name attribute out in nickname. writeObject (nickname. getName ();} private void readObject (java. io. objectInputStream in) throws IOException, ClassNotFoundException {// default deserialization mechanism in. defaultReadObject (); // deserialize a nickname object nickname = new Nickname (in. readObject (). toString ());}
In this way, the name attribute serialization and deserialization in the composite Nickname can be processed. WriteObject and ReadObject are run. serialization and deserialization are successful. The implementation of these two methods depends on the final requirement. The above example is an example that is more suitable for the application scenario.
This article is from: Gao Shuang | Coder. The original Article address is http://blog.csdn.net/ghsau/article/details/192.1619. For more information, see.

 

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.