溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何解析Java加載property文件配置過(guò)程

發(fā)布時(shí)間:2021-10-15 15:34:39 來(lái)源:億速云 閱讀:114 作者:柒染 欄目:編程語(yǔ)言

本篇文章為大家展示了如何解析Java加載property文件配置過(guò)程,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

1 properties簡(jiǎn)介:

properties是一種文本文件,內(nèi)容格式為:

key = value #單行注釋

適合作為簡(jiǎn)單配置文件使用,通常作為參數(shù)配置、國(guó)際化資源文件使用。

對(duì)于復(fù)雜的配置,就需要使用XML、YML、JSON等了

2 java加載Properties:

java加載properties主要通過(guò)2個(gè)util包下的工具類(lèi): Properties類(lèi)、 ResourceBundle類(lèi)

2.1 通過(guò)Properties類(lèi)加載:

Properties類(lèi)通過(guò)load()方法加載配置信息,這個(gè)方法接收一個(gè)輸入流參數(shù):InputStream、Reader。

Properties提供get(String key) 方法讀取指定的配置項(xiàng)。

2.1.1 通過(guò)ClassLoader獲取InputStream加載:

該方式只能讀取類(lèi)路徑下的配置文件

(1)先創(chuàng)建Properties對(duì)象

(2)獲取property文件對(duì)應(yīng)的輸入流in

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

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

/** * 基于ClassLoader讀取properties; * 該方式只能讀取類(lèi)路徑下的配置文件,有局限但是如果配置文件在類(lèi)路徑下比較方便。 */public static void method1() {   System.out.println("使用ClassLoader方式加載properties");   Properties properties = new Properties();   /*   * 使用ClassLoader加載properties配置文件生成對(duì)應(yīng)的輸入流。   * 使用此種方式,要求property文件必須要放在src目錄下,編譯之后會(huì)被放到class文件相同目錄下。   * 因?yàn)镃lassLoad的基礎(chǔ)路徑是相對(duì)于編譯后class文件所在目錄(可能是bin,classes),如果將properties放在項(xiàng)目根目錄下,使用此種方式可能會(huì)找不到 properties文件   */   //必須要以 /開(kāi)始  , 是在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 通過(guò)構(gòu)建Reader加載:

該方式的優(yōu)點(diǎn)在于可以讀取任意路徑下的配置文件

(1)創(chuàng)建Properties對(duì)象

(2)創(chuàng)建一個(gè)Reader,可以指定路徑,不局限于類(lèi)路徑

(3)使用Properties加載Reader

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

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

ResourceBundle讀取配置有2種方式:

(1)指定文件路徑 :相對(duì)于src、classes的相對(duì)路徑

(2)提供InputStream

ResourceBundle提供方法getString(key) 和 getObject(key)讀取配置項(xiàng)使用路徑加載,不需要指定文件后綴名,且不需要手動(dòng)關(guān)閉相關(guān)資源,比Properties類(lèi)操作要簡(jiǎn)單。

/** * 通過(guò)ResourceBundle 讀取配置, 此種方式項(xiàng)目Properties要簡(jiǎn)單 * ResourceBundle讀取配置有2種方式: (1)指定文件路徑 (2)提供InputStream */public static void method3() {  System.out.println("使用ResourceBundle方式加載properties");  /*  * (1)直接指定文件路徑:  * 可以使用相對(duì)路徑,從類(lèi)路徑開(kāi)始,且不需要指定properties文件的后綴  */  ResourceBundle resource = ResourceBundle.getBundle("properties/b");  System.out.println(resource.getString("name"));  System.out.println(resource.getString("age"));   try {   /*   * (2)通過(guò)InputStream加載配置:   * 通過(guò)當(dāng)前類(lèi)的class實(shí)例,獲取資源輸入流   */   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();  }}

上述內(nèi)容就是如何解析Java加載property文件配置過(guò)程,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI