?
Solved

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

Posted on 2003-03-25
15
Medium Priority
?
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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