Home arrow Flash arrow Page 2 - 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 - Accessing Files and Directories
(Page 2 of 4 )

Apollo applications can run on multiple platforms, including Windows and Mac OS. The Apollo file API uses platform-neutral code syntax so you don’t have to write any OS-specific code.

For example, the way you represent a path to a file differs between Mac OS and Windows:

  1. A typical file path on Mac OS is /Users/joe/Documents/test.txt
  2. A typical file path on Windows is C:\Documents and Settings\joe\My Documents\test.txt

However, you can use exactly the same Apollo components, classes, methods, and properties to access files in either operating system.

An ActionScriptFile object is a pointer to a file or directory. TheFileclass includes the static propertydocumentsDirectory, which contains aFileobject that points to the user’s documents directory. This is the My Documents directory on Windows, and it is the Documents subdirectory of the user directory on Mac OS, as illustrated in the following code:

  trace(File.documentsDirectory.nativePath)
       // On Windows:
       //       C:\Documents and Settings\joe\MyDocuments
       // On Mac OS:  /Users/joe/Documents

Once you point aFileobject to a directory, you can use theresolve()method to modify it to point to a file or subdirectory within that directory (or within a subdirectory). For example, the following code creates an Apollo Test subdirectory of the user’s documents directory:

  var newDir:File = File.documentsDirectory;
  newDir = newDir.resolve("ApolloTest");
  newDir.createDirectory();

AFileobject can point to either a file or a directory. Also, aFileobject may point to a file or directory that does not exist, as in the previous example. This lets you point aFileobject to a directory location that you wish to create.

File Class Properties for Accessing Common Directory Locations

The Fileclass includes the following static properties, which point to commonly used directory locations:

Property

Description

File.appStorageDirectory

Each installed Apollo application is given a unique application storage directory. This is a good place to store files that the application may want to maintain but that the user will probably need not see. This may include log files, cache files, and preferences files.

File.appResourceDirectory

The application’s install directory.

File.currentDirectory

This is the directory from which the file was launched. You may use this property to resolve the file path of any command-line parameters that were passed to the application.

File.desktopDirectory

This is the user’s desktop directory.

Continued

Property

Description

File.documentsDirectory

This is the My Documentsdirectory on Windows, and the Documentssubdirectory of the user directory on Mac OS.

File.userDirectory

This is the user’s home directory. For example, on Mac OS, it is the Users/ usernamedirectory, and on Windows it is typically c:\\Document and Settings\username.

The url and nativePath Properties of a File Object

The urlproperty of aFile object returns the location of a file or folder as a platform-independent string that begins with a URL scheme, such asfile, as in the following:

  var directory:File = File.userDirectory;
  trace(directory.url)
   
// on Windows: file:///C:/Documents%20and%20Settings
   
// on Mac OS: file:///Users

whereas thenativePath property of aFileobject returns a string that is unique to Windows or Mac OS. For example, you can use this code to point to a specific file on a Windows computer:

  var file:File = new File();
  file.nativePath = "c:/ApolloTest/surprise.txt";

However, it is generally better to start with one of the static properties listed in the table in the previous section (such as theFile.appStorageDirectory)—that point to known directories on the operating system—and then use theresolve()method to create to a relative path based on that directory, as in this code:

  var logFile:File = File.appStorageDirectory;
  logFile = logFile.resolve("log.txt");

Use the application store directory to store files that you want your application to be able to access in the future but that the end user may not need to know about. For instance, this is a good place to store preferences files.


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