java to load the configuration file property

1 properties Description:


properties is a text file, content format is:
     Key = value     # single-line comments
suitable for use as a simple configuration file, usually configured as a parameter, an international resource files.
For complex configurations, we need to use XML, YML, JSON wait

2 java Load Properties:


    java properties mainly by loading tools in util Package 2: Properties Class, ResourceBundle Class

2.1 Properties class is loaded by:

    Properties class loading configuration information load () method, which receives a stream of input parameters: InputStream, Reader.
     Properties provide get (String key) method reads the specified configuration items.

2.1.1 InputStream get loaded via ClassLoader:

   This method can only read the configuration file class path

(1) create a Properties object

(2) obtain the property file corresponding to the input stream in

(3) Properties loaded in the input stream

(4) arranged acquired by Properties.get (key) method, if the configuration information does not exist, null is returned

/ **
  * based on the read ClassLoader Properties;
  * This embodiment can only read the configuration file class path , but there are limitations in the more convenient if the profile class path.
  * /
Public static void the method1 () {
     System.out.println ( "Use mode ClassLoader loading properties");
     the Properties properties = new new the Properties ();
     / *
      * ClassLoader loading properties using the profile generator corresponding to the input stream.
      * Use this way, requires property files must be placed in the src directory, after compilation will be placed in the same directory class files.
      * Because ClassLoad base path is relative to the directory where the class files compiled (possibly bin, classes), if the properties in the project root directory, use this way could not find the properties file
      * /
     // must be begin with /     , is looking for a file in the root directory of classes
     InputStream in = PropertiesDemo.class.getResourceAsStream ( " /properties/a.properties ");
     System.out.println(PropertiesDemo.class.getClassLoader().getResource(".").getPath());
     try {
        properties.load(in);
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }finally {
         closeResource(in);
     }
    
     System.out.println(properties.get("name"));
     System.out.println(properties.get("age"));
}

By building Reader 2.1.2 loading:

An advantage of this embodiment is that the configuration file can be read in any path

(1) create a Properties object

(2) create a Reader, you can specify the path, the path is not limited to class

(3) Use Properties loaded Reader

(4) Properties.get (key) method reads the configuration

/ **
  * reads the configuration file using inputStream
  * The advantage of this embodiment is that the configuration file can be read at an arbitrary path
  * /
public static void method2 () {
     System.out.println ( "load mode an InputStream Properties");
     the Properties = the Properties new new Properties ();
      the BufferedReader Reader = null;
      the try {
          / *
           * the System.getProperty ( "user.dir"): Get the project root path, then additional configuration file path
          * /
          Reader the BufferedReader new new = (the FileReader new new (the System .getProperty ( "user.dir") + "/properties/a.properties"));
          Properties.load (Reader);
          System.out.println (properties.get ( "name"));
          System.out.println ( properties.get ( "age"));
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }finally {
          closeResource(reader);
      }
  }

 

2.2 by ResourceBundle class loader:


ResourceBundle read disposed in two ways:

   (1) the specified file path: with respect src, classes relative path
    (2) providing the InputStream
      ResourceBundle provides a method getString (key) and getObject (key) to read configuration item
     using the path loads, need not specify a file extension , and does not require manually shut down related resources, simpler operation than the Properties class .

/**
  * 通过ResourceBundle 读取配置, 此种方式项目Properties要简单
  * ResourceBundle读取配置有2种方式: (1)指定文件路径 (2)提供InputStream
  */
public static void method3() {
     System.out.println("使用ResourceBundle方式加载properties");
     /*
      * (1)直接指定文件路径:

     *   可以使用相对路径,从类路径开始,且不需要指定properties文件的后缀
      */
     ResourceBundle resource = ResourceBundle.getBundle("properties/b");
     System.out.println(resource.getString("name"));
     System.out.println(resource.getString("age"));

 


     try {
         /*
          * (2)通过InputStream加载配置:

         *    通过当前类的class实例,获取资源输入流
          */
         resource = new PropertyResourceBundle(PropertiesDemo.class.getResourceAsStream("/properties/a.properties"));
         System.out.println(resource.getString("name"));
         System.out.println(resource.getString("age"));
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
}

Guess you like

Origin www.cnblogs.com/zyj-468161691/p/12299277.html