Adobe Flex 2 Language Reference - collected by Jimbob | Back to MY RESOURCES


Packageflash.filesystem
Classpublic class File
InheritanceFile Inheritance FileReference Inheritance EventDispatcher Inheritance Object

A File object represents a path to a file or directory. This can be an existing file or directory, or it can be one that does not yet exist (for instance, it can represent the path to a file or directory that you wish to create).

The File class has a number of properties and methods for getting information about the file system and for performing operations (such as copying files and directories).

You can use File objects along with FileStream class to read and write files.

The File class extends the FileReference class. The FileReference class, which is available in Flash Player as well as Apollo, represents a pointer to a file, but the File class adds properties and methods that are not exposed in Flash Player (in a SWF running in a browser), due to security considerations.

See also

FileStream


Public Properties
 PropertyDefined by
  appResourceDirectory : File
[static][read-only] The application's resource directory.
File
  appStorageDirectory : File
[static][read-only] The application's private storage directory.
File
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 InheritedcreationDate : Date
The creation date of the file on the local disk.
FileReference
 Inheritedcreator : String
The Macintosh creator type of the file, which is only used in Mac OS versions prior to Mac OS X.
FileReference
  currentDirectory : File
[static][read-only] The "current directory" at the time the application was launched.
File
  desktopDirectory : File
[static][read-only] The user's desktop directory.
File
  documentsDirectory : File
[static][read-only] The user's documents directory.
File
  exists : Boolean
[read-only] Checks whether referenced file or directory exists.
File
  isDirectory : Boolean
[read-only] Checks whether the reference is to a directory.
File
  isHidden : Boolean
[read-only] Checks whether the referenced file or directory is "hidden."
File
  lineEnding : String
[static][read-only] The line-ending character sequence used by the host operating system.
File
 InheritedmodificationDate : Date
The date that the file on the local disk was last modified.
FileReference
 Inheritedname : String
The name of the file on the local disk.
FileReference
  nativePath : String
The full path in the host operating system representation.
File
  parent : File
[read-only] The parent of this File reference.
File
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
  separator : String
[static][read-only] The host operating system's path component separator character.
File
 Inheritedsize : Number
The size of the file on the local disk in bytes.
FileReference
  systemCharset : String
[static][read-only] The default encoding used by the host operating system.
File
 Inheritedtype : String
The file type.
FileReference
  url : String
The URL for this File path.
File
  userDirectory : File
[static][read-only] The user's directory.
File
Public Methods
 MethodDefined by
  
File(path:String = null)
The constructor function for the File class.
File
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
EventDispatcher
 Inherited
browse(typeFilter:Array = null):Boolean
Displays a file-browsing dialog box that lets the user select a file to upload.
FileReference
  
cancel():void
Cancels any pending asynchronous operation.
File
  
canonicalize():void
Canonicalizes the File path.
File
  
Returns a copy of this File object.
File
  
copyTo(newLocation:FileReference, clobber:Boolean = false):void
Copies the file or directory at the location specified by this File object to the location specified by newLocation parameter.
File
  
copyToAsync(newLocation:FileReference, clobber:Boolean = false):void
Begins copying the file or directory at the location specified by this File object to the location specified by the destination parameter.
File
  
Creates the specified directory and any necessary parent directories.
File
  
[static] Returns a reference to a new temporary directory.
File
  
[static] Returns a reference to a new temporary file.
File
  
deleteDirectory(deleteDirectoryContents:Boolean = false):void
Deletes the directory.
File
  
deleteDirectoryAsync(deleteDirectoryContents:Boolean = false):void
Deletes the directory asynchronously.
File
  
deleteFile():void
Deletes the file.
File
  
Deletes the file asynchronously.
File
 Inherited
Dispatches an event into the event flow.
EventDispatcher
 Inherited
download(request:URLRequest, defaultFileName:String = null):void
Opens a dialog box that lets the user download a file from a remote server.
FileReference
 Inherited
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
  
Returns an array of File objects corresponding to files and directories in the directory represented by this File object.
File
  
Asynchronously retrieves an array of File objects corresponding to the contents of the directory represented by this File object.
File
  
[static] Returns an array of file system root directories.
File
  
moveTo(newLocation:FileReference, clobber:Boolean = false):void
Moves the file or directory at the location specified by this File object to the location specified by the destination parameter.
File
  
moveToAsync(newLocation:FileReference, clobber:Boolean = false):void
Begins moving the file or directory at the location specified by this File object to the location specified by the newLocation parameter.
File
  
