如何在 Java 属性中为多个键设置相同的值? [英] How can I set same Value for multiple Keys in Java properties?

查看:38
本文介绍了如何在 Java 属性中为多个键设置相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望实现类似 -

File.properties contains,

    prop1, prop2 = valueX
    prop3, prop4 = valueZ

where `props.getProperty("prop1");` should return `valueX`

我知道一种方法是在不同的键上再次写入相同的值,但这会使我的属性文件变得杂乱无章,并且无法提供用于业务目的的分析视图.

I know one way is to write the same value again different keys but that makes my property file cluttered and does not provide an analytical view for the business purpose.

PS:如果有可能,请描述同一个键出现不同值的情况.

PS: If it is somehow possible then, please describe the case where the same key appears with different values.

推荐答案

我希望实现类似的目标

I wish to achieve something like

File.properties 包含,

File.properties contains,

prop1, prop2 = valueX
prop3, prop4 = valueZ

where props.getProperty("prop1"); 应该返回 valueX

where props.getProperty("prop1"); should return valueX

.properties 文件格式XML 属性格式的 DTD 两者都只提供一次将单个键映射到相关文字值的功能.这些简单的格式不支持您所描述的内容.他们提供的唯一选择是分别为每个键指定一个值,根据需要复制值.

The documentation for the .properties file format and the DTD for the XML properties format both provide only for mapping a single key at a time to an associated literal value. These simple formats do not support what you describe. The only alternative they afford is to give a value for each key separately, duplicating values as needed.

这使我的财产文件变得杂乱无章,并且无法提供用于业务目的的分析视图.

that makes my property file cluttered and does not provide an analytical view for the business purpose.

我想这是旁观者的眼睛.就个人而言,我认为您的变化只是同一枚硬币的另一面.无论哪种方式,都不清楚具有相同值的键是否偶然或因为它们的值相同很重要.就编写供人类使用的此类文件而言,我更喜欢将密钥分开,因为这样更容易找到它们,并且更好地对它们进行分组和排序.

I guess that's in the eye of the beholder. Personally, I think your variation is just the flip side of the same coin. Either way, it is unclear whether keys bearing identical values do so incidentally or because it is important that their values be the same. And as far as writing such files for human consumption, I prefer the keys to be separate, as that makes them easier to find, and better affords grouping and sorting them.

如果有可能,请描述相同键出现不同值的情况.

If it is somehow possible then, please describe the case where the same key appears with different values.

嗯?每个键应该只有一个值.如果同一个键出现不止一次会发生什么,没有记录.

Huh? Each key should have exactly one value. It is not documented what should happen if the same key appears more than once.

也许您的意思是将相同的 value 分配给不同的 ,但我不明白这是为什么.尽管如此,这里:

Perhaps you mean the same value being assigned to different keys, but I don't understand why that's in any question. Nevertheless, here:

prop1 = valueX
prop2 = valueX
prop3 = valueZ
prop4 = valueZ

请注意,= 符号可以替换为 : 或者如果您愿意,可以仅替换为空格.如果您不喜欢那样,那么您可以定义自己的格式,并编写自己的代码将实例解析为 Properties 对象.

Note that the = symbols can be replaced with : or just whitespace if you prefer. If you don't like that then you can define your own format, and write your own code for parsing an instance into a Properties object.

这篇关于如何在 Java 属性中为多个键设置相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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