Need help loading text file

Hi All,

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

You check to see if f is nil, but you didn’t check to see if f exists. I prefer to do something like this:

[code]if f = nil or f.exists = false then
msgbox “File can’t be found”
return
end

try
Dim t As TextinputStream = TextInputStream.Open(f)
dim S as string = t.ReadAll
t.close
catch
//handle exception here
end[/code]

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.

I suspect that f is Nil though.