使用PropertiesConfiguration 读取Properties配置文件

1. 加依赖

<dependency>  
    <groupId>commons-configuration</groupId>  
    <artifactId>commons-configuration</artifactId>  
    <version>1.9</version>  
</dependency>  
<!-- commons-configuration 自动加载的是2.1的版本,编译时会报错,所以再加上这个 -->  
<dependency>  
    <groupId>commons-lang</groupId>  
    <artifactId>commons-lang</artifactId>  
    <version>2.6</version>  
</dependency>  

2. 小demo


// 引的是这个类,在未使用上面的依赖时,可能会发现PropertiesConfiguration这个类,可以引进来,
// 注意: 引的可能是同名的不同的类
import org.apache.commons.configuration.PropertiesConfiguration;

public class Test {

	public static void main(String[] args) throws Exception {
		//PropertiesConfiguration properties = new PropertiesConfiguration("src/main/resources/log4j.properties");
		String url = Test.class.getClassLoader().getResource("log4j.properties").toString();
		
		System.out.println(url);
		
		PropertiesConfiguration properties = new PropertiesConfiguration(url);
		
		System.out.println(properties.getString("key"));
		
//		int key = properties.getInt("key");
		
//		BigDecimal key1 = properties.getBigDecimal("key");
		
//		double key2 = properties.getDouble("key");
		
//		boolean key3 = properties.getBoolean("key");
		
//		float key4 = properties.getFloat("key");
		
//		byte key5 = properties.getByte("key");
		
//		String name = properties.getFile().getName();

	}
}

说明: log4j.properties 文件在 Resources/

参考: https://www.cnblogs.com/fengli9998/p/7341231.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值