• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

code throws error "java.io.EOFException"?

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FileInputStream fis=new FileInputStream("emp.ser");
ObjectInputStream ois=new ObjectInputStream(fis);

System.out.println("De-serializing object...");

Employee e1[]=new Employee[5];
int i=0;
while((e1[i]=(Employee)ois.readObject())!=null){
//e1[i]=(Employee)ois.readObject();
System.out.println(e1[i].getEmpno()+"\t"+e1[i].getSalary());
}
System.out.println("Object de-serialize...");
ois.close();
fis.close();
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ObjectInputStream.readObject does not return null on EOF, as your while loop expects. It throws an EOFException. Have a look at the API documentation for the particulars. I don't like relying on exceptions to signal something that is expected, like EOF so I usually store a bunch of objects in a collection of some sort and serialize that. Other folks will recommend first writing the number of objects to the stream, then writing the objects.
 
reply
    Bookmark Topic Watch Topic
  • New Topic