I know that I can build a Map as below.
private static final ImmutableMap<String,String> WordMap =
ImmutableMap.<String, String>builder()
.put("blah", "blahblah").put("blabla", "blahblahblah").build()
I'd like to store the values of my map in a config file.
I'm already storing the values for a different hashset in the config file by doing
values=value1,value2,value3
and thennew HashSet<String>(Arrays.asList(prop.getProperty(values).split(",")))
I'd like to do something similar for my map.
Any tips?
I'm using java.util.Properties
Creating a .properties file − Instantiate the Properties class. Populate the created Properties object using the put() method. Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.
A map is meant for normal key-value pair usage in code. Properties are typically used for storing and loading configuration values from a file.
Learn to use the Spring @Value annotation to configure fields from property files, system properties, etc.
Since you've indicated you don't want to use JSON, you could store the map as a single property like this:
map=key1=value1,key2=value2,key3=value3
Use Guava's Splitter and Joiner to simplify reading and writing the map:
String formatMap(Map<String, String> map) {
return Joiner.on(",").withKeyValueSeparator("=").join(map);
}
Map<String, String> parseMap(String formattedMap) {
return Splitter.on(",").withKeyValueSeparator("=").split(formattedMap);
}
This will work so long as the keys and values do not contain "," or "=" characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With