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 .

[1564 byte] By [iftikhar-4w-programmer-J2EEa] at [2007-10-3 7:27:46]
# 1
> i need help! how to avoid from java.io.EOFException .Why do you think you need to avoid it?
sabre150a at 2007-7-15 2:26:49 > top of Java-index,Core,Core APIs...
# 2
> while((obj=input.readObject())!=null)and what's this test for? Where does it say that readObject() can return null? Is it really the correct test for what you're testing for?
ejpa at 2007-7-15 2:26:49 > top of Java-index,Core,Core APIs...