How to use Java property files?

后端 未结 17 1459
时光取名叫无心
时光取名叫无心 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:28

    You can load the property file suing the following way:

    InputStream is = new Test().getClass().getClassLoader().getResourceAsStream("app.properties");
            Properties props =  new Properties();
            props.load(is);
    

    And then you can iterate over the map using a lambda expression like:

    props.stringPropertyNames().forEach(key -> {
                System.out.println("Key is :"+key + " and Value is :"+props.getProperty(key));
            });
    

提交回复
热议问题