Read. Properties configuration file in the actual development of the use of a lot, summed up a bit, there are several methods (just I know):
First, the Java.util.Properties class provided through the JDK.
This class inherits from Java.util.HashTable, which implements the map interface, so you can manipulate the properties file using the appropriate method, but it is not recommended to use two methods like put, Putall, because the put method not only allows to deposit string type value, It can also be stored in object type. So the Java.util.Properties class provides the GetProperty () and SetProperty () methods to manipulate the properties file while using store or save (obsolete) To save the property value (write the property value to the. Properties profile). Before using it, you also need to load the properties file, which provides two methods: Load and LoadFromXML.
Load has two methods of overloading: Load (inputstream instream), Load (reader reader), so you can load the properties file in different ways.
InputStream can be obtained according to different ways, such as:
1, through the current class loader getResourceAsStream method to obtain
[Java]View PlainCopy
- InputStream instream = testproperties. Class.getclassloader (). getResourceAsStream ("test.properties");
2. Get from File
[Java]View PlainCopy
- InputStream instream = new FileInputStream (new File ("FilePath"));
3, also through the class loader to obtain, and the first kind of
[Java]View PlainCopy
- InputStream in = Classloader.getsystemresourceasstream ("FilePath");
4, in the servlet, you can also use the context to get InputStream
[Java]View PlainCopy
- InputStream in = Context.getresourceasstream ("FilePath");
5, through the URL to get
[Java]View PlainCopy
- URL url = new URL ("path");
- InputStream instream = Url.openstream ();
The Read method is as follows:
[Java]View PlainCopy
- Properties prop = new properties ();
- Prop.load (instream);
- String key = Prop.getproperty ("username");
- String key = (string) prop.get ("username");
Second, through the Java.util.ResourceBundle class to read, this way than the use of properties to facilitate some.
1, by Resourcebundle.getbundle () static method to obtain (ResourceBundle is an abstract class), this way to get the Properties property file does not need to add the. Properties suffix name, only the file name is required.
[Java]View PlainCopy
- ResourceBundle resource = Resourcebundle.getbundle ("com/mmq/test"); Test is the property file name, placed under the package COM.MMQ, if it is placed under SRC, directly with test can
- String key = resource.getstring ("username");
2, read from the InputStream, get InputStream method and the same, no longer repeat.
[Java]View PlainCopy
- ResourceBundle resource = new propertyResourceBundle (instream);
Note: The biggest problem encountered in the use of the configuration file may be the path problem, if the configuration file in the current class is located under the package, you need to use the package name qualification, such as: Test.properties into the COM.MMQ package, you want to use com/mmq/ Test.properties (obtained through properties) or Com/mmq/test (obtained by ResourceBundle); The properties file is in the SRC root, and you can use test.properties or test directly.
Java read. Properties configuration file Several methods