Академический Документы
Профессиональный Документы
Культура Документы
Developing AIR applications with Flex / Files and data / Working with the file system
About the File class Paths of File objects Pointing a File object to a directory Pointing a File object to a file Modifying File paths Supported URL schemes Finding the relative path between two files Obtaining canonical versions of file names Working with packages and symbolic links
Getting the path to special directories, including the user directory, the user's documents directory, the directory from
which the application was launched, and the application directory Coping files and directories Moving files and directories Deleting files and directories (or moving them to the trash) Listing files and directories contained in a directory Creating temporary files and folders
Once a File object points to a file path, you can use it to read and write file data, using the FileStream class. A File object can point to the path of a file or directory that does not yet exist. You can use such a File object in creating a file or directory.
Each File object has two properties that each define its path:
Property
Description a path might be "c:\Sample directory\test.txt" whereas on Mac OS it could be "/Sample directory/test.txt". Note that a nativePath property uses the backslash (\) character as the directory separator character on Windows, and it uses the forward slash (/) character on Mac OS.
nativePath This specifies the platform-specific path to a file. For example, on Windows
url
This may use the file URL scheme to point to a file. For example, on Windows a path might be "file:///c:/Sample%20directory/test.txt" whereas on Mac OS it could be "file:///Sample%20directory/test.txt". The runtime includes other special URL schemes besides file, and these are described in Supported URL schemes.
The File class includes properties for pointing to standard directories on both Mac and Windows, and these are described in the following section.
The application storage directory location is based on the user name, the application ID, and the publisher ID:
On Mac OS--In:
Data/applicationID.publisherID/Local Store/
The URL (and url property) for a File object created with File.applicationStorageDirectory uses the
app-storage URL scheme (see Supported URL schemes), as in the following: var dir:File = File.applicationStorageDirectory; dir = dir.resolvePath("preferences"); trace(dir.url); // app-storage:/preferences
You can point a File object to the directory in which the application was installed, known as the application directory. You can reference this directory using the File.applicationDirectory property. You may use this directory to examine the application descriptor file or other resources installed with the application. For example, the following code points a File object to a directory named images in the application directory:
The URL (and url property) for a File object created with File.applicationDirectory uses the app URL scheme (see Supported URL schemes), as in the following:
You can also use the url property of a File object to point it to a directory based on a URL string, as in the following:
var urlStr:String = "file:///C:/AIR Test/"; var file:File = new File() file.url = urlStr;
var file:File = new File(); file.addEventListener(Event.SELECT, dirSelected); file.browseForDirectory("Select a directory"); function dirSelected(e:Event):void { trace(file.nativePath); }
You can use the url property of a File object to point it to a file or directory based on a URL string, as in the following:
var urlStr:String = "file:///C:/AIR Test/test.txt"; var file:File = new File() file.url = urlStr;
You can also pass the URL to the File() constructor function, as in the following:
Note that url property always returns the URI-encoded version of the URL (for example, blank spaces are replaced with "%20):
You can also use the nativePath property of a File object to set an explicit path. For example, the following code, when run on a Windows computer, sets a File object to the test.txt file in the AIR Test subdirectory of the C: drive:
You can also pass this path to the File() constructor function, as in the following:
On Windows you can use the forward slash (/) or backslash (\) character as the path delimiter for the nativePath property. On Mac OS, use the forward slash (/) character as the path delimiter for the nativePath:
These methods are each asynchronous. The browseForOpen() and browseForSave() methods dispatch the select event when the user selects a file (or a target path, in the case of browseForSave()). With the browseForOpen() and
browseForSave() methods, upon selection the target File object points to the selected files. The browseForOpenMultiple() method dispatches a selectMultiple event when the user selects files. The selectMultiple event is of type FileListEvent, which has a files property that is an array of File objects (pointing to
the selected files). For example, the following code presents the user with an "Open" dialog box in which the user can select a file.
var fileToOpen:File = File.documentsDirectory; selectTextFile(fileToOpen); function selectTextFile(root:File):void { var txtFilter:FileFilter = new FileFilter("Text", "*.as;*.css;*.html;*.txt;*.xml"); root.browseForOpen("Open", [txtFilter]); root.addEventListener(Event.SELECT, fileSelected); } function fileSelected(event:Event):void { trace(fileToOpen.nativePath); }
If the application has another browser dialog box open when you call a browse method, the runtime throws an Error exception.
nativePath or url property of the object, as in the following examples (on Windows): var file1:File = File.documentsDirectory; file1 = file1.resolvePath("AIR Test"); trace(file1.nativePath); // C:\Documents and Settings\userName\My Documents\AIR Test var file2:File = File.documentsDirectory; file2 = file2.resolvePath(".."); trace(file2.nativePath); // C:\Documents and Settings\userName var file3:File = File.documentsDirectory; file3.nativePath += "/subdirectory"; trace(file3.nativePath); // C:\Documents and Settings\userName\My Documents\subdirectory var file4:File = new File();
When using the nativePath property, you use either the forward slash (/) or backslash (\) character as the directory separator character on Windows; use the forward slash (/) character on Mac OS. On Windows, remember to type the backslash character twice in a string literal.
Description Use this to specify a path relative to the root of the file system. For
file:///c:/AIR Test/test.txt
The URL standard specifies that a file URL takes the form
app:/images
app-storage Use this to specify a path relative to the application store directory. For each installed application, AIR defines a unique application store directory, which is a useful place to store data specific to that application. For example, the following path points to a prefs.xml file in a settings subdirectory of the application store directory:
app-storage:/settings/prefs.xml
var file1:File = File.documentsDirectory.resolvePath("AIR Test"); var file2:File = File.documentsDirectory file2 = file2.resolvePath("AIR Test/bob/test.txt"); trace(file1.getRelativePath(file2)); // bob/test.txt
The second parameter of the getRelativePath() method, the useDotDot parameter, allows for .. syntax to be returned in results, to indicate parent directories:
var file1:File = File.documentsDirectory; file1 = file1.resolvePath("AIR Test"); var file2:File = File.documentsDirectory; file2 = file2.resolvePath("AIR Test/bob/test.txt"); var file3:File = File.documentsDirectory; file3 = file3.resolvePath("AIR Test/susan/test.txt"); trace(file2.getRelativePath(file1, true)); // ../.. trace(file3.getRelativePath(file2, true)); // ../../bob/test.txt
File.documentsDirectory.resolvePath("test.txt"); File.documentsDirectory.resolvePath("TeSt.TxT");
However, documents and directory names do include capitalization. For example, the following assumes that there is a folder named AIR Test in the documents directory, as in the following examples:
var file:File = File.documentsDirectory.resolvePath("AIR test"); trace(file.nativePath); // ... AIR test file.canonicalize(); trace(file.nativePath); // ... AIR Test
The canonicalize method converts the nativePath object to use the correct capitalization for the file or directory name.
You can also use the canonicalize() method to convert short file names ("8.3" names) to long file names on Windows, as in the following examples:
var path:File = new File(); path.nativePath = "C:\\AIR~1"; path.canonicalize(); trace(path.nativePath); // C:\AIR Test
var desktopNodes:File = File.desktopDirectory.getDirectoryListing(); for (var i:uint = 0; i < desktopNodes.length; i++) { if (desktopNodes[i].isDirectory && !!desktopNodes[i].isPackage) { trace(desktopNodes[i].name); } }
The following code iterates through the user's desktop directory, listing files and directories that are not symbolic links:
var desktopNodes:File = File.desktopDirectory.getDirectoryListing(); for (var i:uint = 0; i < desktopNodes.length; i++) { if (!desktopNodes[i].isSymbolicLink) { trace(desktopNodes[i].name); } }
The canonicalize() method changes the path of a symbolic link to point to the file or directory to which the link refers. The following code iterates through the user's desktop directory, and reports the paths referenced by files that are symbolic links:
var desktopNodes:File = File.desktopDirectory.getDirectoryListing(); for (var i:uint = 0; i < desktopNodes.length; i++) { if (desktopNodes[i].isSymbolicLink) { var linkNode:File = desktopNodes[i] as File; linkNode.canonicalize(); trace(linkNode.nativePath); } }