| Adobe AIR 1.1 |
|
|
Working with filesUsing the AIR file API, you can add basic file interaction capabilities to your applications. For example, you can read and write files, copy and delete files, and so on. Since your applications can access the local file system, refer to AIR security, if you haven't already done so. Note: You can associate a file type with an AIR application
(so that double-clicking it opens the application). For details,
see Managing file associations.
Getting file informationThe File class includes the following properties that provide information about a file or directory to which a File object points:
For details on these properties, see the File class entry in the Flex 3 Language Reference (http://www.adobe.com/go/learn_flex3_aslr). Copying and moving filesThe File class includes two methods for copying files or directories: copyTo() and copyToAsync(). The File class includes two methods for moving files or directories: moveTo() and moveToAsync(). The copyTo() and moveTo() methods work synchronously, and the copyToAsync() and moveToAsync() methods work asynchronously (see AIR file basics). To copy or move a file, you set up two File objects. One points to the file to copy or move, and it is the object that calls the copy or move method; the other points to the destination (result) path. The following copies a test.txt file from the AIR Test subdirectory of the user's documents directory to a file named copy.txt in the same directory: var original:File = File.documentsDirectory.resolvePath("AIR Test/test.txt");
var newFile:File = File.resolvePath("AIR Test/copy.txt");
original.copyTo(newFile, true);
In this example, the value of overwrite parameter of the copyTo() method (the second parameter) is set to true. By setting this to true, an existing target file is overwritten. This parameter is optional. If you set it to false (the default value), the operation dispatches an IOErrorEvent event if the target file exists (and the file is not copied). The “Async” versions of the copy and move methods work asynchronously. Use the addEventListener() method to monitor completion of the task or error conditions, as in the following code: var original = File.documentsDirectory;
original = original.resolvePath("AIR Test/test.txt");
var destination:File = File.documentsDirectory;
destination = destination.resolvePath("AIR Test 2/copy.txt");
original.addEventListener(Event.COMPLETE, fileMoveCompleteHandler);
original.addEventListener(IOErrorEvent.IO_ERROR, fileMoveIOErrorEventHandler);
original.moveToAsync(destination);
function fileMoveCompleteHandler(event:Event):void {
trace(event.target); // [object File]
}
function fileMoveIOErrorEventHandler(event:IOErrorEvent):void {
trace("I/O Error.");
}
The File class also includes the File.moveToTrash() and File.moveToTrashAsync() methods, which move a file or directory to the system trash. Deleting a fileThe File class includes a deleteFile() method and a deleteFileAsync() method. These methods delete files, the first working synchronously, the second working asynchronously (see AIR file basics). For example, the following code synchronously deletes the test.txt file in the user's documents directory: var file:File = File.documentsDirectory.resolvePath("test.txt");
file.deleteFile();
The following code asynchronously deletes the test.txt file of the user's documents directory: var file:File = File.documentsDirectory.resolvePath("test.txt");
file.addEventListener(Event.COMPLETE, completeHandler)
file.deleteFileAsync();
function completeHandler(event:Event):void {
trace("Deleted.")
}
Also included are the moveToTrash() and moveToTrashAsync methods, which you can use to move a file or directory to the System trash. For details, see Moving a file to the trash. Moving a file to the trashThe File class includes a moveToTrash() method and a moveToTrashAsync() method. These methods send a file or directory to the System trash, the first working synchronously, the second working asynchronously (see AIR file basics). For example, the following code synchronously moves the test.txt file in the user's documents directory to the System trash: var file:File = File.documentsDirectory.resolvePath("test.txt");
file.moveToTrash();
Creating a temporary fileThe File class includes a createTempFile() method, which creates a file in the temporary directory folder for the System, as in the following example: var temp:File = File.createTempFile(); The createTempFile() method automatically creates a unique temporary file (saving you the work of determining a new unique location). You may use a temporary file to temporarily store information used in a session of the application. Note that there is also a createTempDirectory() method, for creating a unique temporary directory in the System temporary directory. You may want to delete the temporary file before closing the application, as it is not automatically deleted. |