• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving Hashtable to file  RSS feed

 
russakis russakis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! i'm a student at www.it.teithe.gr
i want to write the contents of a hashtable into a file
Having tried these 2 methods i get errors
In the first attempt i get the result in a single line
and in the second i
1)out.write(htable.toString());

2)
All keys are stored in a single string key which then is splitted to keys_table. I get an error that tofileKey is an object (as well as keys_table[j])

for(int j=1;j<keys_table.length;j++)
{
tofileKey= keys_table[j].toString();
tofileValue=htable.get(tofileKey);
tofile=tofileKey.concat(" ").concat(tofileValue);
out.write(tofile);
}
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get an error that tofileKey is an object (as well as keys_table[j])


This is probably your main issue. Except for serialization, you can't just write any object to a stream, in a fashion will preserve the object. System.out which is a PrintStream will just call toString() prior to printing it, which is somewhat okay, if the toString() method converts the object to something that decently describes the object.

In any case, if it is a file, how would you reverse the process? It is now a string and you need the original object.


If it is a file that you are writing to, and the objects that you want to write are serializable, you can use ObjectInputStream and ObjectOutputStream on the file streams to read and write the objects.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!