I want to write a Log File containing the names of all Files processed by the program.Im wondering what will be the right approach to do this
1.Array-Should I store the filenames in a string array and later write the array to text file.Will this result in memory wastage?
2.The second approach would be to create a file and keep the pointer to it open and write the result to it directly without using an intermediate array.But I don't know how to do this.
Currently I'm writing the file using
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
    let fileURL = dir.appendingPathComponent(file)
    //writing
    do {
        try text.write(to: fileURL, atomically: false, encoding: .utf8)
    }
    catch {/* error handling here */}
    //reading
    do {
        let text2 = try String(contentsOf: fileURL, encoding: .utf8)
    }
    catch {/* error handling here */}
}
UPDATE:
As per @Martin 's comment I have followed the following approach.But I'm getting some random characters in the output file
  //--------------Setting Up Logging and FileStream
       let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
            .appendingPathComponent("log.txt")
        let logstream = OutputStream(url: fileURL, append: true)
        logstream?.open()
 //-------------------------------------
   writelog(stream: (logstream)!, fname: x.path)
   logstream.close()
 ----------------------------------
WriteLog Function
func writelog(stream:OutputStream,fname:String)
    {
        let text = fname
        let bytesWritten = stream.write(text, maxLength: 100)
        if bytesWritten < 0 { print("write failure") }
    }
Output:
/Users/me/Desktop/file1.jpg≠æ≠fifi¿›∫ÄZ+Ä`XTUM/Users/me/Desktop/file2.jpg≠æ≠fifi¿›∫Ä+JÄ`6@ø/Users/me/Desktop/test.jpgġˇˇˇˇˇÿ©ÔÕ´âˇˇˇˇ¿3©
Note: I had to set the Max Length attribute to 100 as the argument was required by the write function to work.