Several methods of java reading.properties configuration file

Reading the .properties configuration file is used a lot in actual development. To sum up, there are the following methods (just what I know):

1. The java.util.Properties class provided by jdk.
This class inherits from java.util.HashTable, which implements the Map interface. Therefore, the corresponding method can be used to operate the property file, but it is not recommended to use the two methods like put and putAll, because the put method not only allows storing the String type The value can also be stored in the Object type. Therefore, the java.util.Properties class provides getProperty() and setProperty() methods to manipulate property files, and use store or save (obsolete) to save property values ​​(write property values ​​to the .properties configuration file). Before use, also need to load the properties file, it provides two methods: load and loadFromXML.
There are two overloads of load: load(InputStream inStream), load(Reader reader), so the properties file can be loaded in different ways.
The InputStream can be obtained in different ways, such as:

1. Obtained through the getResourceAsStream method of the current class loader
InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("test.properties");


2. Get from file
InputStream inStream = new FileInputStream(new File("filePath"));


3. It is also obtained through the class loader, the same as the first one
InputStream in = ClassLoader.getSystemResourceAsStream("filePath");


4. In servlet, InputStream can also be obtained through context
InputStream in = context.getResourceAsStream("filePath");


5. Get through URL
URL url = new URL("path");
InputStream inStream = url.openStream();


The reading method is as follows:
Properties prop = new Properties();
prop.load(inStream);
String key = prop.getProperty("username");
//String key = (String) prop.get("username");



Second, read through the java.util.ResourceBundle class, which is more convenient than using Properties.

1. Obtain through the ResourceBundle.getBundle() static method (ResourceBundle is an abstract class). This way to obtain the properties property file does not need to add the .properties suffix name, only the file name is required.
ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");//test is the attribute file name, placed under the package com.mmq, if it is placed under src, just use test directly
String key = resource.getString("username");


2. The method of reading from the InputStream and obtaining the InputStream is the same as the above, so I won't repeat it.
ResourceBundle resource = new PropertyResourceBundle(inStream);


Note: The biggest problem encountered in use may be the path of the configuration file. If the configuration file is under the package where the current class is located, it needs to be qualified by the package name, such as: test.properties is under the com.mmq package, then To use com/mmq/test.properties (obtained through Properties) or com/mmq/test (obtained through ResourceBundle); the properties file is in the src root directory, just use test.properties or test directly.

Reference URL
http://blog.csdn.net/mhmyqn/article/details/7683909

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326768714&siteId=291194637