我有一个.properties文件,里面有很多评论,当我尝试更新文件中除评论之外的内容时,即使我的评论消失了,如何保留我的评论?
这是 config.properties 文件:
#name of user
name=user1
#id of user
id=id1
以及我使用的更新属性文件的代码
public class SetLog {
public static void setPath()
{
File file = new File("./config.properties");
Properties prop = new Properties();
FileInputStream objInput = null;
try {
objInput = new FileInputStream(file);
prop.load(objInput);
objInput.close();
FileOutputStream out = new FileOutputStream("./config.properties");
//update the content
prop.setProperty("name", "user2");
prop.store(out, null);
out.close();
} catch (Exception e) {}
}
}
运行上面的代码后,属性文件内容更改为:
name=user2
id=id1
但我想要这种格式:
#name of user
name=user2
#id of user
id=id1
如何保留我的评论,请帮忙!
最佳答案
这已在类似问题中得到回答:Adding comments in a Property File 。基本上,它可以归结为使用 Apache Commons 扩展来读取和写入属性文件。
关于java - 更新 .properties 文件后评论被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456669/