Note: I/O Stream-Object serialization and serialization

Source: Internet
Author: User

Note: I/O Stream-Object serialization and serialization

The Java language supports a very general mechanism called ObjectSerialization, which can write any object to the stream and read it back later, first, classes that support object serialization must inherit from the Serializable interface. This interface does not have any methods, but only serves to mark the class, and then uses the ObjectOutputStream stream to serialize the object, use the ObjectInputStream stream for deserialization. The sample code is as follows:

  • Object Class declaration:

    Public class Employee implements Serializable {

    Private String name;

    Private String sex;

    PublicEmployee (){

    }

    PublicEmployee (String name, String sex ){

    This. name = name;

    This. sex = sex;

    }

    // Getter and setter Methods

    }

    Public class Manager extends Employee {

    Private Employee secretary;

    PublicManager (){

    }

    PublicManager (String name, String sex ){

    Super (name, sex );

    }

    // Getter and setter Methods

    }

  • Create an object instance:

    Employee harry = newEmployee ("Harry Hacker", "male ");

    Manager boss = newManager ("Carl Cracker", "female ");

    Boss. setSecretary (harry );

  • Serialize to a file

    ObjectOutputStream outputStream = null;

    Try {

    OutputStream = newObjectOutputStream (newFileOutputStream ("serializableApp. dat "));

    OutputStream. writeObject (boss );

    } Catch (FileNotFoundException ex ){

    Ex. printStackTrace ();

    } Finally {

    If (outputStream! = Null ){

    OutputStream. close ();

    }

    }

  • Deserialization from files

ObjectInputStream inputStream = null;

Try {

InputStream = newObjectInputStream (newFileInputStream ("serializableApp. dat "));

Manager serializableBoss = (Manager) inputStream. readObject ();

System. out. println ("manager name is" + serializableBoss. getName () + "sex is"

+ SerializableBoss. getSex () + "secretary is"

+ SerializableBoss. getSecretary (). getName ());

} Catch (ClassNotFoundException ex ){

Ex. printStackTrace ();

} Catch (FileNotFoundException ex ){

Ex. printStackTrace ();

} Finally {

If (inputStream! = Null ){

InputStream. close ();

}

}

Each object is saved with a serial number. The object serialization mechanism is as follows:

  • Each object reference is associated with a serial number.
  • For each object, when it is first encountered, the data of the object stored in the stream
  • If an object has been saved, only the saved serial number is written.
  • When an object in a stream encounters its serial number for the first time, it is created and initialized using the data in the stream. Then, the sequence number and the association between the object are recorded.
  • When an object references the serial number of another object, obtain the object reference associated with this serial number.

Some data fields cannot be serialized, for example, the storage file handle or window handle integer that is meaningful only to the local method, java has a simple mechanism to prevent such domains from being serialized, that is, to mark them as transient. If they are marked as non-serializable classes, they also need to be marked as transient, the instantaneous domain is always skipped during Object serialization, for example:

Private transient Point2D. Double point;

  • Base class code:

    PublicvoidwriteExternal (ObjectOutput out) throws IOException {

    Out. writeUTF (this. name );

    Out. writeUTF (this. sex );

    }

    PublicvoidreadExternal (ObjectInput in) throws IOException, ClassNotFoundException {

    This. name = in. readUTF ();

    This. sex = in. readUTF ();

    }

  • Subclass code:

    @ Override

    PublicvoidreadExternal (ObjectInput in) throws IOException, ClassNotFoundException {

    Super. readExternal (in );

    This. secretary = newEmployee ();

    This. secretary. readExternal (in );

    }

    @ Override

    PublicvoidwriteExternal (ObjectOutput out) throws IOException {

    Super. writeExternal (out );

    This. secretary. writeExternal (out );

    }

  • If the names of data domains match but the types do not match, the object stream will not undergo type conversion, so it is not compatible.
  • If the object in the stream has data fields not in the current version, the object stream ignores the additional data.
  • If the current version has data domains that are not included in the streaming object, these newly added domains will be set to their default values (the object is null, the number is 0, and the boolean type is false)

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.