gpt4 book ai didi

java - 有没有办法在 Apache PropertiesConfiguration 中使用占位符

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

我使用 Apache 配置设置了以下配置:

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;

Configuration config = new PropertiesConfiguration("config.properties");

我想知道是否有一些方法可以在属性文件中使用占位符?例如,我想要这个:

some.message = You got a message: {0}

并且能够传入 {0} 占位符的值。通常,您通常可以执行类似 config.getString("some.message", String[] of values) 的操作,但看不到类似的内容。

最佳答案

据我所知,配置类不提供任何格式化程序。所以,为了你的任务

  1. 您可以按照 Bilguun 的建议使用 MessageFormat。请请注意,该格式化方法是静态

  2. 您可以使用String.format 函数。

示例如下:

config.properties

enter some.message.printf=%1$s\, you've got a message from %2$s \n
some.message.point.numbers =Hey {0}\, you got message: {1}!

示例类

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import java.text.MessageFormat;

public class ConfigurationTest {


public static void main(String[] args) throws ConfigurationException {
Configuration config = new PropertiesConfiguration("config.properties");

String stringFormat = String.format(config.getString("some.message.printf"), "Thomas", "Andrew");
// 1 String format
System.out.println(stringFormat);
// 2 Message Format
System.out.println(MessageFormat.format(config.getString("some.message.point.numbers"), "Thomas", "Hello"));
}
}

关于java - 有没有办法在 Apache PropertiesConfiguration 中使用占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35351181/

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