如何跳转到 Java 属性文件中的某个部分?

发布于 2024-08-06 16:45:40 字数 322 浏览 11 评论 0 原文

我有一个 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 部分,而不循环遍历文件并检查键值。

有办法做到这一点吗?

I have a Java properties file that has lots of different properties in it for different things:

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

The file is a lot bigger than this.

I want to jump to the ui.outputtype section without looping through the file and checking the key value.

Is there a way to do this?

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

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

发布评论

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

评论(7

想念有你 2024-08-13 16:45:40

您应该加载属性,然后可以通过其键获取值:

Properties props = new Properties();
props.load(new FileInputStream("my.properties"));

props.getProperty("ui.outputtype.snapshot");

You should load the properties, and then you can get the value by its key:

Properties props = new Properties();
props.load(new FileInputStream("my.properties"));

props.getProperty("ui.outputtype.snapshot");
暗喜 2024-08-13 16:45:40

由于 部分的概念“nofollow noreferrer”>Properties 类,必须想出某种方法来查找所需属性文件的部分

一种可能的方法是获取 Set 键,并找到包含所需前缀的键。

这就像对属性的键应用过滤器并挑选出所需的键。在这种情况下,我们可以将前缀视为属性文件中以点分隔的条目。

这里有一个小示例代码来演示总体想法:(

Properties p = new Properties();
p.put("hello.world", "42");
p.put("hello.earth", "1");
p.put("abc.def", "3");
p.put("def.ghi", "5");

// Go through the keys in the property.
for (Object propKey : p.keySet()) {
  String key = (String)propKey;

  // Select the keys with the prefix "hello."
  if (key.startsWith("hello.")) {
    System.out.println(key + ", " + p.getProperty(key));
  }
}

该代码不是很令人愉快,因为 Properies 没有泛型化。)

输出:

hello.world, 42
hello.earth, 1

在上面的示例中,没有从外部加载条目属性文件,但这可以使用 Properties.load 方法,如之前在其他答案中提到的。


有点不同的方法,其中键由所需的前缀过滤,然后可以通过简单的 for 循环读取信息。

以下是使用 Google Collections,可以过滤 特定 谓词。过滤结果(具有所需前缀的键)被提供给 for 循环,以获得键和值对:

Properties p = new Properties();
p.put("hello.world", "42");
p.put("hello.earth", "1");
p.put("abc.def", "3");
p.put("def.ghi", "5");

for (Object propKey : Collections2.filter(p.keySet(), new Predicate<Object>() {
  public boolean apply(Object o) {
    return ((String)o).startsWith("hello.");
  }
}))
{
  String key = (String) propKey;
  System.out.println(key + ", " + p.getProperty(key));
}

这可能有点过大,但它是实现过滤器的另一种方法使用所需的键缩小属性范围。

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:

Properties p = new Properties();
p.put("hello.world", "42");
p.put("hello.earth", "1");
p.put("abc.def", "3");
p.put("def.ghi", "5");

// Go through the keys in the property.
for (Object propKey : p.keySet()) {
  String key = (String)propKey;

  // Select the keys with the prefix "hello."
  if (key.startsWith("hello.")) {
    System.out.println(key + ", " + p.getProperty(key));
  }
}

(The code is not very pleasant because Properies is not genericized.)

Output:

hello.world, 42
hello.earth, 1

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 a Collection by a certain Predicate. The filtered result (keys which have the desired prefix) is given to the for loop in order to obtain the key and value pair:

Properties p = new Properties();
p.put("hello.world", "42");
p.put("hello.earth", "1");
p.put("abc.def", "3");
p.put("def.ghi", "5");

for (Object propKey : Collections2.filter(p.keySet(), new Predicate<Object>() {
  public boolean apply(Object o) {
    return ((String)o).startsWith("hello.");
  }
}))
{
  String key = (String) propKey;
  System.out.println(key + ", " + p.getProperty(key));
}

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.

£烟消云散 2024-08-13 16:45:40

您可以覆盖 Java Properties 类以实现属性文件中的部分处理:

@Override
public synchronized Object put(Object key, Object value) {
    String sKey = key.toString();
    if (sKey.startsWith("[") && sKey.endsWith("]"))

完整示例如下: http://devsite.pl/blog.2011-06-25 .java_ini_sections.html

You can override Java Properties class to achieve section handling in properties file:

@Override
public synchronized Object put(Object key, Object value) {
    String sKey = key.toString();
    if (sKey.startsWith("[") && sKey.endsWith("]"))

Full example is here: http://devsite.pl/blog.2011-06-25.java_ini_sections.html

微暖i 2024-08-13 16:45:40

是否 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?

戏蝶舞 2024-08-13 16:45:40

基本上,我的属性文件中有“部分”,该文件是由一位同事开发的,我正在考虑每个“部分”一个文件。

不管怎样,我对这一切如何运作的理解越来越深入,我想出的解决方案是:

for (String key : props.stringPropertyNames()) {
    // Jump to the relevant section of a properties file
    if (key.contains("datamode")) {
        OptionItem oi = new OptionItem(key, props.getProperty(key));
        // String value = props.getProperty(key);
        this.cbo_mode.add(oi);
    }
}

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:

for (String key : props.stringPropertyNames()) {
    // Jump to the relevant section of a properties file
    if (key.contains("datamode")) {
        OptionItem oi = new OptionItem(key, props.getProperty(key));
        // String value = props.getProperty(key);
        this.cbo_mode.add(oi);
    }
}
黒涩兲箜 2024-08-13 16:45:40

不。

嗯,我需要至少 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.

无力看清 2024-08-13 16:45:40

这是一个奇怪的要求。如果您担心性能,请在打包之前而不是在运行时执行此操作。

一个简单的:

cat settings.properties | grep ui.outputtype > mysmaller.properties

应该足够了!

This is a strange requirement. If you are worried about performance then do this before when packaging not at runtime.

A simple:

cat settings.properties | grep ui.outputtype > mysmaller.properties

should suffice!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文