Java deserialization test

Source: Internet
Author: User

Foreword: Have you ever thought about how to write an object "encrypted" after writing to disk? Serialization helps you achieve!

1. Concept

Serialization (serialization) transforms the state information of an object into a process that can be stored or transmitted in a form. During serialization, an object writes its current state to a temporary or persistent store. Later, the object can be recreated by reading or deserializing the state of the object from the store.

2. Deserializing the Java Experiment

--Testing the entity class--

1  Packageexercise;2 3 Importjava.io.Serializable;4 5  Public classPersonImplementsserializable{6     PrivateString name;7     Private intAge ;8     9      PublicPerson () {Ten     } One  A      PublicString GetName () { -         returnname; -     } the  -      Public voidsetName (String name) { -          This. Name =name; -     } +  -      Public intGetage () { +         returnAge ; A     } at  -      Public voidSetage (intAge ) { -          This. Age =Age ; -     } -  -      PublicPerson (String name,intAge ) { in         Super(); -          This. Name =name; to          This. Age =Age ; +     } -      the}

1) Single object serialization

1  Packageexercise;2 3 ImportJava.io.FileInputStream;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.ObjectInputStream;7 ImportJava.io.ObjectOutputStream;8 Importjava.util.ArrayList;9 Importjava.util.List;Ten  One  A  Public classObjectStreamDemo1 { -     /** -      * @paramargs the      * @throwsIOException -      * @throwsclassnotfoundexception -      */ -      +      Public Final StaticString PATH = "Obj.object1"; -      +      A      Public Static voidMain (string[] args)throwsIOException, at ClassNotFoundException { -         //writeobj (); - readobj (); -System.out.println ("--end--"); -     } -  in      Public Static voidReadobj ()throwsIOException, ClassNotFoundException { -ObjectInputStream Ois =NewObjectInputStream (NewFileInputStream ( to PATH)); +          -          thePerson p =(person) ois.readobject (); *System.out.println (P.getname () + "|" +p.getage ()); $         Panax Notoginseng     } -  the      Public Static voidWriteobj ()throwsIOException { +ObjectOutputStream Oos =NewObjectOutputStream (NewFileOutputStream ( A PATH)); the  +Oos.writeobject (NewPerson ("Zhang San", 30)); - oos.close (); $     } $}

Results show

2) Serialization of multiple objects

1  Packageexercise;2 3 ImportJava.io.FileInputStream;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.ObjectInputStream;7 ImportJava.io.ObjectOutputStream;8 Importjava.util.ArrayList;9 Importjava.util.List;Ten  One  A  Public classObjectStreamDemo2 { -     /** -      * @paramargs the      * @throwsIOException -      * @throwsclassnotfoundexception -      */ -      Public Final StaticString PATH = "Obj.object"; +      Public Static voidMain (string[] args)throwsIOException, - ClassNotFoundException { +          A         //writeobj (); at readobj (); -SYSTEM.OUT.PRINTLN ("---end!---"); -     } -  -      Public Static voidReadobj ()throwsIOException, ClassNotFoundException { -ObjectInputStream Ois =NewObjectInputStream (NewFileInputStream ( in PATH)); -          tolist<person> persons = (list<person>) Ois.readobject (); +          for(person p:persons) { -System.out.println (P.getname () + "|" +p.getage ()); the         } *     } $ Panax Notoginseng      Public Static voidWriteobj ()throwsIOException { -ObjectOutputStream Oos =NewObjectOutputStream (NewFileOutputStream ( the PATH)); +          Alist<person> persons =NewArraylist<person>(); theperson P1 =NewPerson ("Zhang San", 18);  +person P2 =NewPerson ("John Doe", 19);  - Persons.add (p1); $ Persons.add (p2); $ oos.writeobject (persons); - oos.close (); -     } the}

Results show

Attention:

• The entity class must implement the serialization interface "java.io.Serializable"

• Generated Obj.object because it is a binary file, it can not open normally, if the Notepad open is garbled!

Summary: The application of serialization technology in web-side technology is very important, and I hope that friends who learn Java can understand the technology and apply it.

Java deserialization test

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.