From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../add-ons/sdk/low-level_apis/io_file/index.html | 196 +++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 files/fr/mozilla/add-ons/sdk/low-level_apis/io_file/index.html (limited to 'files/fr/mozilla/add-ons/sdk/low-level_apis/io_file/index.html') diff --git a/files/fr/mozilla/add-ons/sdk/low-level_apis/io_file/index.html b/files/fr/mozilla/add-ons/sdk/low-level_apis/io_file/index.html new file mode 100644 index 0000000000..51900f5799 --- /dev/null +++ b/files/fr/mozilla/add-ons/sdk/low-level_apis/io_file/index.html @@ -0,0 +1,196 @@ +--- +title: io/file +slug: Mozilla/Add-ons/SDK/Low-Level_APIs/io_file +translation_of: Archive/Add-ons/Add-on_SDK/Low-Level_APIs/io_file +--- +

{{AddonSidebar}}

+ +
+

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 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:

+ + + +
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.

-- cgit v1.2.3-54-g00ecf