属性文件读取工具类

PropertiesUtils:

package com.zhihua.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 属性文件读取工具类
 * <请替换成功能描述> <br>
 * <请替换成详细描述>
 * @author caizh
 * @since [1.0.0]
 * @version [1.0.0,2017年2月11日]
 */
public class PropertiesUtils {

    /**
     * 默认属性集合(文件在Constants中配置)
     */
    protected static Properties defaultProp = null;

    /**
     * 所有读取过的属性集合
     * 文件名<->属性集合
     */
    protected static Map<String,Properties> allProps = new HashMap<String,Properties>();

    static{
        if(defaultProp==null){
            // 注意获取配置文件的路径 包名+文件名
            defaultProp = loadProperties("com/zhihua/config/config.properties");
            allProps.put("config.properties", defaultProp);
        }
    }

    /**
     * 读取属性文件,并将读出来的属性集合添加到【allProps】当中
     * 如果该属性文件已经读取过,则直接从【allProps】获得
     * <请替换成功能描述> <br>
     * <请替换成详细描述>
     * @param fileName
     * @return
     * @author caizh
     * @since [1.0.0]
     * @version [1.0.0,2017年2月11日]
     */
    public static Properties getProperties(String fileName) {
        if(fileName == null || "".equals(fileName)){
            return defaultProp;
        }else{
            Properties prop = allProps.get(fileName);
            if(prop == null){
                prop = loadProperties(fileName);
                allProps.put(fileName, prop);
            }
            return prop;
        }
    }

    /**
     * 从指定的属性文件中获取某一属性值
     * 如果属性文件不存在该属性则返回null
     * <请替换成功能描述> <br>
     * <请替换成详细描述>
     * @param fileName 属性文件
     * @param name 属性名
     * @return
     * @author caizh
     * @since [1.0.0]
     * @version [1.0.0,2017年2月11日]
     */
    public static String getProperty(String fileName, String name){
        return getProperties(fileName).getProperty(name);
    }

    /**
     * 从默认的属性文件中获取某一属性
     * 如果属性文件不存在则该属性则返回 null
     * <请替换成功能描述> <br>
     * <请替换成详细描述>
     * @param fileName
     * @return
     * @author caizh
     * @since [1.0.0]
     * @version [1.0.0,2017年2月11日]
     */
    public static String getProperty(String name){    
        return getProperties(null).getProperty(name);
    }

    /**
     * 解析属性文件,将文件中的所有属性都读取到【Properties】当中
     * <请替换成功能描述> <br>
     * <请替换成详细描述>
     * @param fileName
     * @return
     * @author caizh
     * @since [1.0.0]
     * @version [1.0.0,2017年2月11日]
     */
    private static Properties loadProperties(String fileName) {
        Properties prop = new Properties();
        InputStream inStream = null;
        //PropertiesUtils.class.getClassLoader().getResourceAsStream()查找在src文件夹下的文件。
        inStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName);
        if(inStream == null){
            System.err.println("Can not find the resource!");
        }else{
            try{
                prop.load(inStream);
                System.out.println("find the resource!");
            }catch (IOException e) {
                System.err.println("An error occurred when reading from the input stream, "+e.getMessage());
            } catch (IllegalArgumentException e) {
                System.err.println("The input stream contains a malformed Unicode escape sequence, "+e.getMessage());
            }
        }
        return prop;
    }
}

配置文件 config.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root

测试类:

package com.zhihua.config.test;

import org.junit.Test;

import com.zhihua.config.PropertiesUtils;

public class PropertiesUtilsTest {

    @SuppressWarnings("static-access")
    @Test
    public void loadProperties(){        
        PropertiesUtils propUtils = new PropertiesUtils();
        /*String fileName = "/com/zhihua/config/config.properties";*/
        String username = propUtils.getProperty("jdbc.username");//查看配置文件中jdbc.username的值
        System.out.println(username);
    }
}

亲测通过!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值