Java属性文件追加新值 [英] Java Properties File appending new values
问题描述
我有一个实现JTree的应用程序,并使用java属性文件填充树作为默认值;节点是键,值是节点的内容。该应用程序设计为动态的,因此实现JButton和JTextField以接收新值并将值放在属性文件中的exists键中。
I have an application that implements a JTree and populates the tree with a java properties file as default; The nodes are the keys and the values are the contents of the node. The application was designed to be dynamic so a JButton and JTextField are implemented to take in new values and put the values in the exists keys in the properties file.
例如我将下面的行作为sample.properties文件中的默认值
So for example I have the line below as a default value in a sample.properties file
node = cat,dog,mice
node=cat,dog,mice
并使用JTextField和JButton输入rabbit以附加到节点,看起来像:
and using the JTextField and JButton I input "rabbit" to append to the node, to look like:
node = cat,dog,mice,rabbit
node=cat,dog,mice,rabbit
我已经实现了JTextField和JButton并使它们正常工作但我似乎无法找到一种将新值附加到属性文件中现有键的好方法。
I've implemented JTextField and JButton and have them working but I just can't seem to find a good way to append new values to existing keys in the properties file.
推荐答案
只需 FileWriter
FileWriter fileWritter = new FileWriter("example.properties", true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.append("PROPERTES_YOUR_KEY=PROPERTES_YOUR_VALUE");
bufferWritter.close();
更新
Properties API不支持,我不确定为什么你需要这个功能。
您可以尝试如下:
Properties API does not support, I am not sure why you need this functionality.
You can try as below :
example.properties
example.properties
PROPERTIES_KEY_3=PROPERTIES_VALUE_3
PROPERTIES_KEY_2=PROPERTIES_VALUE_2
PROPERTIES_KEY_1=PROPERTIES_VALUE_1
程序
Properties pop = new Properties();
pop.load(new FileInputStream("example.properties"));
pop.put("PROPERTIES_KEY_3", "OVERWRITE_VALUE");
FileOutputStream output = new FileOutputStream("example.properties");
pop.store(output, "This is overwrite file");
输出
PROPERTIES_KEY_3=OVERWRITE_VALUE
PROPERTIES_KEY_2=PROPERTIES_VALUE_2
PROPERTIES_KEY_1=PROPERTIES_VALUE_1
这篇关于Java属性文件追加新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!