• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write Vector object to a text file?

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic