Java 如何在一个.properties文件的末尾添加属性
我正在使用下面的代码在.properties文件中编写属性Java 如何在一个.properties文件的末尾添加属性,java,properties,eclipse-plugin,Java,Properties,Eclipse Plugin,我正在使用下面的代码在.properties文件中编写属性 InputStream inputStream = null; try { inputStream = file.getContents(); Properties properties = new Properties(); properties.load(inputStream); inputStream.close(); properties.
InputStream inputStream = null;
try {
inputStream = file.getContents();
Properties properties = new Properties();
properties.load(inputStream);
inputStream.close();
properties.setProperty(propertyKey.trim(), propertyValue.trim());
File file1 = file.getRawLocation().makeAbsolute().toFile();
FileOutputStream outputStream = new FileOutputStream(file1);
properties.store(outputStream, null);
outputStream.close();
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (CoreException ex) {
ex.printStackTrace();
}
虽然它是在排序位置添加属性,但不是在末尾。但我希望它始终添加到末尾的
。我如何才能得到它?使用FileWriter
将其追加到文件的末尾(构造函数的第二个参数定义追加模式):
true告诉它将新内容附加到文件的末尾
阅读更多信息如果您检查属性文件java doc,您可以找到它使用哈希表存储键值对,键的顺序也不保证。因此,一旦完成文件操作,您就需要使用文件操作来重新加载属性文件。Thank u.但它也在附加文件的现有内容。Thank u.但它也在附加文件的现有文件内容。@Anu请检查属性对象中存在的数据。我猜您正在将整个文件内容加载到其中,这就是为什么它也会附加现有文件内容的原因。只需确保您在属性对象中只具有所需的密钥。因为您似乎正在使用
IFile
,所以应该使用IFile.setContents
来保存文件。您当前的方法将使该文件与Eclipse不同步。是的,右。它显示的是与Eclipse不同步的文件。
InputStream inputStream = null;
try {
inputStream = file.getContents();
Properties properties = new Properties();
properties.load(inputStream);
inputStream.close();
properties.setProperty(propertyKey.trim(), propertyValue.trim());
File file1 = file.getRawLocation().makeAbsolute().toFile();
FileOutputStream outputStream = new FileOutputStream(file1);
properties.store(outputStream, null);
outputStream.close();
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (CoreException ex) {
ex.printStackTrace();
}
FileOutputStream outputStream = new FileOutputStream(file1,true);