• Post Reply Bookmark Topic Watch Topic
  • New Topic

Write hashset to txt using filewriter  RSS feed

 
Bob Smithy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.



 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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. :/
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 1837
10
  • Mark post as helpful
  • send pies
  • 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.




 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not misuse the forum. Same question asked here. Please do not duplicate question.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As JN says, you should inform people on both websites if you post twice.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!