2012-02-17 23 views
5

因此,如果我们这样做:如何将值保存到属性而不删除未修改的密钥?

Properties props = new Properties(); 
OutputStream osr = new FileOutputStream(Store.class.getResource("my.properties").getFile()); 

props.setProperty("wallboard_text_rgb", "aaa"); 
props.setProperty("wallboard_back_rgb", "bbb"); 

props.store(osr, ""); 

在现有的属性其他键将被删除,如何避免这种情况?

回答

4

修改该文件之前从该文件加载属性。换句话说,更换

Properties props = new Properties(); 

Properties props = Properties.load(new FileInputStream(Store.class.getResource("my.properties").getFile())); 
1

最简单的解决方法是使用

String filename = Store.class.getResource("my.properties").getFile(); 
OutputStream osr = new FileOutputStream(filename, true); // append. 

如果您不希望保留追加到该文件,你必须阅读所有现有的价值并重新编写它们。不幸的是,物业不保留订单,或评论,空白行等。

相关问题