Java读取配置文件

前提:配置文件的设置

因为我们在配置文件中,有可能使用到汉字,那么我们就应该避免在读取数据时出现乱码,那么我们就需要调制好环境
在这里插入图片描述
Properties Files文件,仅仅从名字上看,就知道它是针对properties文件的,那么我们可以根据自己的需要设置编码方式,以及建议将它勾选上

方式一:使用java.util.Properties读取

在这里插入图片描述

    @Test
    public void  test4() throws IOException {
        Properties properties = new Properties();

        FileInputStream fis = new FileInputStream("jdbc.properties");
        properties.load(fis);
        System.out.println(properties.getProperty("name"));
        System.out.println(properties.getProperty("age"));
	}

在这里插入图片描述
因为我们使用的是单例测试模式,那么此时的jdbc.properties文件应该放在当前module的目录之下

方式二:使用类加载器

在这里插入图片描述

 @Test
    public void  test4() throws IOException {
        Properties properties = new Properties();
        
        InputStream systemResourceAsStream = ClassLoader.getSystemResourceAsStream("jdbc1.properties");
        properties.load(systemResourceAsStream);
        
        System.out.println(properties.getProperty("name"));
        System.out.println(properties.getProperty("age"));
    }

在这里插入图片描述

此时的jdbc1.properties应该放在src目录下。而且在实际开发中,我们也会偏向于第二种。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值