apache.commons.configuration.PropertiesConfiguration實例

使用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文件TOjavaBean
			XDSEntry 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>


發佈了44 篇原創文章 · 獲贊 13 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章