以下是需要获得配置文件的类,其中获得 /config.properties 文件的数据
package com.chinacache.boss.queryservice.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public final class ConfigConstant {
static Logger logger = Logger
.getLogger(ConfigConstant.class);
public static Properties p = new Properties();
public static void init() {
InputStream in = null;
try {
in = ConfigConstant.class
.getResourceAsStream("/config.properties");
if (in != null)
p.load(in);
} catch (IOException e) {
e.printStackTrace();
logger.error("load " + "/config.properties" + " into Contants error");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
public static String getProperty(String key, String defaultValue) {
return p.getProperty(key, defaultValue);
}
public static String getProperty(String key) {
return p.getProperty(key);
}
public static Object setProperty(String key, String Value) {
return p.setProperty(key, Value);
}
}
此句是用来调用/config.properties 里的selectdays属性值:
String shouldLastDaysStr=ConfigConstant.getProperty("selectdays");
/config.properties 文件内容:
selectdays=33basicbak=200808