首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java读取property.property资料

2012-11-09 
java读取property.property文件import java.io.IOExceptionimport java.io.UnsupportedEncodingException

java读取property.property文件

import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Properties;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;/** * 读取配置文件 *  * */public class PropertiesUtil {        private final static Logger log = Logger.getLogger(PropertiesUtil.class);    private static Properties properties = new Properties();        public static void loadCodeDip(){        try {        properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("uploadxls.properties"));        } catch (IOException e) {            log.error("load uploadxls.properties error:"+e);        }    }        /**     * 获取值     * @param key     * @return     */    public static String getPropertyValue(String key){    loadCodeDip();        String value = (String)properties.get(key);        if(StringUtils.isNotEmpty(value)){        try {        value=new String(value.getBytes("ISO8859_1"),"UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}        }else{        value="";        }        return value;    }        public static void main(String arg[]){    PropertiesUtil.loadCodeDip();    String urlValue = getPropertyValue("URL");    System.out.println(urlValue);    }}

?当然peroperty文件放在classpath下即可

配置文件格式见附件: 格式 URL=E:\\

热点排行