六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 52|回复: 0

属性配置文件读取类ConfigHelper

[复制链接]

升级  1.33%

58

主题

58

主题

58

主题

举人

Rank: 3Rank: 3

积分
204
 楼主| 发表于 2013-1-27 05:10:17 | 显示全部楼层 |阅读模式
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);    }    }   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}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表