如何在不覆盖整个文件的情况下覆盖 .properties 中的一个属性? [英] How to overwrite one property in .properties without overwriting the whole file?

查看:26
本文介绍了如何在不覆盖整个文件的情况下覆盖 .properties 中的一个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我必须通过 Java 应用程序覆盖 .properties 文件中的某个属性,但是当我使用 Properties.setProperty() 和 Properties.Store() 时,它会覆盖整个文件,而不仅仅是那个属性.

Basically, I have to overwrite a certain property in a .properties file through a Java app, but when I use Properties.setProperty() and Properties.Store() it overwrites the whole file rather than just that one property.

我已经尝试使用 append = true 构建 FileOutputStream,但它会添加另一个属性并且不会删除/覆盖现有属性.

I've tried constructing the FileOutputStream with append = true, but with that it adds another property and doesn't delete/overwrite the existing property.

我该如何编码,以便设置一个属性覆盖该特定属性,而不覆盖整个文件?

How can I code it so that setting one property overwrites that specific property, without overwriting the whole file?

我尝试读取文件并添加到其中.这是我更新的代码:

I tried reading the file and adding to it. Here's my updated code:

FileOutputStream out = new FileOutputStream("file.properties");
FileInputStream in = new FileInputStream("file.properties");
Properties props = new Properties();

props.load(in);
in.close();

props.setProperty("somekey", "somevalue");
props.store(out, null);
out.close();

推荐答案

Properties API 不提供任何用于在属性文件中添加/替换/删除属性的方法.API 支持的模型是从文件加载所有属性,对内存中的 Properties 对象进行更改,然后将所有属性存储到一个文件(相同的或不同的).

The Properties API doesn't provide any methods for adding/replacing/removing a property in the properties file. The model that the API supports is to load all of the properties from a file, make changes to the in-memory Properties object, and then store all of the properties to a file (the same one or a different one).

但是 Properties API 在这方面并不罕见.实际上,在不重写整个文件的情况下很难实现文本文件的就地更新.这种困难是现代操作系统实现文件/文件系统方式的直接后果.

But the Properties API is not unusual in the respect. In reality, in-place updating of a text file is difficult to implement without rewriting the entire file. This difficulty is a direct consequence of the way that files / file systems are implemented by a modern operating system.

如果您确实需要进行增量更新,那么您需要使用某种数据库来保存属性,而不是.properties"文件.

If you really need to do incremental updates, then you need to use some kind of database to hold the properties, not a ".properties" file.

其他答案以各种形式提出了以下方法:

Other Answers have suggested the following approach in various guises:

  1. 将文件中的属性加载到 Properties 对象中.
  2. 更新 Properties 对象.
  3. Properties 对象保存在现有文件之上.
  1. Load properties from file into Properties object.
  2. Update Properties object.
  3. Save Properties object on top of existing file.

这适用于某些用例.然而,加载/保存可能会重新排序属性,删除嵌入的注释和空白.这些事情可能很重要1.

This works for some use-cases. However the load / save is liable to reorder the properties, remove embedded comments and white space. These things may matter1.

另一点是这涉及重写整个属性文件,OP 明确试图避免这种情况.

The other point is that this involves rewriting the entire properties file, which the OP is explicitly trying to avoid.

1 - 如果 API 是按照设计者的意图使用的,属性顺序、嵌入的注释等都无关紧要.但是让我们假设 OP 这样做是出于务实的原因".

1 - If the API is used as the designers intended, property order, embedded comments, and so on wouldn't matter. But lets assume that the OP is doing this for "pragmatic reasons".

这篇关于如何在不覆盖整个文件的情况下覆盖 .properties 中的一个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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