This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to write Vector object to a text file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to write Vector object to a text file?" Watch "How to write Vector object to a text file?" New topic
Author

How to write Vector object to a text file?

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
I have the following class:

I created another class which uses Vector to store the object like that:

How can I write vector object (name, sex and age for all objects) to a text file LINE by LINE?
How can I cast that object's name, sex and age to be String?
Thanks for help.
Andrew
Eric Fletcher
Ranch Hand

Joined: Oct 26, 2000
Posts: 188
I think you mean how do you write what's IN the object to a text file, right?
In this case you don't have to cast them to Strings, they are already Strings. If you mean a single step to return the instance variables as a single String, you could override the toString() method of X to put them all together in one String:
In X...

Or something like that depending on your needs.
Then you have a choice of whether you want to build a big String(delimited by newlines) and write it to the file all at once, or write once for each object in the Vector. Check out these links for writers:
BufferedWriter
FileWriter
PrintWriter
HTH,
E


My theory of evolution is that Darwin was adopted. - Steven Wright
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write Vector object to a text file?
 
Similar Threads
File Operations Program Problem
Merging rows with same ID together with dynamic headers with CSV
Tokenizing after reading from a file
How to search a Vector array?
equals and hashcodes method query