【发布时间】:2015-05-26 22:00:46
【问题描述】:
我有一个 .properties 文件,其中包含多个 cmets,当我尝试更新 cmets 以外的文件内容时,即使我的 cmets 正在消失,如何保留我的 cmets?
这里是 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
如何留住我的cmets,请帮忙!
【问题讨论】:
-
可能是 XML 格式? storeToXML 和 loadFromXML?这也有助于以 UTF-8 的形式使用 Unicode。