Properties Files - pford68/groovy-examples GitHub Wiki

Reading Properties Files

  1. Get the file with new File("file path").

  2. Then call withInputStream (closure) on the File object.

    propertiesFile.withInputStream {
       properties.load(it)   // "properties" is a Properties object created above.
    }
    
  3. Within the withInputStream closure, call load(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().

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'

References