user3132347
user3132347

Reputation: 363

How to read properties file in java, which has value in key-value pair

if properties file contains below type values how to read it directly into map

user={'name':'test','age':'23','place':'london'}.

Thanks in advance!

Upvotes: 1

Views: 15271

Answers (4)

1218985
1218985

Reputation: 8012

You can inject values into a Map from the properties file using the @Value annotation like this.

@Value("#{${user}}")  
private Map<String, String> user;

Entry in your application.properties must be:

user = {"name":"test","age":"23","place":"London"}

Upvotes: 8

kleash
kleash

Reputation: 1319

This will read a property file and put it in Properties object.

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesConfig {

    private Properties prop;

    private PropertiesConfig() {
        super();

        init();
    }

    private static class PropertiesInstance {
        private static PropertiesConfig instance = null;

        public static PropertiesConfig getInstance() {
            if (null == instance) {
                instance = new PropertiesConfig();
            }
            return instance;
        }
    }

    public static PropertiesConfig getInstance() {
        return PropertiesInstance.getInstance();
    }

    private void init() {

        prop = new Properties();
        try (InputStream input = getClass().getResourceAsStream("/sample_config.properties")) {

            // load a properties file
            prop.load(input);

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public String getProperty(String key) {
        return prop.getProperty(key);
    }

}

Now you can use any library to convert a value to Map as your value looks like a JSON.

Code example to achieve this through Jackson:

public static Map < String, Object > covertFromJsonToMap(String json) throws JsonTransformException {
    try {
        return mapper.readValue(json, new TypeReference < HashMap < String, Object >> () {});
    } catch (Exception e) {
        LOGGER.error("Error " + json, e);
        throw new JsonTransformException("Error in json parse", e);
    }
}

So something like this will do:

covertFromJsonToMap(PropertiesConfig.getInstance().get("user"));

Upvotes: 1

Abhishek Gautam
Abhishek Gautam

Reputation: 1767

test.properties file

name=test
age=23
place=london

code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class ReadPropertiesFile {
    public static void main(String[] args) {
        try {
            File file = new File("test.properties");
            FileInputStream fileInput = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(fileInput);
            fileInput.close();

            Enumeration enuKeys = properties.keys();
            while (enuKeys.hasMoreElements()) {
                String key = (String) enuKeys.nextElement();
                String value = properties.getProperty(key);
                System.out.println(key + ": " + value);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Hope, this would help.. :)

Upvotes: 2

Amit Phaltankar
Amit Phaltankar

Reputation: 3424

Looks like you have a JSON representation of your map as a value. Once you read the value as a String in Java you can use Gson to convert it to map

Gson gson = new Gson(); 
String json = <YOUR_STRING>
Map<String,Object> map = new HashMap<String,Object>();
map = (Map<String,Object>) gson.fromJson(json, map.getClass());

Upvotes: 0

Related Questions