Java讀取Properties配置文件的6種方式匯總
Java讀取Properties的方式
項目結構:經(jīng)典的maven項目結構
配置文件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 { //不加/會從當前包進行尋找,加上/會從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.當前類的加載器進行讀取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() 從當前類所在的位置開始查找配置文件位置。要找到jdbc.properties和jdbc2.properties必須加/從classpath下開始查找
Class.getClassLoader().getResourceAsStream() 默認就從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")); }
總結
到此這篇關于Java讀取Properties配置文件的6種方式的文章就介紹到這了,更多相關Java讀取Properties配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot3集成mybatis-plus報sqlSession異常的問題解決
springboot3已經(jīng)發(fā)布正式版,但是在集成mybatis-plus最新版3.5.2的時候發(fā)現(xiàn)提示異常,本文就來介紹一下報sqlSession異常的問題解決,具有一定的參考價值,感興趣的可以了解一下2024-02-02JavaBean和SpringBean的區(qū)別及創(chuàng)建SpringBean方式
這篇文章主要介紹了JavaBean和SpringBean的區(qū)別及創(chuàng)建SpringBean方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10request如何獲取body的json數(shù)據(jù)
這篇文章主要介紹了request如何獲取body的json數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06解決java.sql.SQLException:The?server?time?zone?value?&apo
這篇文章主要介紹了解決java.sql.SQLException:The?server?time?zone?value?'?D1ú±ê×?ê±??'?is?unrecognized問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03SpringBoot集成ip2region實現(xiàn)ip白名單的代碼示例
ip2region v2.0 - 是一個離線IP地址定位庫和IP定位數(shù)據(jù)管理框架,10微秒級別的查詢效率,提供了眾多主流編程語言的 xdb 數(shù)據(jù)生成和查詢客戶端實現(xiàn),本文介紹了SpringBoot集成ip2region實現(xiàn)ip白名單的代碼工程,需要的朋友可以參考下2024-08-08