gpt4 book ai didi

java - 是否可以像 C# 中的 app.config 那样在 Java 中转换 .properties 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:42 27 4
gpt4 key购买 nike

我的项目中有包含调试设置的 debug.properties 文件。这个文件位于 repo 中,每个团队成员在调试之前都应该在这个文件中进行更改。

我可以在构建之前基于某些debug.template.properties 文件修改此文件,例如在 C# 中进行 xml 转换吗?

最佳答案

您可以使用 cfg4j库和以下代码:

private ConfigurationProvider configureProvider() {
ClassLoader classLoader = getClass().getClassLoader();
String envPath = classLoader.getResource("properties").getPath();
Path localPropertiesFilePath = Paths.get(envPath, "local.properties");
Path basePropertiesFilePath = Paths.get(envPath, "base.properties");

if (!Files.exists(localPropertiesFilePath)) {
try {
Files.createFile(localPropertiesFilePath);
}
catch (IOException exception) {
exception.printStackTrace();
}
}

ConfigFilesProvider localConfigFilesProvider =
() -> Collections.singletonList(localPropertiesFilePath);
ConfigFilesProvider baseConfigFilesProvider =
() -> Collections.singletonList(basePropertiesFilePath);

ConfigurationSource local = new FilesConfigurationSource(localConfigFilesProvider);
ConfigurationSource base = new FilesConfigurationSource(baseConfigFilesProvider);
// If you have 'some' property in base and local files - it takes from local
ConfigurationSource mergedSource = new MergeConfigurationSource(base, local);

Environment environment = new ImmutableEnvironment(envPath);

return new ConfigurationProviderBuilder()
.withConfigurationSource(mergedSource)
.withEnvironment(environment)
.build();
}

关于java - 是否可以像 C# 中的 app.config 那样在 Java 中转换 .properties 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39346280/

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