FileGuide:IO
From MDC
Contents |
[edit] Files and Streams
This section describes how to access and get information about files, read from files and create and write files.
[edit] Using Files
A XUL application or extension may access files on disk, and either read or write to files. A file is accessed via a File object. To get a reference to a File object use the getFile method of the top-level IO object. The following example will retrieve a reference to a file located in the profile directory named sample.txt.
var file = IO.getFile("Profile", "sample.txt");
The IO object is a global object always available within an application or extension. It provides a number of useful functions for dealing with files.
- Retrieve a File Object
- For information about getting a File Object, see Accessing Files
- Get Information about a File
- Available information about a file include the permissions, size, and last modified date of a file. See Getting File Information for more details.
- Reading from a File
- For information about reading from text and binary files, see Reading from Files.
- Writing to a File
- For information about writing to text and binary files, see Writing to Files.
- Copying and Moving Files
- Files may be moved and copied on disk. For details, see Copying a File and Moving a File.
- Deleting Files
- To learn how to delete a file, see Deleting a File.
- Open and Save Dialogs
- You can show a dialog to the user to allow them to select a file to open or enter a filename for saving. For details about this, see Open and Save Dialogs.
- File Errors
- See File Errors for a list of errors that might occur while using files.
[edit] Working With Directories
Directories may be accessed in a similar manner using the getFile method. For instance, in the following example, a subdirectory is retrieved. If the name 'subdirname' refers to a directory, then the returned object will refer to this directory. The returned value is still a File object, however a number of methods may be used which are only of value for subdirectories.
var file = IO.getFile("Profile", "subdirname");
For more information about using directories, see Working with Directories. Specifically, to create a directory, see Creating Directories and to retrieve a list of files or subdirectories within a directory, see Iterating over the Files in a Directory.
[edit] Upload and Download Files
- Upload Files
- To learn how to upload and post files to a web site, see Uploading and Downloading Files.
- Download Files
- To learn how to download and save a file from a web site to disk, see Downloading Files.