Created
July 2, 2018 10:46
-
-
Save valtoni/8396c2edd6c49c63b184eaf9cd47282b to your computer and use it in GitHub Desktop.
Facility to read and write scaped values to properties file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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