属性配置文件读取类ConfigHelper

1.属性文件读取类ConfigHelper:
package com.zx.props;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
/**
 * 读取配置文件类
 * @author penghuaiyi
 * @date  2008-8-20
 */
public class ConfigHelper {
	private static final String CONFIG_FILE="E:\\resource\\application.properties";
	private static Properties prop;
	private static File mFile;
	
	static{
		System.out.println("初始化配置文件开始 ");
		prop=new Properties();
		mFile=new File(CONFIG_FILE);
		if(mFile.exists()){
			try{
				prop.load(new FileInputStream(CONFIG_FILE));
			}catch(IOException e){
				System.out.println (e.getMessage());
			}
		}else{
			System.out.println ("config file not exist");
		}
	
	}
	
	/**
	 * 根据key获取属性培植文件中对应的value
	 * @param key
	 * @return
	 */
	public static String getProperty(String key){
	    String value = prop.getProperty(key);
        try{
        	value = new String(value.getBytes("ISO-8859-1"),"GBK");
	    }catch(UnsupportedEncodingException e){
	    	System.out.println (e.getMessage());
	    }
		return value;
	}
	
	/**
	 * 得到resource文件中的属性,将根据字符串数组依次将字符串中的“${0}”、“${1}”...替换<br>
	 * 如:aaa=c${0}d${1}efg<br>
	 * getProerty("aaa",["k","l"])==ckdlefg
	 * @param key
	 * @param values
	 * @return
	 */
	public static String getProperty(String key,String[] values){
	    String value = prop.getProperty(key);
        try{
        	value = new String(value.getBytes("ISO-8859-1"),"GBK");
	    }catch(UnsupportedEncodingException e){
	    	System.out.println (e.getMessage());
	    }
	    if(value!=null){
	    	for(int i=0;i<values.length;i++){
	    		value=value.replaceFirst("\\$\\{"+i+"\\}", values[i]);
	    	}
	    }
	   
		return value;
	}
	
	
	public static void main(String args[]){
		String values[]={"penghuaiyi","dsdsdgsgd"};
		String msg=ConfigHelper.getProperty("zx.log.circuit.newCircuit",values);
		System.out.println(msg);
		
	}
		
	
	

}



2.资源配置文件application.properties:
zx.log.circuit.newCircuit=用户${0}新增了电路${1}
zx.log.circuit.editCircuit=用户${0}调整了电路${1}
zx.log.circuit.stopCircuit=用户${0}停闭了电路${1}

你可能感兴趣的:(java,C++,c,C#)