moveToTrash():void
Moves a file or directory to the trash.
File
  
Asynchronously moves a file or directory to the Trash.
File
  
relativize(ref:FileReference, useDotDot:Boolean = false):String
Finds relative path between two File paths.
File
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
EventDispatcher
  
Creates a new File object with a path relative to this this File object's path, based on the path parameter (a String).
File
 Inherited
setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
upload(request:URLRequest, uploadDataFieldName:String = "Filedata", testUpload:Boolean = false):void
Starts the upload of a file selected by a user to a remote server.
FileReference
 Inherited
Returns the primitive value of the specified object.
Object
 Inherited
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Events
 EventSummaryDefined by
 Inherited Dispatched when Flash Player gains operating system focus and becomes active.EventDispatcher
   Dispatched when a pending asynchronous operation is canceled.File
 Inherited Dispatched when a file upload or download is canceled by the user.FileReference
   Dispatched when an asynchronous operation is complete.File
 Inherited Dispatched when download is complete or when upload generates an HTTP status code of 200.FileReference
 Inherited Dispatched when Flash Player loses operating system focus and is becoming inactive.EventDispatcher
   Dispatched when a directory list is available as a result to a call to the listDirectoryAsync() method.File
 Inherited Dispatched when an upload fails and an HTTP status code is available to describe the failure.FileReference
   Dispatched when an error occurs during an asynchronous file operation.File
 Inherited Dispatched when the upload or download fails.FileReference
 Inherited Dispatched when an upload or download operation starts.FileReference
 Inherited Dispatched periodically during the file upload or download operation.FileReference
   Dispatched when a an operation violates a security constraint.File
 Inherited Dispatched when a call to the FileReference.upload() or FileReference.download() method tries to upload a file to a server or get a file from a server that is outside the caller's security sandbox.FileReference
   Dispatched when the user selects a file or directory from the a file- or directory-browsing dialog box.File
 Inherited Dispatched when the user selects a file for upload or download from the file-browsing dialog box.FileReference
 Inherited Dispatched after data is received from the server after a successful upload.FileReference
Property detail
appResourceDirectoryproperty
appResourceDirectory:File  [read-only]

The application's resource directory.

The url property for this object uses the app-resource URL scheme (not the file URL scheme). This means that the url string is specified starting with "app-resource:" (not file:). Also, if you create a File object relative to the appResourceDirectory directory (using the resolve() method), the url of the File object also uses the app-resource URL scheme.

Implementation
    public static function get appResourceDirectory():File
appStorageDirectoryproperty 
appStorageDirectory:File  [read-only]

The application's private storage directory. Each Apollo application has a unique, persistent application storage directory, which is created when you first access appStorageDirectory. This directory is a convenient location to store application-specific data.

The url property for this object uses the app-storage URL scheme (not the file URL scheme). This means that the url string is specified starting with "app-storage:" (not file:). Also, if you create a File object relative to the applicationStoreDirectory directory (using the resolve() method), the url of the File object also uses the app-storage URL scheme (as in the example).

Implementation
    public static function get appStorageDirectory():File
currentDirectoryproperty 
currentDirectory:File  [read-only]

The "current directory" at the time the application was launched.

Note: Use the currentDirectory property of the invoke event dispatched by the Shell object to resolve the relative paths provided in any command-line parameters provided when the file is launched. Do not use the File.currentDirectory property.

Implementation
    public static function get currentDirectory():File

See also


Example
The following code outputs the path to the current directory for the application:
import flash.filesystem.File;
trace(File.currentDirectory.nativePath);

desktopDirectoryproperty 
desktopDirectory:File  [read-only]

The user's desktop directory.

Implementation
    public static function get desktopDirectory():File

Example
The following code outputs a list of files and directories contained in the user's desktop directory:
import flash.filesystem.File;
var desktop:File = File.desktopDirectory;

var files:Array = desktop.listDirectory()

for (var i:uint = 0; i < files.length; i++) {
    trace(files[i].nativePath)
}

documentsDirectoryproperty 
documentsDirectory:File  [read-only]

The user's documents directory.

On Windows, this is the "My Documents" directory (for example, C:\Documents and Settings\userName\My Documents). On Mac OS, it is /Users/userName/Documents.

Implementation
    public static function get documentsDirectory():File

Example
The following code uses the File.documentsDirectory property and the File.createDirectory() method to ensure that a directory named "Apollo Test" exists in the user's documents directory.
import flash.filesystem.File;

