Several Methods for java to read the. properties configuration file: java. properties
Reading the. properties configuration file is much used in actual development. To sum up, there are several methods (I only know ):
1. java. util. Properties class provided by jdk
This class is inherited from java. util. hashTable implements the Map interface. Therefore, you can use corresponding methods to operate attribute files. However, we do not recommend using methods such as put and putAll, because the put method can not only store values of the String type, but also objects. Therefore, java. util. the Properties class provides the getProperty () and setProperty () Methods to operate on the property file, and uses store or save (obsolete) to save the property value (write the property value. properties configuration file ). Before using it, you also need to load the attribute file. It provides two methods: load and loadFromXML.
Load has two methods: load (InputStream inStream) and load (Reader reader). Therefore, you can load attribute files in different ways.
You can obtain InputStream in different ways, such:
1. Get it through the getResourceAsStream method of the current Class Loader
Java code
- InputStream inStream = TestProperties. class. getClassLoader (). getResourceAsStream ("test. properties ");
2. Get from File
Java code
- InputStream inStream = new FileInputStream (new File ("filePath "));
3. It is also obtained through the class loader, which is the same as the first one.
Java code
- InputStream in = ClassLoader. getSystemResourceAsStream ("filePath ");
4. In servlet, you can also use context to obtain InputStream
Java code
- InputStream in = context. getResourceAsStream ("filePath ");
5. Get through URL
Java code
- URL url = new URL ("path ");
- InputStream inStream = url. openStream ();
The read method is as follows:
Java code
- Properties prop = new Properties ();
- Prop. load (inStream );
- String key = prop. getProperty ("username ");
- // String key = (String) prop. get ("username ");
Ii. Read through java. util. ResourceBundle class
This method is more convenient than using Properties.
1. Obtain it through the ResourceBundle. getBundle () Static Method
ResourceBundle is an abstract class. To obtain the properties attribute file in this way, you do not need to add a. properties suffix, but only need the file name.
Java code
- ResourceBundle resource = ResourceBundle. getBundle ("com/mmq/test"); // test is the attribute file name, which is placed under the package com. mmq. If it is placed under src, use test directly.
- String key = resource. getString ("username ");
2. Read from InputStream
The method for getting InputStream is the same as above.
Java code
- ResourceBundle resource = new PropertyResourceBundle (inStream );
Note: The biggest problem encountered during use may be the path of the configuration file. If the configuration file is included in the package of the current class, use the package name limitation, for example, test. properties. in the mmq package, use com/mmq/test. properties (obtained through Properties) or com/mmq/test (obtained through ResourceBundle). If the property file is in the src root directory, use test directly. properties or test.
Iii. Instances
ResourceLoader. java
Java code
- Package com. bijian. study;
- Import java. io. File;
- Import java. io. FileInputStream;
- Import java. util. HashMap;
- Import java. util. Map;
- Import java. util. Properties;
- Public final class ResourceLoader {
- Private static ResourceLoader loader = new ResourceLoader ();
- Private static Map <String, Properties> loaderMap = new HashMap <String, Properties> ();
- Private ResourceLoader (){
- }
- Public static ResourceLoader getInstance (){
- Return loader;
- }
- Public Properties getPropFromProperties (String fileName) throws Exception {
- Properties prop = loaderMap. get (fileName );
- If (prop! = Null ){
- Return prop;
- }
- String filePath = null;
- String configPath = System. getProperty ("configurePath ");
- If (configPath = null ){
- FilePath = this. getClass (). getClassLoader (). getResource (fileName). getPath ();
- } Else {
- FilePath = configPath + "/" + fileName;
- }
- Prop = new Properties ();
- Prop. load (new FileInputStream (new File (filePath )));
- LoaderMap. put (fileName, prop );
- Return prop;
- }
- }
PropertiesUtil. java
Java code
- Package com. bijian. study;
- Import java. util. Properties;
- Import java. util. concurrent. ConcurrentHashMap;
- Import java. util. concurrent. ConcurrentMap;
- /**
- * Use ConcurrentMap to cache the key-value of the property File
- */
- Public class PropertiesUtil {
- Private static ResourceLoader loader = ResourceLoader. getInstance ();
- Private static ConcurrentMap <String, String> configMap = new ConcurrentHashMap <String, String> ();
- Private static final String DEFAULT_CONFIG_FILE = "test. properties ";
- Private static Properties prop = null;
- Public static String getStringByKey (String key, String propName ){
- Try {
- Prop = loader. getPropFromProperties (propName );
- } Catch (Exception e ){
- Throw new RuntimeException (e );
- }
- Key = key. trim ();
- If (! ConfigMap. containsKey (key )){
- If (prop. getProperty (key )! = Null ){
- ConfigMap. put (key, prop. getProperty (key ));
- }
- }
- Return configMap. get (key );
- }
- Public static String getStringByKey (String key ){
- Return getStringByKey (key, DEFAULT_CONFIG_FILE );
- }
- Public static Properties getProperties (){
- Try {
- Return loader. getPropFromProperties (DEFAULT_CONFIG_FILE );
- } Catch (Exception e ){
- E. printStackTrace ();
- Return null;
- }
- }
- }
Constant. java
Java code
- Package com. bijian. study;
- Public class Constant {
- Public static final String TEST = PropertiesUtil. getStringByKey ("test", "test. properties ");
- }
Main. java
Java code
- Package com. bijian. study;
- Public class Main {
- Public static void main (String [] args ){
- System. out. println (Constant. TEST );
- }
- }