Getting java.io.EOFException while reading objects from ObjectInputStream.
hi there,
i am trying to read unknow number of objects saved in a file (serialized) , when i loop through, it is working fine, but at the end it throughs EOFException,
i want to know how to control it? how i can detect the end of file. my code is look like this.
import java.io.*;
public class TestPersonSave {
public static void main(String args[]) throws Exception
{
Person personOne = new Person("abdsd","29");
Person personTwo = new Person("xyz","34");
// save Person object to the file
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("D:\\person.txt")));
out.writeObject(personOne);
out.flush();
out.writeObject(personTwo);
out.flush();
out.close();
ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("D:\\person.txt")));
Object obj=null;
Person person=null;
int i=1;
while((obj=input.readObject())!=null)
{
person=(Person)obj;
System.out.println("Person No. "+i);
System.out.println("Name: "+person.name);
System.out.println("Age: "+person.age);
++i;
}
}
}
///////// class person
class Person implements Serializable {
public String name;
public String age;
public Person(String name,String age) {
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
}
i need help! how to avoid from java.io.EOFException .

