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