How to write a hash table <string, string> in a text file, java?

I have a hastable htmlcontent - this is the html string of the urlstring string. I want to write a hastable in a .text file.

Can anyone suggest a solution?

+4
source share
6 answers

For a textual representation, I would recommend choosing a few characters that are unlikely to appear on your lines, and then output a CSV file with these characters as delimiters, quotation marks, terminators, and screens. In fact, each row (indicated by the terminator, as otherwise there may be lines ending with characters on any line), will have a hash table entry key as the first CSV "field", and the value for it in the second field.

A simpler approach on the same lines would be to designate one arbitrary character, such as a backslash \ , as an escape character. You will have to double the backslash when they occur on any line, and express the tab ( \t ) and the end of the line ( '\n ) in an escape form; then as a field separator between two fields (key and value) you can use a real (non-escape sequence) tab character, and at the end of each line you can use a real (non-escape sequence) end of line.

+1
source

What about one line for each record and two lines separated by a comma? Example:

 "key1","value1" "key2","value2" ... "keyn","valuen" 

keep quotes and you can also write keys that also refer to null entries, for example

 "key", null 

To create a table, you can use code similar to:

 public void write(OutputStreamWriter out, HashTable<String, String> table) throws IOException { String eol = System.getProperty("line.separator"); for (String key: table.keySet()) { out.write("\""); out.write(key); out.write("\",\""); out.write(String.valueOf(table.get(key))); out.write("\""); out.write(eol); } out.flush(); } 
+3
source

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

+3
source
 import java.io.*; class FileWrite { public static void main(String args[]) { HashTable table = //get the table try{ // Create file BufferedWriter writer = new BufferedWriter(new FileWriter("out.txt")); writer.write(table.toString()); }catch (Exception e){ e.printStackTrace(); }finally{ out.close(); } } } 
+1
source

You may try

 public static void save(String filename, Map<String, String> hashtable) throws IOException { Properties prop = new Properties(); prop.putAll(hashtable); FileOutputStream fos = new FileOutputStream(filename); try { prop.store(fos, prop); } finally { fos.close(); } } 

Saves a hash table (or any map) as a properties file. You can use the Properties class to reload data.

+1
source

Since you do not have file format requirements, I would not create a custom one. Just use something standard. I would recommend using json for this!

Alternatives include xml and csv, but I think json is the best option here. Csv does not handle complex types, such as having a list in one of the keys of your card, and xml can be quite complicated for encoding / decoding.

Using json-simple as an example:

 String serialized = JSONValue.toJSONString(yourMap); 

and then just save the line in your file (which is not specific to your domain or using Apache Commons IO):

 FileUtils.writeStringToFile(new File(yourFilePath), serialized); 

To read the file:

 Map map = (JSONObject) JSONValue.parse(FileUtils.readFileToString(new File(yourFilePath)); 

You can also use a different json library, but I think it suits your needs.

+1
source

Source: https://habr.com/ru/post/1309070/


All Articles