1

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);

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.. Both answers helped me a lot, but I used propertynames() to get the keys as a collection so check this, but both got one up on answer.
1

Try the following.

Properties properties = new Properties();
properties.load(new FileInputStream("/somePath/file.map"));

3 Comments

Thanks, I was not having a problem opening the property file, just was not sure how to get it back into my hashmap, but I figured it out and added a comment to my question.
One thing to note is properties is an implementation of Hashtable which technically is a Map (has key-value pairs). Good luck.
That helped a lot. Thanks.. They seem to be handled a little different in getting the data, like in a for loop if I understand what I'm reading, but that did help me understand it better.
-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.