gpt4 book ai didi

java - 如何通过前缀读取应用程序属性文件?

转载 作者:行者123 更新时间:2023-11-30 07:00:17 25 4
gpt4 key购买 nike

我试图首先将我的应用程序属性打印到java中,然后将其推送到thymeleaf html页面中。目的:允许用户使用 GET/POST 编辑属性文件。我当前的代码将在控制台上显示属性的值键和值(如果它等于某些值)。我怎样才能得到它只提取特定和多个前缀的地方?

代码/尝试

public class ReadPropertiesFile {

public static void readProp() {

try {
Properties prop = new Properties();
prop.load(ReadPropertiesFile.class.getResourceAsStream("/application.properties"));

Enumeration enuKeys = prop.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = prop.getProperty(key);
System.out.println(key + "= " + value);
}
} catch (FileNotFoundException e) {
//System.out.print("System cannot find file");
e.printStackTrace();
} catch (IOException e) {
//System.out.print("System cannot find file");
e.printStackTrace();
}
}
}

示例 application.properties

prefix.foo = bar@!car@!war@!scar
prefix.cool = honda@!toyota@lexus
some.feed = live@!stream@!offline
some.feed = humans@!dogs@!cat
noprefix = dont@!print@!me
host = host1@!host2@!host3

能够打印前缀的所有值和一些值。

最佳答案

public class ReadPropertiesFile {


public static void readProp() {

try {
Properties prop = new Properties();
prop.load(ReadPropertiesFile.class.getResourceAsStream("/application.properties"));

Enumeration enuKeys = prop.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = prop.getProperty(key);
if (key.startsWith("prefix") || key.startsWith("some")) {
System.out.println(key + "= " + value);
}
}
} catch (FileNotFoundException e) {
//System.out.print("System cannot find file");
e.printStackTrace();
} catch (IOException e) {
//System.out.print("System cannot find file");
e.printStackTrace();
}
}

这是你想要的吗?只打印以“prefix”或“some”开头的键?

关于java - 如何通过前缀读取应用程序属性文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41044548/

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