Home arrow Flash arrow Page 4 - Using the File System API
FLASH

Using the File System API


Adobe's Apollo is a cross platform desktop runtime. Use it with Flex, and you can build Flash-based Rich Internet Applications (RIA). This article, the first of two parts, shows you how to use Apollo's file system API. It is excerpted from chapter four of the book Apollo for Adobe Flex Developer's Pocket Guide, written by Mike Chambers, Rob Dixon and Jeff Swartz (O'Reilly, 2007; ISBN: 0596513917). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
March 13, 2008
TABLE OF CONTENTS:
  1. · Using the File System API
  2. · Accessing Files and Directories
  3. · URI Schemes
  4. · Asynchronous and Synchronous Versions of Methods

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using the File System API - Asynchronous and Synchronous Versions of Methods
(Page 4 of 4 )

Some of the methods of the File class (such as
File.copyFile()andFile.copyFileAsync()) and of theFileStream class have both synchronous and asynchronous versions.

The synchronous methods don’t relinquish control until the file operation is complete. The asynchronous methods run in the background, allowing other ActionScript processes to take place at the same time. When the asynchronous file operation finishes, an event is dispatched to notify listeners that it is done.

Here’s an example of copying a file using the synchronouscopyTo()method:

  var file1:File = File.documentsDirectory.
  resolve("ApolloTest/test.txt");
  var file2:File = File.documentsDirectory.
  resolve("ApolloTest/copy of test.txt");
  file1.copyTo(file2);
  trace("Not output until the file is copied.");

Here’s an example of copying a file using the asynchronouscopyToAsync()method:

  var file1:File = File.documentsDirectory.
  resolve("ApolloTest/test.txt");
  var file2:File = File.documentsDirectory.
  resolve("ApolloTest/copy of test.txt");
  file1.copyToAsync(file2);

  file1.addEventListener(Event.COMPLETE, completeHandler);
  trace("This line executes before the complete event.");
  trace("So does this line.");

  private function completeHandler(event:Event):void {
    trace("Done.");
  }

The following table lists the asynchronous methods of theFile class (all of which have synchronous counterparts) and the events that can fire after the method is called:

Asynchronous File method Events
copyToAsync() complete,ioError
deleteDirectoryAsync( ) complete,ioError
deleteFileAsync() complete,ioError
listDirectoryAsync()

directoryListing,ioError

moveToAsync() complete,ioError
moveToTrashAsync( ) complete,ioError

When you open a file, use either theopen()oropenAsync()method of theFileStreamobject. The first opens the file for synchronous operations, and the second opens the file for asynchronous operations. For more information, see “The open( ) and openAsync( ) Methods” later in this chapter.

Use asynchronous methods whenever you want to make sure that other essential ActionScript-driven processes—such as progress bar animation—continue while the file operations take place. For example, you could use theopen()(synchronous) method of aFileStreamobject if you are going to write a small file (1 MB or less) and use theopenAsync()method when writing larger files, or when the file size is unknown.

For more information on asynchronous methods in general, see the “Handling Events” chapter in Programming ActionScript 3.0, which is available at:

http://livedocs.macromedia.com/flex/2/docs/Part5_ProgAS.html

Please check back next week for the conclusion to this article.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials