This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes code throws error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "code throws error "java.io.EOFException"?" Watch "code throws error "java.io.EOFException"?" New topic
Author

code throws error "java.io.EOFException"?

Minal Silimkar-Urankar
Ranch Hand

Joined: Nov 25, 2007
Posts: 136
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();


Minal Silimkar
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 9137
    
    9

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.


[How To Ask Questions On JavaRanch]
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: code throws error "java.io.EOFException"?
 
It's not a secret anymore!