java加载properties配置文件的三种方式及注意事项

一、准备配置文件

1.配置文件xxxx.properties

 xxxx.properties配置文件放在D:盘下,配置信息如下所示:

#连接处理线程池大小
dispatcher.conn.thread=50

#请求处理线程池的大小
dispatcher.request.thread    =100

#响应处理线程池的大小
dispatcher.response.thread=               200

#响应处理线程池的大小
dispatcher.response.thread1==               "500"

#响应处理线程池的大小
dispatcher.response.thread2="500"

2.配置文件xxxx.properties

 xxxx.properties配置文件放src目录下,如下图所示:
在这里插入图片描述

 配置信息如下所示:

#连接处理线程池大小
dispatcher.conn.thread=50

#请求处理线程池的大小
dispatcher.request.thread    =100

#响应处理线程池的大小
dispatcher.response.thread=               200

#响应处理线程池的大小
dispatcher.response.thread1==               "500"

#响应处理线程池的大小
dispatcher.response.thread1=               "500"

3.配置文件xxxx2.properties

 xxxx2.properties配置文件放src目录下,如下图所示:
在这里插入图片描述
 配置信息如下所示:

#连接处理线程池大小
dispatcher.conn.thread=50

#请求处理线程池的大小
dispatcher.request.thread    =100

#响应处理线程池的大小
dispatcher.response.thread=               200

#响应处理线程池的大小
dispatcher.response.thread1==               "500"

#响应处理线程池的大小
dispatcher.response.thread1=               "500"

二、用三种方式加载properties配置文件

1.三种加载方式的编码

 具体代码如下所示:

public class PropertiesLoader {
	public static void main(String[] args) throws IOException {
		//方式一 、使用属性对象Properties和文件输入流对象FileInputStream加载properties配置文件。
		Properties properties = new Properties();//创建属性对象
		FileInputStream fileInputStream = new FileInputStream("D:\\xxxx.properties");//创建文件输入流对象
		properties.load(fileInputStream);
		System.out.println("dispatcher.conn.thread="+properties.getProperty("dispatcher.conn.thread"));
		System.out.println("dispatcher.request.thread="+properties.getProperty("dispatcher.request.thread"));
		System.out.println("dispatcher.response.thread="+properties.getProperty("dispatcher.response.thread"));
        
		//注意:此处可以获取带双引号的值,需要双等号。
		System.out.println("dispatcher.response.thread1="+properties.getProperty("dispatcher.response.thread1"));
		
		//注意:此处获取的值为null,因为value被双引号包裹。
		System.out.println("dispatcher.response.thread2="+properties.getProperty("dispatcher.response.thread2"));
		System.out.println("===================================分割线1===================================");
		
		
		//方式二、通过属性对象Properties和类加载器加载properties配置文件,注意配置文件的位置必须在src目录下。
		Properties properties2 = new Properties();
		InputStream in = PropertiesLoader.class.getClassLoader().getResourceAsStream("xxxx.properties");
		properties2.load(in);
		System.out.println("dispatcher.conn.thread="+properties2.getProperty("dispatcher.conn.thread"));
		System.out.println("dispatcher.request.thread="+properties2.getProperty("dispatcher.request.thread"));
		System.out.println("dispatcher.response.thread="+properties2.getProperty("dispatcher.response.thread"));
		
		//注意:此处可以获取带双引号的值,需要双等号。
		System.out.println("dispatcher.response.thread1="+properties2.getProperty("dispatcher.response.thread1"));

		//注意:此处获取的值为null,因为value被双引号包裹。
		System.out.println("dispatcher.response.thread2="+properties2.getProperty("dispatcher.response.thread2"));
		
		
		System.out.println("===================================分割线2===================================");
		//方式三、通过资源包对象ResourceBundle和基名(即文件前缀名)加载properties配置文件,注意配置文件的位置必须在src目录下。
		ResourceBundle bundle = ResourceBundle.getBundle("xxxx2");
		System.out.println("dispatcher.conn.thread="+bundle.getString("dispatcher.conn.thread"));
		System.out.println("dispatcher.request.thread="+bundle.getString("dispatcher.request.thread"));
		System.out.println("dispatcher.response.thread="+bundle.getString("dispatcher.response.thread"));
		
		//注意:此处可以获取带双引号的值,需要双等号。
		System.out.println("dispatcher.response.thread1="+bundle.getString("dispatcher.response.thread1"));
		
		//注意此处会报异常,因为value被双引号包裹。
		System.out.println("dispatcher.response.thread2="+bundle.getString("dispatcher.response.thread2"));
		
	}
}

2.输出结果

 输出结果如下所示:

dispatcher.conn.thread=50
dispatcher.request.thread=100
dispatcher.response.thread=200
dispatcher.response.thread1="500"
dispatcher.response.thread2=null
===================================分割线1===================================
dispatcher.conn.thread=50
dispatcher.request.thread=100
dispatcher.response.thread=200
dispatcher.response.thread1="500"
dispatcher.response.thread2=null
===================================分割线2===================================
dispatcher.conn.thread=50
dispatcher.request.thread=100
dispatcher.response.thread=200
dispatcher.response.thread1="500"
Exception in thread "main" java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key dispatcher.response.thread2
	at java.util.ResourceBundle.getObject(Unknown Source)
	at java.util.ResourceBundle.getString(Unknown Source)
	at com.zjt.test.PropertiesLoader.main(PropertiesLoader.java:41)
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值