Skip to content

Instantly share code, notes, and snippets.

@valtoni
Created July 2, 2018 10:46
Show Gist options
  • Save valtoni/8396c2edd6c49c63b184eaf9cd47282b to your computer and use it in GitHub Desktop.
Save valtoni/8396c2edd6c49c63b184eaf9cd47282b to your computer and use it in GitHub Desktop.
Facility to read and write scaped values to properties file
class Property {
def static Properties instance
public static readProps(String file_name_prop) {
if (!file_name_prop) throw new Exception("You must provide filename to read properties: this is a static method")
def file_prop = new File(file_name_prop)
if (!file_prop.exists()) {
throw new Exception("The file ${file_name_prop} must exists")
}
def props = new Properties()
file_prop.withInputStream {
stream -> props.load(stream)
}
Property.instance = props
props
}
static void set(String key, String value) {
instance.setProperty(key, value)
}
static void save(String file_name_prop) {
if (!file_name_prop) throw new Exception("You must provide filename to save properties: this is a static method")
def file = new File(file_name_prop)
file.withWriter('UTF-8') { fileWriter ->
instance.each { key, value ->
fileWriter.writeLine("${key}=${value}")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment