使用apache commons包读取配置文件【转】
来源:互联网 发布:我国教育经费机制知 编辑:程序博客网 时间:2024/04/20 08:55
在java项目中, 经常需要从配置文件中读取配置信息. 常见的配置文件有properties文件和XML文件.本文以读取properties配置文件为例.
加载并读取配置:
配置文件 app.properties 截取如下:
ftp.username=userOneftp.password=p@sswordftp.hostip=127.0.0.1ftp.port=21
加载并读取配置:
import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; private static String ftpUser = null; private static String ftpPassword = null; private static String ftpHost = null; private static String ftpPort = null; private static String CONFIG_FILEPATH = ClassLoader.getSystemResource( "app.properties").getPath();private static void initFromProperties(){ try { CONFIG_FILEPATH = URLDecoder.decode(CONFIG_FILEPATH, "utf-8"); setProperties(new PropertiesConfiguration(CONFIG_FILEPATH)); getProperties().setReloadingStrategy( new FileChangedReloadingStrategy()); getProperties().setAutoSave(true); readValues(); } catch (UnsupportedEncodingException e) { //处理异常 } catch (ConfigurationException e) { //处理异常 } } private static void readValues() { // ftp setFtpUser(getStrValue("ftp.username")); setFtpPassword(getStrValue("ftp.password")); setFtpHost(getStrValue("ftp.hostip")); setFtpRootPath(getStrValue("ftp.ftproot")); setFtpPort(getStrValue("ftp.port")); }同样对于XML配置文件,也可以使用apache commons包。
转自大雨大雨:【http://www.blogjava.net/cmscer/archive/2013/05/22/399633.html】
- 使用apache commons包读取配置文件【转】
- 使用Apache commons-configuration读取配置文件
- 使用apache的commons.io包读取文件
- 配置文件读取类:commons-configuration使用
- 使用Apache Commons Configuration读取配置信息
- 使用Apache commons IO包规范文件名
- apache commons-lang包
- apache commons 包简介
- Apache commons包简介
- apache commons包简介
- apache commons包简介
- apache commons包简介
- apache commons-lang包
- Apache Commons 公用包
- 使用apache.commons.mail包发送邮件,例子
- 使用Apache Commons Net包实现javaFTP来上传目录
- apache 里commons包里的 ftp使用
- 使用apache-commons-net包遇到的bug
- Socket编程小结
- 修饰函数和函数返回值的const的差别
- KindEditor 单独调用图片插件
- 超好用的Templates
- C#串口操作类
- 使用apache commons包读取配置文件【转】
- 获取java项目根目录
- Android中全屏或者取消标题栏
- Selenium 处理富文本
- ANDROID 隐藏 任务栏 systemui systembar 全屏显示
- Socket异步发送接收,Windows
- jQuery验证控件jquery.validate.js使用说明
- [Openstack]client api源码学习
- Java编程中写出好代码的建议