Java foundation of reading the configuration file properties

A substantially .Properties class definitions:
the Properties class represents a persistent set of properties. Properties can be saved or loaded from the stream in the stream. Each key and its corresponding value of the property list is a string.

A property list may contain a list of other properties as its "default values"; if not in the list of search key attribute to the original attribute, the second attribute search list.

Because Properties inherits from Hashtable, the put and can be applied putAll methods Properties object. However, these methods are not recommended because they allow the caller to insert entries whose keys or values ​​are not a String. Instead, use the setProperty method. If (that contains a non- String key or value) in a "compromised" Properties object to call the store or save method, the call will fail. Similarly, if the "unsafe" Properties object (that contains a non- String key) method on the list, or call propertyNames, the call will fail.

Two common methods:.
①getProperty (String Key)
search attributes in this property list with the specified key.
②load (InputStream inStream)
Reads a property list (key and element pairs) from the input stream.
③propertyNames ()
returns the property list enumerates all the keys of the same name if the key is not found in the main property list, the default property list, including distinct keys.
④setProperty (String key, String value)
calls the Hashtable method put.

Three properties .java read configuration files in several ways

public class PropertiesDemo {
    public static void main(String[] args) throws IOException {
        loadProperties1();
        readProperty2();
        readProperty3();
    }
    //方式一:基于InputStream读取配置文件:
    private static void loadProperties1() throws IOException {
        Properties properties = new Properties();
       // FileInputStream stream = new FileInputStream("db.properties");
        InputStream stream = PropertiesDemo.class.getClassLoader().getResourceAsStream("db.properties");
        properties.load(stream);
        System.out.println(properties.getProperty("username"));
        System.out.println(properties.getProperty("password"));
    }

    //方法二通过Spring中的PropertiesLoaderUtils工具类进行获取:
    private static void readProperty2() {
        Properties properties = new Properties();
        try {
            properties = PropertiesLoaderUtils.loadAllProperties("db.properties");
            System.out.println(new String(properties.getProperty("username").getBytes("iso-8859-1"), "gbk"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方法三通过 java.util.ResourceBundle 类读取:
    private static void readProperty3() {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("db");
        //遍历取值
        Enumeration enumeration = resourceBundle.getKeys();
        while (enumeration.hasMoreElements()) {
            try {
                String value = resourceBundle.getString((String) enumeration.nextElement());
                System.out.println(new String(value.getBytes("iso-8859-1"), "gbk"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }
}
Published 99 original articles · won praise 2 · Views 2592

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105342564