TextOutputStream.Create

Two questions

  1. Does textoutputstream.create delete a file if it already exists, or does it throw an error?

f = SpecialFolder.Preferences.Child(“com.app.plist”)
try
tos = TextOutputStream.Create(f)
Catch err As IOException
msgbox ( ioexception.message ) //This should work I think, but doesn’t.
End Try

  1. I can’t get Xojo to recognize an error message (ioexception.message), even though the autocomplete is saying it is valid syntax

I’m not sure if create deletes the file if it already exists so I can’t help you with that, but your problem with the second part should be that you are catching err as IOException. Therefore you don’t display the class message, but instead the err message. Using

f = SpecialFolder.Preferences.Child("com.app.plist")
try
tos = TextOutputStream.Create(f)
Catch err As IOException
msgbox (err.message ) //switch ioexception to err because err is the instance of ioexception caught by the catch block
End Try

should work.

Thanks, but weird. No err.message was returned. Just blank. At least it compiled. But it was not available to autocomplete.

I need to define my antecedents. The app compiled, but err.message was empty.

What error message are you expecting?

tos=TextoutputStream.create(f)

will create a new file assuming that “f” is a valid folder item, and the app has permission to write to that location.

Does it “delete” the existing file? Possibly… but since the request is CREATE… that is what would happen… delete or not, the existing contents (if any) are destroyed.

So… pre-existing or not… as long as valid folderitem and permission… a file will be created…

I am going nuts. Can you see where I am off in my code? It would write the file and I am not getting an error message (it is still blank).

dim f as folderitem
#if TargetMacOS
f = SpecialFolder.Preferences.Child(“com.teknia.flashworks.plist”)
#else
f = SpecialFolder.Preferences.Child(“com.teknia.flashworks.xml”)
#endif

If f <> Nil Then

Try
  Dim tos as TextOutputStream = TextOutputStream.Create(f)     //I get a nilobject error right here
  tos.Write "Stuff stuff stuff."
  tos.Close
  
Catch err As IOException
  msgbox ("Error writing the preferences file." + chr(13) + chr(13) + "(" + err.message + ")" )     //err.message has no content
End Try

end if

Should it be giving you an error? Is the message being shown as though there is an error but there is no error message supplied? It’s possible that there is no error message for the error that is occurring, if an error is occurring.

When I run the line, Dim tos as TextOutputStream, Xojo says it is a nilobject. I don’t think it should throw an error, but it does. I can’t see anything wrong in the code.

Are you saying it IS going ot the CATCH?
And you ARE getting a Msgbox… that says

because I copied you code verbatim… and it runs just fine

I DO get the nilobject error at TextOutputStream.Create(f), which cancels the app.

It DOES go to the catch, whcih I guess is right since an error has been generated.

But why does TextOutputStream.Create(f) throw an error, and why is err.message empty?

You need to create a new instance of TextOutputStream when you declare it. Add new to your declaration and it should fix that nilobjectexception. Like this:

dim tos as NEW TextOutputStream

No, Create is a shared method. It will work whether the object is nil or not. That is not what’s generating the exception. Also, your Catch shouldn’t be triggered, since you’re not catching a nilobjectexception. It should skip past it.

The only thing that could possibly trigger an exception on that line is “f”. Recheck your code.

Write permission may not be available to the user. Dave has already mentioned that above.

Your code works fine when used on Windows 7, Xojo 3.2 without any error and the resulting file “com.teknia.flashworks.xml” does contain the text “Stuff stuff stuff.”.

May be the f.IsWritable is False.

Okay. Here is a weird thing. When I compile the app, it works. When I run it in the development mode, it generates the error. No — it runs now. I didn’t do anything. Hmmm. Thanks for all your help. For whatever reason, it is working.

Bill:

you are not alone on that: I got there and get the IOException…

And this vanished alone.

And I do not know why.

if you debug run your project you should see a red error line marked. Hover over it with your mouse and it says what’s the problem.

Just a guess here… seeing as how it is not consistent.

Do you have some backup software? anti-virus? or something that may lock the drive for a few seconds?
and you just HAPPEN to hit that? just grasping straws here for you.

I am grasping too. Thanks. No virus software. I just can’t a consistent behavior. Sometimes the file is created, sometimes it is created with garbled content, and other times it seems nothing has happened. Xojo folks (that’s you, Jason), could this be an issue in Xojo?

I’m no Xojo folk, Bill, so I couldn’t tell you. File a feedback case if you can figure out how to replicate it is what I would say.

Has your app got a bundle id of com.teknia.flashworks ?
And your trying to create com.teknia.flashworks.plist ?
Name the file you’re trying to create something else just for fun (like comtekniaflashworks.plist)