Java Object Stream (Input-Output) ObjectOutputStream serialization error

Source: Internet
Author: User

Error: Java.io.notserializableexception

Workaround: Add the following code implementation

Object class

1 Package Com.etc._07objectdemo;2 3 import java.io.Serializable;4 5  Public classPerson implements Serializable {6     Private intID;7     PrivateString name;8     PrivateString sex;9      Public intgetId () {Ten         returnID; One     } A      Public voidSetId (intID) { -          This. ID =ID; -     } the      PublicString GetName () { -         returnname; -     } -      Public voidsetName (String name) { +          This. Name =name; -     } +      PublicString Getsex () { A         returnsex; at     } -      Public voidsetsex (String sex) { -          This. Sex =sex; -     } -      PublicPerson () { - super (); in         //TODO auto-generated Constructor stub -     } to      PublicPerson (intID, string name, string sex) { + super (); -          This. ID =ID; the          This. Name =name; *          This. Sex =sex; $     }Panax Notoginseng @Override -      PublicString toString () { the         return "Person [id="+ ID +", Name="+ name +", sex="+ Sex +"]"; +     } A      the      +  -}
Object Input stream
1 Package Com.etc._07objectdemo;2 3 import Java.io.FileInputStream;4 import java.io.FileNotFoundException;5 import java.io.IOException;6 import Java.io.ObjectInputStream;7 8  Public classObjectinputstreamdemo {9     //file-------->person Object deserializationTen      Public Static voidMain (string[] args) { One         Try { AObjectInputStream Ois =NewObjectInputStream ( -                     NewFileInputStream ("D:/my123.ini")); -              theObjectObject=Ois.readobject (); -             if(Objectinstanceof Person) { -Person P = (person)Object; -System. out. println (p); +             } -              +              A ois.close (); at}Catch(FileNotFoundException e) { -             //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(IOException e) { -             //TODO auto-generated Catch block - e.printstacktrace (); in}Catch(ClassNotFoundException e) { -             //TODO auto-generated Catch block to e.printstacktrace (); +         } -     } the  *}
Object output stream
1 Package Com.etc._07objectdemo;2 3 import Java.io.File;4 import java.io.FileNotFoundException;5 import Java.io.FileOutputStream;6 import java.io.IOException;7 import Java.io.ObjectOutputStream;8 9  Public classObjectoutputstreamdemo {Ten      One     //Person Object------> file Serialization A      Public Static voidMain (string[] args) { -          -         Try { theObjectOutputStream Oos =NewObjectOutputStream ( -                     NewFileOutputStream ("D:/my123.ini")); - //Creating Objects -Person p =NewPerson (1001,"Zhang San","male"); + Oos.writeobject (p); - oos.close (); +              ASystem. out. println ("ending ...."); at              -             //java.io.NotSerializableException -}Catch(FileNotFoundException e) { -             //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(IOException e) { in             //TODO auto-generated Catch block - e.printstacktrace (); to         } +          -     } the  *}

Java Object Stream (Input-Output) ObjectOutputStream serialization error

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.