ResourceProperties

本文介绍了资源文件管理类的设计,包括资源文件加载、存储、访问及配置项获取与设置的功能实现。
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;
    }




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值