Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1

80 | Chapter 4: AIR Mini-Cookbook


The steps for synchronously reading a file are almost always
the same:


1.Get aFile reference
2.Create aFileStream object
3.Open the stream for synchronous access
4.Call the appropriateFileStream read methods
5.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. Calling File.resolve( ) will
also be required when using this approach as the static File
class properties always return a directory:


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

TheFileStreamclass has an empty constructor and can be
instantiated anywhere in your application. The file reference
just established is used during the physical process of open-
ing the file. The mode for which the file is going to be
opened must also be specified.


TheFileModeobject serves no other purpose but to provide
constants for the types of file access that can be performed.
These operations are FileMode.READ, FileMode.WRITE,
FileMode.UPDATE andFileMode.APPEND:


var stream = new air.FileStream( );
stream.open( file, air.FileMode.READ );

There are threeFileStreammethods that can be used to read
character data from a file. TheFileStream.readUTF( )and
FileStream.readUTFBytes( )methods are specifically tuned
for UTF data.

Free download pdf