• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

how to persist the contents of HashTable ??

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When I run m yjava program, then i am using HashTable to store some inofrmation. I want to store that information and then use it again when I run my program again...how can i write this impelementation using HashTable ?? any cluses ??
thanks
Oni
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use Properties, a subclass of Hashtable, instead of Hashtable, it also provides load and store methods for writing its contents to a file for reloading later.
 
Oni Anand
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply.
I had thought of that initially...but I wanted a dynamic approach solution and I dont want to bind my data with any static entity (properties file). I applied another approach for this. Now as soon as my program starts up and running , i read the inofrmation directly in to a hashtable. looks like that will solve our purpose.
thanks again
Anand

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not real solid on this, but with the Collections interface can't you implement a hashmap, for example, and then use an ObjectOutputStream linked to a FileOutputStream to write the hashmap to a file and then reverse it with an ObjectInputStream linked to a FileInputStream to re-instantiate that data at a later time? Here is a snippet of code I found in the Collections tutorial at Javasoft regarding this:

import java.io.*;
import java.util.*;
public class serial1 {
public static void main(String args[]) {
Map hm = new HashMap();
hm.put("Mary", "123-4567");
hm.put("Larry", "234-5678");
hm.put("Mary", "456-7890");
hm.put("Felicia", "345-6789");
try { FileOutputStream fos = new FileOutputStream("test.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(hm);
oos.close();
}
catch (Throwable e) {
System.err.println(e);
}
}
}

import java.io.*;
import java.util.*;
public class serial2 {
public static void main(String args[]) {
Map hm = null;
try { FileInputStream fis = new FileInputStream("test.ser"); ObjectInputStream ois = new ObjectInputStream(fis);
hm = (Map)ois.readObject();
ois.close();
}
catch (Throwable e) {
System.err.println(e);
}
if (hm != null) {
Iterator iter = hm.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry e = (Map.Entry)iter.next();
System.out.println(e.getKey() + " " + e.getValue());
}
}
}
}

--Octavyn
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to have a preference file for an application one time and then I discovered serialized objects. You can simple create a class that implements serializable, include your HashTable as a non-transient class member and then write it out to disk using the ObjectInput/Output Stream classes. It very effective in that you just write it out and then read it in and cast it back to you "preference" object when the app loads up again.
Seanm
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic