Java加载Properties属性文件

 /**
     * 加载Properties属性文件
     *
     * @param propFilePath Properties属性文件相对classpath的路径
     * @param encode Properties属性文件编码
     * @return Properties对象
     */
    public static Properties loadProperties(String propFilePath, String encode) {
        if (StringUtils.isBlank(propFilePath)) {//StringUtils 工具类来做字符串的操作,判断文件路径是否为空
            return null;
        }

        Properties prop = null;
        InputStream in = null;
        try {

/*path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。*/
            in = PropertiesUtil.class.getResourceAsStream(propFilePath);

/*Properties的load方法其实就是传进去一个输入流,字节流或者字符流,字节流利用InputStreamReader转化为字符流,然后字符流用BufferedReader包装,BufferedReader读取properties配置文件,每次读取一行,分割成两个字符串。因为Properties是Map的子类,然后用put将两个字符串装进Properties对象。*/包装了Reader reader = new InputStreamReader(in, encode);

            prop = loadProperties(in, encode);//loadProperties为自定义函数
        } catch (Exception e) {
            prop = null;
            LOGGER.error("loadProperties - Excp : {}; propFilePath = {}", e, propFilePath);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }

        return prop;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值