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; } }
获取项目properties文件属性值
最新推荐文章于 2022-07-28 11:22:37 发布