【发布时间】:2014-10-09 12:02:12
【问题描述】:
我正在尝试将我的配置数据保存在 config.properties 文件中。我正在像这样保存我的属性:
public static void setProperty(Parameter property) {
// set the property's value
prop.setProperty(property.getIndex(), property.getValue());
// save properties to project root folder
try {
prop.store(output, null);
} catch (IOException e) {
e.printStackTrace();
}
}
并像这样加载它们:
public static String getProperty(String componentName) {
// get the property value
return prop.getProperty(componentName);
}
一切正常,但是当我重新启动程序时,我没有任何属性了。我在程序开始时调用以下方法来加载我的属性文件:
static String FILE = "config.properties";
static InputStream input;
static OutputStream output;
static Properties prop = new Properties();
public static void loadExistingProperties() {
try {
input = new FileInputStream(FILE);
output = new FileOutputStream(FILE);
// load properties from file
prop.load(input);
System.out.println("amount of saved properties: " + prop.size());
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
谁能告诉我为什么我在重新启动程序后找不到我的属性?我试图让我的prop 出错的方式可能是错误的吗?我不知道如何以另一种方式做到这一点。
【问题讨论】:
-
您是否随时关闭输出流?
标签: java properties inputstream outputstream restart