How to write the result of the command

No, you can not save the output from System.out into file. Why? See this post

Of course you can write a string into an arbitrary file directly by your Groovy script. If you want to see an example of java.io.PrintStream#println() usage, for example I found the following:

You need to manage file (open it, println into it, close it) for yourself.