Java IO streams-serializing and deserializing streams

Source: Internet
Author: User

2017-11-05 20:42:06

serialized Stream : The object is stored as a stream in a text file or transmitted over a network. Object-stream data (ObjectOutputStream)

deserialization Stream : Restores the Stream object data in the text file or the stream object data in the network to an object. Stream data--object (ObjectInputStream)

    • ObjectOutputStream

objectoutputstream:ObjectOutputStream writes the basic data type and graphics of the Java object to OutputStream. You can use ObjectInputStream (deserialization) to read (refactor) an object. Persistent storage of objects can be achieved by using files in the stream. If the stream is a network socket stream, you can refactor the object on another host or in another process.

Note: the class to be serialized needs to implement the serializable interface , otherwise it cannot be serialized, but the class is a markup interface and there is no way to rewrite it.

* Construction Method

* Common Methods

    • ObjectInputStream

objectinputstream: ObjectInputStream the basic data and objects previously written using ObjectOutputStream are deserialized.

When ObjectOutputStream and ObjectInputStream are used with FileOutputStream and FileInputStream respectively, you can provide the application with persistent storage of object graphics. ObjectInputStream is used to restore objects that were previously serialized. Other uses include using a socket stream to pass objects between hosts, or for marshaling and formal parameters in a group's remote communication system.

* Construction Method

* Common Methods

import java.io.*;p ublic class Demo5 {public static void main (string[] args) throw        S IOException, classnotfoundexception {//write ();    Read (); } public static void Write () throws ioexception{objectoutputstream Oos = new ObjectOutputStream (New FILEOUTPUTST        Ream ("E:/test.txt"));        The class needs to have a serialized interface in order to be serialized Student st = new Student ("Zhang San", 20);        Serialization of Oos.writeobject (ST);    Oos.close (); } public static void Read () throws IOException, classnotfoundexception {objectinputstream ois = new ObjectInput        Stream (New FileInputStream ("E:/test.txt"));        Deserializes Object obj = Ois.readobject ();        Ois.close ();    Output Object System.out.println (obj);    }}import Java.io.serializable;public class Student implements serializable{private String name;    Private Integer age;        Student (String Name,int age) {this.name=name;    This.age=age; }}

Problem one, if you modify the student class before reading, an error occurs during the execution of the Read () function:

Exception in thread "main" java.io.InvalidClassException:DemoFile.Student; Local class Incompatible:stream Classdesc serialversionuid = -8639914051049964771, local class Serialversionuid = 757908 4536321238113

The reason is that because the student class implements the serialization interface, a serialized tag value is generated, but if you modify the student class, the serialization value changes. When reading, you will find that the serialization value mismatch problem, will be an error.

The solution is that the serial number becomes constant, so that when we modify the class, its data read will not produce the above problem.

public class Student implements serializable{    private static final long Serialversionuid = -7579084536321238113l;
   private String name;    private int age;    int age;    Student (String name,int age)    {        this.name=name;        this.age=age;}    }

Question two: A method in which member variables in a class are not serialized: plus the transient keyword.

Java IO streams-serializing and deserializing streams

Related Article

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.