• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading/writing vectors  RSS feed

 
Javier Mannich
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm new here. I'm taking an intro Java class in which we're mainly writing stuff for the console, and we're just getting into reading and writing to files. This particular program is an addressbook program which allows the user to enter information on contacts which is then saved as either a PersonalFriend or BusinessAssociate (both extending class Person) in a vector called "contacts"
So here is the method that writes the vector to a file called "Contacts.dat" in the current directory.



Now this is the code that allows the user to locate and load up the file. The problem seems to be that the program isn't hitting most of it after I locate the file.



The upload method throws java.io.EOFException when I try to do the upload, so perhaps my vector isn't being written properly?

I've been poring over the javadocs and banging my head on the wall for a while on this. Any help would be GREATLY appreciated.

Thanks,
Javier

[ March 06, 2005: Message edited by: Javier Mannich ]
[ March 06, 2005: Message edited by: Javier Mannich ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First your if test reads the first object from the file (I assume the Vector since you're not writing anything else in it) and tests if it's a Vector. It is, so the next line reads the next object from the file and ... EOFException since you only write one Vector to the file.

Instead, you need to store the object read from the file with the first readObject() call. Then test if that object is a Vector, and if so continue.

Welcome to JavaRanch!
[ March 07, 2005: Message edited by: David Harkness ]
 
Javier Mannich
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, that solved my problem! So every time I do a read in that fashion, it will look for another vector? Anyway, thanks for the great advice!

Javier
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Javier Mannich:
So every time I do a read in that fashion, it will look for another vector?
Well, it will read another object -- whatever type it is, but yes I think that's what you meant. And as you've seen, it will read entire object graphs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!