13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with the file system<br />

var temp:File = File.createTempDirectory();<br />

The createTempDirectory() method automatically creates a unique temporary directory (saving you the work of<br />

determining a new unique location).<br />

You can use a temporary directory to temporarily store temporary files used for a session of the application. Note that<br />

there is a createTempFile() method for creating new, unique temporary files in the System temporary directory.<br />

You may want to delete the temporary directory before closing the application, as it is not automatically deleted on all<br />

devices.<br />

Enumerating directories<br />

Adobe AIR 1.0 and later<br />

You can use the getDirectoryListing() method or the getDirectoryListingAsync() method of a File object to<br />

get an array of File objects pointing to files and subfolders in a directory.<br />

For example, the following code lists the cont<strong>en</strong>ts of the user's docum<strong>en</strong>ts directory (without examining<br />

subdirectories):<br />

var directory:File = File.docum<strong>en</strong>tsDirectory;<br />

var cont<strong>en</strong>ts:Array = directory.getDirectoryListing();<br />

for (var i:uint = 0; i < cont<strong>en</strong>ts.l<strong>en</strong>gth; i++)<br />

{<br />

trace(cont<strong>en</strong>ts[i].name, cont<strong>en</strong>ts[i].size);<br />

}<br />

Wh<strong>en</strong> using the asynchronous version of the method, the directoryListing ev<strong>en</strong>t object has a files property that<br />

is the array of File objects pertaining to the directories:<br />

var directory:File = File.docum<strong>en</strong>tsDirectory;<br />

directory.getDirectoryListingAsync();<br />

directory.addEv<strong>en</strong>tList<strong>en</strong>er(FileListEv<strong>en</strong>t.DIRECTORY_LISTING, dirListHandler);<br />

function dirListHandler(ev<strong>en</strong>t:FileListEv<strong>en</strong>t):void<br />

{<br />

var cont<strong>en</strong>ts:Array = ev<strong>en</strong>t.files;<br />

for (var i:uint = 0; i < cont<strong>en</strong>ts.l<strong>en</strong>gth; i++)<br />

{<br />

trace(cont<strong>en</strong>ts[i].name, cont<strong>en</strong>ts[i].size);<br />

}<br />

}<br />

Copying and moving directories<br />

Adobe AIR 1.0 and later<br />

You can copy or move a directory, using the same methods as you would to copy or move a file. For example, the<br />

following code copies a directory synchronously:<br />

var sourceDir:File = File.docum<strong>en</strong>tsDirectory.resolvePath("AIR Test");<br />

var resultDir:File = File.docum<strong>en</strong>tsDirectory.resolvePath("AIR Test Copy");<br />

sourceDir.copyTo(resultDir);<br />

Last updated 6/6/2012<br />

682

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!