save map to file java

Solutions on MaxInterview for save map to file java by the best coders in the world

showing results for - "save map to file java"
Irene
18 Apr 2016
1// Save map in file
2Map<String, String> map = new HashMap<String, String>();
3Properties properties = new Properties();
4
5for (Map.Entry<String,String> entry : map.entrySet()) {
6    properties.put(entry.getKey(), entry.getValue());
7}
8
9properties.store(new FileOutputStream("data.properties"), null);
10
11// Load the map
12Map<String, String> map_from_file = new HashMap<String, String>();
13Properties properties = new Properties();
14properties.load(new FileInputStream("data.properties"));
15
16for (String key : properties.stringPropertyNames()) {
17   map_from_file.put(key, properties.get(key).toString());
18}
19