?
Solved

Is there a way to save a hashtable to a text file?

Posted on 2003-03-25
15
Medium Priority
?
243 Views
Last Modified: 2010-03-31
Is there way to save a hashtable (and its contents) to a text file, so that when I run the program at a later time, I can load it from the text file.

Thanx
0
Comment
Question by:espadana
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8202142
If you use the subclass Properties instead:

http://java.sun.com/j2se/1.4/docs/api/java/util/Properties.html#store(java.io.OutputStream, java.lang.String)

otherwise you'll have to emulate this
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8202145
Why not? Since it is easiest to deal with text-files which contain characters/ strings, you can get the keys and contents of the HashTable, convert them into String type and write them to a file using the numerous available I/O classes.

Mayank.
0
 

Author Comment

by:espadana
ID: 8202176
How about if you don't know how many elements are in the hashtable?  Is it possible to save the whole hashtable at once rather than doing it element by element?

Thanx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8202190
No, it must be iterated unless you use the subclass Properties.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8202206
You have to do it element-by-element:

HashTable ht = new HashTable () ;

// put elements into it

Set s = ht.keySet () ;
Iterator it = s.iterator () ;

while ( itr.hasNext () )
{
  // read objects using itr.next () method and convert values to string-type, then write them to the file

} // end while


This will write all current values into the file.

Mayank.
0
 

Author Comment

by:espadana
ID: 8202211
Are there any examples or tutorails available on using  subclass Properties for saving hashtables?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8202227
> Is there way to save a hashtable (and its contents) to a text file, so that when I run the program at a later time, I can load it from the text file.

  Why don't you serialize it (it is saving to a file at the end of the day)? It will just be easier, faster and more convenient.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8202238
Theoretically it's a simple as changing

>>Hashtable hashtable;

to

>>Properties hashtable

and

>>hashtable = new Hashtable();

to

>>hashtable = new Properties();

You can now call store
0
 

Author Comment

by:espadana
ID: 8202243
girionis,

That exactly what I want to do.

mayankeagle,

Thanx for the links, I'll check them out now.
0
 

Author Comment

by:espadana
ID: 8202256
oh, i forgot to say, thanks CEHJ,  I'll give your suggestion a try
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8202258
You can also try the list () and load () methods defined by Properties.

Mayank.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8202260
 Then do (assuming your Hashtable instance is called "myHashtable"):

FileOutputStream out = new FileOutputStream("myfile.out");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject(myHashtable);
s.flush();

  and to read it back:

FileInputStream in = new FileInputStream("myfile.out");
ObjectInputStream s = new ObjectInputStream(in);
Hashtable myHashtable = (Hashtable)s.readObject();

   Serializing is different than storing contents to a text file.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8202266
>>Are there any examples or tutorails available on using  subclass Properties for saving hashtables?

See my post above. There's no mystery here about calling store - just open a FileOutputStream
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8202290
If for some reason you can't use the subclass, you can use this (the source for Properties store):

    public synchronized void store(OutputStream out, String header)
    throws IOException
    {
        BufferedWriter awriter;
        awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));
        if (header != null)
            writeln(awriter, "#" + header);
        writeln(awriter, "#" + new Date().toString());
        for (Enumeration e = keys(); e.hasMoreElements();) {
            String key = (String)e.nextElement();
            String val = (String)get(key);
            key = saveConvert(key, true);

         /* No need to escape embedded and trailing spaces for value, hence
          * pass false to flag.
          */
            val = saveConvert(val, false);
            writeln(awriter, key + "=" + val);
        }
        awriter.flush();
    }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question