Created
May 16, 2014 07:42
-
-
Save js1972/22d9c24a62776d8e85cc to your computer and use it in GitHub Desktop.
How to write content to a new file (overwrite if already existing) in Groovy.
This file contains 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
// | |
// Write the mock request payload to a file for checking later... | |
// newWrite() is the important it to ensure you get a *new* file each time. | |
// | |
def filename = "C:\\MyScratchFolder\\soapUI projects\\Testing\\procon\\mock_po_activity_request.xml" | |
def file = new File(filename) | |
def w = file.newWriter() | |
w << mockRequest.requestContent | |
w.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! For future readers who get here from a web search (like I did), I think it's unnecessary to use both
newWriter
andwithWriter
in the examples above. Using justwithWriter
will create the file if it doesn't exist yet, or replace the existing content if it already exists:Similarly, using
withWriterAppend
will create the file if it doesn't exist (just like above), or append to the existing content if it already exists:Note that when using
withWriter
orwithWriterAppend
you can also specify a specific character encoding for the writer, and doing so is recommended as a best practice.