读书人

java读取property.property资料

发布时间: 2012-11-09 10:18:48 作者: rapoo

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:\\

读书人网 >编程

热点推荐