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目录开始写。

注:本文均为个人理解,如有错误,欢迎指正

原创粉丝点击