JAVA读取properties配置文件的方法

最近在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=超越40
jdbc.mysql.driver=com.mysql.jdbc.Driver

三、总结

1.在使用getResourceAsStream()方法的四个测试中,只有在读取和测试类同一个包下的配置文件时(即第一种的第一个测试),没有使用getClassLoader()方法来加载类加载器。

2.在使用getResourceAsStream()方法的四个测试中,只有读取另一个包下的配置文件时(即第三种的第一个测试),需要写全路径,其他状态只需要写文件名。

3.在使用InputStream()流去读取配置文件的四个测试中,路径均要从src目录开始写。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值