Skip to content
Philip Ford edited this page May 28, 2017 · 9 revisions

Writing to Files

Appending Content

You can also get a File object (passing the path to the File constructor) and append text to it with a << operator:

new File('/Users/paford/Documents/test.txt') << 'Hello!'

Overwriting Files

However, that method appends, what if you want to overwrite the file content completely? Groovy provides a File.write() method that does that:

new File('config.json').write JsonOutput.prettyPrint(JsonOutput.toJson(json))  //json is a JSON string

References

Clone this wiki locally