Here is a simple file writer. Others are very similar.
If you have a %Stream.GlobalCharacter and you want to write to a file use the copy method on the file in place of the .Write and .WriteLine in the example.
objFile.CopyFrom(stmInputData) where stmInputData is your %GlobalCharacterStream.
The class/method:
Class LHH.TestStream.Main Extends %Persistent
{
ClassMethod TestFile(
strDirectory As %String,
strFilename As %String) As %Status
{
Set sc=$$$OK
Set strDirectory=##class(%File).NormalizeDirectory(strDirectory)
Set strFullPath=strDirectory_strFilename
Set strFilename=##class(%File).NormalizeFilename(strFullPath)
Set objFile=##class(%FileCharacterStream).%New()
Write "Full path = ",strFullPath,!
Set objFile.Filename=strFullPath
If '$IsObject(objFile) {
If $Data(%objlasterror) Quit %objlasterror
Quit $$$ERROR($$$GeneralError,"Failed to open file")
}
Do objFile.Write("Hi there")
Do objFile.Write(" and more")
Do objFile.WriteLine(" and the end")
Set sc=objFile.%Save()
If $$$ISERR(sc) Write $system.Status.GetErrorText(sc),!
Else Write "Ok",!
Quit sc
}
}
Run the class:
>s sc=##class(LHH.TestStream.Main).TestFile("U:\lharris\test\","testfile.txt")
Output File:
-----------------------------
Hi there and more and the end
-----------------------------