dcsimg
program stopping with end of file
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Maddy_Alasyam
Posted On:   Thursday, October 21, 2004 01:24 AM

I have two problems. one is how to append in to a file. Second is i'm trying to read the file using readObject(), and catching two exceptions IOException and ClassNotFoundException. but at the end of reading, we know that EOF occurs. in my program it stops, when it get end of file. please help me. for ur reference i'm enclosing some code. public void personRead() throws Exception { FileInputStream fis=new FileInputStream("person.dat"); ObjectInputStream ois=new ObjectInputStream(fis); Person p; try { while ((p=(Person)ois.readObject())!=null) { System.out.println(p.toString()); persons.add(p); // add person to persons vector } } catch(   More>>

I have two problems. one is how to append in to a file.
Second is i'm trying to read the file using readObject(),
and catching two exceptions IOException and ClassNotFoundException. but at the end of reading, we know that EOF occurs. in my program it stops, when it get end of file. please help me. for ur reference i'm enclosing some code.

			
public void personRead() throws Exception {
FileInputStream fis=new FileInputStream("person.dat");
ObjectInputStream ois=new ObjectInputStream(fis);
Person p;
try {
while ((p=(Person)ois.readObject())!=null) {
System.out.println(p.toString());
persons.add(p); // add person to persons vector
}
}
catch(IOException ioe) {}
catch(ClassNotFoundException cnf) {
}
ois.close();
fis.close();

}

public void testAll() {
try {
personRead();
}
catch(Exception e) {}
Vector v=new Vector();
copyVector(v);
printVector(v);
}


in my program, the program stops executing at personRead() method. and not continue to run copyVector() and printVector() methods. please help me.

Maddy    <<Less

Re: program stopping with end of file

Posted By:   Anonymous  
Posted On:   Friday, October 22, 2004 04:24 AM

Not very familiar with serialisation. But some thoughts:


Why storing multiple persons? You can serialize the Vector (it´s implementing the Serializable interface). And I think the readObject() method will return only one Object (e.g. the Vector)).


You should print the Stacktraces in the catch clause while development. Nice infos :)

About | Sitemap | Contact