gpt4 book ai didi

java - Java .properties 文件中与键值模式不匹配的行会发生什么情况

转载 作者:行者123 更新时间:2023-12-01 23:26:10 24 4
gpt4 key购买 nike

我有一个java库,我需要将其视为黑匣子,并且需要类路径上存在一个属性文件。但是,我有一个系统,只允许提供包含有效 xml 的配置文件。

由于 jar 的黑盒性质,我无法使用 loadFromXml() 方法重新编译,或要求所有者这样做。

为了解决这种明显的不匹配问题,我提供了一个如下所示的配置文件:

<dummy>
key1=value1
key2=value2
</dummy>

这工作正常并提供了我想要访问的值。

我找不到有关此格式错误的属性文件的第一行和最后一行发生的情况的引用。

有人知道会发生什么吗?他们有更好的解决方案吗?

最佳答案

您最终得到键 "<dummy>" 的空字符串值和"</dummy>" 。您可以轻松地展示这一点:

import java.io.*;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
try (InputStream input = new FileInputStream("test.properties")) {
properties.load(input);
}

for (String key : properties.stringPropertyNames()) {
System.out.println(key + " = '" + properties.get(key) + "'");
}
}
}

这至少在 documentation 中有所表明。与这部分:

As a third example, the line:

cheeses

specifies that the key is "cheeses" and the associated element is the empty string "".

...但它并没有写得那么清楚。

关于java - Java .properties 文件中与键值模式不匹配的行会发生什么情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19959641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com