A Closer Look at Apollo`s File System API (Page 1 of 5 )
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.
Reading Directory Contents
The File.listDirectory() method returns an array listing ofFileobjects that represent the files and directories contained within the specified directory. For example, the following code lists the contents of the desktop directory:
var directory:File = File.desktopDirectory;
var contents:Array = directory.listDirectory();
for (var i:uint = 0; i < contents.length; i++) {
if (contents[i].isDirectory) {
trace(contents[i].name);
} else {
trace(contents[i].name,
contents[i].size,
"bytes");
}
}
TheFile.listDirectory()method returns only the root level files and directories in a directory. It does not recursively search through subdirectories for their contents. You can, of course, write code to traverse subdirectories, though if you do so, you might want to use theFile.listDirectoryAsync()method so that other ActionScript-driven processes can continue while the directory listings are being compiled.
Also see “Getting a Directory Listing” inChapter 5.
Getting File Information
The File class includes a number of properties that contain information about a file or directory.
Property | Description |
exists | States whether the file or directory exists. This is worth checking, for example, before you attempt to read, write, copy, or move a file. |
isDirectory | States whether the Fileobject points to a directory (true) or a file (false). You will want to check this before attempting directory-specific operations (such as the listDirectory() method) or attempting file-specific operations (such as reading a file). |
isHidden | States whether the file or directory is hidden. |
nativePath | Notes the operating system-specific path to the file or directory. |
parent | Notes the parent directory of the File instance. |
url | Notes the operating system-independent path to the file or directory. |
TheFileclass also inherits the following useful properties from theFileReferenceclass:
Property creationDate | Description The date the file or folder was created. |
modificationDate | The date when the file was last modified. |
name | The file or folder name. |
size | The size of the file, in bytes. |
Next: Copying and Moving Files and Directories >>
More Flash Articles
More By O'Reilly Media
|
This article 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). Check it out today at your favorite bookstore. Buy this book now.
|
|