Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1
File API | 89

Creating a Temporary File


Problem


An application needs to store transient information during
file processing, and cannot assume that adequate memory
exists to store the data in memory.


Solution


Creating temporary files withFile.createTempFile( )is an
ideal means to store transient information while relieving the
overhead of additional memory.


Discussion


The File class contains a static File.createTempFile( )
method which can be used to establish a temporary file. The
temporary file is created at a destination determined by the
operating system. Temporary files are also automatically
given a unique name to avoid collision with other files that
may be present:


var temp = air.File.createTempFile( );

Once a temporary file has been created, the otherFileand
FileStreammethods may be used as in processing a known
file at a known location:


var stream = new air.FileStream( );

stream.open( temp, air.FileMode.WRITE );
stream.writeMultiByte( "Hello", air.File.systemCharset );
stream.close( );

TheFile.moveTo( )andFile.moveToAsync( )can be used after
the fact, should you decide that it is necessary to keep the
temporary file for later reference. Both move methods take
two arguments. The first argument is a File reference to the
destination location. The second argument is a Boolean value
which controls overwriting any existing file. If the second

Free download pdf