java中使用Configuration类读取properties文件
简单记录读取properties文件的一种方式
jar包引用
<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>
代码
public static String getField(String key){
String value = null;
Configuration configuration = null;
try {
configuration = new PropertiesConfiguration("classpath:data.properties");
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
value = configuration.getString(key);
// value = configuration.getStringArray(key);
// ...
return value;
}