如何解析Java加載property文件配置過程
本篇文章為大家展示了如何解析Java加載property文件配置過程,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)是一家專業(yè)提供寬甸企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為寬甸眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
1 properties簡介:
properties是一種文本文件,內(nèi)容格式為:
key = value #單行注釋
適合作為簡單配置文件使用,通常作為參數(shù)配置、國際化資源文件使用。
對于復(fù)雜的配置,就需要使用XML、YML、JSON等了
2 java加載Properties:
java加載properties主要通過2個util包下的工具類: Properties類、 ResourceBundle類
2.1 通過Properties類加載:
Properties類通過load()方法加載配置信息,這個方法接收一個輸入流參數(shù):InputStream、Reader。
Properties提供get(String key) 方法讀取指定的配置項(xiàng)。
2.1.1 通過ClassLoader獲取InputStream加載:
該方式只能讀取類路徑下的配置文件
(1)先創(chuàng)建Properties對象
(2)獲取property文件對應(yīng)的輸入流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配置文件生成對應(yīng)的輸入流。 * 使用此種方式,要求property文件必須要放在src目錄下,編譯之后會被放到class文件相同目錄下。 * 因?yàn)镃lassLoad的基礎(chǔ)路徑是相對于編譯后class文件所在目錄(可能是bin,classes),如果將properties放在項(xiàng)目根目錄下,使用此種方式可能會找不到 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 通過構(gòu)建Reader加載:
該方式的優(yōu)點(diǎn)在于可以讀取任意路徑下的配置文件
(1)創(chuàng)建Properties對象
(2)創(chuàng)建一個Reader,可以指定路徑,不局限于類路徑
(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 通過ResourceBundle類加載:
ResourceBundle讀取配置有2種方式:
(1)指定文件路徑 :相對于src、classes的相對路徑
(2)提供InputStream
ResourceBundle提供方法getString(key) 和 getObject(key)讀取配置項(xiàng)使用路徑加載,不需要指定文件后綴名,且不需要手動關(guān)閉相關(guān)資源,比Properties類操作要簡單。
/** * 通過ResourceBundle 讀取配置, 此種方式項(xiàng)目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加載配置: * 通過當(dāng)前類的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文件配置過程,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:如何解析Java加載property文件配置過程
路徑分享:http://www.xueling.net.cn/article/pjhcjj.html