栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

这篇文章主要介绍了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


public static void method1() {
   System.out.println("使用ClassLoader方式加载properties");
   Properties properties = new 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)方法读取配置


public static void method2() {
   System.out.println("使用InputStream方式加载properties");
   Properties properties = new Properties();
   BufferedReader reader = null;
   try {
     
     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类操作要简单。


public static void method3() {
  System.out.println("使用ResourceBundle方式加载properties");
  
  ResourceBundle resource = ResourceBundle.getBundle("properties/b");
  System.out.println(resource.getString("name"));
  System.out.println(resource.getString("age"));

 


  try {
   
   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();
  }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135207.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号