加载properties文件属性的Java工具类实现

今天有点无聊,写了一个如何读取properties文件配置信息的类,其实这个功能在java.util包中已经有了相应的功能,但是我还是想要实现一下,以检测自己的编程能力
代码如下:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class Properties {
    static HashMap<String, String> hashMap = new HashMap<String, String>();
    @SuppressWarnings("resource")
    public static void getProperties(String properties) throws IOException{
        BufferedReader bufferedReader = null;
        bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(properties)));
        String str = null;
        while ((str=bufferedReader.readLine()) != null) {
            int index = str.indexOf("=");
            if(index != -1){
                hashMap.put(str.substring(0,index).trim(), str.substring(index+1).trim());
            }
        }
    }

    public static String getProperties(String key, String defalutvalue){
        String value = hashMap.get(key);
        if(value == null){
            value = defalutvalue;
        }
        return value;
    }

    public static String getProperties(String propertiesFileName, String key, String defalutvalue) throws IOException{
        getProperties(propertiesFileName);
        return getProperties(key, defalutvalue);
    }

    public static void main(String[] args) throws IOException {
        String valueString = getProperties("C:/Users/Kin.Liufu/Desktop/一点小心得/config.properties","userName","kin");
        System.out.println(valueString);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值