不需要另外的jar包,直接使用jdk提供的即可,配置文件在/src目录下,名称为test.properties内容如下
key = var
使用方法如下,
package com.caipeng;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
* 读取配置文件properties
*
* @author CaiPeng
*
*/
public class PropertiesTest {
private static Logger logger = Logger.getLogger(PropertiesTest.class);
private static Properties testProperties;
static {
testProperties = new Properties();
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
testProperties.load(loader.getResourceAsStream("test.properties"));
} catch (Exception e) {
logger.error("test.properties配置文件读取异常", e);
}
// 使用
String var = testProperties.getProperty("key");
logger.info("key = " + var);
}
}