博主的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文件夾。