获取项目properties文件属性值

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class Prop {
    //用于缓存配置
    private static final Hashtable<String, HashMap<String,String>> cacheProps = new Hashtable<>();
    private String customerPropFile = "";
    private Properties prop;
    private FileInputStream localFileInputStream = null;
    private static String propName;

    public String getCustomerPropFile() {
        return this.customerPropFile;
    }

    public void setCustomerPropFile(String customerPropFile) {
        this.customerPropFile = customerPropFile;
    }

    public Prop(String propName) {
        this.propName = propName;
        this.setCustomerPropFile(GCONST.getPropertyPath("prop") + propName + ".properties");
        this.prop = new Properties();

        try {
            this.localFileInputStream = new FileInputStream(this.getCustomerPropFile());
            this.prop.load(this.localFileInputStream);
            Set<String> keys = this.prop.stringPropertyNames();
            HashMap<String,String> keyValues = new HashMap<>();
            if (null!=keys && !keys.isEmpty()) {
                for (String key : keys) {
                    keyValues.put(key,doGetPropValue(key));
                }
            }
            cacheProps.put(propName,keyValues);
        } catch (FileNotFoundException var3) {
            var3.printStackTrace();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

    }

    public String doGetPropValue(String propKey) {
        String returnPropValue = "";

        try {
            returnPropValue = StringUtil.trimStr(this.prop.getProperty(propKey));
            returnPropValue = new String(returnPropValue.getBytes("iso8859-1"), "utf-8");
        } catch (Exception var15) {
            System.out.println("读取参数失败!");
            if (this.localFileInputStream != null) {
                try {
                    this.localFileInputStream.close();
                } catch (IOException var14) {
                }
            }
        } finally {
            if (this.localFileInputStream != null) {
                try {
                    this.localFileInputStream.close();
                } catch (IOException var13) {
                }
            }

        }

        return returnPropValue;
    }

    public String getPropValue(String propKey){
        for (Map.Entry<String, HashMap<String, String>> prop : cacheProps.entrySet()) {
            HashMap<String, String> keyValues = prop.getValue();
            if (keyValues.containsKey(propKey)) {
                return keyValues.get(propKey);
            }
        }
        return null;
    }

    public static void refresh(String ...propNames){
        cacheProps.clear();
        for (String propName : propNames) {
            Prop prop = new Prop(propName);
        }
    }

    public static String getPropValue(String fileName, String propKey) {
        String returnValue = "";

        try {
            Prop props = new Prop(fileName);
            returnValue = props.getPropValue(propKey);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值