How to use Java property files?

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

    You can pass an InputStream to the Property, so your file can pretty much be anywhere, and called anything.

    Properties properties = new Properties();
    try {
      properties.load(new FileInputStream("path/filename"));
    } catch (IOException e) {
      ...
    }
    

    Iterate as:

    for(String key : properties.stringPropertyNames()) {
      String value = properties.getProperty(key);
      System.out.println(key + " => " + value);
    }
    

提交回复
热议问题