1 Reply Latest reply on Jan 2, 2007 2:55 PM by Lukewig

    Is fileIO readfile() method asynchronous?

    PowerfulWebsites
      I'd like to use the fileIO extra to read in a text file, however, there may be instances in which the file is quite large. Initially, I'd planned to creating some sort of callback for the readfile() method however I cannot seem to find any documentation that indicates whether or not it is asynchronous or how to set up a callback in director for it.

      Any help is greatly appreciated.
        • 1. Re: Is fileIO readfile() method asynchronous?
          Lukewig Level 1
          Not sure if it is asynchronous (try reading a huge file), but you could create a wrapper object that reads line-by-line asynchronously. For example --


          -- Script "FileIOWrapper"
          property Listeners, IO, Output, Callback

          on AddListener (me, obj)
          if voidP(Listeners) then Listeners = []
          Listeners.append(obj)
          end

          on SetCallback (me, methodName)
          Callback = methodName
          end

          on ReadFile (me, fPath)
          IO = xtra("fileio").new()
          IO.openFile(fPath, 1)
          thread = timeout().new(me.string, 100, #ReadNext, me)
          end

          on ReadNext (me, thread)
          str = IO.readLine()
          if str <> "" then
          put str after Output
          else
          thread.forget()
          IO.closeFile()
          IO = VOID
          if voidP(Callback) then Callback = #TextRead
          call(Callback, Listeners, Output)
          end if
          end

          ----------------------
          Example use: (note - there is no need to store a reference to the wrapper object. The wrapper object creates a timeout object which has a reference back to the wrapper - enough to keep the wrapper alive. When the file is read, the wrapper kills the timeout which removes the last reference to the wrapper - neatly destroying the wrapper as well)

          on beginSprite (me)
          f = script("FileIOwrapper").new()
          f.addListener(me)
          f.readFile(the moviepath & "Notes on tracking")
          end

          on TextRead (me, t)
          alert ("text read!")
          put t
          end

          -- Luke