读取Properties工具类

尝试一下封装一个读取Properties工具类,自己大概看了看DBUtils和Druid源码,学习一下封装思想,希望有大佬指点。

public class PropertiesUtil {

    private Properties properties = null;
    private String fileName = null;
    private Logger log = Logger.getLogger(PropertiesUtil.class);

    public PropertiesUtil(){
    }

    public PropertiesUtil(String fileName){
        this.fileName = fileName;
    }

    private void creatProperties(){
        if(this.fileName == null){
            log.error("PropertiesUtil|error|配置文件未设置");
            throw new RuntimeException("配置文件未设置");
        }
        log.info("PropertiesUtil|info|fileName:" + this.fileName);
        if(!this.fileName.endsWith(".properties")){
            log.error("PropertiesUtil|error|文件格式错误");
            throw new RuntimeException("文件格式错误");
        }
        this.properties = new Properties();
    }

    public String getValue(String key){
        return this.getValue(null,key);
    }

    /*
    通过key值读取配置文件的单一值
     */
    public String getValue(String fileName,String key) throws RuntimeException{
        if(fileName != null){
            this.fileName = fileName;
        }
        if(this.properties == null){
            creatProperties();
            fileReadLoad();
        }
        Object val = this.properties.get(key);
        if(val == null){
            log.error("PropertiesUtil|error|没有找到对应的值!");
            throw new RuntimeException("没有找到对应的值!");
        }
        return val.toString();
    }

    public void setValue(Map<String, String> map){
        this.setValue(null,map);
    }

    public void setValue(String fileName,Map<String, String> map){
        if (fileName != null){
            this.fileName = fileName;
        }
        creatProperties();
        Set<String> keySet = map.keySet();
        Iterator<String> iterator = keySet.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            setProperties(key,map.get(key));
        }
        fileOutLoad();
    }

    public void setValue(String key,String val){
        this.setValue(this.fileName,key,val);
    }

    /*
    向配置文件里保存数据
     */
    public void setValue(String fileName,String key,String val){
        if (fileName != null){
            this.fileName = fileName;
        }
        creatProperties();
        setProperties(key,val);
        fileOutLoad();
    }

    private void setProperties(String key,String val){
        this.properties.setProperty(key,val);
    }

    /*
    加载配置文件
     */
    private void fileReadLoad(){
        InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(this.fileName);
        if(in == null){
            log.error("PropertiesUtil|error|配置文件设置错误");
            throw new RuntimeException("配置文件设置错误");
        }
        try {
            this.properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
    保存配置文件
     */
    private void fileOutLoad(){
        File file = new File("bin" + File.separator + this.fileName);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            this.properties.store(fileOutputStream, String.valueOf(System.nanoTime() + "--" + Math.random()));
        } catch (Exception e) {
            log.error("PropertiesUtil|error|" + e.getMessage());
            e.printStackTrace();
        }finally {
            try {
                if(fileOutputStream != null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                log.error("PropertiesUtil|error|" + e.getMessage());
                e.printStackTrace();
            }
            if(this.properties != null){
                this.properties.clear();
            }
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值