Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For reading property files in Groovy you can use the utility class <a href="http://groovy.codehaus.org/ConfigSlurper" rel="noreferrer">ConfigSlurper</a> and access the contained properties using GPath expressions. However, you have to be aware that <code>ConfigSlurper</code> doesn't support standard Java property files. Normally the <code>ConfigSlurper</code> will be used to read .groovy files that may be similar to a property file, but adhere to standard groovy notation, thus Strings are inside quotes and comments start with <code>//</code> or are inside a <code>/* */</code> block. So, to read a Java properties file you need to create a <code>java.util.Properties</code> object and use that to create a <code>ConfigSlurper</code>:</p> <pre><code>def props = new Properties() new File("message.properties").withInputStream { stream -&gt; props.load(stream) } // accessing the property from Properties object using Groovy's map notation println "capacity.created=" + props["capacity.created"] def config = new ConfigSlurper().parse(props) // accessing the property from ConfigSlurper object using GPath expression println "capacity.created=" + config.capacity.created </code></pre> <p>If you only use the property file from within Groovy code you should use the Groovy notation variant directly. </p> <pre><code>def config = new ConfigSlurper().parse(new File("message.groovy").toURL()) </code></pre> <p>This also gives you some nice advantages over standard property files, e.g. instead of </p> <pre><code>capacity.created="x" capacity.modified="y" </code></pre> <p>you can write</p> <pre><code>capacity { created="x" modified="y" } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload