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

来源:互联网 发布:windows.h的头文件 编辑:程序博客网 时间:2024/06/04 18:20

这里写图片描述

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

#测试数据库连接信息test.db.url=jdbc:mysql://localhost:3306/xxxxtest.db.username=xxxxtest.db.password=xxxx#线上数据库连接信息online.db.url=jdbc:mysql://localhost:3306/xxxxonline.db.username=xxxxonline.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文件夹。
这里写图片描述
这里写图片描述

0 0
原创粉丝点击