附加新值的 Java 属性文件 [英] Java Properties File appending new values

查看:16
本文介绍了附加新值的 Java 属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它实现了一个 JTree 并默认使用 java 属性文件填充树;节点是键,值是节点的内容.该应用程序设计为动态的,因此实现了 JButton 和 JTextField 以接收新值并将这些值放入属性文件的现有键中.

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=猫、狗、老鼠

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=猫、狗、老鼠、兔子

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.

推荐答案

Just FileWriter

FileWriter fileWritter = new FileWriter("example.properties", true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.append("PROPERTES_YOUR_KEY=PROPERTES_YOUR_VALUE");
bufferWritter.close();

更新

不支持属性 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