从 Properties 获取 int、float、boolean 和 string

新手上路,请多包涵

我有属性文件中的 int、float、boolean 和 string。一切都已加载到属性中。目前,我正在解析值,因为我知道特定键的预期值。

 Boolean.parseBoolean("false");
Integer.parseInt("3")

设置这些常量值的更好方法是什么,如果我不知道键的原始值数据类型是什么。

 public class Messages {

    Properties appProperties = null;
    FileInputStream file = null;

    public void initialization() throws Exception {

        appProperties = new Properties();
        try {

            loadPropertiesFile();

        } catch (Exception e) {
            throw new Exception(e.getMessage(), e);
        }
    }

    public void loadPropertiesFile() throws IOException {

        String path = "./cfg/message.properties";
        file = new FileInputStream(path);
        appProperties.load(file);
        file.close();
    }
}

属性文件。消息.properties

 SSO_URL = https://example.com/connect/token
SSO_API_USERNAME = test
SSO_API_PASSWORD = Uo88YmMpKUp
SSO_API_SCOPE = intraday_api
SSO_IS_PROXY_ENABLED = false
SSO_MAX_RETRY_COUNT = 3
SSO_FLOAT_VALUE = 3.0

常数.java

 public class Constants {
    public static String SSO_URL = null;
    public static String SSO_API_USERNAME = null;
    public static String SSO_API_PASSWORD = null;
    public static String SSO_API_SCOPE = null;
    public static boolean SSO_IS_PROXY_ENABLED = false;
    public static int SSO_MAX_RETRY_COUNT = 0;
    public static float SSO_FLOAT_VALUE = 0;
}

原文由 Pratiyush Kumar Singh 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 738
2 个回答

如果你有一类配置值,比如你的 Constants 类,并且你想从配置(属性)文件中加载所有值,你可以创建一个小帮助类并使用反射:

 public class ConfigLoader {
    public static void load(Class<?> configClass, String file) {
        try {
            Properties props = new Properties();
            try (FileInputStream propStream = new FileInputStream(file)) {
                props.load(propStream);
            }
            for (Field field : configClass.getDeclaredFields())
                if (Modifier.isStatic(field.getModifiers()))
                    field.set(null, getValue(props, field.getName(), field.getType()));
        } catch (Exception e) {
            throw new RuntimeException("Error loading configuration: " + e, e);
        }
    }
    private static Object getValue(Properties props, String name, Class<?> type) {
        String value = props.getProperty(name);
        if (value == null)
            throw new IllegalArgumentException("Missing configuration value: " + name);
        if (type == String.class)
            return value;
        if (type == boolean.class)
            return Boolean.parseBoolean(value);
        if (type == int.class)
            return Integer.parseInt(value);
        if (type == float.class)
            return Float.parseFloat(value);
        throw new IllegalArgumentException("Unknown configuration value type: " + type.getName());
    }
}

然后你这样称呼它:

 ConfigLoader.load(Constants.class, "/path/to/constants.properties");

您可以扩展代码以处理更多类型。您还可以更改它以忽略缺少的属性,而不是像现在这样失败,这样字段声明中的分配将保持不变,即默认。

原文由 Andreas 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您知道常量的类型,则可以使用 Apache Commons Collections

例如,您可以根据常量类型使用一些实用程序方法。

 booelan SSO_IS_PROXY_ENABLED = MapUtils.getBooleanValue(appProperties, "SSO_IS_PROXY_ENABLED", false);
String SSO_URL = MapUtils.getString(appProperties, "SSO_URL", "https://example.com/connect/token");

您甚至可以使用默认值来避免错误。

原文由 josivan 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题