Java 加载资源文件
/**
* @功能 根据类和配置文件名获取properties对象
* @示例 Properties prop=new Hello().getProperties(Hello.class,"Test.properties");
* prop.getProperty("keyname");
* @param className 如Hello.class
* @param propName 如Test.properties
* @return 加载完毕的properties对象
*/
@SuppressWarnings("unchecked")
public Properties getProperties(Class className,String propName){
String url=className.getResource(propName).toString().substring(6);//获得file:\后面的地址
Properties prop=new Properties();
InputStream is;
try {
is = new FileInputStream(url);//将地址加在到文件输入流中
prop.load(is);//properties对象加载文件输入流
is.close();//文件流关闭
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
