Reputation: 1465
Answer I just found what I was looking for:
Properties properties = new Properties();
FileInputStream in = new FileInputStream("/somePath/file.map");
properties.load(in);
in.close();
HashMap<String, String> propMap = new HashMap<String, String>((Map) properties);
This allowed me to get the opened property data back into the hashmap with out needing to know the property names.
original question.
I have the following code that writes out the results of a HashMap. I'm wondering the easiest way to open this property back up and pull the data back into a HashMap, the putAll was a nice way to get the data into the property and store the data. I don't see a getAll to retrieve it, and the HashMap Key/Value data was not known before the hashmap was created so can't just retrieve by property name. The data after it was created will be static, so I could physically open the file written based off the hashmap to get the property names, but would rather not have to do it that way. Thanks for any help.
Properties properties = new Properties();
properties.putAll(mapTabSets);
properties.store(new FileOutputStream("/somePath/file.map"),"Java properties);
Upvotes: 1
Views: 8455
Reputation: 1
package com.mkyong.common;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
public class ClassName {
private static HashMap<String, String> mymap = new HashMap<String, String>();
public ClassName() throws IOException{
Properties prop = new Properties();
prop.load(ClassName.class.getClassLoader().getResourceAsStream("ini.properties"));
mymap.put("1", prop.getProperty("required.firstName"));
mymap.put("2", prop.getProperty("required.lastName"));
mymap.put("3", prop.getProperty("equired.address"));
}
public static void main(String[] args) throws IOException {
ClassName className = new ClassName();
System.out.println("The Value of Key-1 ::" + mymap.get("1"));
}
}
Use the properties file:-
****************************
required.firstName=Nitesh
required.lastName=Rathore
required.address=DangeChowk
Upvotes: -1
Reputation: 2758
Although properties do not have a getAll function, they do have
propertynames()
stringPropertyNames()
both of which will deliver a collection of all keys of the property. You can then iterate over the collection and extract all values from the property via
properties.getProperty(String)
Upvotes: 2
Reputation: 4676
Try the following.
Properties properties = new Properties();
properties.load(new FileInputStream("/somePath/file.map"));
Upvotes: 1