var directory:File = File.documentsDirectory;
directory = directory.resolve("Apollo Test");

File.createDirectory(directory);
trace(directory.exists); // true

existsproperty 
exists:Boolean  [read-only]

Checks whether referenced file or directory exists.

Implementation
    public function get exists():Boolean

Example
The following code creates a temporary file, then deletes it, and uses the File.exists property to check for the existence of the file.
import flash.filesystem.*;

var temp:File = File.createTempFile();
trace(temp.exists) // true
temp.deleteFile();
trace(temp.exists) // false

isDirectoryproperty 
isDirectory:Boolean  [read-only]

Checks whether the reference is to a directory.

Implementation
    public function get isDirectory():Boolean

Example
The following code creates an array of File objects pointing to files and directories in the user directory, and then uses the isDirectory property to list only those File objects that point to directories (not files).
import flash.filesystem.*;

var userDirFiles:Array = File.userDirectory.listDirectory();
for (var i:uint = 0; i < userDirFiles.length; i++) {
    if (userDirFiles[i].isDirectory) {
        trace(userDirFiles[i].nativePath);
    }
}

isHiddenproperty 
isHidden:Boolean  [read-only]

Checks whether the referenced file or directory is "hidden."

Implementation
    public function get isHidden():Boolean

Example
The following code creates an array of File objects pointing to files and directories in the user directory, and then uses the isHidden property to list hidden files and directories.
import flash.filesystem.*;

var userDirFiles:Array = File.userDirectory.listDirectory();
for (var i:uint = 0; i < userDirFiles.length; i++) {
    if (userDirFiles[i].isHidden) {
        trace(userDirFiles[i].nativePath);
    }
}

lineEndingproperty 
lineEnding:String  [read-only]

The line-ending character sequence used by the host operating system.

On Mac OS, this is the line-feed character (character code 0x0A hexadecimal). On Windows, this is the carriage return character (character code 0x0D hexadecimal) followed by the line-feed character (character code 0x0A hexadecimal).

Implementation
    public static function get lineEnding():String

Example
The following code writes a string (str) to a text file, and uses the File.lineEnding static property to replace all instances of the new-line character (represented in the code by the regular expression /\n/g) with the prefered line ending character for the host operating system.
import flash.filesystem.*;

var str:String = "Hello\n" + 
        "World\n";
