Home arrow Flash arrow Page 4 - A Closer Look at Apollo`s File System API
FLASH

A Closer Look at Apollo`s File System API


In this second article in a two-part series devoted to Apollo's file system API, we delve heavily into the intricacies of creating and manipulating files and directories. It is excerpted from chapter four of the 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: 5 stars5 stars5 stars5 stars5 stars / 1
March 20, 2008
TABLE OF CONTENTS:
  1. · A Closer Look at Apollo`s File System API
  2. · Copying and Moving Files and Directories
  3. · Reading and Writing Files
  4. · The open() and openAsync() Methods
  5. · File Open Modes

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
A Closer Look at Apollo`s File System API - The open() and openAsync() Methods
(Page 4 of 5 )

Your application needs to open a file before it can read from or write to the file.

When you open a file with theFileStream.openAsync()method, the file is opened for asynchronous operations, and you’ve registered event listeners to monitor progress.

TheFileStream.open()method opens the file for synchronous operations. If your application opens the file using this synchronous method, all subsequent calls to methods that read or write to the file will be done synchronously as well. In the following example, each of the calls tostream.open(),stream.writeUTFBytes(), andstream.close()will complete before the next call is made.

  var newFile:File = File.documentsDirectory;
  file = file.resolve("ApolloTest/test.txt");

  var stream:FileStream = new FileStream()
  stream.open(file, FileMode.WRITE);
  stream.writeUTFBytes("This is some sample text.");
  stream.close();

The advantage of opening a file for synchronous operations is that you can write less code to complete a task. The disadvantage is that execution of other ActionScript code can be delayed if the file operations take a while. As a result, if you are working with large files or opening files that are shared on slow networks, you should consider using theFileStream.openAsync()method instead.

When you use theopenAsync()method, the following processes are all handled asynchronously:

Closing the file

TheFileStream object dispatches acloseevent when the file is closed.

Reading data into the read buffer

TheFileStreamobject dispatchesprogressevents as data is read, and it dispatches acompleteevent once all the data is read. However, once data is read, calling a read method (such asreadBytes()) to read data is a synchronous process.

I/O errors

TheFileStream object dispatches anioErrorevent upon encountering an error. This may occur for a number of reasons, such as attempting to open a file that doesn’t exist or attempting to write to a file that is locked. However, some errors, such as attempting to read from a file that has not been opened, throw exceptions (rather than dispatchioErrorevents) because the Apollo runtime can detect the error condition instantly.

Before calling theFileStream.openAsync()method, your application should set up event listener functions to handle those events in which it is interested.

The following example opens a file in asynchronous read mode. After the file has been opened, the complete event will be dispatched (unless there is an error, in which case theioErrorevent will be dispatched instead). ThecompleteHandler()method then calls theFileStream.readBytes()method, which starts reading data from the file as an array of bytes, in asynchronous mode. When all the bytes have been read from the file, thecompleteevent will be dispatched:

  var file:File = File.documentsDirectory.
  resolve("ApolloTest/test.txt");
  var stream:FileStream = new FileStream();

  stream.addEventListener(ProgressEvent.PROGRESS,
  progressHandler);
  stream.addEventListener(Event.COMPLETE, completeHandler);
  stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  stream.addEventListener(Event.CLOSE, closeHandler);

  stream.openAsync(file, FileMode.READ);

  var data:ByteArray = new ByteArray();

  private function progressHandler(event:ProgressEvent):void {
     
trace(stream.bytesAvailable, "bytes read.");
  }
  private function completeHandler(event: Event):void {
     
data = stream.readBytes(stream.bytesAvailable);
     
stream.close();
  }
  private function ioErrorHandler(event:IOErrorEvent):void {
     
trace("An I/O error was encountered.");
  }
  private function closeHandler(event: Event):void {
     
trace("File closed.");
  }


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 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials