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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Working with the file system<br />

The “Async” versions of the copy and move methods work asynchronously. Use the addEv<strong>en</strong>tList<strong>en</strong>er() method to<br />

monitor completion of the task or error conditions, as in the following code:<br />

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

original = original.resolvePath("AIR Test/test.txt");<br />

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

destination = destination.resolvePath("AIR Test 2/copy.txt");<br />

original.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.COMPLETE, fileMoveCompleteHandler);<br />

original.addEv<strong>en</strong>tList<strong>en</strong>er(IOErrorEv<strong>en</strong>t.IO_ERROR, fileMoveIOErrorEv<strong>en</strong>tHandler);<br />

original.moveToAsync(destination);<br />

function fileMoveCompleteHandler(ev<strong>en</strong>t:Ev<strong>en</strong>t):void {<br />

trace(ev<strong>en</strong>t.target); // [object File]<br />

}<br />

function fileMoveIOErrorEv<strong>en</strong>tHandler(ev<strong>en</strong>t:IOErrorEv<strong>en</strong>t):void {<br />

trace("I/O Error.");<br />

}<br />

The File class also includes the File.moveToTrash() and File.moveToTrashAsync() methods, which move a file<br />

or directory to the system trash.<br />

Deleting a file<br />

Adobe AIR 1.0 and later<br />

The File class includes a deleteFile() method and a deleteFileAsync() method. These methods delete files, the<br />

first working synchronously, the second working asynchronously (see “AIR file basics” on page 666).<br />

For example, the following code synchronously deletes the test.txt file in the user's docum<strong>en</strong>ts directory:<br />

var file:File = File.docum<strong>en</strong>tsDirectory.resolvePath("test.txt");<br />

file.deleteFile();<br />

The following code asynchronously deletes the test.txt file of the user's docum<strong>en</strong>ts directory:<br />

var file:File = File.docum<strong>en</strong>tsDirectory.resolvePath("test.txt");<br />

file.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.COMPLETE, completeHandler)<br />

file.deleteFileAsync();<br />

function completeHandler(ev<strong>en</strong>t:Ev<strong>en</strong>t):void {<br />

trace("Deleted.")<br />

}<br />

Also included are the moveToTrash() and moveToTrashAsync methods, which you can use to move a file or directory<br />

to the System trash. For details, see “Moving a file to the trash” on page 685.<br />

Moving a file to the trash<br />

Adobe AIR 1.0 and later<br />

The File class includes a moveToTrash() method and a moveToTrashAsync() method. These methods s<strong>en</strong>d a file or<br />

directory to the System trash, the first working synchronously, the second working asynchronously (see “AIR file<br />

basics” on page 666).<br />

Last updated 6/6/2012<br />

685

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

Saved successfully!

Ooh no, something went wrong!