一个软编码资源文件的路径问题

1 static {
ResourceBundle res =
ResourceBundle.getBundle("conn"); // 读取属性文件
drivers = res.getString(“driver”).trim(); // 读取数据库驱动
url = res.getString("url").trim(); // 读取数据库连接URL
username = res.getString("username").trim(); // 读取用户名
password = res.getString("pwd").trim(); // 读取密码
}

2

static {
Properties p = new Properties(); // 获取Properties实例
try {
p.load(Test2.class.getResourceAsStream("Conn.properties")); // 获取
Conn.properties文件
} catch (IOException e) {
System.out.println("properties file not found" + e.getMessage());
}
drivers = p.getProperty("driver"); // 读取数据据驱动
url = p.getProperty("url"); // 读取数据库连接URL
username = p.getProperty("username"); // 读取用户名
password = p.getProperty("pwd"); // 读取密码

 

比较1 和2

1的conn是相对项目根路径!而2是conn.properties是相对当前文档。

posted @ 2012-06-18 15:44  一个人的深夜  阅读(157)  评论(0编辑  收藏  举报