Java->io Stream _ serialized stream and deserialization stream

Source: Internet
Author: User
Tags object serialization

serializing and deserializing streams

The flow of operations used to read an object from a stream ObjectInputStream called a deserialization stream

The flow of operations used to write objects to the stream ObjectOutputStream is called a serialized stream (the object is saved to a file)

L Features: For manipulating objects . You can write an object to a file, or you can read an object from a file.

object Serialization Stream ObjectOutputStream

ObjectOutputStream writes the basic data type and graphics of the Java object to the OutputStream. You can use ObjectInputStream to read (refactor) objects. Persistent storage of objects can be achieved by using files in the stream.

Note: Java.io can only be supported. The Serializable interface object is written to the stream

L Code Demo:

Public class Objectstreamdemo {

Public Static void Main (string[] args) throws IOException, classnotfoundexception {

/*

* Store an object on a persistent (hard disk) device.

*/

Writeobj ();//serialization of the object.

}

Public Static void writeobj () throws IOException {

1, explicitly stores the object's files.

FileOutputStream fos = new fileoutputstream ("Tempfile\\obj.object");

2, add the Write object function to the operation file object.

ObjectOutputStream oos = new objectoutputstream (FOS);

3, the method to write to the object is called.

Oos.writeobject (new person ("Wangcai"));

Close the resource.

Oos.close ();

}

}

L Person Class

Only classes that implement the Serializable interface can be serialized

Public class Person implements Serializable {

Private String name;

Private int age;

Public Person () {

Super ();

}

Public Person (String name, int age) {

Super ();

this. name = name;

this. Age = age;

}

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;

}

@Override

Public String toString () {

return "Person [name=" + name + ", age=" + Age + "]";

}

}

Object deserialization Stream ObjectInputStream

ObjectInputStream the basic data and objects previously written using ObjectOutputStream are deserialized. An object that supports the Java.io.Serializable interface can be read from the stream.

L Code Demo

Public class Objectstreamdemo {

Public Static void Main (string[] args) throws IOException, classnotfoundexception {

Readobj ();//deserialization of the object.

}

Public Static void readobj () throws IOException, classnotfoundexception {

1, define the Stream object association to store the object file.

FileInputStream fis = new fileinputstream ("Tempfile\\obj.object");

2, set up a function object to read the object.

ObjectInputStream ois = new objectinputstream (FIS);

3. Downward transition

Person obj = (person) ois.readobject ();

System. out. println (Obj.tostring ());

}

}

Serializing Interfaces

When an object is to be serialized, the class to which the object belongs must implement the Serializable interface. Otherwise, an exception notserializableexception exception will occur.

Also, when deserializing an object, an exception invalidclassexception occurs if the object's class file is modified after serialization. This exception occurs for the following reasons:

L The sequence version number of the class does not match the version number of the class descriptor read from the stream

L This class contains unknown data types

L This class has no accessible parameterless construction method

Serializable tag interface. This interface provides a sequence version number for classes that require serialization. Serialversionuid. The purpose of this version number is to verify that the serialized object and the corresponding class are version-matched.

L Code modified as follows, modified to write to the object again, read the object test

Public class Person implements Serializable {

Declare a sequence version number to the class display, and as long as the Serivalversionuid is set, the class will not have an exception when serializing and deserializing

Private Static Final Long Serialversionuid = 1L;

Private String name;

Private int age;

Public Person () {

Super ();

}

Public Person (String name, int age) {

Super ();

this. name = name;

this. Age = age;

}

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;

}

@Override

Public String toString () {

return "Person [name=" + name + ", age=" + Age + "]";

}

}

transient keyword transient

When an object of a class needs to be serialized, some attributes do not need to be serialized, and attributes that do not need to be serialized can be decorated with the keyword transient. This property is not serialized when serialized, as long as it is modified by transient or static .

Static adornments are not serialized at the same time, because serialization is the persistent storage of object data, while static data belonging to the class load is not serialized.

L Code modified as follows, modified to write to the object again, read the object test

Public class Person implements Serializable {

/*

* Declare a sequence version number to the class display.

*/

Private Static Final Long Serialversionuid = 1L;

Private Static String name;

Private transient/* transient */ int age;

Public Person () {

Super ();

}

Public Person (String name, int age) {

Super ();

this. name = name;

this. Age = age;

}

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;

}

@Override

Public String toString () {

return "Person [name=" + name + ", age=" + Age + "]";

}

}

Java->io Stream _ serialized stream and deserialization stream

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.