Home arrow Flash arrow Page 3 - 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 - Reading and Writing Files
(Page 3 of 5 )

The FileStream class provides methods that let your application read and write files.

Here’s the general process for reading and writing to a file:

  1. Set up aFile object that points to the file you want to read or write.

    For details, see “Accessing Files and Directories,” earlier in this chapter. 
  2. Instantiate aFileStream object—for example:

       var stream:FileStream = new FileStream();. 
  3. Call theFileStream.open()orFileStream.openAsync()method, passing in theFileobject as thefileparameter and passing an appropriate file mode as thefileModeparameter. For example:

      stream.open(file, FileMode.READ);

    For more information, see “File Open Modes” later in this chapter.
  4. If you called theFileStream.openAsync()method, set up the appropriate event listener functions.

    For more information, see “The open( ) and openAsync( ) Methods,” next.
  5. Call the appropriate read and write method for your data.

    For more information, see “Read and Write Methods” later in this chapter
  6. Close the file, using theFileStream.close()method. For example:

      stream.close();

Steps 3, 4, and 5 are described in more detail the sections that follow. First, here is a sample of code for reading UTF-8 text from a file synchronously:

  var file:File = File.appStorageDirectory;
  file = file.resolve("settings.xml");
  var stream:FileStream = new FileStream();
  stream.open(file, FileMode.READ);
  var data:String = stream.readUTFBytes(stream.
  bytesAvailable);
  stream.close();

Here is some code that reads the same data asynchronously:

  var file:File = File.appStorageDirectory;
  file = file.resolve("settings.xml");
  var stream:FileStream = new FileStream();
  stream.openAsync(file, FileMode.READ);
  stream.addEventListener(Event.COMPLETE, readData);
  var data:String;

  private function readData(event:Event):void {
    data = stream.readUTFBytes(stream.bytesAvailable);
    stream.close();
 
}


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