idea工具下使用Java讀取配置文件的方式

這裏寫圖片描述

博主的application.properties的內容如下:

#測試數據庫連接信息
test.db.url=jdbc:mysql://localhost:3306/xxxx
test.db.username=xxxx
test.db.password=xxxx

#線上數據庫連接信息
online.db.url=jdbc:mysql://localhost:3306/xxxx
online.db.username=xxxx
online.db.password=xxxx

當需要使用Java程序讀取application.properties配置文件的內容並按照key-value形式讀取時,代碼如下:

 Properties properties = new Properties();
        //當前類
        InputStream inputStream = CompareDB.class.getResourceAsStream("/application.properties");
        properties.load(inputStream);
        String url = properties.getProperty("test.db.url");
        String username = properties.getProperty("test.db.username");
        String password = properties.getProperty("test.db.password");

如果還是無法讀取該屬性,檢查是否已經在idea中將resources配置爲Resources文件夾。
這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章