File API | 83
that event listeners must be created and registered on the
FileStream object.
The steps for asynchronously reading a file are almost always
the same:
1.Get aFile reference
2.Create aFileStream object
3.Create event handlers for processing data
4.Add event listeners for asynchronous operations
5.Open the stream for asynchronous access
6.Close the file
The first step to reading a text file is to get a reference to the
resource on disk. Establishing a reference can be accom-
plished by programmatically designating a path using the
appropriate property on the File object such as File.
applicationStorageDirectory:
var file =
air.File.applicationStorageDirectory.resolve( “myFile.txt” );
AFileStreaminstance is necessary in order to read or write to
the file:
stream = new air.FileStream( );
Before registering event handlers on aFileStreamobject,
those handlers will need to be created. The events that are
triggered by file IO operations using theFileStreamclass will
always pass an event object as an argument. The properties
on the event object will depend on the type of event being
raised. This object can be helpful in determining the target
FileStreamobject, how much data is available for reading,
how much data is waiting to be written, and so on:
function doProgress( event )
{
// Read all the data that is currently available
var data = stream.readMultiByte( stream.
bytesAvailable, air.File.systemCharset );