java中读取Properties配置文件

时间:2022-04-08 11:59:35

自定义类获取Properties文件,代码如下:

Properties pro = null;

try {

                     /*第一种,通过类加载器进行获取properties文件流 JDBCHelper为类名*/

InputStream reader = JDBCHelper.class.getClassLoader().getResourceAsStream("dbconfig.properties");

              /*二种,通过类进行获取properties文件流*/
                      //reader = PropertyUtil.class.getResourceAsStream("/jdbc.properties");

pro = new Properties();
pro.load(reader);
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取配置文件错误");
}
driver = pro.getProperty("driver");
userName = pro.getProperty("username");
password = pro.getProperty("password");

dbUrl = pro.getProperty("dburl");


配置文件:

driver=com.mysql.jdbc.Driver
dburl=jdbc\:mysql\://localhost\:3306/jsp_db?useSSL\=false
username=root

password=