The
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 = air.File.documentsDirectory.resolvePath("AIR Test/test.txt");
var newFile = air.File.documentsDirectory.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
overwrite
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 = air.File.documentsDirectory;
original = original.resolvePath("AIR Test/test.txt");
var destination = air.File.documentsDirectory;
destination = destination.resolvePath("AIR Test 2/copy.txt");
original.addEventListener(air.Event.COMPLETE, fileMoveCompleteHandler);
original.addEventListener(air.IOErrorEvent.IO_ERROR, fileMoveIOErrorEventHandler);
original.moveToAsync(destination);
function fileMoveCompleteHandler(event){
alert(event.target); // [object File]
}
function fileMoveIOErrorEventHandler(event) {
alert("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.