str = str.replace(/\n/g, File.lineEnding);
var file:File = File.documentsDirectory.resolve("Apollo Test/test.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTF(str);
fileStream.close();

nativePathproperty 
nativePath:String  [read-write]

The full path in the host operating system representation. On Mac OS, the forward slash (/) character is used as the path separator. However, in Windows, you can set the nativePath property using the forward slash (/) character or the backslash (\) character as the path separator, and Apollo automatically replaces forward slashes with the appropriate backslash (\) character.

Implementation
    public function get nativePath():String
    public function set nativePath(value:String):void

Throws
SecurityError — The caller does not have File privileges and this is a reference to a file.
 
ArgumentError — The specified path is null or is not an absolute path.

Example
The following code shows the difference between the nativePath property and the url property of a File object. The comments show results on an example Windows computer.
import flash.filesystem.File;

var docs:File = File.documentsDirectory;
trace(docs.nativePath); // C:\Documents and Settings\turing\My Documents
trace(docs.url); // file:///C:/Documents%20and%20Settings/turing/My%20Documents

parentproperty 
parent:File  [read-only]

The parent of this File reference.

This property is identical to the return value for resolve("..") except that the parent of a root directory is null.

Implementation
    public function get parent():File

Example
The following code uses the parent property to show the directory that contains a temporary file.
import flash.filesystem.File;

var tempFile:File = File.createTempDirectory();
trace(tempFile.parent.nativePath);
tempFile.deleteFile();

separatorproperty 
separator:String  [read-only]

The host operating system's path component separator character.

On Mac OS, this is the forward slash (/) character. On Windows, it is the backslash (\ character.

Note: When using the backslash character in a String literal, remember to type the character twice (as in "directory\\file.ext"). Each pair of backslashes in a String literal represent a single backslash in the String.

Implementation
    public static function get separator():String

Example
The following code uses the relativize() method to get the relative path between a directory and a file. The code then uses the File.separator static property to replace forward slash (/) characters in the path with the separator character used by the operating system, which is the backslash character (\) on Windows and the forward slash character on other operating systems.
import flash.filesystem.File;

var directory:File = File.documentsDirectory.resolve("Apollo Test");
var file:File = File.documentsDirectory.resolve("Apollo Test/employees/bob/test.txt");

var relativePath:String = directory.relativize(file); // employees/bob/test.txt
relativePath = relativePath.replace(/\//g, File.separator);
trace(relativePath); 
In this example, the replace() method uses a regular expression, /\//g, to match all forward slash characters.

systemCharsetproperty 
systemCharset:String  [read-only]

The default encoding used by the host operating system. Possible values include "windows-1252" "shift-jis", "cn-gb", "iso-8859-1", and others. For a complete list, see Supported Character Sets.

You may use this value when using the readMultiByte() and writeMultiByte() methods of the FileStream class.

Implementation
    public static function get systemCharset():String

See also


Example
The following code opens a file, (a test.txt file in the Apollo Test subdirectory of the user's documents directory), and uses the File.systemCharset static property as the charSet parameter of a call to the readMultiByte() method of a FileStream object.
import flash.filesystem.File;

var file:File = File.documentsDirectory.resolve("Apollo Test/test.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var str:String = fileStream.readMultiByte(file.size, File.systemCharset);
trace(str);

urlproperty 
url:String  [read-write]

The URL for this File path.

If this is a reference to the persistent store, the URL scheme is "app-storage", otherwise the scheme is "file".

You can use blank space characters (rather than "%20") when assigning a value to the url property, and Apollo automatically encodes the strings (for instance, converting spaces to "%20").

Implementation
    public function get url():String
    public function set url(value:String):void

Throws
SecurityError — The caller does not have file privileges.
 
SecurityError — The caller does not have privileges to access the persistent store, and this is a reference to the persistent store.

See also


Example
The following code shows the difference between the nativePath property and the url property of a File object. The comments show results on an example Windows computer.
import flash.filesystem.File;

var docs:File = File.documentsDirectory;
trace(docs.nativePath); // C:\Documents and Settings\turing\My Documents
trace(docs.url); // file:///C:/Documents%20and%20Settings/turing/My%20Documents

userDirectoryproperty 
userDirectory:File  [read-only]

The user's directory.

On Windows, this is the parent of the "My Documents" directory (for example, C:\Documents and Settings\userName). On Mac OS it is /Users/userName.

Implementation
    public static function get userDirectory():File

Example
The following code outputs a list of files and directories contained in the root level of the user directory:
import flash.filesystem.File;

var files:Array = File.userDirectory.listDirectory()
for (var i:uint = 0; i < files.length; i++) {
    trace(files[i].nativePath)
}

Constructor detail
File()constructor
public function File(path:String = null)

The constructor function for the File class. If you pass a path argument, the File object points to the specified path, and the nativePath property and and url property are set to reflect that path.

Parameters
path:String (default = null) — The path to the file. You can specify the path using either a URL or using native path (platform-specific) notation.

If you specify a URL, you can use any of the following URL schemes: file, app-resource, or app-storage. The following are valid values for the path parameter using URL notation:

  • "app-resource:/DesktopPathTest.xml"
  • "app-storage:/preferences.xml"
  • "file:///C:/Documents%20and%20Settings/bob/Desktop" (the desktop on Bob's Windows computer)
  • "file:///Users/bob/Desktop" (the desktop on Bob's Mac computer)

Using the app-resource and app-storage URL schemes is useful, because these can point to a valid file on both Mac and Windows. However, in the other two examples, which use the file URL scheme to point to the user's desktop directory, it would be better to pass no path argument to the File() constructor and then assign File.desktopDirectory to the File object, as it is way to access the desktop directory that is both platform- and user-independent.

If you specify a native path, on Windows you can use either the backslash ("\") character or the forward slash ("/") character as the path separator in this argument; on Mac OS, use the forward slash ("/") character. The following are valid values for the path parameter using native path notation:

  • "C:/Documents and Settings/bob/Desktop"
  • "/Users/bob/Desktop"

However, for these two examples, you should pass no path argument to the File() constructor and then assign File.desktopDirectory to the File object, as it is way to access the desktop directory that is both platform- and user-independent.

See also

Method detail
cancel()method
public override function cancel():void

Cancels any pending asynchronous operation.

canonicalize()method 
public function canonicalize():void

Canonicalizes the File path.

If the File object represents an existing file or directory, canonicalization adjusts the path so that it matches the case of the actual file or directory name.

In addition, canonicalization converts short files names to long file names on Windows.


Example
The following code shows how to use the canonicalize() method to find the correct capitalization of a directory name. Before running this example, create a directory named Apollo Test on the desktop of your computer.
import flash.filesystem.*;

var path:File = File.desktopDirectory.resolve("apollo test");
trace(path.nativePath); 
path.canonicalize();
trace(path.nativePath); // ...\Apollo Test

The following code shows how to use the canonicalize() method to find the long name of a Windows directory based on its short name. This example assumes that there is an Apollo Test directory at the root of the C: drive, and that the system has assigned the short name APOLLO~1 to the directory.
import flash.filesystem.*;

var path:File = new File();
path.nativePath = "C:\\APOLLO~1";
path.canonicalize();
trace(path.nativePath); // C:\Apollo Test

clone()method 
public function clone():File

Returns a copy of this File object. Event registrations are not copied.

Note: This method does not copy the file itself. It simply makes a copy of the instance of the ActionScript File object. To copy a file, use the copyTo() method.

Returns
File
copyTo()method 
public function copyTo(newLocation:FileReference, clobber:Boolean = false):void

Copies the file or directory at the location specified by this File object to the location specified by newLocation parameter.

Parameters
newLocation:FileReference — The target location of the new file. Note that this File object specifies the resulting (copied) file or directory, not the path to the containing directory.
 
clobber:Boolean (default = false) — If false, the copy will fail if the file specified by the target parameter already exists. If true, the operation will first delete any existing file or directory of the same name.

Throws
IOError — The source does not exist or the destination exists and clobber is false or the source could not be copied to the target. On Windows, you cannot copy a file that is open or a directory that contains a file that is open.

See also


Example
The following code shows how to use the copyTo() method to copy a file. Before running this code, be sure to create a test1.txt file in the Apollo Test subdirectory of the documents directory on your computer. The resulting copied file is named test2.txt, and it is also in the Apollo Test subdirectory. By setting the clobber parameter to true, the operation overwrites any existing test2.txt file.
import flash.filesystem.File;
import flash.events.Event;

var sourceFile:FileReference = File.documentsDirectory;
sourceFile = sourceFile.resolve("Apollo Test/test1.txt");
var destination:FileReference = File.documentsDirectory;
destination = destination.resolve("Apollo Test/test2.txt");

if (sourceFile.copyTo(destination, true)) {
    trace("Done.")
}

The following code shows how to use the copyTo() method to copy a file. Before running this code, be sure to create a test1.txt file in the Apollo Test subdirectory of the home directory on your computer. The resulting copied file is named test2.txt. The try and catch statements show how to respond to errors.
import flash.filesystem.File;

var sourceFile:File = File.documentsDirectory;
sourceFile = sourceFile.resolve("Apollo Test/test1.txt");
var destination:File = File.documentsDirectory;
destination = destination.resolve("Apollo Test/test2.txt");

try 
{
    if (sourceFile.copyTo(destination, true)) {
}
catch (error:Error)
{
    trace("Error:" error.message)
}

copyToAsync()method 
public function copyToAsync(newLocation:FileReference, clobber:Boolean = false):void

Begins copying the file or directory at the location specified by this File object to the location specified by the destination parameter. Upon completion, either a complete event (successful) or an ioError event (unsucessful) is dispatched.

Parameters
newLocation:FileReference — The target location of the new file. Note that this File object specifies the resulting (copied) file or directory, not the path to the containing directory.
 
clobber:Boolean (default = false) — If false, the copy will fail if the file specified by the target file already exists. If true, the operation will first delete any existing file or directory of the same name.

Events
Event — Dispatched when the file or directory has been successfully copied.
 
IOErrorEvent — The source does not exist or the destination exists and clobber is false or the source could not be copied to the target. On Windows, you cannot copy a file that is open or a directory that contains a file that is open.

See also


Example
The following code shows how to use the copyToAsync() method to copy a file. Before running this code, be sure to create a test1.txt file in the Apollo Test subdirectory of the documents directory on your computer. The resulting copied file is named test2.txt, and it is also in the Apollo Test subdirectory. By setting the clobber parameter to true, the operation overwrites any existing test2.txt file.
import flash.filesystem.File;
import flash.events.Event;

var sourceFile:File = File.documentsDirectory;
sourceFile = sourceFile.resolve("Apollo Test/test1.txt");
var destination:File = File.documentsDirectory;
destination = destination.resolve("Apollo Test/test2.txt");

var sourceFile.copyToAsync(destination, true);
sourceFile.addEventListener(Event.COMPLETE, fileCopiedHandler);

function fileCopiedHandler(event:Event): void {
    trace("Done.")
}

createDirectory()method 
public function createDirectory():void

Creates the specified directory and any necessary parent directories. If the directory already exists, no action is taken.


Throws
IOError — The directory did not exist and could not be created.

Example
The following code moves a file named test.txt on the desktop to the Apollo Test subdirectory of the documents directory. The createDirectory() method to ensure that the Apollo Test directory exists before the file is moved.
import flash.filesystem.*;

var source:File = File.desktopDirectory.resolve("test.txt");
var target:File = File.documentsDirectory.resolve("Apollo Test/test.txt");
var targetParent:File = target.parent;
targetParent.createDirectory();
source.moveTo(target, true);

createTempDirectory()method 
public static function createTempDirectory():File

Returns a reference to a new temporary directory. This is a new directory in the system's temporary directory path.

This method lets you identify a new, unique directory, without having to query the system to see that the directory is new and unique.

Returns
File — A File referencing the new temporary directory.

See also

createTempFile()method 
public static function createTempFile():File

Returns a reference to a new temporary file. This is a new file in the system's temporary directory path.

This method lets you identify a new, unique file, without having to query the system to see that the file is new and unique.

Returns
File — A File referencing the new temporary file.

See also

deleteDirectory()method 
public function deleteDirectory(deleteDirectoryContents:Boolean = false):void

Deletes the directory.

Parameters
deleteDirectoryContents:Boolean (default = false) — Whether or not to delete a directory that contains files or subdirectories. When false, if the directory contains files or directoriess, a call to this method throws an exception.

Throws
IOError — The directory does not exist or the directory could not be deleted. On Windows, you cannot delete a directory that contains a file that is open.

See also


Example
The following code creates an empty directory, and then uses the deleteDirectory() method to delete the directory.
import flash.filesystem.File;

var directory:File = File.documentsDirectory.resolve("Empty Junk Directory/");
File.createDirectory(directory);
trace(directory.exists); // true
directory.deleteDirectory();
trace(directory.exists); // false

deleteDirectoryAsync()method 
public function deleteDirectoryAsync(deleteDirectoryContents:Boolean = false):void

Deletes the directory asynchronously.

Parameters
deleteDirectoryContents:Boolean (default = false) — Whether or not to delete a directory that contains files or subdirectories. When false, if the directory contains files or directories, the File object dispatches an ioError event.

Events
Event — Dispatched when the directory has been deleted successfully.
 
IOErrorEvent — The directory does not exist or could not be deleted. On Windows, you cannot delete a directory that contains a file that is open.

See also

deleteFile()method 
public function deleteFile():void

Deletes the file.


Throws
IOError — The file does not exist or could not to be deleted. On Windows, you cannot delete a file that is currently open.

See also


Example
The following code creates a temporary file and then calls the deleteFile() method to delete it.
import flash.filesystem.*;

var file:File = File.createTempFile();
trace(file.exists); // true
file.deleteFile();
trace(file.exists); // false

deleteFileAsync()method 
public function deleteFileAsync():void

Deletes the file asynchronously.


Events
Event — Dispatched when the file has been deleted successfully.
 
IOErrorEvent — The file does not exist or could not be deleted. On Windows, you cannot delete a file that is currently open.

See also

listDirectory()method 
public function listDirectory():Array

Returns an array of File objects corresponding to files and directories in the directory represented by this File object. This method does not explore the contents of subdirectories.

Returns
Array — An array of File objects.

See also


Example
The following code shows how to use the listDirectory() method to enumerate the contents of the user directory.
import flash.filesystem.File;

var directory:File = File.userDirectory;
var list:Array = directory.listDirectory();
for (var i:uint = 0; i < list.length; i++) {
    trace(list[i].nativePath);
}

listDirectoryAsync()method 
public function listDirectoryAsync():void

Asynchronously retrieves an array of File objects corresponding to the contents of the directory represented by this File object.


Events
ErrorEvent — You do not have adequate permissions to read this directory, or the directory does not exist.
 
DirectoryEntriesEvent — The directory contents have been enumerated successfully. The contents event includes a files property, which is the resulting array of File objects.

See also


Example
The following code shows how to use the listDirectoryAsync() method to enumerate the contents of the user directory.
import flash.filesystem.File;
import flash.events.FileListEvent;

var directory:File = File.userDirectory;
directory.listDirectoryAsync();
directory.addEventListener(FileListEvent.DIRECTORY_LISTING, directoryListingHandler);

function directoryListingHandler(event:FileListEvent):void {
    var list:Array = event.files;
    for (var i:uint = 0; i < list.length; i++) {
        trace(list[i].nativePath);
    }
}

listRootDirectories()method 
public static function listRootDirectories():Array

Returns an array of file system root directories.

Returns
Array — An array of File objects, listing the root directories.
moveTo()method 
public function moveTo(newLocation:FileReference, clobber:Boolean = false):void

Moves the file or directory at the location specified by this File object to the location specified by the destination parameter.

To rename a file, set the destination parameter to point to a path that is in the file's directory, but with a different filename

Parameters
newLocation:FileReference — The target location for the move. This object specifies the path to the resulting (moved) file or directory, not the path to the containing directory.
 
clobber:Boolean (default = false) — If false, the move will fail if the target target file already exists. If true, the operation will overwrite any existing file or directory of the same name.

Throws
IOError — The source does not exist, or the destination exists and clobber is set to false, or the source file could not be moved to the target location. On Windows, you cannot move a file that is open or a directory that contains a file that is open.

See also


Example
The following code shows how to use the moveTo() method to rename a file. The original file name is test1.txt and the resulting name is test2.txt. Since both the source and destination File object point to the same directory (the Apollo Test subdirectory of the user's documents directory), the moveTo() method renames the file (rather than moving it to a new directory). Before running this code, be sure to create a test1.txt file in the Apollo Test subdirectory of the documents directory on your computer. By setting the clobber parameter to true, the operation overwrites any existing test2.txt file.

import flash.filesystem.File;
import flash.events.Event;

var sourceFile:File = File.documentsDirectory;
sourceFile = sourceFile.resolve("Apollo Test/test1.txt");
var destination:File = File.documentsDirectory;
destination = destination.resolve("Apollo Test/test2.txt");

try  
{
    sourceFile.moveTo(destination, true);
}
catch (error:Error)
{
    trace("Error:" error.message);
}

The following code shows how to use the moveTo() method to move a file. The original file the test1.txt file in the Apollo Test subdirectory of the user's documents directory, and the method moves the file to the Results subdirectory). Before running this code, be sure to create a test1.txt file in the Apollo Test subdirectory of the home directory on your computer. The try and catch statements show how to respond to errors.
import flash.filesystem.File;

var sourceFile:File = File.documentsDirectory;
sourceFile = sourceFile.resolve("Apollo Test/test1.txt");
var destination:File = File.documentsDirectory;
destination = destination.resolve("Apollo Test/Results/test1.txt");

try 
{
    sourceFile.moveTo(destination, true);
}
catch (error:Error)
{
    trace("Error:" error.message);
}

moveToAsync()method 
public function moveToAsync(newLocation:FileReference, clobber:Boolean = false):void

Begins moving the file or directory at the location specified by this File object to the location specified by the newLocation parameter.

To rename a file, set the destination parameter to point to a path that is in the file's directory, but with a different filename

Parameters
newLocation:FileReference — The target location for the move. This object specifies the path to the resulting (moved) file or directory, not the path to the containing directory.
 
clobber:Boolean (default = false) — If false, the move will fail if the target file already exists. If true, the operation will overwrite any existing file or directory of the same name.

Events
Event — Dispatched when the file or directory has been successfully moved.
 
IOErrorEvent — The source does not exist or the destination exists and clobber is false or the source could not be moved to the target.
 
IOErrorEvent — You do not have adequate permissions to read the source file or to write to the target location; or the source file is missing. On Windows, you cannot move a file that is open or a directory that contains a file that is open.

See also


Example
The following code shows how to use the moveToAsync() method to rename a file. The original file name is test1.txt and the resulting name is test2.txt. Since both the source and destination File object point to the same directory (the Apollo Test subdirectory of the user's documents directory), the moveToAsync() method renames the file (rather than moving it to a new directory). Before running this code, be sure to create a test1.txt file in the Apollo Test subdirectory of the documents directory on your computer. By setting the clobber parameter to true, the operation overwrites any existing test2.txt file.
import flash.filesystem.File;
import flash.events.Event;

var sourceFile:File = File.documentsDirectory;
sourceFile = sourceFile.resolve("Apollo Test/test1.txt");
var destination:File = File.documentsDirectory;
destination = destination.resolve("Apollo Test/test2.txt");

var sourceFile.moveToAsync(destination, true);
sourceFile.addEventListener(Event.COMPLETE, fileMoveCompleteHandler);

function fileMoveCompleteHandler(event:Event): void 
{
    trace("Done.")
}

moveToTrash()method 
public function moveToTrash():void

Moves a file or directory to the trash.


Throws
IOError — The operating system did not allow the operation or the file or directory does not exist. On Windows, you cannot move a file that is open or a directory that contains a file that is currently open.

See also

moveToTrashAsync()method 
public function moveToTrashAsync():void

Asynchronously moves a file or directory to the Trash.


Events
IOErrorEvent — The operating system did not allow the operation or the file or directory does not exist. On Windows, you cannot move a file that is open or a directory that contains a file that is currently open.
 
Event — Dispatched when the file or directory has been successfully moved to the trash.

See also

relativize()method 
public function relativize(ref:FileReference, useDotDot:Boolean = false):String

Finds relative path between two File paths.

The relative path is the list of components which may be appended to (resolved against) this reference in order to locate the second (parameter) reference. The relative path is returned using the "/" separator character.

Optionally, relative paths may include ".." references, but such a paths will not cross conspicuous volume boundaries.

Parameters
ref:FileReference — A File object against which the path is given.
 
useDotDot:Boolean (default = false) — Specifies whether the resulting relative path can use ".." components.

Returns
String — The relative path between this File and the ref file, if possible; otherwise null.

Throws
ArgumentError — The reference is null.
 
SecurityError — The caller does not have File privileges and either reference is to a file.
 
SecurityError — The caller does not have Persistent Store privileges and either reference is to the persistent store.

Example
The following code shows how to use the relativize() method to find the relative path between two File objects.
import flash.filesystem.File;

var file1:File = File.documentsDirectory.resolve("Apollo Test");
var file2:File = File.documentsDirectory.resolve("Apollo Test/bob/test.txt");

trace(file1.relativize(file2));       //   bob/test.txt
trace(file2.relativize(file1));       //   null
trace(file2.relativize(file1, true)); //   ../..

resolve()method 
public function resolve(path:String):File

Creates a new File object with a path relative to this this File object's path, based on the path parameter (a String).

If the given path is an absolute file reference, then that path is used, otherwise the path is interpreted relative to this File object as follows.

The given path is "appended" to this path (however, use of ".." in the path can return a resulting path that is not a child of the File object). The resulting reference need not refer to an actual file system location. Use the forward slash (/) character as the path separator (not \).

All resulting paths are normalized as follows:

Parameters
path:String — The path to append to this File's path.

Returns
File — A new File object, with the specified relative file path.
Event detail
cancelevent 
Event object type: flash.events.Event
Event.type property = flash.events.Event.CANCEL

Dispatched when a pending asynchronous operation is canceled.

Defines the value of the type property of a cancel event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
targetA reference to the object on which the operation is canceled.
completeevent  
Event object type: flash.events.Event
Event.type property = flash.events.Event.COMPLETE

Dispatched when an asynchronous operation is complete.

Defines the value of the type property of a complete event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
targetThe network object that has completed loading.
directoryListingevent  
Event object type: flash.events.FileListEvent

Dispatched when a directory list is available as a result to a call to the listDirectoryAsync() method.

See also

ioErrorevent  
Event object type: flash.events.IOErrorEvent
IOErrorEvent.type property = flash.events.IOErrorEvent.IO_ERROR

Dispatched when an error occurs during an asynchronous file operation.

Defines the value of the type property of an ioError event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
errorIDA reference number associated with the specific error.
targetThe network object experiencing the input/output error.
textText to be displayed as an error message.
securityErrorevent  
Event object type: flash.events.SecurityErrorEvent
SecurityErrorEvent.type property = flash.events.SecurityErrorEvent.SECURITY_ERROR

Dispatched when a an operation violates a security constraint.

The SecurityErrorEvent.SECURITY_ERROR constant defines the value of the type property of a securityError event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
targetThe network object reporting the security error.
textText to be displayed as an error message.
selectevent  
Event object type: flash.events.Event
Event.type property = flash.events.Event.SELECT

Dispatched when the user selects a file or directory from the a file- or directory-browsing dialog box.

Defines the value of the type property of a select event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
targetThe FileReference object on which an item has been selected.





collected by Jimbob 2007.05