一、准备配置文件
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)