getting multiple objects from serialization in java -


i want multiple oblects of employee class file using serialization. here getting 1 object solution??

    package com.jbt;      import java.io.serializable;      public class employee implements serializable     {        public string firstname;        public string lastname;        private static final long serialversionuid = 5462223600l;     }         package com.jbt;      import java.io.fileoutputstream;     import java.io.ioexception;     import java.io.objectoutputstream;      public class serializaitonclass {          public static void main(string[] args) {             employee emp = new employee();             emp.firstname = "vivekanand";             emp.lastname = "gautam";              try {                 fileoutputstream fileout = new fileoutputstream("./employee.txt");                 objectoutputstream out = new objectoutputstream(fileout);                 out.writeobject(emp);                 out.close();                 fileout.close();                 system.out.printf("serialized data saved in ./employee.txt file");             } catch (ioexception i) {                 i.printstacktrace();             }         }     }    package com.jbt;  import java.io.*;  public class deserializationclass {     public static void main(string[] args) {         employee emp = null;         try {             fileinputstream filein = new fileinputstream("./employee.txt");             objectinputstream in = new objectinputstream(filein);             emp = (employee) in.readobject();             in.close();             filein.close();         } catch (ioexception i) {             i.printstacktrace();             return;         } catch (classnotfoundexception c) {             system.out.println("employee class not found");             c.printstacktrace();             return;         }         system.out.println("deserializing employee...");         system.out.println("first name of employee: " + emp.firstname);         system.out.println("last name of employee: " + emp.lastname);     } } 

here in deserialization class how can take multiple objects in list?? in advance..!!

list<employee> employeelist = new arraylist<employee>(); fileinputstream fis = null; objectinputstream in = null; try {     fis =new fileinputstream("./employee.txt");     in = = new objectinputstream(fis);     while (true) {         employeelist.add((employee) in.readobject());     } } catch (eofexception eof) {     // ignore. means reached eof } {     if (fis != null)         fis.close(); // close stream     if(in != null)         in.close(); } 

note : can serialize list , store on file , deserialize it. (added list serializable list)


Comments