Java语言读取配置文件config.properties

配置文件中的内容如下:

left=com.sunny.project.LeftHair
right=com.sunny.project.RightHair
in=com.sunny.project.InHair

读取配置文件中的代码如下:

public class PropertiesReader {

    public static void main(String[] args) {
        new PropertiesReader().getProperties();
    }
    public Map<String, String> getProperties() {

        Properties props = new Properties();
        Map<String, String> map = new HashMap<String, String>();
        try {

            InputStream in = getClass().getResourceAsStream("type.properties");
            props.load(in);
            Enumeration en = props.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String property = props.getProperty(key);
                map.put(key, property);
                System.out.println(key + "  " + property);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}

以下是读取到的结果。

发布了143 篇原创文章 · 获赞 31 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/qq_42112846/article/details/102616051