apache.commons.configuration.PropertiesConfiguration实例

来源:互联网 发布:火影忍者网络连接异常 编辑:程序博客网 时间:2024/06/01 17:44

使用PropertiesConfiguration操作properties配置文件实例

package my.apache.commons.configuration;import java.util.Iterator;import my.apache.commons.configuration.entry.XDSEntry;import org.apache.commons.beanutils.PropertyUtilsBean;import org.apache.commons.configuration.ConfigurationException;import org.apache.commons.configuration.PropertiesConfiguration;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class Main {private static Logger logger = LoggerFactory.getLogger(Main.class);/** * 读取文件操作 * @param fileName * @return */private static PropertiesConfiguration loadFile( String fileName ) throws Exception {logger.debug("开始读取"+ fileName +"文件");PropertiesConfiguration config = new PropertiesConfiguration(fileName);if(null != config) {logger.debug("文件读取成功");}else{logger.debug("文件读取失败");}return config;}/** * 输出文件内容 * @param properties * @throws Exception */private static void showProperties( PropertiesConfiguration properties ) throws Exception {logger.debug("开始输出" + properties.getFileName() + "文件内容");Iterator<String> iterator = properties.getKeys();while( iterator.hasNext() ) {String key = iterator.next();Object value = properties.getProperty(key);logger.debug("key:{},value:{},valueClass:{}",key,value,value.getClass().getName());} }private static void operate( PropertiesConfiguration properties ) {logger.debug( properties.subset("xds").getString("url") );}/** * 转换方法 * @param properties * @param entryClass * @throws IllegalAccessException  * @throws InstantiationException  */private static Object pro2bean( PropertiesConfiguration properties , Class entryClass ) throws InstantiationException, IllegalAccessException{Object obj = entryClass.newInstance();PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();Iterator<String> iterator = properties.subset("xds").getKeys();while(iterator.hasNext()) {String key = iterator.next();try{propertyUtilsBean.setProperty(obj, key, properties.subset("xds").getProperty(key) );}catch(Exception ex) {ex.printStackTrace();}}return obj;}private static void save() throws ConfigurationException{PropertiesConfiguration newFile = new PropertiesConfiguration();newFile.setEncoding("UTF-8");newFile.setProperty("k_t", "v_t");newFile.setProperty("k_z", "v_出去");newFile.save("./newFile_chujie_chujie.properties");}public static void main(String[] args) throws Exception {try {//获取properties文件PropertiesConfiguration config = loadFile("appid.properties");showProperties(config);//获取properties文件PropertiesConfiguration xds = loadFile("xds.properties");//操作properties文件对象operate(xds);//properties文件TOjavaBeanXDSEntry xdsentry = (XDSEntry)pro2bean(xds,XDSEntry.class);logger.debug("输出javaBean"+xdsentry.getUrl());//存储properties文件save();} catch (Exception e) {e.printStackTrace();}  }}

package my.apache.commons.configuration.entry;public class XDSEntry {String enable;String url;String app;public String getEnable() {return enable;}public void setEnable(String enable) {this.enable = enable;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getApp() {return app;}public void setApp(String app) {this.app = app;}}

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>my.apache</groupId><artifactId>commons-configuration</artifactId><version>0.0.1-SNAPSHOT</version><name>commons-configuration</name><description>commons-configuration</description><dependencies><!-- 读取配置文件依赖 --><dependency><groupId>commons-configuration</groupId><artifactId>commons-configuration</artifactId><version>1.8</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.8.3</version></dependency><!-- 日志 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.5</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>1.7.5</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.0.7</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></exclusion></exclusions></dependency></dependencies></project>


0 0