Properties Files - pford68/groovy-examples GitHub Wiki
Reading Properties Files
-
Get the file with
new File("file path"). -
Then call
withInputStream (closure)on the File object.propertiesFile.withInputStream { properties.load(it) // "properties" is a Properties object created above. } -
Within the
withInputStreamclosure, callload(it)on the properties instance, as shown in the previous example, and in the example below.- "it" is the default closure parameter in Groovy, so it represents the prperties read from the InputStream and is being passed to
properties.load().
- "it" is the default closure parameter in Groovy, so it represents the prperties read from the InputStream and is being passed to
Example
For given test.properties file:
a=1
b=2
Properties properties = new Properties()
File propertiesFile = new File('test.properties')
propertiesFile.withInputStream {
properties.load(it)
}
// testing...
def runtimeString = 'a'
assert properties."$runtimeString" == '1'
assert properties.b == '2'