Add existing key as value to a new key in a java property file

I am using a properties file in my java project for storing the path of various resources. Ex :- Here is my properties file :-

MACHINE_NAME = "//pranay"
Json_path1 = MACHINE_NAME//Json//j1.txt
Json_path2 = MACHINE_NAME//Json//j2.txt

The value of key MACHINE_NAME is not getting replaced with its value i.e pranay in another keys such as Json_path1 and Json_path2. Therefore i am unable to get the correct path. How to give the key MACHINE_NAME so that its value gets replaced in the other key values.

Jon Skeet
people
quotationmark

You can't do this automatically - it's simply not a feature of Java property files. You'll need to write code to do this wherever you plan to load/use the properties file.

You should think about:

  • whether you want to make this more explicit, e.g. using ${MACHINE_NAME} instead of just MACHINE_NAME
  • whether you have a fixed list of replacements you want to support, or whether you want to pick up anything that looks like it's a replacement (much easier if you have a syntax such as ${...} of course)
  • whether replacements can be recursive, e.g. MACHINE_NAME = ${USER_NAME}-laptop - and how to handle cycles if so

people

See more on this question at Stackoverflow