添加或更新值时属性文件重置 (Java)

发布于 2025-01-21 02:50:15 字数 118 浏览 3 评论 0原文

我将用户数据(没什么重要的,只是用户ID和XP金额/级别)存储在属性文件中。使用prop.store(输出,注释)时,它会完全重置文件,然后存储添加的数据。如何在不重置整个文件的情况下添加数据?

I'm storing user data (nothing important, just user ID and xp amount/level) in a properties file. When using prop.store(output, comment) it resets the file completely and then stores the added data. How can I add data without it resetting the entire file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2025-01-28 02:50:15

每次使用 store() 方法时,它都会删除旧数据并写入新数据。

为了防止这种情况发生,您必须首先加载数据,然后使用要添加的新数据恢复它。

此代码片段可能会帮助您:

Properties prop = new Properties();
prop.load(new FileInputStream(YOUR_PROPERTIE_FILE));
prop.setProperty(key, value);
prop.store(new FileOutputStream(YOUR_PROPERTIE_FILE), COMMENT);

Each time you use the store() method, it removes the old data and writes the new data.

To prevent this from happening, you must first load your data and then restore it with new data that you want to add.

This code snippet may help you:

Properties prop = new Properties();
prop.load(new FileInputStream(YOUR_PROPERTIE_FILE));
prop.setProperty(key, value);
prop.store(new FileOutputStream(YOUR_PROPERTIE_FILE), COMMENT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文