Several Methods for java to read the. properties configuration file: java. properties

Source: Internet
Author: User

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
  1. InputStream inStream = TestProperties. class. getClassLoader (). getResourceAsStream ("test. properties ");

2. Get from File

Java code
  1. 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
  1. InputStream in = ClassLoader. getSystemResourceAsStream ("filePath ");

4. In servlet, you can also use context to obtain InputStream

Java code
  1. InputStream in = context. getResourceAsStream ("filePath ");

5. Get through URL

Java code
  1. URL url = new URL ("path ");
  2. InputStream inStream = url. openStream ();

The read method is as follows:

Java code
  1. Properties prop = new Properties ();
  2. Prop. load (inStream );
  3. String key = prop. getProperty ("username ");
  4. // 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
  1. 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.
  2. String key = resource. getString ("username ");

2. Read from InputStream

The method for getting InputStream is the same as above.

Java code
  1. 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
  1. Package com. bijian. study;
  2. Import java. io. File;
  3. Import java. io. FileInputStream;
  4. Import java. util. HashMap;
  5. Import java. util. Map;
  6. Import java. util. Properties;
  7. Public final class ResourceLoader {
  8. Private static ResourceLoader loader = new ResourceLoader ();
  9. Private static Map <String, Properties> loaderMap = new HashMap <String, Properties> ();
  10. Private ResourceLoader (){
  11. }
  12. Public static ResourceLoader getInstance (){
  13. Return loader;
  14. }
  15. Public Properties getPropFromProperties (String fileName) throws Exception {
  16. Properties prop = loaderMap. get (fileName );
  17. If (prop! = Null ){
  18. Return prop;
  19. }
  20. String filePath = null;
  21. String configPath = System. getProperty ("configurePath ");
  22. If (configPath = null ){
  23. FilePath = this. getClass (). getClassLoader (). getResource (fileName). getPath ();
  24. } Else {
  25. FilePath = configPath + "/" + fileName;
  26. }
  27. Prop = new Properties ();
  28. Prop. load (new FileInputStream (new File (filePath )));
  29. LoaderMap. put (fileName, prop );
  30. Return prop;
  31. }
  32. }

PropertiesUtil. java

Java code
  1. Package com. bijian. study;
  2. Import java. util. Properties;
  3. Import java. util. concurrent. ConcurrentHashMap;
  4. Import java. util. concurrent. ConcurrentMap;
  5. /**
  6. * Use ConcurrentMap to cache the key-value of the property File
  7. */
  8. Public class PropertiesUtil {
  9. Private static ResourceLoader loader = ResourceLoader. getInstance ();
  10. Private static ConcurrentMap <String, String> configMap = new ConcurrentHashMap <String, String> ();
  11. Private static final String DEFAULT_CONFIG_FILE = "test. properties ";
  12. Private static Properties prop = null;
  13. Public static String getStringByKey (String key, String propName ){
  14. Try {
  15. Prop = loader. getPropFromProperties (propName );
  16. } Catch (Exception e ){
  17. Throw new RuntimeException (e );
  18. }
  19. Key = key. trim ();
  20. If (! ConfigMap. containsKey (key )){
  21. If (prop. getProperty (key )! = Null ){
  22. ConfigMap. put (key, prop. getProperty (key ));
  23. }
  24. }
  25. Return configMap. get (key );
  26. }
  27. Public static String getStringByKey (String key ){
  28. Return getStringByKey (key, DEFAULT_CONFIG_FILE );
  29. }
  30. Public static Properties getProperties (){
  31. Try {
  32. Return loader. getPropFromProperties (DEFAULT_CONFIG_FILE );
  33. } Catch (Exception e ){
  34. E. printStackTrace ();
  35. Return null;
  36. }
  37. }
  38. }

Constant. java

Java code
  1. Package com. bijian. study;
  2. Public class Constant {
  3. Public static final String TEST = PropertiesUtil. getStringByKey ("test", "test. properties ");
  4. }

Main. java

Java code
  1. Package com. bijian. study;
  2. Public class Main {
  3. Public static void main (String [] args ){
  4. System. out. println (Constant. TEST );
  5. }
  6. }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.