You can use the TextIO structure for writing to files.
The openAppend lets you open a file in append mode, allowing you to append lines to the file if the file already exists, or create a file if none exists. This function provides you with an output stream you can use with the other IO functions.
The output function lets you write to a output stream. For instance:
TextIO.output(yourStream, "This is a message to write to your stream.");
Note, you need to remember to flush or close the stream, before you can be sure the data is actually written to the file on disk. Until you do one of those, the compiler may choose to delay writing the data to disk, to speed up execution.