您的位置:首页 > 其它

得到配置资源的一般做法--CLASS.getResourceAsStream(String resource)

2008-04-17 14:29 483 查看
protected InputStream getConfigurationInputStream(String resource) throws HibernateException {

InputStream stream = Environment.class.getResourceAsStream(resource);
if (stream==null) {

throw new Exception(resource + " not found");
}
return stream;

}

client应该这么写:

//hibernate.cfg.xml位于classes目录下(classPath目录)

InputStream stream = getConfigurationInputStream("/hibernate.cfg.xml");

如果资源是.property的配置文件,则可以这么装载

Properties property=new Properties();

property.load(stream);

总起来可以这么写:

public Properties getPropFromFile(String filePath){

InputStream stream = Environment.class.getResourceAsStream(resource);
if (stream==null) {

throw new Exception(resource + " not found");
}

Properties property=new Properties();

property.load(stream);
return stream;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