如何从属性文件中删除键和值?我的属性文件包含以下内容:
key1=value1
key2=value2
我使用下面的代码删除条目 key2=value2
。之后,现在该文件具有以下值:
key1=value1
key2=value2
Wed Mar 06 12:36:32 IST 2013
key1=value1
删除条目的java代码:
FileOutputStream out1 = new FileOutputStream(file, true);
prop.remove(key);
prop.store(out1,null);
我在做什么错误。如何在写入文件之前清除文件的全部内容。
最佳答案
1) 属性文件内容应如下所示:
key1=value1
key2=value2
2) 您正在以追加模式打开文件,这是错误的。应该是:
new FileOutputStream(file);
3) 显式关闭 out1
,Properties.store API:
The output stream remains open after this method returns.
如果不想使用Properties.store,可以直接写Properties
PrintWriter pw = new PrintWriter("test.properties");
for(Entry e : props.entrySet()) {
pw.println(e);
}
pw.close();
关于java - 从属性文件中删除条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15241186/