Home arrow Flash arrow Page 2 - 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 - Copying and Moving Files and Directories
(Page 2 of 5 )

The File.copyTo() andFile.moveTo()methods copy or move a file or directory to a specified new location. For example, the following code copies the test.txt file in the Apollo Test subdirectory of the user’s documents directory to the User Data subdirectory of the application storage directory:

  var file1:File = File.documentsDirectory.resolve("Apollo
  Test/test.txt");
  var destination:File = File.appStorageDirectory.
  resolve("User Data");
  destination.createDirectory();
  var file2:File = destination.resolve("test.txt");
  file1.copyTo(file2);

Note the call to theFile.createDirectory()method, which ensures that the destination directory exists.

The following code moves the Apollo Test 1 subdirectory of the user’s documents directory to the Apollo Test 2 subdirectory (effectively renaming the directory):

  var dir1:File = File.documentsDirectory;
  dir1 = dir1.resolve("Apollo Test 1");
  var dir2:File = File.documentsDirectory;
  dir2 = dir2.resolve("Apollo Test 2");

You might want to use the asynchronous versions of these methods,File.copyToAsync()andFile.moveToAsync(), if the copy or move operation could take a long time.

Each of these methods includes aclobber parameter, which you can set totrueto have the operation overwrite existing files. By default, this parameter is set tofalse.

Creating Files and Directories

The File.createTempFile() and File.createTempDirectory()static methods of theFileclass let you create a temporary file or directory. Apollo ensures that the temporary file or directory created by these methods is new and unique. For example, the following code creates a temporary file:

  var bufferStorage:File = File.createTempFile();

Temporary files and directories are not automatically deleted when you close an Apollo application. You will generally want to delete temporary files and directories before closing the application. See the next section, “Deleting Files and Directories,” for more details.

TheFile.createDirectory()method lets you create a directory in the location specified by theFileobject:

  var directory = File.documentsDirectory;
  directory = directory.resolve("ApolloTest");

When you open aFileStreamobject with write capabilities then directories are created automatically, if needed. For more information aboutFileStreamobjects, see "Reading and Writing Files" later in this chapter.

Deleting Files and Directories

The File.deleteFile() method permanently deletes a file, and the File.deleteDirectory()method permanently deletes a directory. TheFile.moveToTrash()method lets you move a file or directory to the system trash.

Each of these methods also has an asynchronous counterpart.


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