现在的位置: 首页 > 编程语言 > 正文

Java加载property文件配置过程解析

2020年02月13日 编程语言 ⁄ 共 3137字 ⁄ 字号 评论关闭

这篇文章主要介绍了java加载property文件配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1 properties简介:

properties是一种文本文件,内容格式为:

key = value #单行注释

适合作为简单配置文件使用,通常作为参数配置、国际化资源文件使用。

对于复杂的配置,就需要使用XML、YML、JSON等了

2 java加载Properties:

java加载properties主要通过2个util包下的工具类: Properties类、 ResourceBundle类

2.1 通过Properties类加载:

Properties类通过load()方法加载配置信息,这个方法接收一个输入流参数:InputStream、Reader。

Properties提供get(String key) 方法读取指定的配置项。

2.1.1 通过ClassLoader获取InputStream加载:

该方式只能读取类路径下的配置文件

(1)先创建Properties对象

(2)获取property文件对应的输入流in

(3)使用Properties加载输入流in

(4)通过Properties.get(key)方法获取配置,如果配置信息不存在,则返回null

/** * 基于ClassLoader读取properties; * 该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。 */public static void method1() { System.out.println("使用ClassLoader方式加载properties"); Properties properties = new Properties(); /* * 使用ClassLoader加载properties配置文件生成对应的输入流。 * 使用此种方式,要求property文件必须要放在src目录下,编译之后会被放到class文件相同目录下。 * 因为ClassLoad的基础路径是相对于编译后class文件所在目录(可能是bin,classes),如果将properties放在项目根目录下,使用此种方式可能会找不到 properties文件 */ //必须要以 /开始 , 是在classes文件根目录下寻找 InputStream in = PropertiesDemo.class.getResourceAsStream("/properties/a.properties"); System.out.println(PropertiesDemo.class.getClassLoader().getResource(".").getPath()); try { properties.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { closeResource(in); } System.out.println(properties.get("name")); System.out.println(properties.get("age"));}

2.1.2 通过构建Reader加载:

该方式的优点在于可以读取任意路径下的配置文件

(1)创建Properties对象

(2)创建一个Reader,可以指定路径,不局限于类路径

(3)使用Properties加载Reader

(4)Properties.get(key)方法读取配置

/** * 使用inputStream读取配置文件 * 该方式的优点在于可以读取任意路径下的配置文件 */public static void method2() { System.out.println("使用InputStream方式加载properties"); Properties properties = new Properties(); BufferedReader reader = null; try { /* * System.getProperty("user.dir"): 获取项目根路径, 而后附加配置文件的路径 */ reader = new BufferedReader(new FileReader(System.getProperty("user.dir") + "/properties/a.properties")); properties.load(reader); System.out.println(properties.get("name")); System.out.println(properties.get("age")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { closeResource(reader); } }

2.2 通过ResourceBundle类加载:

ResourceBundle读取配置有2种方式:

(1)指定文件路径 :相对于src、classes的相对路径

(2)提供InputStream

ResourceBundle提供方法getString(key) 和 getObject(key)读取配置项使用路径加载,不需要指定文件后缀名,且不需要手动关闭相关资源,比Properties类操作要简单。

/** * 通过ResourceBundle 读取配置, 此种方式项目Properties要简单 * ResourceBundle读取配置有2种方式: (1)指定文件路径 (2)提供InputStream */public static void method3() { System.out.println("使用ResourceBundle方式加载properties"); /* * (1)直接指定文件路径: * 可以使用相对路径,从类路径开始,且不需要指定properties文件的后缀 */ ResourceBundle resource = ResourceBundle.getBundle("properties/b"); System.out.println(resource.getString("name")); System.out.println(resource.getString("age")); try { /* * (2)通过InputStream加载配置: * 通过当前类的class实例,获取资源输入流 */ resource = new PropertyResourceBundle(PropertiesDemo.class.getResourceAsStream("/properties/a.properties")); System.out.println(resource.getString("name")); System.out.println(resource.getString("age")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: Java加载property文件配置过程解析

以上就上有关Java加载property文件配置过程解析的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.