class File
package openfl.filesystem
extends FileReference › EventDispatcher
Available on AIR, Android, HashLink, Linux, Neko, Windows, iOS, macOS
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 plan 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 the FileStream class to read and write files.
The File class extends the FileReference class. The FileReference class represents a pointer to a file, but the File class adds properties and methods that are not exposed in Flash or HTML5.
The File class includes static properties that let you reference commonly used directory locations. These static properties include:
- File.applicationStorageDirectory—a storage directory unique to each installed application
- File.applicationDirectory—the read-only directory where the application is installed (along with any installed assets)
- File.desktopDirectory—the user's desktop directory
- File.documentsDirectory—the user's documents directory
- File.userDirectory—the user directory
These properties have meaningful values on different operating systems. For example, Mac OS, Linux, and Windows each have different native paths to the user's desktop directory. However, the File.desktopDirectory property points to the correct desktop directory path on each of these platforms. To write applications that work well across platforms, use these properties as the basis for referencing other files used by the application. Then use the resolvePath() method to refine the path. For example, this code points to the preferences.xml file in the application storage directory:
var prefsFile:File = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("preferences.xml");
If you use a literal native path in referencing a file, it will only work on one platform. For example, the following File object would only work on Windows:
new File("C:\Documents and Settings\joe\My Documents\test.txt")
The application storage directory is particularly useful. It gives an application-specific storage directory for the OpenFL application. It is defined by the File.applicationStorageDirectory property.
Events:
cancel | Dispatched when a pending asynchronous operation is canceled. |
---|---|
complete | Dispatched when an asynchronous operation is complete. |
directoryListing | Dispatched when a directory list is available as a result of a call to the getDirectoryListingAsync() method. |
ioError | Dispatched when an error occurs during an asynchronous file operation. |
securityError | Dispatched when an operation violates a security constraint. |
select | Dispatched when the user selects a file or directory from a file- or directory-browsing dialog box. |
selectMultiple | Dispatched when the user selects files from the dialog box opened by a call to the browseForOpenMultiple() method. |
See also:
Static variables
staticread onlyapplicationDirectory:File
The folder containing the application's installed files.
The url property for this object uses the app URL scheme (not the file URL scheme). This means that the url string is specified starting with "app:" (not "file:"). Also, if you create a File object relative to the File.applicationDirectory directory (by using the resolvePath() method), the url property of the File object also uses the app URL scheme. The url property is currently unsupported on all targets execpt AIR.
Note: You cannot write to files or directories that have paths that use the app: URL scheme. Also, you cannot delete or create files or folders that have paths that use the app: URL scheme. Modifying content in the application directory is a bad practice, for security reasons, and is blocked by the operating system on some platforms. If you want to store application-specific data, consider using the application storage directory (File.applicationStorageDirectory). If you want any of the content in the application storage directory to have access to the application-privileged functionality (AIR APIs), you can expose that functionality by using a sandbox bridge.
The applicationDirectory property provides a way to reference the application directory that works across platforms. If you set a File object to reference the application directory using the nativePath or url property, it will only work on the platform for which that path is valid.
On Android, the nativePath property of a File object pointing to the application directory is an empty string. Use the url property to access application files.
See also:
staticread onlyapplicationStorageDirectory:File
The application's private storage directory.
Each application has a unique, persistent application storage directory, which is created when you first access File.applicationStorageDirectory. This directory is unique to each application and user. This directory is a convenient location to store user-specific or 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 File.applicationStoreDirectory directory (by using the resolvePath() method), the url of the File object also uses the app-storage URL scheme (as in the example). The url property is currently unsupported on all targets execpt AIR.
The applicationStorageDirectory property provides a way to reference the application storage directory that works across platforms. If you set a File object to reference the application storage directory using the nativePath or url property, it will only work on the platform for which that path is valid.
The following code creates a File object pointing to the "images" subdirectory of the application storage directory.
import openfl.filesystem.File;
var tempFiles:File = File.applicationStorageDirectory;
tempFiles = tempFiles.resolvePath("images/");
trace(tempFiles.url); // app-storage:/images
See also:
staticread onlydesktopDirectory:File
The user's desktop directory.
The desktopDirectory property provides a way to reference the desktop directory that works across platforms. If you set a File object to reference the desktop directory using the nativePath or url property, it will only work on the platform for which that path is valid.
If an operating system does not support a desktop directory, a suitable directory in the file system is used instead.
The following code outputs a list of files and directories contained in the user's desktop directory.
import openfl.filesystem.File;
var desktop:File = File.desktopDirectory;
var files:Array = desktop.getDirectoryListing();
for (i in 0...files.length) {
trace(files[i].nativePath);
}
See also:
staticread onlydocumentsDirectory:File
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, the default location is /Users/userName/Documents. On Linux, the default location is /home/userName/Documents (on an English system), and the property observes the xdg-user-dirs setting.
The documentsDirectory property provides a way to reference the documents directory that works across platforms. If you set a File object to reference the documents directory using the nativePath or url property, it will only work on the platform for which that path is valid.
If an operating system does not support a documents directory, a suitable directory in the file system is used instead.
The following code uses the File.documentsDirectory property and the File.createDirectory() method to ensure that a directory named "OpenFL Test" exists in the user's documents directory.
import openfl.filesystem.File;
var directory:File = File.documentsDirectory;
directory = directory.resolvePath("OpenFL Test");
File.createDirectory(directory);
trace(directory.exists); // true
See also:
staticread onlylineEnding:String
The line-ending character sequence used by the host operating system.
On Mac OS and Linux, 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).
See also:
staticread onlyseparator:String
The host operating system's path component separator character.
On Mac OS and Linux, 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.
See also:
staticread onlyuserDirectory:File
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. On Linux, it is /home/userName.
The userDirectory property provides a way to reference the user directory that works across platforms. If you set the nativePath or url property of a File object directly, it will only work on the platform for which that path is valid.
If an operating system does not support a user directory, a suitable directory in the file system is used instead.
The following code outputs a list of files and directories contained in the root level of the user directory:
import openfl.filesystem.File;
var files:Array = File.userDirectory.listDirectory();
for (i in 0...files.length) {
trace(files[i].nativePath);
}
See also:
Static methods
staticcreateTempDirectory():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.
You may want to delete the temporary directory before closing the application, since on some devices it is not deleted automatically.
The following code uses the createTempFile() method to obtain a reference to a new temporary directory.
import openfl.File;
var temp:File = File.createTempDirectory();
trace(temp.nativePath);
Each time you run this code, a new (unique) file is created.
Returns:
File A File object referencing the new temporary directory.
See also:
staticcreateTempFile():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.
You may want to delete the temporary file before closing the application, since it is not deleted automatically.
The following code uses the createTempFile() method to obtain a reference to a new temporary file.
import openfl.File;
var temp:File = File.createTempFile();
trace(temp.nativePath);
Returns:
File A File object referencing the new temporary file;
See also:
staticgetRootDirectories():Array<File>
Returns an array of File objects, listing the file system root directories.
For example, on Windows this is a list of volumes such as the C: drive and the D: drive. An empty drive, such as a CD or DVD drive in which no disc is inserted, is not included in this array. On Mac OS and Linux, this method always returns the unique root directory for the machine (the "/" directory)
On file systems for which the root is not readable, such as the Android file system, the properties of the returned File object do not always reflect the true value. For example, on Android, the spaceAvailable property reports 0.
The following code outputs a list of root directories:
import flash.filesystem.File;
var rootDirs:Array = File.getRootDirectories();
for (i in 0...rootDirs.length) {
trace(rootDirs[i].nativePath);
}
Returns:
Array An array of File objects, listing the root directories.
Constructor
new(?path:String)
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.
Although you can pass a path argument to specify a file path, consider whether doing so may result in platform-specific code. For example, a native path such as "C:\Documents and Settings\bob\Desktop" or a URL such as "file:///C:/Documents%20and%20Settings/bob/Desktop" is only valid on Windows. It is far better to use the following static properties, which represent commonly used directories, and which are valid on all platforms:
*File.applicationDirectory
*File.applicationStorageDirectory
*File.desktopDirectory
*File.documentsDirectory
*File.userDirectory
You can then use the resolvePath() method to get a path relative to these directories. For example, the following code sets up a File object to point to the settings.xml file in the application storage directory:
var file:File = File.applicationStorageDirectory.resolvePath("settings.xml");
Parameters:
path | The path to the file. You can specify the path by using either a URL or native path (platform-specific) notation. |
---|
Throws:
ArgumentError | The syntax of the path parameter is invalid. |
---|
Variables
read onlyexists:Bool
Indicates whether the referenced file or directory exists. The value is true if the File object points to an existing file or directory, false otherwise.
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 openfl.filesystem.*;
var temp:File = File.createTempFile();
trace(temp.exists); // true
temp.deleteFile();
trace(temp.exists); // false
read onlyicon:Icon = null
An Icon object containing the icons defined for the file. An Icon object
is an array of BitmapData objects corresponding to the various icon
states. On Linux, the Icon object contains no icons. On Android, the
icon
property is null
.
read onlyisDirectory:Bool
Indicates whether the reference is to a directory. The value is true if the File object points to a directory; false otherwise.
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 to files).
import openfl.filesystem.*;
var userDirFiles:Array = File.userDirectory.getDirectoryListing();
for (i in 0...userDirFiles.length) {
if (userDirFiles[i].isDirectory) {
trace(userDirFiles[i].nativePath);
}
}
See also:
read onlyisHidden:Bool
Indicates whether the referenced file or directory is "hidden." The value is true if the referenced file or directory is hidden, false otherwise.
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 openfl.filesystem.*;
var userDirFiles:Array = File.userDirectory.getDirectoryListing();
for (i in 0...userDirFiles.length) {
if (userDirFiles[i].isHidden) {
trace(userDirFiles[i].nativePath);
}
}
nativePath:String
The full path in the host operating system representation. On Mac OS and Linux, the forward slash (/) character is used as the path separator. However, in Windows, you can set the nativePath property by using the forward slash character or the backslash () character as the path separator, and AIR automatically replaces forward slashes with the appropriate backslash character.
Before writing code to set the nativePath property directly, consider whether doing so may result in platform-specific code. For example, a native path such as "C:\Documents and Settings\bob\Desktop" is only valid on Windows. It is far better to use the following static properties, which represent commonly used directories, and which are valid on all platforms:
*File.applicationDirectory
*File.applicationStorageDirectory
*File.desktopDirectory
*File.documentsDirectory
*File.userDirectory
You can use the resolvePath() method to get a path relative to these directories.
Throws:
ArgumentError | The syntax of the path is invalid. |
---|---|
SecurityError | The caller is not in the application security sandbox. 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.
|
read onlyparent:File
The directory that contains the file or directory referenced by this File object.
If the file or directory does not exist, the parent property still returns the File object that points to the containing directory, even if that directory does not exist.
This property is identical to the return value for resolvePath("..") except that the parent of a root directory is null.
The following code uses the parent property to show the directory that contains a temporary file.
import openfl.filesystem.File;
var tempFile:File = File.createTempDirectory();
trace(tempFile.parent.nativePath);
tempFile.deleteFile();
See also:
Methods
browse(?typeFilter:Array<FileFilter>):Bool
Displays a file-browsing dialog box that lets the user select a file to upload.
browseForDirectory(title:String):Void
Displays a directory chooser dialog box, in which the user can select a directory. When the user selects the directory, the select event is dispatched. The target property of the select event is the File object pointing to the selected directory.
The directory chooser dialog is not always displayed in front of windows that are "owned" by another window (windows that have a non-null owner property). To avoid window ordering issues, hide owned windows before calling this method.
Parameters:
title | The string that is displayed in the title bar of the dialog box. |
---|
Throws:
IllegalOperationError | A browse operation (browseForOpen(), browseForOpenMultiple(), browseForSave(), browseForDirectory()) is currently running. The following code uses the File.browseForDirectory() method to let the user select a directory. When the directory is selected, the code lists the contents of the selected directory in the trace() output.
|
---|
Events:
cancel | Dispatched when the user clicks the Cancel button in the Open File dialog box. |
---|---|
select | Dispatched when the user selects a directory and closes the directory chooser dialog box. |
browseForOpen(title:String, ?typeFilter:Array<FileFilter>):Void
Displays the Open File dialog box, in which the user can select a file to open.
When the user selects the file, the select event is dispatched. The target property of the select event is the File object pointing to the selected file.
The Open File dialog is not always displayed in front of windows that are "owned" by another window (windows that have a non-null owner property). To avoid window ordering issues, hide owned windows before calling this method.
Parameters:
title | The string that is displayed in the title bar of the dialog box. |
---|---|
typeFilter | An array of FileFilter instances used to filter the files that are displayed in the dialog box. If you omit this parameter, all files are displayed. For more information, see the FileFilter class. |
Throws:
IllegalOperationError | A browse operation (browseForOpen(), browseForOpenMultiple(), browseForSave(), browseForDirectory()) is currently running. |
---|---|
SecurityError | The application does not have the necessary permissions. The following code uses the File.browseForOpen() method to let the user choose a text file. When the file is selected, the code reads the file data into a string.
|
Events:
cancel | Dispatched when the user clicks the Cancel button in the Open File dialog box. |
---|---|
select | Dispatched when the user selects a directory and closes the directory chooser dialog box. |
browseForOpenMultiple(title:String, ?typeFilter:Array<FileFilter>):Void
Displays the Open File dialog box, in which the user can select one or more files to open.
When the user selects the files, the selectMultiple event is dispatched. The target property of the select event is this File object. Unlike browseForOpen(), with the browseForOpenMultiple() method, this File object is not updated to reference any of the chosen files. Instead, the resulting selectMultiple event contains an array of the chosen files.
The Open File dialog is not always displayed in front of windows that are "owned" by another window (windows that have a non-null owner property). To avoid window ordering issues, hide owned windows before calling this method.
Parameters:
title | The string that is displayed in the title bar of the dialog box. |
---|---|
typeFilter | An array of FileFilter instances used to filter the files that are displayed in the dialog box. If you omit this parameter, all files are displayed. For more information, see the FileFilter class. |
Throws:
IllegalOperationError | A browse operation (browseForOpen(), browseForOpenMultiple(), browseForSave(), browseForDirectory()) is currently running. |
---|---|
SecurityError | The application does not have the necessary permissions. The following code uses the File.browseForOpenMultiple() method to let the user choose multiple files. When the files are selected, the code outputs the paths for the selected files.
|
Events:
cancel | Dispatched when the user clicks the Cancel button in the Open File dialog box. |
---|---|
select | Dispatched when the user selects a directory and closes the directory chooser dialog box. |
browseForSave(title:String):Void
Displays the Save File dialog box, in which the user can select a file destination.
When the user selects the file, the select event is dispatched. The target property of the select event is the File object pointing to the selected Save destination.
The Save File dialog is not always displayed in front of windows that are "owned" by another window (windows that have a non-null owner property). To avoid window ordering issues, hide owned windows before calling this method.
Parameters:
title | The string that is displayed in the title bar of the dialog box. If you omit this parameter, all files are displayed. For more information, see the FileFilter class. |
---|
Throws:
IllegalOperationError | A browse operation (browseForOpen(), browseForOpenMultiple(), browseForSave(), browseForDirectory()) is currently running. |
---|---|
SecurityError | The application does not have the necessary permissions. The following code uses the File.browseForSave() method to let the user select a path for saving a file. When the files are selected, the code saves data to the selected file path.
|
Events:
cancel | Dispatched when the user clicks the Cancel button in the Open File dialog box. |
---|---|
select | Dispatched when the user selects a directory and closes the directory chooser dialog box. |
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. If the File object is a symbolic link, canonicalization adjusts the path so that it matches the file or directory that the link points to, regardless of whether the file or directory that is pointed to exists. On case sensitive file systems (such as Linux), when multiple files exist with names differing only in case, the canonicalize() method adjusts the path to match the first file found (in an order determined by the file system).
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 OpenFL Test on the desktop of your computer.
import openfl.filesystem.*;
var path:File = File.desktopDirectory.resolvePath("openfl test");
trace(path.nativePath);
path.canonicalize();
trace(path.nativePath); // ...\OpenFL Test
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 Haxe File object. To copy a file, use the copyTo() method.
copyTo(newLocation:FileReference, overwrite:Bool = false):Void
Copies the file or directory at the location specified by this File object to the location specified by the newLocation parameter. The copy process creates any required parent directories (if possible). When overwriting files using copyTo(), the file attributes are also overwritten.
Parameters:
newLocation | 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. |
---|---|
overwrite | If false, the copy fails if the file specified by the target parameter already exists. If true, the operation overwrites existing file or directory of the same name. |
Throws:
IOError | The source does not exist; or the source could not be copied to the target; or the source and destination refer to the same file or folder and overwrite is set to true. On Windows, you cannot copy a file that is open or a directory that contains a file that is open. |
---|---|
SecurityError | The application does not have the necessary permissions. The following code shows how to use the copyTo() method to copy a file. Before running this code, create a test1.txt file in the AIR Test subdirectory of the documents directory on your computer. The resulting copied file is named test2.txt, and it is also in the OpenFL Test subdirectory. When you set the overwrite parameter to true, the operation overwrites any existing test2.txt file.
The following code shows how to use the copyTo() method to copy a file. Before running this code, create a test1.txt file in the OpenFL 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.
|
See also:
copyToAsync(newLocation:FileReference, overwrite:Bool = 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 (unsuccessful) is dispatched. The copy process creates any required parent directories (if possible).
Parameters:
newLocation | 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. |
---|---|
overwrite | If false, the copy fails if the file specified by the target parameter already exists. If true, the operation overwrites existing file or directory of the same name. |
Throws:
SecurityError | The application does not have the necessary permissions to write to the destination. 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 OpenFL Test subdirectory of the documents directory on your computer. The resulting copied file is named test2.txt, and it is also in the OpenFL Test subdirectory. When you set the overwrite parameter to true, the operation overwrites any existing test2.txt file.
|
---|
Events:
complete | Dispatched when the file or directory has been successfully copied. |
---|---|
ioError | The source does not exist; or the source could not be copied to the target; or the source and destination refer to the same file or folder and overwrite is set to true. On Windows, you cannot copy a file that is open or a directory that contains a file that is open. |
See also:
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. |
---|---|
SecurityError | The application does not have the necessary permissions. The following code moves a file named test.txt on the desktop to the OpenFL Test subdirectory of the documents directory. The call to the createDirectory() method ensures that the OpenFL Test directory exists before the file is moved.
|
See also:
deleteDirectory(deleteDirectoryContents:Bool = false):Void
Deletes the directory.
Parameters:
deleteDirectoryContents | Specifies whether or not to delete a directory that contains files or subdirectories. When false, if the directory contains files or directories, 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. |
---|---|
SecurityError | The application does not have the necessary permissions to delete the directory. The following code creates an empty directory and then uses the deleteDirectory() method to delete the directory.
|
See also:
deleteDirectoryAsync(deleteDirectoryContents:Bool = false):Void
Deletes the directory asynchronously.
Parameters:
deleteDirectoryContents | Specifies whether or not to delete a directory that contains files or subdirectories. When false, if the directory contains files or directories, a call to this method throws an exception. |
---|
Throws:
SecurityError | The application does not have the necessary permissions to delete the directory. |
---|
Events:
complete | Dispatched when the directory has been deleted successfully. |
---|---|
ioError | 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():Void
Deletes the file.
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. |
---|---|
SecurityError | The application does not have the necessary permissions to delete the directory. The following code creates a temporary file and then calls the deleteFile() method to delete it.
|
See also:
deleteFileAsync():Void
Deletes the file asynchronously.
Throws:
SecurityError | The application does not have the necessary permissions to delete the directory. |
---|
Events:
complete | Dispatched when the directory has been deleted successfully. |
---|---|
ioError | 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:
getDirectoryListing():Array<File>
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.
The following code shows how to use the getDirectoryListing() method to enumerate the contents of the user directory.
import openfl.filesystem.File;
var directory:File = File.userDirectory;
var list:Array = directory.getDirectoryListing();
for (i in 0...list.length) {
trace(list[i].nativePath);
}
Returns:
Array An array of File objects.
See also:
getDirectoryListingAsync():Void
Asynchronously retrieves an array of File objects corresponding to the contents of the directory represented by this File object.
Events:
ioError | You do not have adequate permissions to read this directory, or the directory does not exist. |
---|---|
directoryListing | The directory contents have been enumerated successfully. The contents event includes a files property, which is the resulting array of File objects. The following code shows how to use the getDirectoryListingAsync() method to enumerate the contents of the user directory.
|
See also:
getRelativePath(ref:FileReference, useDotDot:Bool = false):String
Finds the relative path between two File paths.
The relative path is the list of components that can 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 paths will not cross conspicuous volume boundaries.
Parameters:
ref | A File object against which the path is given. |
---|---|
useDotDot | Specifies whether the resulting relative path can use ".." components. |
Returns:
String The relative path between this file (or directory) and the ref file (or directory), if possible; otherwise null.
Throws:
ArgumentError | The reference is null. |
---|---|
SecurityError | The caller is not in the application security sandbox. |
moveTo(newLocation:FileReference, overwrite:Bool = 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.
The move process creates any required parent directories (if possible).
Parameters:
newLocation | 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. |
---|---|
overwrite | If false, the move fails if the target file already exists. If true, the operation overwrites any existing file or directory of the same name. |
Throws:
IOError | The source does not exist; or the destination exists and overwrite is set to false; or the source file or directory could not be moved to the target location; or the source and destination refer to the same file or folder and overwrite is set to true. On Windows, you cannot move a file that is open or a directory that contains a file that is open. |
---|---|
SecurityError | The application does not have the necessary permissions to move the file. The following code shows how to use the moveTo() method to rename a file. The original filename is test1.txt and the resulting filename 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, create a test1.txt file in the OpenFL Test subdirectory of the documents directory on your computer. When you set the overwrite parameter to true, the operation overwrites any existing test2.txt file.
|
See also:
moveToAsync(newLocation:FileReference, overwrite:Bool = 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.
The move process creates any required parent directories (if possible).
@see Working with files
Parameters:
newLocation | 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. |
---|---|
overwrite | If false, the move fails if the target file already exists. If true, the operation overwrites any existing file or directory of the same name. |
Throws:
SecurityError | The application does not have the necessary permissions to move the file. The following code shows how to use the moveToAsync() method to rename a file. The original filename is test1.txt and the resulting name is test2.txt. Since both the source and destination File object point to the same directory (the OpenFL 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, create a test1.txt file in the OpenFL Test subdirectory of the documents directory on your computer. When you set overwrite parameter to true, the operation overwrites any existing test2.txt file.
|
---|
Events:
complete | Dispatched when the file or directory has been successfully moved. |
---|---|
ioError | The source does not exist; or the destination exists and overwrite is false; or the source could not be moved to the target; or the source and destination refer to the same file or folder and overwrite is set to true. On Windows, you cannot move a file that is open or a directory that contains a file that is open. |
openWithDefaultApplication():Void
Opens the file in the application registered by the operating system to open this file type.
resolvePath(path:String):File
Creates a new File object with a path relative to this File object's path, based on the path parameter (a string).
You can use a relative path or absolute path as the path parameter.
If you specify a relative path, the given path is "appended" to the path of the File object. 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.
If you specify an absolute file reference, the method returns the File object pointing to that path. The absolute file reference should use valid native path syntax for the user's operating system (such as "C:\test" on Windows). Do not use a URL (such as "file:///c:/test") as the path parameter.
All resulting paths are normalized as follows:
Any "." element is ignored.
Any ".." element consumes its parent entry.
No ".." reference that reaches the file system root or the application-persistent storage root passes
that node; it is ignored.
You should always use the forward slash (/) character as the path separator. On Windows, you can also use the backslash () character, but you should not. Using the backslash character can lead to applications that do not work on other platforms.
Filenames and directory names are case-sensitive on Linux.
Parameters:
path | The path to append to this File object's path (if the path parameter is a relative path); or the path to return (if the path parameter is an absolute path). |
---|
Returns:
File A new File object pointing to the resulting path.