最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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類的方法

    這篇文章給大家分享了Java反射獲取所有Controller和RestController類的方法,文中有詳細(xì)的代碼示例講解,具有一定的參考價值,需要的朋友可以參考下
    2023-08-08
  • 網(wǎng)絡(luò)爬蟲案例解析

    網(wǎng)絡(luò)爬蟲案例解析

    本文主要介紹了網(wǎng)絡(luò)爬蟲的小案例。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • SpringBoot整合Nacos注冊中心的實現(xiàn)示例

    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三種倉庫,本地倉庫,私服,中央倉庫的配置

    Java maven三種倉庫,本地倉庫,私服,中央倉庫的配置

    今天給大家簡單介紹Maven三種倉庫的配置,文中有非常詳細(xì)的解釋,對Java初學(xué)者很有幫助喲,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-09-09
  • Java模擬HTTP Get Post請求 輕松實現(xiàn)校園BBS自動回帖

    Java模擬HTTP Get Post請求 輕松實現(xiàn)校園BBS自動回帖

    這篇文章主要介紹了Java模擬HTTP Get Post請求,輕松實現(xiàn)校園BBS自動回帖,感興趣的小伙伴們可以參考一下
    2015-12-12
  • java使用UDP實現(xiàn)點對點通信

    java使用UDP實現(xiàn)點對點通信

    這篇文章主要為大家詳細(xì)介紹了java使用UDP實現(xiàn)點對點通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java中Collection遍歷的幾種方式

    Java中Collection遍歷的幾種方式

    本文主要介紹了Java中Collection遍歷的幾種方式,包括迭代器遍歷、增強for循環(huán)和forEach遍歷,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-02-02
  • 詳解Java?SSM項目部署上線配置方法(阿里云服務(wù)器ECS?+?云數(shù)據(jù)庫RDS?MySQL)(寶塔)

    詳解Java?SSM項目部署上線配置方法(阿里云服務(wù)器ECS?+?云數(shù)據(jù)庫RDS?MySQL)(寶塔)

    這篇文章主要介紹了Java?SSM項目部署上線(阿里云服務(wù)器ECS?+?云數(shù)據(jù)庫RDS?MySQL)(寶塔)的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-01-01
  • Springmvc DispatcherServlet原理及用法解析

    Springmvc DispatcherServlet原理及用法解析

    這篇文章主要介紹了Springmvc DispatcherServlet原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • Java生成唯一id的幾種實現(xiàn)方式

    Java生成唯一id的幾種實現(xiàn)方式

    本文主要介紹了Java生成唯一id的幾種實現(xiàn)方式,主要介紹了5種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01

最新評論

边坝县| 东平县| 泰宁县| 昌宁县| 沐川县| 嘉鱼县| 耒阳市| 崇阳县| 怀柔区| 美姑县| 长丰县| 长武县| 京山县| 固镇县| 澄城县| 黄浦区| 平罗县| 福泉市| 北票市| 海丰县| 舒城县| 田东县| 汝城县| 基隆市| 辽源市| 来宾市| 攀枝花市| 漳浦县| 长顺县| 油尖旺区| 香格里拉县| 惠水县| 新闻| 蒙自县| 道真| 宣恩县| 梅河口市| 渝中区| 陇西县| 临澧县| 株洲县|