i/o error with TextOutputStream

The following works fine when I create a new file, but if I try and replace an existing file OS X warns me that I am about to overwrite the file and I get IOException 104.

My understanding is that TextOutputStream.Create should overwrite an existing file.

Any advice would be appreciated.

Thank you.

OS X El Capitan 10.11.5

Dim f As FolderItem = GetSaveFolderItem(FileTypes1.Text,“Export.xml”)

If f <> Nil then
Try
Error-> Dim t as TextOutputStream = TextOutputStream.Create(f)
Try
t.Write(sTest)
Finally
t.Close
t = nil
End Try
Catch e as IOException
//Error Message
//Return
End Try
End if

Is there anything in the Message property of the Exception?

Here is the same code a bit easier to read. (Pease use the Code tags in the future to preserve the indents and readability.)

  Dim f As FolderItem = GetSaveFolderItem(FileTypes1.Text,"Export.xml")
  
  If f <> Nil then
    Try
      Error-> Dim t as TextOutputStream = TextOutputStream.Create(f)
      Try
        t.Write(sTest)
      Finally
        t.Close
        t = nil
      End Try
    Catch e as IOException
      //Error Message
      //Return
    End Try
  End if

Are you attempting to save to a network volume, by chance? What happens if you call f.Delete before attempting to create the file?

No, Message and Reason properties are empty.

Oh, I can reproduce this exactly if I open Export.xml elsewhere (or internally) first.

  Dim f As FolderItem = GetSaveFolderItem(FileTypes1.Text,"Export.xml")
  
  If f <> Nil then
    //
    // Force it open to recreate the problem
    //
    dim tis as TextInputStream
    if f.Exists then
      tis = TextInputStream.Open( f )
    end if
    
    Try
      Dim t as TextOutputStream = TextOutputStream.Create(f)
      Try
        t.Write(sTest)
      Finally
        t.Close
        t = nil
      End Try
    Catch e as IOException
      //Error Message
      //Return
    End Try
  End if

No, not a network volume. I tried f.Delete with the same result.

I think Kem is onto it. I’m away for a while.

Thanks for looking at this for me. I’ll use Code Tags next time.

here ! hear !

Emile from Alsaceshire ;-

That was it.

If I open the xml file in Open Office (It’s an Excel XML export generated by the Xojo app) and leave it open I get the error. If it is opened in TextEdit or TextWrangler and leave it open there isn’t an error.

TextWrangler is elegant, it notes the file has changed and offers to re-load it whilst it is open.

Thank you for pointing me in the right direction.

Did you try to save the file using the same name and append -a (or -b…) to make the name different ?

I was doing that when I got IOException: that file was nearly never open; I had no reason to keep it open and if that was the case, I used TextEdit (Apple) to adjust its contents (move Column [then, no more now]], remove a part of text in a Column, etc.)

Nota: I do not have seen IOException since I put all the code in a Method and call it from the "Export As…” MenuHandler. (you can’t see me crossing my fingers with my two hands…) :wink:

Yes, but how can you be sure that your users (you) will not open that file or keep it open when (s)he use your application ?

You can’t. This is the nature of file locking. He’ll have to catch the exception of deal with the error code appropriately.