The configuration file consists of statements in the format key=value or key:value . Their possible way is when the key value can refer to another key value. The line between opening "$ {" and closing "}" is interpreted as a key. The value of the substituted variable can be defined as a system property or in the configuration file itself.
Because Properties inherits from Hashtable , the put and putAll can be applied to the Properties object .
Map<String, String> map = new LinkedHashMap<String, String>(); map.put("key", "vlaue"); Properties props = new Properties(); props.putAll( map );
work out the @Adam Gent message in detail. commons-text-1.1.jar
import org.apache.commons.text.StrLookup; import org.apache.commons.text.StrSubstitutor; public class Properties_With_ReferedKeys { public static void main(String[] args) { ClassLoader classLoader = Properties_With_ReferedKeys.class.getClassLoader(); String propertiesFilename = "keys_ReferedKeys.properties"; Properties props = getMappedProperties(classLoader, propertiesFilename); System.out.println( props.getProperty("jdk") ); } public static Properties getMappedProperties( ClassLoader classLoader, String configFilename ) { Properties fileProperties = new Properties(); try { InputStream resourceAsStream = classLoader.getResourceAsStream( configFilename ); Map<String, String> loadPropertiesMap = loadPropertiesMap( resourceAsStream ); Set<String> keySet = loadPropertiesMap.keySet(); System.out.println("Provided 'Key':'Value' pairs are..."); for (String key : keySet) { System.out.println( key + " : " + loadPropertiesMap.get(key) ); } fileProperties.putAll( loadPropertiesMap ); } catch ( IOException e ) { e.printStackTrace(); } return fileProperties; } public static Map<String,String> loadPropertiesMap( InputStream inputStream ) throws IOException { final Map<String, String> unResolvedProps = new LinkedHashMap<String, String>(); @SuppressWarnings("serial") Properties props = new Properties() { @Override public synchronized Object put(Object key, Object value) { unResolvedProps.put( (String)key, (String)value ); return super.put( key, value ); } }; props.load( inputStream ); final Map<String,String> resolvedProps = new LinkedHashMap<String, String>( unResolvedProps.size() );
Configuration file "If you want the link to be ignored and not replaced, you can use the format below.
$${${name}} must be used for output ${ Yash }. EX: jdk = ${jre-1.8}
File: keys_ReferedKeys.properties
Example Java properties (key = value) log4j.properties
Yash Dec 15 '17 at 9:14 2017-12-15 09:14
source share