属性文件:使用键作为变量 [英] Properties file : Use key as variable

查看:44
本文介绍了属性文件:使用键作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用属性文件中定义的键作为这样的变量:

I want to use a key defined in properties file as a variable like this :

key1= value1
key2= value2
key3= key1

我尝试:

key3= {key1}

key3= ${key1}

但这不起作用!

请问有什么想法吗?

推荐答案

Java的内置Properties类无法满足您的需求.

Java's built-in Properties class doesn't do what you're looking for.

但是有第三方库可以执行此操作.公用配置是我成功使用的一种. PropertiesConfiguration 类完全可以满足您的需求.

But there are third-party libraries out there that do. Commons Configuration is one that I have used with some success. The PropertiesConfiguration class does exactly what you're looking for.

所以您可能有一个名为 my.properties 的文件,如下所示:

So you might have a file named my.properties that looks like this:

key1=value1
key2=Something and ${key1}

使用该文件的代码可能看起来像这样:

Code that uses this file might look like this:

CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(new SystemConfiguration());
config.addConfiguration(new PropertiesConfiguration("my.properties"));

String myValue = config.getString("key2");

myValue 将是东西和值1" .

这篇关于属性文件:使用键作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