欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java讀取Properties配置文件的6種方式匯總

 更新時間:2023年07月22日 08:54:35   作者:怡人蝶夢  
這篇文章主要給大家介紹了關(guān)于Java讀取Properties配置文件的6種方式,java中的properties文件是一種配置文件,主要用于表達(dá)配置信息,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

Java讀取Properties的方式

項(xiàng)目結(jié)構(gòu):經(jīng)典的maven項(xiàng)目結(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路徑下開始查找,加上/會報(bào)空指針

十分有必要知道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)文章

  • springboot3集成mybatis-plus報(bào)sqlSession異常的問題解決

    springboot3集成mybatis-plus報(bào)sqlSession異常的問題解決

    springboot3已經(jīng)發(fā)布正式版,但是在集成mybatis-plus最新版3.5.2的時候發(fā)現(xiàn)提示異常,本文就來介紹一下報(bào)sqlSession異常的問題解決,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • Java強(qiáng)制類型轉(zhuǎn)換原理詳解(父類轉(zhuǎn)子類、子類轉(zhuǎn)父類)

    Java強(qiáng)制類型轉(zhuǎn)換原理詳解(父類轉(zhuǎn)子類、子類轉(zhuǎn)父類)

    這篇文章主要給大家介紹了關(guān)于Java強(qiáng)制類型轉(zhuǎn)換原理(父類轉(zhuǎn)子類、子類轉(zhuǎn)父類)的相關(guān)資料,所謂的強(qiáng)制類型轉(zhuǎn)換,其實(shí)是自動類型轉(zhuǎn)換的逆過程,在數(shù)據(jù)類型兼容的情況下,將容量大的數(shù)據(jù)類型轉(zhuǎn)換為容量小的數(shù)據(jù)類型,需要的朋友可以參考下
    2023-12-12
  • JavaBean和SpringBean的區(qū)別及創(chuàng)建SpringBean方式

    JavaBean和SpringBean的區(qū)別及創(chuàng)建SpringBean方式

    這篇文章主要介紹了JavaBean和SpringBean的區(qū)別及創(chuàng)建SpringBean方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 淺談Spring中單例Bean是線程安全的嗎

    淺談Spring中單例Bean是線程安全的嗎

    這篇文章主要介紹了淺談Spring中單例Bean是線程安全的嗎?具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • java實(shí)現(xiàn)桌球小游戲

    java實(shí)現(xiàn)桌球小游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)桌球小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • request如何獲取body的json數(shù)據(jù)

    request如何獲取body的json數(shù)據(jù)

    這篇文章主要介紹了request如何獲取body的json數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Spring Boot常見外部配置文件方式詳析

    Spring Boot常見外部配置文件方式詳析

    這篇文章主要給大家介紹了關(guān)于Spring Boot常見外部配置文件方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Gradle 6.6.1 安裝配置的詳細(xì)教程

    Gradle 6.6.1 安裝配置的詳細(xì)教程

    Gradle是一個基于Apache Ant和Apache Maven概念的項(xiàng)目自動化構(gòu)建開源工具。這篇文章主要介紹了Gradle 6.6.1 安裝配置的詳細(xì)教程,需要的朋友可以參考下
    2020-09-09
  • 解決java.sql.SQLException:The?server?time?zone?value?'?D1ú±ê×?ê±??'?is?unrecognized問題

    解決java.sql.SQLException:The?server?time?zone?value?&apo

    這篇文章主要介紹了解決java.sql.SQLException:The?server?time?zone?value?'?D1ú±ê×?ê±??'?is?unrecognized問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • SpringBoot集成ip2region實(shí)現(xiàn)ip白名單的代碼示例

    SpringBoot集成ip2region實(shí)現(xiàn)ip白名單的代碼示例

    ip2region v2.0 - 是一個離線IP地址定位庫和IP定位數(shù)據(jù)管理框架,10微秒級別的查詢效率,提供了眾多主流編程語言的 xdb 數(shù)據(jù)生成和查詢客戶端實(shí)現(xiàn),本文介紹了SpringBoot集成ip2region實(shí)現(xiàn)ip白名單的代碼工程,需要的朋友可以參考下
    2024-08-08

最新評論