Cette traduction est incomplète. Aidez à traduire cet article depuis l'anglais.
Expérimental
Permet d'accéder au système de fichiers local.
Utilisation
Paths
Les specifications Path de cette API sont spécifiques à l'OS. Cela signifie que les chemins Windows sont spécifiés en utilisant le séparateur antislash (\
), et sur les systèmes de type Unix comme Linux et OS X les slash sont utilisés (/
).
To ensure your add-on works for everyone, generate paths using the join
function. Unfortunately this API does not currently provide a way to obtain an absolute base path which you could then use with join
. For now, you need to require("chrome")
and use the XPCOM directory service as described in this article about File I/O.
Note that if you do decide to hardcode Windows-style paths, be sure to escape backslashes in strings. For example, to specify the file at C:\Users\Myk
, you need to use the string "C:\\Users\\Myk"
, not "C:\Users\Myk"
. Read more about escaping characters in strings.
Globals
Functions
basename(path)
The path
parameter must be an absolute path, relative paths will cause an error.
Use the fs/path module for relative path support.
Returns the last component of the given path. For example, basename("/foo/bar/baz")
returns "baz"
. If the path has no components, the empty string is returned.
Parameters
path : string
The path of a file.
Returns
string : The last component of the given path.
dirname(path)
Returns the path of the directory containing the given file. If the file is at the top of the volume, the empty string is returned.
Parameters
path : string
The path of a file.
Returns
string : The path of the directory containing the file.
exists(path)
Returns true if a file exists at the given path and false otherwise.
Parameters
path : string
The path of a file.
Returns
boolean : True if the file exists and false otherwise.
join(...)
Takes a variable number of strings, joins them on the file system's path separator, and returns the result.
Parameters
... : strings
A variable number of strings to join. The first string must be an absolute path.
Returns
string : A single string formed by joining the strings on the file system's path separator.
list(path)
Returns an array of file names in the given directory.
Parameters
path : string
The path of the directory.
Returns
array : An array of file names. Each is a basename, not a full path.
mkpath(path)
Makes a new directory named by the given path. Any subdirectories that do not exist are also created. mkpath
can be called multiple times on the same path.
Parameters
path : string
The path to create.
open(path, mode)
Returns a stream providing access to the contents of a file.
Parameters
path : string
The path of the file to open.
mode : string
An optional string, each character of which describes a characteristic of the returned stream.
- If the string contains
"r"
, the file is opened in read-only mode. "w"
opens the file in write-only mode."b"
opens the file in binary mode. If"b"
is not present, the file is opened in text mode, and its contents are assumed to be UTF-8.
If mode
is not given, "r"
is assumed, and the file is opened in read-only text mode.
Apart from these options, this API always passes the following options: CREATE_FILE
, TRUNCATE
(see https://dxr.mozilla.org/mozilla-central/source/nsprpub/pr/include/prio.h#550). This means that:
- if the file does not exist it is created
- if the file exists, its length is truncated to zero
- it is not possible to open the file in append mode.
Returns
stream : If the file is opened in text read-only mode
, a TextReader
is returned, and if text write-only mode, a TextWriter
is returned. See text-streams
for information on these text stream objects. If the file is opened in binary read-only mode
, a ByteReader
is returned, and if binary write-only mode, a ByteWriter
is returned. See byte-streams
for more information on these byte stream objects. Opened files should always be closed after use by calling close
on the returned stream.
read(path, mode)
Opens a file and returns a string containing its entire contents.
Parameters
path : string
The path of the file to read.
mode : string
An optional string, each character of which describes a characteristic of the returned stream. If the string contains "b"
, the contents will be returned in binary mode. If "b"
is not present or mode
is not given, the file contents will be returned in text mode.
Returns
string : A string containing the file's entire contents.
remove(path)
Removes a file from the file system. To remove directories, use rmdir
.
Parameters
path : string
The path of the file to remove.
rmdir(path)
Removes a directory from the file system. If the directory is not empty, an exception is thrown.
Parameters
path : string
The path of the directory to remove.
isFile(path)
Returns true only if this path specifies a file.
const fileIO = require("sdk/io/file"); let path = "/Users/Work/"; let list = fileIO.list(path); for (i = 0; i < list.length; i++) { let item = fileIO.join(path, list[i]); if (fileIO.isFile(item)) { console.log(item + " is a file"); } else { console.log(item + " is a directory"); } }
Parameters
path : string
The path of the object.