I am reading a property file in Java.
Properties myProp = new Properties();
InputStream in = new FileInputStream(pathOfPropertyFile);
myProp.load(in);
in.close();
The values in the property file have references to Linux shell variables. For example, an entry in the property file might look like:
DATA_PATH=/data/${PROJECT}/${YEAR}${MONTH}${DAY}
I have to execute a shell script from java and so I have ProcessBuilder instance and also the environment variables (envMap
as given below):
List<String> command = new ArrayList<String>();
command.add(actualCommand);
command.add(param1);
command.add(param2);
ProcessBuilder processBuilder = new ProcessBuilder(command);
Map<String, String> envMap = processBuilder.environment();
The envMap
has the environment variables I require along with over one hundred (> 100) other environment variables which I do not require.
I want to replace the ${USER}
,${PROJECT}
,etc., from the property-value string "/home/${USER}/${PROJECT}/data"
with actual values from the shell.
I would consider iterating the Map as the last option(as the Map has between 100 and 200 elements to iterate) as it is not an efficient approach.
Please advise some approach that will fetch the environment variable enclosed by braces from the string, so that I can directly use the get() of the Map and replace. Or, any better approaches are most welcome.
Note: The reference offered ( Replace String values with value in Hash Map that made my question to look duplicate) is not the best fit in my case.