public class ResourceProperties { private static HashMap hashInstance = new HashMap(); private Properties propertiesInstance = new Properties(); private String sFileName = ""; private String sFilePath = ""; public static ResourceProperties getResourceFile(String psResourceFile,Locale locale) throws Exception{ String lsFileKey = psResourceFile; if (null != locale) lsFileKey = psResourceFile + "_" + locale.getLanguage(); if (hashInstance.containsKey(lsFileKey)) { return (ResourceProperties) hashInstance.get(lsFileKey); } else { ResourceProperties lrp = new ResourceProperties(); lrp.load(lsFileKey); hashInstance.put(lsFileKey,lrp); return lrp; } } public static ResourceProperties reload(String psResourceFile) throws Exception{ if (hashInstance.containsKey(psResourceFile)) { ResourceProperties lrp = (ResourceProperties) hashInstance.get(psResourceFile); lrp.load(psResourceFile); return lrp; } else { ResourceProperties lrp = new ResourceProperties(); lrp.load(psResourceFile); hashInstance.put(psResourceFile,lrp); return lrp; } }
public void load(String psResourceFile) throws Exception { try { String lsFile = psResourceFile.replace('.', '/') + ".properties"; URL urlResource = this.getClass().getClassLoader().getResource(lsFile); sFileName = urlResource.getFile(); File file = new File(sFileName); sFilePath = file.getParentFile().getPath(); InputStream stream = urlResource.openStream(); propertiesInstance.clear(); propertiesInstance.load(stream); stream.close(); } catch (Exception e) { //BaseUtil.printStackTrace(e); throw new Exception(e); } } /** 获取配置中的一个键值,若没有则用缺省值 */ public String get(String key, String defaultValue) { return propertiesInstance.getProperty(key,defaultValue); } /** 获取配置中的一个键值,若没有则返回null */ public String get(String key) { return propertiesInstance.getProperty(key); } /** 设置配置中的一个键值 */ public void set(String psKey,String psValue) { propertiesInstance.setProperty(psKey,psValue); } /** 保存资源文件 */ public void save(String psUser) throws Exception { try{ OutputStream stream = new FileOutputStream(sFileName); propertiesInstance.store(stream,"Resource file last update by " + psUser); stream.close(); } catch(Exception e) { //BaseUtil.printStackTrace(e); throw new Exception(e); } } public Properties getProperties() { return this.propertiesInstance; }
ResourceProperties
最新推荐文章于 2024-05-22 15:47:19 发布