加载properties格式配置文件的工具方法,properties配置文件,为了加载properti
分享于 点击 47220 次 点评:209
加载properties格式配置文件的工具方法,properties配置文件,为了加载properti
为了加载properties格式的配置文件,我们要调用多个方法和多行代码才能实现,为了简化此操作遂写了一个工具方法getProperties()。
/** * @功能 根据类和配置文件名获取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;}//该片段来自于http://byrx.net
用户点评