Serialized and deserialized streams in Java

Source: Internet
Author: User

Serialized stream: The object is stored in the same way 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)

The serialized stream and the deserialized stream are different from the byte stream memory byte stream, which is a string that writes/reads the file, serializes the stream, and deserializes the stream to write/read the object in the text file.

But what we should note is that the files to be written and read are implemented Serializable This interface, of course, this interface is not any method, the direct implements is good, similar to this no method of the interface is called labeled Interface .

However, when we change the data in the original object, we can read it directly (without writing it and not write it once per change in the actual development), we will get an error:

Java.io.InvalidClassException:DemoPerson; Local class Incompatible:stream classdesc serialversionuid = -2071565876962058344, local class Serialversion UID =-8345153069362641443

This is because the object class implementation Serializable will have an ID value, when the data in the object changes, the ID value will change, but the text file in the write and read ID or the previous ID value, resulting in the ID value mismatch, will be error, solution: A yellow warning is reported in the object class, and clicking on the warning follows the compiler's implementation.

Attached code:

Person Class

1  Public classPersonImplementsSerializable {2     Private Static Final LongSerialversionuid = -2071565876962058344l;3 4     PrivateString name;5 6     //private int age;7 8     Private transient intAge ;9 Ten     //int age; One  A      PublicPerson () { -         Super(); -     } the  -      PublicPerson (String name,intAge ) { -         Super(); -          This. Name =name; +          This. Age =Age ; -     } +  A      PublicString GetName () { at         returnname; -     } -  -      Public voidsetName (String name) { -          This. Name =name; -     } in  -      Public intGetage () { to         returnAge ; +     } -  the      Public voidSetage (intAge ) { *          This. Age =Age ; $     }Panax Notoginseng  - @Override the      PublicString toString () { +         return"Person [name=" + name + ", age=" + Age + "]"; A     } the}

Objectstreamdemo class

1 ImportJava.io.FileInputStream;2 ImportJava.io.FileOutputStream;3 Importjava.io.IOException;4 ImportJava.io.ObjectInputStream;5 ImportJava.io.ObjectOutputStream;6 7  Public classObjectstreamdemo {8      Public Static voidMain (string[] args)throwsIOException, ClassNotFoundException {9 //write ();Ten          One read (); A     } -  -     Private Static voidRead ()throwsIOException, ClassNotFoundException { the         //deserializing Objects -ObjectInputStream Ois =NewObjectInputStream (NewFileInputStream ("Oss.txt")); -         //the receiving object is a -Object OB =Ois.readobject (); +          - ois.close (); +          A System.out.println (OB); at     } -  -     Private Static voidWrite ()throwsIOException { -         //creating a serialized output stream -ObjectOutputStream Oos =NewObjectOutputStream (NewFileOutputStream ("Oss.txt")); -          inPerson p =NewPerson ("Big Bear", 16); -          to Oos.writeobject (p); +          - oos.close (); the     } *}

Serialized and deserialized streams in Java

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.