java 读取工程下classes配置文件

public static String getConfigKey(String key) {
	String fName = "aa.properties";
	String keyVal = "";
	Properties props = null;
	String url = StringUtils.class.getClassLoader().getResource("/").getPath()+ fName;
    	/*从根目录读取配置文件
        String fileName = "";
        if(File.separatorChar == '\\')
            fileName = "C:\\"+fName;
        else
            fileName = "/"+fName;
    	 */
        //log.info("配置文件地址----------"+url);

        File file = new File(url);
        if (!file.isFile()) {
        	return null;
        }
        
        if (file.exists()) {
            FileInputStream fis = null;
            try
            {
                fis = new FileInputStream(file);
            }
            catch(FileNotFoundException e2)
            {
            	log.info(file + "读取配置文件错误!");
                e2.printStackTrace();
                
            }
            props = new Properties();
            try
            {
                props.load(fis);
                fis.close();
               
                keyVal = props.getProperty(key);
            	if (keyVal == null) {
            	    log.error("配置文件中没有名为:" + key + "的KEY值!");
            	}
                if (keyVal != null && keyVal.endsWith("\"")){
                    keyVal = keyVal.replaceAll("\"", "");
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
                log.info(file + "读取配置文件中属性值错误!");
            }       
        } else {
        	log.info(file + "配置文件没有找到!");
        }
        return keyVal;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值