• 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

Write hashset to txt using filewriter

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to write a hashset to a text file. Normally, I have no issues with writing to txt as it is simple for me. However, in this case I am absolutely clueless.

A bit of background about the class: it is a hashset of ComputerScientist objects with 2 fields; name, and field of research (and yes, I know what I put to fill up the HashSet does not count, I was only trying to test to see if I could get this to work).

I know the basic setup to use filewriter to save strings to a txt, but I am getting confused with the HashSet element of it.

I am eager to learn, and would appreciate it if snide or insulting comments were left out. And if there is already a similar question which deals with writing HashSet of objects to txt file, I apologize for not seeing it.



 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you can probably tell from your code/error message, the class FileWriter does not understand how to write an Object.
It can write characters and strings.

You have a few alternatives
1: Convert the set and the objects in it to strings, and write the strings out to file. One obvious solution would be to do one "ComputerScientust" object per line, and have the name and specialty separated by a comma.

2: Use an ObjectOutputStream. This will require you to write code in your ComputerScientist class telling it how to "write" and "read" an object.

 
Bob Smithy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:As you can probably tell from your code/error message, the class FileWriter does not understand how to write an Object.
It can write characters and strings.

You have a few alternatives
1: Convert the set and the objects in it to strings, and write the strings out to file. One obvious solution would be to do one "ComputerScientust" object per line, and have the name and specialty separated by a comma.

2: Use an ObjectOutputStream. This will require you to write code in your ComputerScientist class telling it how to "write" and "read" an object.



Would it really matter which of those alternatives I use? Also, how would I go about putting each of those on a separate line? Spending a few hours on this (more than I am willing to admit) + the fact that this is all new to me has led my brain to be extremely confused. :/
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Smithy wrote:
Would it really matter which of those alternatives I use?



In my opinion, the first option is probably the better option. The second option depends on object serialization, and I believe that the serialization header is still binary -- ie. not sure if the stream can go into a text file.

Bob Smithy wrote:
Also, how would I go about putting each of those on a separate line?



The easiest way is to simply write the newline character. If you want something more fancy, you can also write it with a PrintWriter wrapper, so the you can format the output.

Henry
 
Bob Smithy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Bob Smithy wrote:
Would it really matter which of those alternatives I use?



In my opinion, the first option is probably the better option. The second option depends on object serialization, and I believe that the serialization header is still binary -- ie. not sure if the stream can go into a text file.

Bob Smithy wrote:
Also, how would I go about putting each of those on a separate line?



The easiest way is to simply write the newline character. If you want something more fancy, you can also write it with a PrintWriter wrapper, so the you can format the output.

Henry



I tried this
but all I got was this: Jane MagnetsJake Nuclear FisionBob RoboticsJoe SnappleMary PCTim VR
On the bright side, at least this is a decent step in the right direction (I hope).
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Smithy wrote:
I tried this
but all I got was this: Jane MagnetsJake Nuclear FisionBob RoboticsJoe SnappleMary PCTim VR



The value for a newline is platform dependent. To get the newline character(s) for your platform, use the System.lineSeparator() method.

Henry
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The value for a newline is platform dependent. To get the newline character(s) for your platform, use the System.lineSeparator() method.



Or even better in my opinion wrap your FileWriter in a PrintWriter and then you can use the println() method.




 
Ranch Hand
Posts: 135
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not misuse the forum. Same question asked here. Please do not duplicate question.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

As JN says, you should inform people on both websites if you post twice.
 
reply
    Bookmark Topic Watch Topic
  • New Topic