`
KG21
  • 浏览: 105451 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java读取property.property文件

    博客分类:
  • java
阅读更多
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:\\

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics