1.用InputStream读取 Properties pro = new Properties(); InputStream in = DBUtils.class.getResourceAsStream("/dbinfo.properties");//配置文件dbi一般需要放在类路径目录下,即resources下 ,使用InputStream读取properties文件代码 pro.load(in); url = pro.getProperty("url"); user = pro.getProperty("user"); password = pro.getProperty("password");
2.ResourceBundle类来读取properties文件
// 用于加载properties文件
ResourceBundle rb = ResourceBundle.getBundle("dbinfo"); //注意这里不需要文件扩展名.properties
classDriver = rb.getString("classDriver");
url = rb.getString("url");
user = rb.getString("user");
password = rb.getString("password");