Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties file : Use key as variable

Tags:

java

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

key1= value1
key2= value2
key3= key1

I try :

key3= {key1}

or

key3= ${key1}

But it dosn't work !

Any idea please ?

like image 442
Khalil Pokari Avatar asked Jun 27 '12 14:06

Khalil Pokari


People also ask

How use variables in properties file?

When you define the value of a key in properties file, it will be parsed as literal value. So when you define key3= ${key1} , key3 will have value of "${key1}".

How get values from properties file?

Core Java bootcamp program with Hands on practice The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties. load() method of Properties class is convenient to load . properties file in the form of key-value pairs.

Where is key properties file?

Our key. properties file is in flutter_app/android/key. properties .


1 Answers

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

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.

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 will be "Something and value1".

like image 137
csd Avatar answered Oct 19 '22 13:10

csd