根据网上的例子总结了一下。
其中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);
}
}
本文介绍了一个使用Java加载并读取属性文件(cache.properties)的例子。该示例展示了如何通过Class对象获取类路径下的资源流,并利用Properties类加载属性文件。
186

被折叠的 条评论
为什么被折叠?



