Writing text files

I am having trouble writing successive text files out of my program. It was working but now crashes with I/O error -1407 on the 3rd file and it doesn’t matter which order I do them in. It is an old program and I noticed I originally used fs.CreatTextFile so I changed that to TextOutputStream.Create(fs). I reuse fs each time


  dlg=New SaveAsDialog                        
  dlg.initialDirectory = fs                     
  dlg.Filter = "text/Html"
  dlg.promptText="Choose a location to save your Web SOLUTION." 
  dlg.Title="Web Page Location"                           
  if fs <> Nil then
    tout = TextOutputStream.Create(fs)
    fs.MacCreator = "html"
    solution = 1
    tout.writeline "<HTML>"
    tout.writeline "<HEAD>"

Do you get a crash or an exception? When does the “crash” occur? Beginning or end of writing?

Have you researched the error? Have you tried to write everything at once?

Hi Beatrix,
I get an I/O Exception, at the beginning of writing.

I looked online for -1407 but could find anything, I wasn’t sure if that is a XOJO or an Apple error code.

I tried allocating a seperate separate folder variable to each file but that didn’t work.

errFSNotAFolder               = -1407, /* Expected a folder, got a file *

For future reference, you can find a list of macOS error codes at:


I think that you’re confusing things here. On the 2nd line, you use “fs” as a folder, but then you use it as a file.

[quote=402730:@Martin Fitzgibbons]dlg=New SaveAsDialog
dlg.initialDirectory = fs

tout = TextOutputStream.Create(fs)

I would suggest using two separate variables. “targetFile” and “lastFolder”. Once you’ve finished writing the file you create “lastFolder” by storing targetFile.parent.

[code]dlg=New SaveAsDialog
dlg.initialDirectory = lastFolder
dlg.Filter = “text/html”
dlg.promptText=“Choose a location to save your Web SOLUTION.”
dlg.Title=“Web Page Location”

if targetFile <> Nil then
tout = TextOutputStream.Create(targetFile)
// fs.MacCreator = “html” — This is no longer needed.
solution = 1
tout.writeline “”
tout.writeline “”
lastFolder = targetFile.parent[/code]

Thanks Sam, that’s an awesome clean up. As I said an old program where I am probably introducing a few errors as I try and update it.


you can start (the next time) by reading the LR. For today’s example:


Read especifically the example and notice the Try / Catch / End Try lines…

It will be of Great Help. Cheers !