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