How to use Java property files?

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

    There are many ways to create and read properties files:

    1. Store the file in the same package.
    2. Recommend .properties extension however you can choose your own.
    3. Use theses classes located at java.util package => Properties, ListResourceBundle, ResourceBundle classes.
    4. To read properties, use iterator or enumerator or direct methods of Properties or java.lang.System class.

    ResourceBundle class:

     ResourceBundle rb = ResourceBundle.getBundle("prop"); // prop.properties
     System.out.println(rb.getString("key"));
    

    Properties class:

    Properties ps = new Properties();
    ps.Load(new java.io.FileInputStream("my.properties"));
    

提交回复
热议问题