根据网上的例子总结了一下。
其中cache.properties放到src下,也可以放到WEB-INF下。
package test.bwl;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test {
private static Properties properties = new Properties();
public static void main(String[] args) {
try {
InputStream is = Test.class.getClassLoader().getResourceAsStream("cache.properties");
properties.load(is);
String size = properties.getProperty("cache.size");
writeLog("配置成功!" + size);
} catch (FileNotFoundException e) {
writeLog("配置文件不存在!" + e.getMessage());
} catch (IOException e) {
writeLog("读取配置文件IO错误!" + e.getMessage());
}
}
public static void writeLog(String strLog) {
System.out.println(strLog);
}
}