Java中加载配置文件的集中方法

第一种方法:用io流实现配置文件的加载

这种方法配置文件需要放在工程项目下跟目录下

<pre name="code" class="java"><pre name="code" class="java">
/** 从配置文件总读取配置内容 **/
InputStream ips=new FileInputStream("config.properties");
Properties props=new Properties();
props.load(ips);
/** 从配置文件中读取相应的值 **/
String classname=props.getProperty("classname");

 

 

第二种方法:使用类加载器实现配置文件的加载

这种方法配置文件需要放在classpath指定的目录下

/** 使用类加载器的方式加载配置文件 **/
InputStream ips=HashTest.class.getClassLoader().getResourceAsStream("com/bbp/test/config.properties");

/** 使用类内部的getResourceAsStream实现 **/
InputStream ips=HashTest.class.getResourceAsStream("config.properties");
InputStream ips=HashTest.class.getResourceAsStream("resouces/config.properties");
InputStream ips=HashTest.class.getResourceAsStream("/com/bbp/test/resouces/config.properties");



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值