如何跳转到 Java 属性文件中的某个部分?
我有一个 Java 属性文件,其中有许多不同的属性用于不同的事情:
ui.datasource.st.name=MyTest
ui.datasource.st.port=111
ui.datasource.st.ip=1.1.1.1
ui.outputtype.snapshot=Snapshot
ui.outputtype.spreadsheet=Spreadsheet - xls
该文件比这个大得多。
我想跳转到 ui.outputtype 部分,而不循环遍历文件并检查键值。
有办法做到这一点吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您应该加载属性,然后可以通过其键获取值:
You should load the properties, and then you can get the value by its key:
由于 部分的概念“nofollow noreferrer”>
Properties
类,必须想出某种方法来查找所需属性文件的部分。一种可能的方法是获取
Set
键,并找到包含所需前缀的键。这就像对属性的键应用过滤器并挑选出所需的键。在这种情况下,我们可以将前缀视为属性文件中以点分隔的条目。
这里有一个小示例代码来演示总体想法:(
该代码不是很令人愉快,因为
Properies
没有泛型化。)输出:
在上面的示例中,没有从外部加载条目属性文件,但这可以使用
Properties.load
方法,如之前在其他答案中提到的。有点不同的方法,其中键由所需的前缀过滤,然后可以通过简单的
for
循环读取信息。以下是使用 Google Collections,可以过滤 特定
谓词
。过滤结果(具有所需前缀的键)被提供给 for 循环,以获得键和值对:这可能有点过大,但它是实现过滤器的另一种方法使用所需的键缩小属性范围。
Since there is no concept of a section in the
Properties
class, one would have to come up with some way to find the section of the property file which is wanted.One possible approach would be to obtain the
Set
of keys, and find the ones which contains the prefix which is wanted.This would be like applying a filter on the keys of the property and picking out the keys which are desired. In this case, we can think of the prefix as being the dot-separated entry in the property file.
Here's a little sample code to demonstrate the overall idea:
(The code is not very pleasant because
Properies
is not genericized.)Output:
In the example above, the one did not load the entries from the an external properties file, but that can be achieved trivially using the
Properties.load
method, as previously mentioned in the other answers.A little bit of a different approach, where the keys are filtered by the desired prefix, then the information can be read through a simple
for
loop.Here's an example using the
Collections2.filter
method from the Google Collections, which can filter aCollection
by a certainPredicate
. The filtered result (keys which have the desired prefix) is given to thefor
loop in order to obtain the key and value pair:This may be a little bit overkill, but it's another approach to implementing a filter to narrow down the properties with the desired keys.
您可以覆盖 Java Properties 类以实现属性文件中的部分处理:
完整示例如下: http://devsite.pl/blog.2011-06-25 .java_ini_sections.html
You can override Java Properties class to achieve section handling in properties file:
Full example is here: http://devsite.pl/blog.2011-06-25.java_ini_sections.html
是否
Properties.getProperty(String key)
没有做你想做的事?您是否正在尝试检索键以给定字符串开头的所有值?
Does
Properties.getProperty(String key)
not do what you want?Are you trying to retrieve all values whose key starts with a given string?
基本上,我的属性文件中有“部分”,该文件是由一位同事开发的,我正在考虑每个“部分”一个文件。
不管怎样,我对这一切如何运作的理解越来越深入,我想出的解决方案是:
Basically I have 'sections' in my properties file which was developed by a colleague, I was thinking of one file per 'section'.
Anyway, my understanding of how this all works is growing nicely and the solution I came up with was:
不。
嗯,我需要至少 15 个字符才能发布...所以让我们扩展一下答案。
我没有看到问题所在。 Java 没有神奇的方法来猜测您的信息在哪里。除非文件的重量有几兆字节,否则加载整个文件(就像属性那样,或者我认为是这样),并且也许消除不需要的数据(从生成的哈希表中),不应该花费那么多时间。更糟糕的是,如果内存是一个问题,则逐行手动解析它,应该不难,并且应该仍然相当快。
如果内存不是问题,则使用 propertyNames() 提供的枚举进行循环并检查给定字符串的键 startWith() 也不会花费太多时间。
No.
Mmm, I need at least 15 chars to post... So let's expand on the answer.
I don't see the problem. Java has no magical way to guess where your information is. And unless the file is weighting several megabytes, loading it whole (as Properties does, or so I think), and perhaps eliminating data you don't need (from the generated hash table), shouldn't take that much time. At worse, if memory is an issue, parse it manually line per line, shouldn't be hard and should be still quite fast.
If memory isn't the problem, looping with the enumeration provided by propertyNames() and checking that the keys startWith() the given string shouldn't take much time either.
这是一个奇怪的要求。如果您担心性能,请在打包之前而不是在运行时执行此操作。
一个简单的:
应该足够了!
This is a strange requirement. If you are worried about performance then do this before when packaging not at runtime.
A simple:
should suffice!