java类读取配置文件方法解析

工具:idea
jdk:1.8

缘由:一下午在实现一个计算器的小栗子,最终目的是将一切常量封装,对应算法抽离,实现高内聚低耦合效果。例子中用到了策略模式、简单工厂加反射,并且将运算符号常量一键值对的形式放在了配置文件中,分别对应不同的算法类名称。
在这里插入图片描述
那么在工厂中怎样读取到配置文件中对应的常量呢?
下面将提供三个方法:

一、基于InputStream读取配置文件信息

 public static CounterInterface counterInterface(String key) throws Exception {
		// 创建Properties对象,用于读取配置文件
		Properties props = new Properties();
		
		//我这里用的是绝对路径,相对路径找不到配置文件,不清楚什么原因
		//或者可以用InputStream ips = counterInterface.class.getClassLoader().getResourceAsStream(文件路径)
        InputStream ips = new FileInputStream("D:\\allproject\\Counter\\src\\main\\resources\\config.properties");

		// 由于配置文件中可能存在中文乱码问题,应用reader类进行转换后解决
        Reader inStream = new InputStreamReader(ips, "UTF-8");
        props.load(inStream);

		//getProperty用于获取properties中 对应键的键值,应用反射 ,实例化出键值对应的类 的对象
        CounterInterface counterInterface= (CounterInterface) Class.forName(props.getProperty(key)).newInstance();
        
        return counterInterface;
 }

二、通过spring中的 PropertiesLoaderUtils工具类进行获取:

 public static CounterInterface counterInterface(String key) throws Exception {
		Properties properties = new Properties();
        try {
            properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
            String res=new String(properties.getProperty(key).getBytes("iso-8859-1"), "gbk");
        } catch (IOException e) {
            e.printStackTrace();
        }
		//getProperty用于获取properties中 对应键的键值,应用反射 ,实例化出键值对应的类 的对象
        CounterInterface counterInterface= (CounterInterface) Class.forName(res).newInstance();
        
        return counterInterface;
 }

便利取值:


		Properties properties = new Properties();
        try {
            properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
            //遍历取值
            Set<Object> objects = properties.keySet();
            for (Object object : objects) {
                System.out.println(new String(properties.getProperty((String) object).getBytes("iso-8859-1"), "gbk"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
 

三、通过 java.util.ResourceBundle 类读取:

 private static void readProperty3() {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("code");
        //遍历取值
        Enumeration enumeration = resourceBundle.getKeys();
        while (enumeration.hasMoreElements()) {
            try {
                String value = resourceBundle.getString((String) enumeration.nextElement());
                System.out.println(new String(value.getBytes("iso-8859-1"), "gbk"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

参考博客链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值