java加載property文件配置


1 properties簡介:


properties是一種文本文件,內容格式為:
     key = value     #單行注釋
適合作為簡單配置文件使用,通常作為參數配置、國際化資源文件使用。
對於復雜的配置,就需要使用XML、YML、JSON等了

2 java加載Properties:


    java加載properties主要通過2個util包下的工具類: Properties類、 ResourceBundle類

2.1 通過Properties類加載:

    Properties類通過load()方法加載配置信息,這個方法接收一個輸入流參數:InputStream、Reader。
     Properties提供get(String key) 方法讀取指定的配置項。

2.1.1 通過ClassLoader獲取InputStream加載:

   該方式只能讀取類路徑下的配置文件

(1)先創建Properties對象

(2)獲取property文件對應的輸入流in

(3)使用Properties加載輸入流in

(4)通過Properties.get(key)方法獲取配置,如果配置信息不存在,則返回null

/**
  * 基於ClassLoader讀取properties;
  * 該方式只能讀取類路徑下的配置文件,有局限但是如果配置文件在類路徑下比較方便。
  */
public static void method1() {
     System.out.println("使用ClassLoader方式加載properties");
     Properties properties = new Properties();
     /*
      * 使用ClassLoader加載properties配置文件生成對應的輸入流。
      * 使用此種方式,要求property文件必須要放在src目錄下,編譯之后會被放到class文件相同目錄下。
      * 因為ClassLoad的基礎路徑是相對於編譯后class文件所在目錄(可能是bin,classes),如果將properties放在項目根目錄下,使用此種方式可能會找不到 properties文件
      */
     //必須要以 /開始    , 是在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"));
}

2.1.2 通過構建Reader加載:

該方式的優點在於可以讀取任意路徑下的配置文件

(1)創建Properties對象

(2)創建一個Reader,可以指定路徑,不局限於類路徑

(3)使用Properties加載Reader

(4)Properties.get(key)方法讀取配置

/**
  * 使用inputStream讀取配置文件
  * 該方式的優點在於可以讀取任意路徑下的配置文件
  */
public static void method2() {
     System.out.println("使用InputStream方式加載properties");
     Properties properties = new Properties();
      BufferedReader reader = null;
      try {
          /*
           * System.getProperty("user.dir"): 獲取項目根路徑,  而后附加配置文件的路徑
          */
          reader = new BufferedReader(new FileReader(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 通過ResourceBundle類加載:


ResourceBundle讀取配置有2種方式:

   (1)指定文件路徑 :相對於src、classes的相對路徑
    (2)提供InputStream
      ResourceBundle提供方法getString(key) 和 getObject(key)讀取配置項
     使用路徑加載,不需要指定文件后綴名且不需要手動關閉相關資源,比Properties類操作要簡單

/**
  * 通過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();
     }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM