首先 ,类开头写上
private Properties properties = new Properties();
主方法里 这样写 String adds = getProperty("adds");
方法实现
public String getProperty(String name) {
String adds = null;
InputStream in = this.getClass().getResourceAsStream(
"acpInterface.properties");
if (in == null) {
throw new RuntimeException("acpInterface.properties not found");
}
if (!(in instanceof BufferedInputStream))
in = new BufferedInputStream(in);
try {
properties.load(in);
adds = properties.getProperty(name);
in.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(
"Error while processing acpInterface.properties", e);
}
return adds;
}
文件和 类放在同一包下面
本文介绍了一个简单的Java程序示例,展示了如何通过类路径加载属性文件并从中获取特定属性值的方法。此示例使用了标准的Java Properties类来处理配置文件。
225

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



