JAVA读取properties配置文件的方法
来源:互联网 发布:阿里云 香港节点 速度 编辑:程序博客网 时间:2024/05/29 06:38
最近在Mybatis框架中使用到了读取properties配置文件,上网查阅了一番读取方法,比较乱,现在选择两种常用的进行说明。
以下测试均在eclipse中,并且使用Junit进行测试。
(1)使用getResourceAsStream()方法读取配置文件
(2)使用InputStream()流去读取配置文件
一、以下是工程结构:
在本工程中包含4个不同路径的配置文件。
1.与测试文件不同的包中的配置文件
2.与测试文件同一个包中的配置文件
3根目录下面的配置文件
4.不同src下的配置文件
二、读取properties
1.读取和测试类同一个包下的配置文件的两种方法
@Test public void test01() throws IOException{ InputStream is =PropertiesTest.class.getResourceAsStream("sh.properties"); //Java当中有的工具类 Properties props = new Properties(); props.load(is); System.out.println(props.getProperty("shxt")); } @Test public void test02() throws IOException{ InputStream is = new BufferedInputStream(new FileInputStream("src/com/shxt/test/sh.properties")); //Java当中有的工具类 Properties props = new Properties(); props.load(is); System.out.println(props.getProperty("shxt")); }
2.读取src根目录下的配置文件的两种方法
@Test public void test03() throws IOException{ InputStream is = PropertiesTest.class.getClassLoader().getResourceAsStream("shxt.properties"); //Java当中有的工具类 Properties props = new Properties(); props.load(is); System.out.println(props.getProperty("shxt")); } @Test public void test04() throws IOException{ InputStream is = new BufferedInputStream(new FileInputStream("src/shxt.properties")); Properties props = new Properties(); props.load(is); System.out.println(props.getProperty("vip")); }
3.读取另一个包下的配置文件的两种方法
@Test public void test05() throws IOException{ InputStream is = PropertiesTest.class.getClassLoader().getResourceAsStream("com/shxt/other/s.properties"); //Java当中有的工具类 Properties props = new Properties(); props.load(is); System.out.println(props.getProperty("shxt")); } @Test public void test07() throws IOException{ InputStream is = new BufferedInputStream(new FileInputStream("src/com/shxt/other/s.properties")); Properties props = new Properties(); props.load(is); System.out.println(props.getProperty("vip")); }
4.读取另一个src文件夹下的配置文件的两种方法
@Test public void test08() throws IOException{ InputStream is = PropertiesTest.class.getClassLoader().getResourceAsStream("shx.properties"); //Java当中有的工具类 Properties props = new Properties(); props.load(is); System.out.println(props.getProperty("vip")); } @Test public void test06() throws IOException{ InputStream is = new BufferedInputStream(new FileInputStream("other_src/shx.properties")); Properties props = new Properties(); props.load(is); System.out.println(props.getProperty("vip")); }
另外附上properties文件中的代码
shxt=四海兴唐vip=超越40jdbc.mysql.driver=com.mysql.jdbc.Driver
三、总结
1.在使用getResourceAsStream()方法的四个测试中,只有在读取和测试类同一个包下的配置文件时(即第一种的第一个测试),没有使用getClassLoader()方法来加载类加载器。
2.在使用getResourceAsStream()方法的四个测试中,只有读取另一个包下的配置文件时(即第三种的第一个测试),需要写全路径,其他状态只需要写文件名。
3.在使用InputStream()流去读取配置文件的四个测试中,路径均要从src目录开始写。
注:本文均为个人理解,如有错误,欢迎指正
阅读全文
0 0
- JAVA读取properties配置文件的方法
- 【Java】读取properties配置文件的方法
- 读取配置文件properties的方法
- 读取配置文件properties的方法
- 读取Java的配置文件Properties
- Java配置文件properties的读取
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- JAVA读取Properties的六种方法--配置文件
- java读取.properties配置文件的几种方法 .
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- [可用] java读取.properties配置文件的几种方法
- 绿盟前端笔试题
- NOIP2012开车旅行&疫情控制解题记录
- javaweb框架之springmvc
- 对拍
- 简单的数据可视化,并加入了animation动态效果
- JAVA读取properties配置文件的方法
- 3.使用annotation配置
- Akka使用入门
- startActivity 的启动过程源码分析学习
- Python3:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
- 分治算法求解kth largest element问题
- Gson解析
- 本文转自:http://www.cnblogs.com/Charles2008/archive/2009/09/03/1559896.html
- struct跟踪算法