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

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中類的加載過程?。。?/p>

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)文章

最新評論

拜城县| 通河县| 桃源县| 保康县| 大竹县| 新竹市| 明溪县| 周宁县| 密云县| 论坛| 阿拉善左旗| 托克逊县| 黑山县| 杭州市| 宝丰县| 泉州市| 卢氏县| 普陀区| 唐海县| 桂林市| 阿鲁科尔沁旗| 女性| 潮安县| 金湖县| 广州市| 辛集市| 丹巴县| 丰县| 东源县| 泗阳县| 赣榆县| 崇明县| 伊金霍洛旗| 灵璧县| 柳河县| 武宣县| 菏泽市| 冕宁县| 三门峡市| 洱源县| 名山县|