Write Stream to File only when stream is finished

I am using a %Stream.FileCharacter object and LinkToFile API to write to a file on a folder. The file is written line by line . Something like below:-

Set tStream = ##class(%Stream.FileCharacter).%New()

Set tStatus =tStream.LinkToFile("MyFileName")

For i=1:1:X { Set result = ..process(i) Do tSream.WriteLine(result) }

An external process then picks up the file at a designated time. Sometimes the process to create this file takes longer than expected and the result is that the external process picks up an incomplete file !! Is there a way to do the write only after the stream is completely written?

Answers

You could buffer it up, but you will still have a period of writing to disk where the other collection process could grab it mid write.

I normally write the file to a temp folder or use a temp file name and then change the file name once its been fully written to. Making sure the collection process ignores the temp file extension or the temp folder location.