How to use Java property files?

后端 未结 17 1444
时光取名叫无心
时光取名叫无心 2020-11-22 11:13

I have a list of key/value pairs of configuration values I want to store as Java property files, and later load and iterate through.

Questions:

  • Do I ne
17条回答
  •  一向
    一向 (楼主)
    2020-11-22 11:37

    In Java 8 to get all your properties

    public static Map readPropertiesFile(String location) throws Exception {
    
        Map properties = new HashMap<>();
    
        Properties props = new Properties();
        props.load(new FileInputStream(new File(location)));
    
        props.forEach((key, value) -> {
            properties.put(key.toString(), value.toString());
        });
    
        return properties;
    }
    

提交回复
热议问题