如何在java中更新属性文件
在我的swing应用程序中,我动态创建组件,然后单击“保存”按钮将其值存储到属性文件中
在每个组件的值更改事件中,我获取值并将其放入地图中。单击“保存”按钮时,我迭代并将值存储到属性文件中。下次打开窗口时,如果属性文件存在,我会从文件中读取属性,并在渲染时为每个组件设置值
再次更新某些值后,单击“保存”按钮将删除以前的值,并且文件仅包含更改的值
如何更新属性文件而不丢失以前的值
以下是我编写属性的代码:
Properties properties = new Properties();
fileOutputStream = new FileOutputStream(propertiesFile);
Iterator itr = attributeMap2.entrySet().iterator();
while (itr.hasNext()) {
Entry<CategoryAttributeObj, String> entry = (Entry<CategoryAttributeObj, String>)itr.next();
properties.setProperty(entry.getKey(), entry.getValue());
}
properties.store(fileOutputStream, "storing index values to properties file");
fileOutputStream.close();
以下是我读取属性的代码:
Properties properties = new Properties();
try {
fileInputStream = new FileInputStream(propertiesFile);
properties.load(fileInputStream);
fileInputStream.close();
}
共 (0) 个答案