Convert Properties string to Map - Java java.util

Java examples for java.util:Map Operation

Description

Convert Properties string to Map

Demo Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

public class Main {
    public static void main(String[] argv) {
        String properties = "java2s.com";
        System.out.println(toMap(properties));
    }/*from   w w  w  .  ja v  a 2s  .  c  o m*/

    public static Map<String, String> toMap(String properties) {
        try {
            InputStream is = new ByteArrayInputStream(
                    properties.getBytes("UTF-8"));
            Properties prop = new Properties();
            prop.load(is);
            Map<String, String> ret = new HashMap<String, String>();
            for (String key : prop.stringPropertyNames()) {
                ret.put(key, prop.getProperty(key));
            }
            return ret;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Related Tutorials