Just starting to learn Xojo. I am having a problem loading a text file. I always get an error on t = TextInputStream.Open(f)
The file is in a subdirectory under Documents. I made sure the file is in Documents/booking. Not sure why I am getting the error.
I can load the file using notepad.
Dim Documents As FolderItem = SpecialFolder.Documents
Dim S as String
If Documents <> Nil Then
Dim f As FolderItem = Documents.Child("/booking/test.txt")
If f <> Nil Then
Try
// TextOutputStream.Create raises an IOException if it can’t open the file for some reason.
Dim t As TextinputStream
t = TextInputStream.Open(f)
t.Encoding = Encodings.MacRoman
S= t.ReadAll
t = Nil
Catch e As IOException
// handle
End Try
End If
End If
Xojo does not recognize partial paths like that. Try this:
dim f as FolderItem = Documents.Child( "booking" )
if f.Exists then
f = f.Child( "test.txt" )
end if
if f is nil or f.Exists = false then
// Raise an exception or something
else
// Process it
end if
TextInputStream.Open throws exceptions when it runs into problems like missing files. It would be best to wrap that code in a try-Catch to catch IOExceptions as well.