PropertiesUtil.java
内容入如下:
package org.aa.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
public static Properties loadProperties(String propertiesFilePath) {
Properties p = new Properties();
InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesFilePath);
if(in==null){
return p ;
}
try {
p.load(in);
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
}
调用:
private static final String DATABASE_CONFIG_FILE =
"/com/company/db.properties";
private static Properties dbProp= PropertiesUtil.loadProperties(DATABASE_CONFIG_FILE);
private static final String driverStr = "oracle.jdbc.driver.OracleDriver";
private static final String defaultIp = dbProp.getProperty("database.server.ip");
private static final String defaultId = dbProp.getProperty("database.server.id");
private static final String defaultPort = dbProp.getProperty("database.server.port");
// private static final String conStr = "jdbc:oracle:thin:@192.168.1.10:1521:orcl";
private static final StringBuffer buf = new StringBuffer(200);
static{
buf.append("jdbc:oracle:thin:@");
buf.append(defaultIp);
buf.append(":");
buf.append(defaultPort);
buf.append(":");
buf.append(defaultId);
}
private static final String conStr = buf.toString();
注意黑体部分的路径,这个是出问题的关键,路径不对就不能取到值。
这个也可以用在国际化中的属性文件的初始化。
|----------------------------------------------------------------------------------------|
版权声明 版权所有 @zhyiwww
引用请注明来源 http://www.blogjava.net/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2010-03-20 17:22
zhyiwww 阅读(387)
评论(0) 编辑 收藏 所属分类:
j2ee