Serialization and deserialization of Java

Source: Internet
Author: User

 PackageOpen_exe;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream; Public classXuliehua { Public voidXulie () {Student St=NewStudent (); St.setname ("Test 1"); St.setage (18); St.setsex (Male); St.setheight ("168cm"); File File=NewFile ("C:" +file.separator+ "File1" +file.separator+ "Student.txt"); Try            {                if(!file.getparentfile (). exists ()) {//determine if the folder is not inFile.getparentfile (). Mkdirs ();//folder is not created folder}if(!file.exists ()) {//determine if the TXT file is not inFile.createnewfile ();//txt does not create a txt              }          }            Catch(IOException e) {e.printstacktrace (); }            Try            {             //Student Object Serialization ProcessFileOutputStream fos =Newfileoutputstream (file); ObjectOutputStream Oos=NewObjectOutputStream (FOS);//Serialization ofOos.writeobject (ST);//Write TXT fileOos.flush ();             Oos.close ();            Fos.close (); System.out.println ("Complete serialization!" "); }              Catch(IOException e) {e.printstacktrace (); System.out.println ("Serialization failed!" "); }     } Public voidFan () {File file=NewFile ("C:\\file\\student.txt"); Try        {         //Student Object Serialization ProcessFileInputStream FIS =Newfileinputstream (file); ObjectInputStream Ois=NewObjectInputStream (FIS);//deserializationStudent St1 =(Student) ois.readobject (); System.out.println ("Name =" +st1.getname ()); System.out.println ("Sex =" +st1.getsex ());             Ois.close ();           Fis.close (); System.out.println ("Deserialization succeeds!" "); }Catch(ClassNotFoundException e) {e.printstacktrace (); System.out.println ("Deserialization failed!" "); }       Catch(IOException e) {e.printstacktrace (); } }     Public Static voidMain (string[] args) {//TODO Auto-generated method stubsXuliehua n=NewXuliehua ();               N.xulie (); }}

 PackageOpen_exe;Importjava.io.Serializable; Public classStudentImplementsserializable{//Note that there is a need to implement the Serializable interfacePrivate Static Final LongSerialversionuid = 7146811040856818256L; PrivateString name; PrivateString sex; Private intAge ; PrivateString height;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getsex () {returnsex; }     Public voidsetsex (String sex) { This. Sex =sex; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString getheight () {returnheight; }     Public voidsetheight (String height) { This. Height =height; }    }

Serialization and deserialization of 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.