Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1
File API | 77

var file =
air.File.applicationStorageDirectory.resolve( "myFile.txt" );

The call toFile.resolve( )creates a reference to a file named
myFile.txtlocated in the application storage directory. Once
a reference has been established, it can be used in file IO
operations. Note that this doesn’t actually create the file at
this point.


Physically reading and writing to disk is available using the
FileStreamclass. TheFileStreamclass does not take any
arguments in its constructor:


var stream = new air.FileStream( );

With the file reference available, and aFileStreamobject
instantiated, the process of writing data to disk can take
place. The type of data that can be written may be anything
from binary, to text, to value objects. These can all be
accessed by using the respectiveFileStreammethod for that
operation.


The first step in writing a file is to open it using the
FileStream.open( )method. TheFileStream.open( )method
takes two arguments. The first argument is the file reference
created earlier that will be the destination of the output. The
second argument is the type of access the application will
need to the file. In the case of writing data to a file, the
FileMode.WRITEstatic property will be most common. A
second possibility isFileMode.APPEND, depending on the
application requirements:


stream.open( file, air.FileMode.WRITE );

When writing text, an application should useFileStream.
writeMultiByte( )to ensure that data is written with the cor-
rect encoding. The FileStream.writeMultiByte( ) method
takes two arguments. The first argument is the String object
(text) that will be written to disk. The second argument is the
character set to be used. The most common character set is

Free download pdf