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文件夹。
这里写图片描述
这里写图片描述

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