亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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集成mybatis-plus報sqlSession異常的問題解決

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

    Java強制類型轉換原理詳解(父類轉子類、子類轉父類)

    這篇文章主要給大家介紹了關于Java強制類型轉換原理(父類轉子類、子類轉父類)的相關資料,所謂的強制類型轉換,其實是自動類型轉換的逆過程,在數(shù)據(jù)類型兼容的情況下,將容量大的數(shù)據(jù)類型轉換為容量小的數(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實現(xiàn)桌球小游戲

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

    這篇文章主要為大家詳細介紹了java實現(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常見外部配置文件方式詳析

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

    Gradle 6.6.1 安裝配置的詳細教程

    Gradle是一個基于Apache Ant和Apache Maven概念的項目自動化構建開源工具。這篇文章主要介紹了Gradle 6.6.1 安裝配置的詳細教程,需要的朋友可以參考下
    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實現(xiàn)ip白名單的代碼示例

    SpringBoot集成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

最新評論