敬业的IT人 >> 编程开发 >> Java >> 得到配置资源的一般做法--CLASS.getResourceAsStream(String resource)

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

敬业的IT人 互联网 佚名 2008-5-16 12:34:06

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;

}


粤ICP备06119539号
Copyright CiscoSky.Org,Some Rights Reserved.
Email:me1228#tom.com