Reading Resource

I’m trying to read a text file from my Resources files and split it using the following but I get an I/O exception, the path looks to be correct. I have dragged the file into the copy files section

Dim f as folderItem = specialFolder.resources.child( “Australiana.txt” )

Dim tis as textinputStream = f )
Dim myText as string = tis.readall
Lines() = mytext.Split(&u0A)

Catch err as IOException
MsgBox “There was an error that prevented the application from reading the file “”” + f.displayName + “”"." + endOfLine + endOfLine + err.message

End Try

Not 100% sure what is happening here, but:
Instead of dragging Australiana.txt into the copy files area, just drag it into your open project.
If you do that, then at runtime you can refer to it without using file access at all.

Your code would become this:

//       ---  not needed  Dim f as folderItem = specialFolder.resources.child( “Australiana.txt” )
//       ---  not needed  Dim tis as textinputStream = f )

//       ---  not needed   Dim myText as string = tis.readall
Lines() = Australiana.Split(&u0A)

//       ---  not needed  tis.close

Never thought of that.
When I try I get a mismatch error expected Text but got String but don’t see how to turn it to text. Tried a few intermediate steps but syntax not right. The text file is UTF16

I think I am getting a bit confused with String and Text moving back from IOS :frowning:

I change everything to string, add UTF16 on the read and all seems to be working.
Keeping the file as a read will keep it hidden in the Content as well… thanks Jeff that helped though

Glad you got something working

Back with this issue, the following attempt to read a file using specialFolder.resource gives an ‘error 2’ not sure what that is. The text file is dragged into the CopyFiles root and shows the Path


Dim f as folderItem = specialFolder.resource(“SydneyInfo.txt”)
Dim tis as textinputStream = f )
Dim myText as string = tis.readall.DefineEncoding(Encodings.UTF16)
End Try

Solved sorry I forgot to label the copy files as Resources :frowning:

Glad you solved the problem.

Yet I’d prefer adding the text files already encoded as utf8 and read them as Jeff suggested.
It means some more work for you in preparing them, but in the end the code execution’d be smoother.

Are the files more visible to the end user on Windows doing it that way as opposed to adding by Copy Files? Also if I use UTF8 does that eliminate the special characters in other languages?

No, of course not. Do you know the difference between utf8 and utf16?

Adding them by Copy Files or dropping them in the IDE, the files will be stored in the Resources folder.
As Beatrix already said, the special characters will remain. My suggestion was to pre-convert the files to UTF8 in order to avoid define-converting them BEFORE using them in the app.
In other words, if my external files are expected to remain unchanged for ever, I prefer to make them ready for use (i.e. editing and encoding them) before adding them either by Copy Files or by dropping.

Have you thought of creating, for each file, a constants in the IDE?

1 Like

I wasn’t 100% sure but I thought it was preferred on Windows for compatibility, after reading a bit more sounds like UTF8 should be used… correct?

Upon further thought I could but have in the past left the data as text files if teachers wanted to edit or add to the files that the program used, which probably doesn’t happen too often.

utf8 is a bit more compact than utf16. In most situations utf8 is fine.

With UTF8 there are the 2 characters at the beginning you need to skip, is there a better way to do it than this

Dim myText as string = tis.readall
myText = myText.right(myText.length-2)

That’s only if the editor you use adds the BOM mark. Most (that I’ve used) don’t. If you’re preprocessing the files anyway, strip it out.


Personally, I would recommend researching and understanding the BOM, that way you’re protecting yourself for any future changes or if you allow the user to edit your file.


The number of apps I encounter on a regular basis that choke on a BOM …

If you could potentially have users modifying the files then it will be wise to check for the BOM so that you can read the data no matter the Unicode encoding. If the input is anything but UTF-8 I recommend that you perform a ConvertEncoding to UTF-8 afterwards as that is the native encoding for Xojo.

By the way - you should always imagine the word BOM in the voice of Peter Sellers playing Inspector Clouseau.