Java加載properties文件實現(xiàn)方式詳解
java加載properties文件的方式主要分為兩大類:一種是通過import java.util.Properties類中的load(InputStream in)方法加載;
另一種是通過import java.util.ResourceBundle類的getBundle(String baseName)方法加載。
注意:一定要區(qū)分路徑格式
實現(xiàn)代碼如下:
package com.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; public class PropertiesUtil { private static String basePath = "src/prop.properties"; private static String name = ""; private static String nickname = ""; private static String password = ""; /** * 一、 使用java.util.Properties類的load(InputStream in)方法加載properties文件 * */ public static String getName1() { try { Properties prop = new Properties(); InputStream is = new FileInputStream(basePath); prop.load(is); name = prop.getProperty("username"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return name; } /** * 二、 使用class變量的getResourceAsStream()方法 * 注意:getResourceAsStream()讀取路徑是與本類的同一包下 * */ public static String getName2() { Properties prop = new Properties(); InputStream is = PropertiesUtil.class .getResourceAsStream("/com/util/prop.properties"); try { prop.load(is); name = prop.getProperty("username"); } catch (IOException e) { e.printStackTrace(); } return name; } /** * 三、 * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 * getResourceAsStream(name)方法的參數(shù)必須是包路徑+文件名+.后綴 否則會報空指針異常 * */ public static String getName3() { Properties prop = new Properties(); InputStream is = PropertiesUtil.class.getClassLoader() .getResourceAsStream("com/util/prop.properties"); try { prop.load(is); } catch (IOException e) { e.printStackTrace(); } return name; } /** * 四、 使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態(tài)方法 * getSystemResourceAsStream()方法的參數(shù)格式也是有固定要求的 * */ public static String getName4() { Properties prop = new Properties(); InputStream is = ClassLoader .getSystemResourceAsStream("com/util/prop.properties"); try { prop.load(is); name = prop.getProperty("username"); } catch (IOException e) { e.printStackTrace(); } return name; } /** * 五、 使用java.util.ResourceBundle類的getBundle()方法 * 注意:這個getBundle()方法的參數(shù)只能寫成包路徑+properties文件名,否則將拋異常 * */ public static String getName5() { ResourceBundle rb = ResourceBundle.getBundle("com/util/prop"); password = rb.getString("password"); return password; } /** * 六、 使用java.util.PropertyResourceBundle類的構造函數(shù) * */ public static String getName6() { try { InputStream is = new FileInputStream(basePath); ResourceBundle rb = new PropertyResourceBundle(is); nickname = rb.getString("nickname"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return nickname; } /** * 測試 * */ public static void main(String[] args) { System.out.println("name1:" + PropertiesUtil.getName1()); System.out.println("name2:" + PropertiesUtil.getName2()); System.out.println("name3:" + PropertiesUtil.getName3()); System.out.println("name4:" + PropertiesUtil.getName4()); System.out.println("password:" + PropertiesUtil.getName5()); System.out.println("nickname:" + PropertiesUtil.getName6()); } }
文件路徑:
prop.properties文件:
username=mamama
nickname=xiaoma
password=123456
輸出結果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Unity&Springboot實現(xiàn)本地登陸驗證
本文主要介紹了Unity&Springboot服務器/本地登陸驗證,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07客戶端Socket與服務端ServerSocket串聯(lián)實現(xiàn)網(wǎng)絡通信
這篇文章主要為大家介紹了客戶端Socket與服務端ServerSocket串聯(lián)實現(xiàn)網(wǎng)絡通信的內容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03Java向數(shù)據(jù)庫中插入數(shù)據(jù)后獲取自增ID的常用方法
有時候因為新增的需求需要獲取剛剛新增的數(shù)據(jù)的自增的主鍵ID,下面這篇文章主要給大家介紹了關于Java向數(shù)據(jù)庫中插入數(shù)據(jù)后獲取自增ID的常用方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-11-11xxl-job 帶參數(shù)執(zhí)行和高可用部署方法
這篇文章主要介紹了xxl-job 帶參數(shù)執(zhí)行和高可用部署,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04Java8新的異步編程方式CompletableFuture實現(xiàn)
這篇文章主要介紹了Java8新的異步編程方式CompletableFuture實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04Java使用PrepareStatement實現(xiàn)數(shù)據(jù)的插入與查詢操作
這篇文章主要為大家詳細介紹了Java如何使用PrepareStatement實現(xiàn)數(shù)據(jù)的插入與查詢操作,文中的示例代碼講解詳細,感興趣的可以了解一下2022-09-09如何使用@AllArgsConstructor和final 代替 @Autowired
這篇文章主要介紹了使用@AllArgsConstructor和final 代替 @Autowired方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09