如何更新属性文件? [英] How to update a property file?
问题描述
我正在使用 classLoader 方法加载属性文件.使用它,我能够检索属性的值,但现在我想更新某些属性的值,但我无法做到.请帮忙.这是代码:
i am using the classLoader method to load a property file. Using this, I am able to retrieve the values of properties but now I want to update the values of some properties and i am not able to do it. Please help. Here, is the code:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("PublishDate.properties");
InputStream inputStream = this.getClass().getClassLoader() .getResourceAsStream("PublishDate.properties");
try {
Properties properties = new Properties();
try {
// load the inputStream using the Properties
properties.load(inputStream);
}
catch(Exception e) {
e.printStackTrace();
}
// get the value of the property
String propValue = properties.getProperty("lastHtlProcessPublishDate");
在这里,在 propValue 中,我从文件中获得了正确的值.我正在更新这个:
here, in propValue i get the right value from file. I am updating this like this:
properties.put("lastHtlProcessPublishDate",dateFormatter.format(new Date()));
properties.store(new FileOutputStream("PublishDate.properties"), null);
使用此值不会更新,但是当我提供 PublishDate.properties
的完整路径时,它就可以工作了.但是,我不想给出完整的路径,因为路径是动态的.有人知道如何使用一些相对路径来做到这一点.请就此给我建议.
Using this the value doesn't get update but when i provide the complete path for PublishDate.properties
then it works.
But, I don't want to give the complete path as the path is dynamic. Does someone knows how to do this using some relative path. Please advise me on this.
推荐答案
你可能想试试 commons-configuration 并使用 PropertiesConfiguration.这应该可以满足您的所有需求.
You might want to try commons-configuration and use a PropertiesConfiguration. That should satisfy all your needs.
这篇关于如何更新属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!