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” )
Try
Dim tis as textinputStream = textinputStream.open( f )
Dim myText as string = tis.readall
Lines() = mytext.Split(&u0A)
tis.close
Catch err as IOException
MsgBox “There was an error that prevented the application from reading the file “”” + f.displayName + “”“.” + endOfLine + endOfLine + err.message
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 = textinputStream.open( 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
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
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”)
Try
Dim tis as textinputStream = textinputStream.open( f )
Dim myText as string = tis.readall.DefineEncoding(Encodings.UTF16)
tis.close
End Try
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?
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.
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.
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.
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.