Java讀取Properties配置文件的6種方式匯總
Java讀取Properties的方式
項目結(jié)構(gòu):經(jīng)典的maven項目結(jié)構(gòu)

配置文件1和2內(nèi)容一致:
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=123456
1. this.getClass().getResourceAsStream()
//讀取配置文件1
public void readProperties1() throws IOException {
//不加/會從當(dāng)前包進(jìn)行尋找,加上/會從src開始找
InputStream inputStream = this.getClass().getResourceAsStream("/jdbc.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
//讀取配置文件2
public void readProperties1() throws IOException {
InputStream inputStream = this.getClass().getResourceAsStream("/config/jdbc2.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}2.當(dāng)前類的加載器進(jìn)行讀取this.getClass().getClassLoader().getResourceAsStream()
//讀取配置文件1
public void readProperties2() throws IOException {
//不加/,若加了會為null
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
//讀取配置文件2
public void readProperties2() throws IOException {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/jdbc2.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
方法1和2區(qū)別: (classpath即為target/classes 這個目錄)
Class.getResourceAsStream() 從當(dāng)前類所在的位置開始查找配置文件位置。要找到j(luò)dbc.properties和jdbc2.properties必須加/從classpath下開始查找
Class.getClassLoader().getResourceAsStream() 默認(rèn)就從classpath路徑下開始查找,加上/會報空指針
十分有必要知道java中類的加載過程?。?!
3. ClassLoader類的static方法 getSystemResourceAsStream()
public void readProperties3() throws IOException {
//InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/jdbc2.properties");
InputStream inputStream = ClassLoader.getSystemResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}4. Spring中的 ClassPathResource讀取
public void readProperties4() throws IOException {
//ClassPathResource resource = new ClassPathResource("jdbc.properties");
ClassPathResource resource = new ClassPathResource("config/jdbc2.properties");
Properties properties= PropertiesLoaderUtils.loadProperties(resource);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}5. PropertyResourceBundle讀取InputStream流
public void readProperties5() throws IOException {
//InputStream inputStream = ClassLoader.getSystemResourceAsStream("jdbc.properties");
InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/jdbc2.properties");
PropertyResourceBundle bundle = new PropertyResourceBundle(inputStream);
System.out.println(bundle.getString("jdbc.driver"));
System.out.println(bundle.getString("jdbc.url"));
System.out.println(bundle.getString("jdbc.username"));
System.out.println(bundle.getString("jdbc.password"));
}6.ResourceBundle.getBundle()
//不用輸入后綴
public void readProperties6() {
//ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
ResourceBundle bundle=ResourceBundle.getBundle("config/jdbc2");
System.out.println(bundle.getString("jdbc.driver"));
System.out.println(bundle.getString("jdbc.url"));
System.out.println(bundle.getString("jdbc.username"));
System.out.println(bundle.getString("jdbc.password"));
}總結(jié)
到此這篇關(guān)于Java讀取Properties配置文件的6種方式的文章就介紹到這了,更多相關(guān)Java讀取Properties配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java反射獲取所有Controller和RestController類的方法
這篇文章給大家分享了Java反射獲取所有Controller和RestController類的方法,文中有詳細(xì)的代碼示例講解,具有一定的參考價值,需要的朋友可以參考下2023-08-08
SpringBoot整合Nacos注冊中心的實現(xiàn)示例
本文通過Nacos實現(xiàn)SpringBoot與SpringCloudGateway的配置熱更新、共享配置及動態(tài)路由,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-07-07
Java maven三種倉庫,本地倉庫,私服,中央倉庫的配置
今天給大家簡單介紹Maven三種倉庫的配置,文中有非常詳細(xì)的解釋,對Java初學(xué)者很有幫助喲,需要的朋友可以參考下,希望能夠給你帶來幫助2021-09-09
Java模擬HTTP Get Post請求 輕松實現(xiàn)校園BBS自動回帖
這篇文章主要介紹了Java模擬HTTP Get Post請求,輕松實現(xiàn)校園BBS自動回帖,感興趣的小伙伴們可以參考一下2015-12-12
詳解Java?SSM項目部署上線配置方法(阿里云服務(wù)器ECS?+?云數(shù)據(jù)庫RDS?MySQL)(寶塔)
這篇文章主要介紹了Java?SSM項目部署上線(阿里云服務(wù)器ECS?+?云數(shù)據(jù)庫RDS?MySQL)(寶塔)的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-01-01
Springmvc DispatcherServlet原理及用法解析
這篇文章主要介紹了Springmvc DispatcherServlet原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09

