For the I / O part, you can use new PrintWriter(new File(filename)) . Just call println methods like System.out , and don't forget close() after that. Make sure you handle any IOException gracefully.
If you have a specific format, you will have to explain it, but otherwise a simple for-each loop on Hashtable.entrySet() is all you need to repeat the Hashtable elements.
By the way, if you don't need the synchronized function, a HashMap<String,String> is likely to be better than a Hashtable .
Related Questions
Here is a simple example of combining things, but without explicitly working through an IOException and using a simple format:
import java.io.*; import java.util.*; public class HashMapText { public static void main(String[] args) throws IOException { //PrintWriter out = new PrintWriter(System.out); PrintWriter out = new PrintWriter(new File("map.txt")); Map<String,String> map = new HashMap<String,String>(); map.put("1111", "One"); map.put("2222", "Two"); map.put(null, null); for (Map.Entry<String,String> entry : map.entrySet()) { out.println(entry.getKey() + "\t=>\t" + entry.getValue()); } out.close(); } }
Running this on my machine generates map.txt containing three lines:
null => null 2222 => Two 1111 => One
As a bonus, you can use the first declaration and initialize out and print the same with standard output instead of a text file.
see also
source share