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.
Next: Reading and Writing Files >>
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.
|
|