Writing and Reading Hashtable to a text file

hashtable along with some other text in one single text file

Page 1 of 1

3 Replies - 14651 Views - Last Post: 10 December 2008 - 09:14 AM Rate Topic: -----

#1 cybernaut09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 01-June 08

Writing and Reading Hashtable to a text file

Posted 09 December 2008 - 03:21 PM

I need to write Hashtable and some text in a single file. Below is the way how I did it .
		out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(pathToSave + ".ind"))));
		out.write(hashEncodeTable + "[#]"+someData);//Write out the specfied string to the file



as I have both Hashtable and some text in one file I have not used out.writeObject(hashEncodeTable) , and separated them with [#] .
Now I am reading the file and extracting the hashtable from the file in a string object using the following code .
		indexOfKey = rawData.indexOf("[#]");
	System.out.println("indexofkey ="+indexOfKey);
	hashFromFile=rawData.substring(0,indexOfKey);
	System.out.println("hashFromFile ="+hashFromFile);



The program is running fine . But now I need to convert the string object hashFromFile back to hashtable . In other words I need a way to save Hashtable along with some text in a text file , and then read the text file and separate the Hashtable and the text . I hope I have expressed my problem clearly . Please help .

This post has been edited by cybernaut09: 09 December 2008 - 03:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Writing and Reading Hashtable to a text file

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Writing and Reading Hashtable to a text file

Posted 09 December 2008 - 10:37 PM

Who needs to read it back ?
If it is a Java Application that can know the objects in your HashTable just writeObject(the whole hasTable)
Was This Post Helpful? 0
  • +
  • -

#3 cybernaut09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 01-June 08

Re: Writing and Reading Hashtable to a text file

Posted 10 December 2008 - 08:06 AM

I tried writeObject method like
out.writeObject(hashEncodeTable + "[#]"+someData );


but I am getting error
 
java:60: cannot find symbol
symbol  : method writeObject(java.lang.String)
location: class java.io.BufferedWriter
						out.writeObject(hashEncodeTable + "[#]"+someData );
						   ^
1 error



I have imported
import java.util.*;
import java.io.*;


and I have created HashTable object as
Hashtable<Character,String> huffEncodeTable = new Hashtable<Character,String>();

So please elaborate what you suggested PBL . Thank you for replying previously.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: Writing and Reading Hashtable to a text file

Posted 10 December 2008 - 09:14 AM

Use Properties instead. They're both Maps but Properties has a method for saving:

http://java.sun.com/...va.lang.String)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1