如何覆盖 .properties 中的一个属性而不覆盖整个文件?
基本上,我必须通过 Java 应用程序覆盖 .properties 文件中的某个属性,但是当我使用 Properties.setProperty() 和 Properties.Store() 时,它会覆盖整个文件,而不仅仅是那个属性。
我尝试使用append = true构造FileOutputStream,但是它添加了另一个属性并且不会删除/覆盖现有属性。
我如何编码才能设置一个属性覆盖该特定属性,而不覆盖整个文件?
编辑:我尝试读取文件并添加到其中。这是我更新的代码:
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();
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.
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?
Edit: 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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Properties
API 不提供任何在属性文件中添加/替换/删除属性的方法。 API 支持的模型是从文件加载所有属性,对内存中的Properties
对象进行更改,然后将所有属性存储到一个文件中(相同或不同的属性)不同的)。但
Properties
API 在这方面并不罕见。实际上,在不重写整个文件的情况下很难实现文本文件的就地更新。这种困难是现代操作系统实现文件/文件系统的方式的直接后果。如果您确实需要进行增量更新,那么您需要使用某种数据库来保存属性,而不是“.properties”文件。
其他答案以各种形式建议了以下方法:
Properties
对象中。Properties
对象。Properties
对象保存在现有文件之上。这适用于某些用例。然而,加载/保存可能会重新排序属性,删除嵌入的注释和空白。这些事情可能很重要1。
另一点是,这涉及重写整个属性文件,OP 明确尝试2 避免这种情况。
1 - 如果按照设计者的意图使用 API,则属性顺序、嵌入注释等将不重要。但让我们假设OP这样做是出于“务实的原因”。
2 - 避免这种情况并不实际;参见前面。
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-memoryProperties
object, and then store all of the properties to a file (the same one or a different one).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.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:
Properties
object.Properties
object.Properties
object on top of existing file.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.
The other point is that this involves rewriting the entire properties file, which the OP is explicitly trying2 to avoid.
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".
2 - Not that it is practical to avoid this; see earlier.
您可以使用 Apache Commons 配置中的 PropertiesConfiguration。
在版本 1.X 中:
从版本 2.0 开始:
You can use PropertiesConfiguration from Apache Commons Configuration.
In version 1.X:
From version 2.0:
我知道这是一个老问题,但这是工作代码(对问题中的代码进行了稍微修改,以防止在加载值之前删除它们):
I know this is an old question, but this is working code (slightly modified from the code in the question, to prevent the deletion of the values before we load them):
属性文件是为应用程序提供配置的一种简单方法,但不一定是进行编程的、特定于用户的自定义的好方法,正如您所发现的那样。
为此,我将使用 首选项 API。
Properties files are an easy way to provide configuration for an application, but not necessarily a good way to do programmatic, user-specific customization, for just the reason that you've found.
For that, I'd use the Preferences API.
我执行以下方法:-
I do the following method:-
该程序的输出将是:
The output of the program would be:
请仅使用更新文件行而不是使用属性,例如
Please use just update file line instead using Properies e.g.
如果您只想覆盖 1 个 prop,为什么不直接将参数添加到您的 java 命令中。无论您在属性文件中提供什么,它们都将被属性参数覆盖。
If you just want to override 1 prop why not just add parameter to your java command. Whatever you provide in your properties file they will be overrided with properties args.