From 691c721252be6d9549d3199b8233b6dbcab685b1 Mon Sep 17 00:00:00 2001 From: Annie_wang Date: Mon, 8 May 2023 09:19:43 +0800 Subject: [PATCH] update docs Signed-off-by: Annie_wang --- .../reference/apis/js-apis-file-fs.md | 1132 +++++++++++------ 1 file changed, 733 insertions(+), 399 deletions(-) diff --git a/en/application-dev/reference/apis/js-apis-file-fs.md b/en/application-dev/reference/apis/js-apis-file-fs.md index d210887e3e..897d38e76b 100644 --- a/en/application-dev/reference/apis/js-apis-file-fs.md +++ b/en/application-dev/reference/apis/js-apis-file-fs.md @@ -2,7 +2,8 @@ The **fs** module provides APIs for file operations, including basic file management, directory management, file information statistics, and data read and write using a stream. -> **NOTE**
+> **NOTE** +> > The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. ## Modules to Import @@ -11,10 +12,6 @@ The **fs** module provides APIs for file operations, including basic file manage import fs from '@ohos.file.fs'; ``` -## Error Code Description - -The APIs of this module supports processing of error codes. For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). - ## Guidelines Before using the APIs provided by this module to perform operations on a file or folder, obtain the application sandbox path of the file or folder as follows: @@ -61,9 +58,13 @@ Obtains detailed file information. This API uses a promise to return the result. **Return value** - | Type | Description | - | ---------------------------- | ---------- | - | Promise<[Stat](#stat)> | Promise used to return the file information obtained.| +| Type | Description | +| ---------------------------- | ---------- | +| Promise<[Stat](#stat)> | Promise used to return the file information obtained.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -91,6 +92,10 @@ Obtains detailed file information. This API uses an asynchronous callback to ret | file | string\|number | Yes | Application sandbox path or FD of the file. | | callback | AsyncCallback<[Stat](#stat)> | Yes | Callback invoked to return the file information obtained.| +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -117,12 +122,15 @@ Obtains detailed file information synchronously. | ------ | ------ | ---- | -------------------------- | | file | string\|number | Yes | Application sandbox path or FD of the file.| - **Return value** - | Type | Description | - | ------------- | ---------- | - | [Stat](#stat) | File information obtained.| +| Type | Description | +| ------------- | ---------- | +| [Stat](#stat) | File information obtained.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -147,9 +155,13 @@ Checks whether a file exists. This API uses a promise to return the result. **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<boolean> | Promise used to return the result. The value **true** means the file exists; the value **false** means the opposite.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<boolean> | Promise used to return the result. The value **true** means the file exists; the value **false** means the opposite.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -164,7 +176,6 @@ Checks whether a file exists. This API uses a promise to return the result. }); ``` - ## fs.access access(path: string, callback: AsyncCallback<boolean>): void @@ -178,7 +189,11 @@ Checks whether a file exists. This API uses an asynchronous callback to return t | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ------------------------------------------------------------ | | path | string | Yes | Application sandbox path of the file. | -| callback | AsyncCallback<boolean> | Yes | Callback invoked to return the result. | +| callback | AsyncCallback<boolean> | Yes | Callback invoked to return the result. The value **true** means the file exists; the value **false** means the opposite.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -211,9 +226,13 @@ Synchronously checks whether a file exists. **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | boolean | Returns **true** if the file exists; returns **false** otherwise.| +| Type | Description | +| ------------------- | ---------------------------- | +| boolean | Returns **true** if the file exists; returns **false** otherwise.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -240,15 +259,19 @@ Closes a file. This API uses a promise to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ------------ | - | file | [File](#file)\|number | Yes | File object or FD of the file to close.| +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ------------ | +| file | [File](#file)\|number | Yes | File object or FD of the file to close.| **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -273,10 +296,14 @@ Closes a file. This API uses an asynchronous callback to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | -------- | ------------------------- | ---- | ------------ | - | file | [File](#file)\|number | Yes | File object or FD of the file to close.| - | callback | AsyncCallback<void> | Yes | Callback invoked when the file is closed asynchronously.| +| Name | Type | Mandatory | Description | +| -------- | ------------------------- | ---- | ------------ | +| file | [File](#file)\|number | Yes | File object or FD of the file to close.| +| callback | AsyncCallback<void> | Yes | Callback invoked when the file is closed asynchronously.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -302,9 +329,13 @@ Synchronously closes a file. **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ------------ | - | file | [File](#file)\|number | Yes | File object or FD of the file to close.| +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ------------ | +| file | [File](#file)\|number | Yes | File object or FD of the file to close.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -324,17 +355,21 @@ Copies a file. This API uses a promise to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ---- | -------------------------- | ---- | ---------------------------------------- | - | src | string\|number | Yes | Path or FD of the file to copy. | - | dest | string\|number | Yes | Destination path of the file or FD of the file created. | - | mode | number | No | Whether to overwrite the file of the same name in the destination path. The default value is **0**, which is the only value supported.
**0**: overwrite the file of the same name.| +| Name | Type | Mandatory | Description | +| ---- | -------------------------- | ---- | ---------------------------------------- | +| src | string\|number | Yes | Path or FD of the file to copy. | +| dest | string\|number | Yes | Destination path of the file or FD of the file created. | +| mode | number | No | Whether to overwrite the file of the same name in the destination path. The default value is **0**, which is the only value supported.
**0**: overwrite the file of the same name.| **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -358,12 +393,16 @@ Copies a file. This API uses an asynchronous callback to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | -------- | -------------------------- | ---- | ---------------------------------------- | - | src | string\|number | Yes | Path or FD of the file to copy. | - | dest | string\|number | Yes | Destination path of the file or FD of the file created. | - | mode | number | No | Whether to overwrite the file of the same name in the destination path. The default value is **0**, which is the only value supported.
**0**: overwrite the file with the same name and truncate the part that is not overwritten.| - | callback | AsyncCallback<void> | Yes | Callback invoked when the file is copied asynchronously. | +| Name | Type | Mandatory | Description | +| -------- | -------------------------- | ---- | ---------------------------------------- | +| src | string\|number | Yes | Path or FD of the file to copy. | +| dest | string\|number | Yes | Destination path of the file or FD of the file created. | +| mode | number | No | Whether to overwrite the file of the same name in the destination path. The default value is **0**, which is the only value supported.
**0**: overwrite the file with the same name and truncate the part that is not overwritten.| +| callback | AsyncCallback<void> | Yes | Callback invoked when the file is copied asynchronously. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -390,11 +429,15 @@ Synchronously copies a file. **Parameters** - | Name | Type | Mandatory | Description | - | ---- | -------------------------- | ---- | ---------------------------------------- | - | src | string\|number | Yes | Path or FD of the file to copy. | - | dest | string\|number | Yes | Destination path of the file or FD of the file created. | - | mode | number | No | Whether to overwrite the file of the same name in the destination path. The default value is **0**, which is the only value supported.
**0**: overwrite the file with the same name and truncate the part that is not overwritten.| +| Name | Type | Mandatory | Description | +| ---- | -------------------------- | ---- | ---------------------------------------- | +| src | string\|number | Yes | Path or FD of the file to copy. | +| dest | string\|number | Yes | Destination path of the file or FD of the file created. | +| mode | number | No | Whether to overwrite the file of the same name in the destination path. The default value is **0**, which is the only value supported.
**0**: overwrite the file with the same name and truncate the part that is not overwritten.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -404,7 +447,6 @@ Synchronously copies a file. fs.copyFileSync(srcPath, dstPath); ``` - ## fs.mkdir mkdir(path: string): Promise<void> @@ -421,9 +463,13 @@ Creates a directory. This API uses a promise to return the result. **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -436,7 +482,6 @@ Creates a directory. This API uses a promise to return the result. }); ``` - ## fs.mkdir mkdir(path: string, callback: AsyncCallback<void>): void @@ -452,6 +497,10 @@ Creates a directory. This API uses an asynchronous callback to return the result | path | string | Yes | Application sandbox path of the directory. | | callback | AsyncCallback<void> | Yes | Callback invoked when the directory is created asynchronously. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -465,7 +514,6 @@ Creates a directory. This API uses an asynchronous callback to return the result }); ``` - ## fs.mkdirSync mkdirSync(path: string): void @@ -480,6 +528,10 @@ Synchronously creates a directory. | ------ | ------ | ---- | ------------------------------------------------------------ | | path | string | Yes | Application sandbox path of the directory. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -487,7 +539,6 @@ Synchronously creates a directory. fs.mkdirSync(dirPath); ``` - ## fs.open open(path: string, mode?: number): Promise<File> @@ -505,9 +556,13 @@ Opens a file. This API uses a promise to return the result. File uniform resourc **Return value** - | Type | Description | - | --------------------- | ----------- | - | Promise<[File](#file)> | Promise used to return the file object.| +| Type | Description | +| --------------------- | ----------- | +| Promise<[File](#file)> | Promise used to return the file object.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -536,6 +591,10 @@ Opens a file. This API uses an asynchronous callback to return the result. File | path | string | Yes | Application sandbox path or URI of the file. | | mode | number | No | [Mode](#openmode) for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.
- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.
- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.
- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.
You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.
- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.
- **OpenMode.TRUNC(0o1000)**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.
- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.
- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.
- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception.
- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.
- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.| +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -566,9 +625,13 @@ Synchronously opens a file. File URIs are supported. **Return value** - | Type | Description | - | ------ | ----------- | - | [File](#file) | File object opened.| +| Type | Description | +| ------ | ----------- | +| [File](#file) | File object opened.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -597,9 +660,13 @@ Reads data from a file. This API uses a promise to return the result. **Return value** - | Type | Description | - | ---------------------------------- | ------ | - | Promise<number> | Promise used to return the data read.| +| Type | Description | +| ---------------------------------- | ------ | +| Promise<number> | Promise used to return the data read.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -626,12 +693,16 @@ Reads data from a file. This API uses an asynchronous callback to return the res **Parameters** - | Name | Type | Mandatory | Description | - | -------- | ---------------------------------------- | ---- | ---------------------------------------- | - | fd | number | Yes | FD of the file. | - | buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | - | options | Object | No | The options are as follows:
- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| - | callback | AsyncCallback<number> | Yes | Callback invoked when the data is read asynchronously. | +| Name | Type | Mandatory | Description | +| -------- | ---------------------------------------- | ---- | ---------------------------------------- | +| fd | number | Yes | FD of the file. | +| buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | +| options | Object | No | The options are as follows:
- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| +| callback | AsyncCallback<number> | Yes | Callback invoked when the data is read asynchronously. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -650,7 +721,6 @@ Reads data from a file. This API uses an asynchronous callback to return the res }); ``` - ## fs.readSync readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number @@ -661,17 +731,21 @@ Synchronously reads data from a file. **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ----------- | ---- | ---------------------------------------- | - | fd | number | Yes | FD of the file. | - | buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | - | options | Object | No | The options are as follows:
- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| +| Name | Type | Mandatory | Description | +| ------- | ----------- | ---- | ---------------------------------------- | +| fd | number | Yes | FD of the file. | +| buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | +| options | Object | No | The options are as follows:
- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| **Return value** - | Type | Description | - | ------ | -------- | - | number | Length of the data read.| +| Type | Description | +| ------ | -------- | +| number | Length of the data read.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -683,7 +757,6 @@ Synchronously reads data from a file. fs.closeSync(file); ``` - ## fs.rmdir rmdir(path: string): Promise<void> @@ -700,9 +773,13 @@ Deletes a directory. This API uses a promise to return the result. **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -715,7 +792,6 @@ Deletes a directory. This API uses a promise to return the result. }); ``` - ## fs.rmdir rmdir(path: string, callback: AsyncCallback<void>): void @@ -731,6 +807,10 @@ Deletes a directory. This API uses an asynchronous callback to return the result | path | string | Yes | Application sandbox path of the directory.| | callback | AsyncCallback<void> | Yes | Callback invoked when the directory is deleted asynchronously. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -744,7 +824,6 @@ Deletes a directory. This API uses an asynchronous callback to return the result }); ``` - ## fs.rmdirSync rmdirSync(path: string): void @@ -759,6 +838,10 @@ Synchronously deletes a directory. | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the directory.| +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -766,7 +849,6 @@ Synchronously deletes a directory. fs.rmdirSync(dirPath); ``` - ## fs.unlink unlink(path: string): Promise<void> @@ -783,9 +865,13 @@ Deletes a file. This API uses a promise to return the result. **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -798,7 +884,6 @@ Deletes a file. This API uses a promise to return the result. }); ``` - ## fs.unlink unlink(path: string, callback: AsyncCallback<void>): void @@ -814,6 +899,10 @@ Deletes a file. This API uses an asynchronous callback to return the result. | path | string | Yes | Application sandbox path of the file.| | callback | AsyncCallback<void> | Yes | Callback invoked when the file is deleted asynchronously. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -827,7 +916,6 @@ Deletes a file. This API uses an asynchronous callback to return the result. }); ``` - ## fs.unlinkSync unlinkSync(path: string): void @@ -842,6 +930,10 @@ Synchronously deletes a file. | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -860,17 +952,21 @@ Writes data into a file. This API uses a promise to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ------------------------------- | ---- | ---------------------------------------- | - | fd | number | Yes | FD of the file. | - | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | - | options | Object | No | The options are as follows:
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| +| Name | Type | Mandatory | Description | +| ------- | ------------------------------- | ---- | ---------------------------------------- | +| fd | number | Yes | FD of the file. | +| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | +| options | Object | No | The options are as follows:
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| **Return value** - | Type | Description | - | --------------------- | -------- | - | Promise<number> | Promise used to return the length of the data written.| +| Type | Description | +| --------------------- | -------- | +| Promise<number> | Promise used to return the length of the data written.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -885,7 +981,6 @@ Writes data into a file. This API uses a promise to return the result. }); ``` - ## fs.write write(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }, callback: AsyncCallback<number>): void @@ -896,12 +991,16 @@ Writes data into a file. This API uses an asynchronous callback to return the re **Parameters** - | Name | Type | Mandatory | Description | - | -------- | ------------------------------- | ---- | ---------------------------------------- | - | fd | number | Yes | FD of the file. | - | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | - | options | Object | No | The options are as follows:
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| - | callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | +| Name | Type | Mandatory | Description | +| -------- | ------------------------------- | ---- | ---------------------------------------- | +| fd | number | Yes | FD of the file. | +| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | +| options | Object | No | The options are as follows:
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| +| callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -918,7 +1017,6 @@ Writes data into a file. This API uses an asynchronous callback to return the re }); ``` - ## fs.writeSync writeSync(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }): number @@ -929,17 +1027,21 @@ Synchronously writes data into a file. **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ------------------------------- | ---- | ---------------------------------------- | - | fd | number | Yes | FD of the file. | - | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | - | options | Object | No | The options are as follows:
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| +| Name | Type | Mandatory | Description | +| ------- | ------------------------------- | ---- | ---------------------------------------- | +| fd | number | Yes | FD of the file. | +| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | +| options | Object | No | The options are as follows:
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| **Return value** - | Type | Description | - | ------ | -------- | - | number | Length of the data written in the file.| +| Type | Description | +| ------ | -------- | +| number | Length of the data written in the file.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -968,9 +1070,13 @@ Truncates a file. This API uses a promise to return the result. **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -984,7 +1090,6 @@ Truncates a file. This API uses a promise to return the result. }); ``` - ## fs.truncate truncate(file: string|number, len?: number, callback: AsyncCallback<void>): void @@ -1001,6 +1106,10 @@ Truncates a file. This API uses an asynchronous callback to return the result. | len | number | No | File length, in bytes, after truncation. The default value is **0**.| | callback | AsyncCallback<void> | Yes | Callback that returns no value. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -1015,7 +1124,6 @@ Truncates a file. This API uses an asynchronous callback to return the result. }); ``` - ## fs.truncateSync truncateSync(file: string|number, len?: number): void @@ -1031,6 +1139,10 @@ Synchronously truncates a file. | file | string\|number | Yes | Application sandbox path or FD of the file. | | len | number | No | File length, in bytes, after truncation. The default value is **0**.| +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -1039,7 +1151,6 @@ Synchronously truncates a file. fs.truncateSync(filePath, len); ``` - ## fs.readText readText(filePath: string, options?: { offset?: number; length?: number; encoding?: string; }): Promise<string> @@ -1057,9 +1168,13 @@ Reads the text content of a file. This API uses a promise to return the result. **Return value** - | Type | Description | - | --------------------- | ---------- | - | Promise<string> | Promise used to return the content read.| +| Type | Description | +| --------------------- | ---------- | +| Promise<string> | Promise used to return the content read.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1072,7 +1187,6 @@ Reads the text content of a file. This API uses a promise to return the result. }); ``` - ## fs.readText readText(filePath: string, options?: { offset?: number; length?: number; encoding?: string; }, callback: AsyncCallback<string>): void @@ -1089,6 +1203,10 @@ Reads the text content of a file. This API uses an asynchronous callback to retu | options | Object | No | The options are as follows:
- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.
- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.
- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.| | callback | AsyncCallback<string> | Yes | Callback invoked to return the content read. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -1102,7 +1220,6 @@ Reads the text content of a file. This API uses an asynchronous callback to retu }); ``` - ## fs.readTextSync readTextSync(filePath: string, options?: { offset?: number; length?: number; encoding?: string; }): string @@ -1120,9 +1237,13 @@ Synchronously reads the text of a file. **Return value** - | Type | Description | - | ------ | -------------------- | - | string | Promise used to return the content of the file read.| +| Type | Description | +| ------ | -------------------- | +| string | Promise used to return the content of the file read.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1148,9 +1269,13 @@ Obtains information about a symbolic link. This API uses a promise to return the **Return value** - | Type | Description | - | ---------------------------- | ---------- | - | Promise<[Stat](#stat)> | Promise used to return the symbolic link information obtained. For details, see **stat**.| +| Type | Description | +| ---------------------------- | ---------- | +| Promise<[Stat](#stat)> | Promise used to return the symbolic link information obtained. For details, see **stat**.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1163,7 +1288,6 @@ Obtains information about a symbolic link. This API uses a promise to return the }); ``` - ## fs.lstat lstat(path: string, callback: AsyncCallback<Stat>): void @@ -1179,6 +1303,10 @@ Obtains information about a symbolic link. This API uses an asynchronous callbac | path | string | Yes | Application sandbox path of the file.| | callback | AsyncCallback<[Stat](#stat)> | Yes | Callback invoked to return the symbolic link information obtained. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -1208,9 +1336,13 @@ Obtains information about a symbolic link synchronously. **Return value** - | Type | Description | - | ------------- | ---------- | - | [Stat](#stat) | File information obtained.| +| Type | Description | +| ------------- | ---------- | +| [Stat](#stat) | File information obtained.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1236,9 +1368,13 @@ Renames a file or folder. This API uses a promise to return the result. **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1268,6 +1404,10 @@ Renames a file or folder. This API uses an asynchronous callback to return the r | newPath | string | Yes | Application sandbox path of the renamed file. | | callback | AsyncCallback<void> | Yes | Callback invoked when the file is asynchronously renamed. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -1297,6 +1437,10 @@ Renames a file or folder synchronously. | oldPath | string | Yes | Application sandbox path of the file to rename.| | newPath | string | Yes | Application sandbox path of the renamed file. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -1305,7 +1449,6 @@ Renames a file or folder synchronously. fs.renameSync(srcFile, dstFile); ``` - ## fs.fsync fsync(fd: number): Promise<void> @@ -1316,15 +1459,19 @@ Flushes data of a file to disk. This API uses a promise to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ------------ | - | fd | number | Yes | FD of the file.| +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ------------ | +| fd | number | Yes | FD of the file.| **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1338,7 +1485,6 @@ Flushes data of a file to disk. This API uses a promise to return the result. }); ``` - ## fs.fsync fsync(fd: number, callback: AsyncCallback<void>): void @@ -1349,10 +1495,14 @@ Flushes data of a file to disk. This API uses an asynchronous callback to return **Parameters** - | Name | Type | Mandatory | Description | - | -------- | ------------------------- | ---- | --------------- | - | fd | number | Yes | FD of the file. | - | Callback | AsyncCallback<void> | Yes | Callback invoked when the file data is synchronized in asynchronous mode.| +| Name | Type | Mandatory | Description | +| -------- | ------------------------- | ---- | --------------- | +| fd | number | Yes | FD of the file. | +| Callback | AsyncCallback<void> | Yes | Callback invoked when the file data is synchronized in asynchronous mode.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1380,9 +1530,13 @@ Flushes data of a file to disk synchronously. **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ------------ | - | fd | number | Yes | FD of the file.| +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ------------ | +| fd | number | Yes | FD of the file.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1393,7 +1547,6 @@ Flushes data of a file to disk synchronously. fs.closeSync(file); ``` - ## fs.fdatasync fdatasync(fd: number): Promise<void> @@ -1404,15 +1557,19 @@ Flushes data of a file to disk. This API uses a promise to return the result. ** **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ------------ | - | fd | number | Yes | FD of the file.| +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ------------ | +| fd | number | Yes | FD of the file.| **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1427,7 +1584,6 @@ Flushes data of a file to disk. This API uses a promise to return the result. ** }); ``` - ## fs.fdatasync fdatasync(fd: number, callback: AsyncCallback<void>): void @@ -1438,10 +1594,14 @@ Flushes data of a file to disk. This API uses an asynchronous callback to return **Parameters** - | Name | Type | Mandatory | Description | - | -------- | ------------------------------- | ---- | ----------------- | - | fd | number | Yes | FD of the file. | - | callback | AsyncCallback<void> | Yes | Callback invoked when the file data is synchronized in asynchronous mode.| +| Name | Type | Mandatory | Description | +| -------- | ------------------------------- | ---- | ----------------- | +| fd | number | Yes | FD of the file. | +| callback | AsyncCallback<void> | Yes | Callback invoked when the file data is synchronized in asynchronous mode.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1468,9 +1628,13 @@ Synchronizes data in a file synchronously. **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ------------ | - | fd | number | Yes | FD of the file.| +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ------------ | +| fd | number | Yes | FD of the file.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1481,7 +1645,6 @@ Synchronizes data in a file synchronously. fs.closeSync(file); ``` - ## fs.symlink symlink(target: string, srcPath: string): Promise<void> @@ -1499,9 +1662,13 @@ Creates a symbolic link based on a file path. This API uses a promise to return **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1531,6 +1698,10 @@ Creates a symbolic link based on a file path. This API uses an asynchronous call | srcPath | string | Yes | Application sandbox path of the symbolic link. | | callback | AsyncCallback<void> | Yes | Callback invoked when the symbolic link is created asynchronously.| +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -1560,6 +1731,10 @@ Synchronously creates a symbolic link based on a file path. | target | string | Yes | Application sandbox path of the source file. | | srcPath | string | Yes | Application sandbox path of the symbolic link.| +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -1581,24 +1756,28 @@ Lists all files in a directory. This API uses a promise to return the result.
- **0**: Throw an exception if the destination directory has folders of the same names with the source folder.
- **1**: Throw an exception if the destination directory has files of the same names with the source folder. All files without conflicts in the source folder are moved to the destination folder, and all files without conflicts in the destination folder are retained. The **data** in the error thrown provides information about the conflict files.
- **2**: Forcibly overwrite the files with the same names in the destination folder. The files with the the same names in the destination folder are overwritten forcibly; the files without conflicts in the destination folder are retained.
- **3**: Forcibly overwrite the destination folder. Move the source folder to the destination directory and make the destination folder completely the same as the source folder. All the original files in the destination folder are not retained.| +| Name | Type | Mandatory | Description | +| ------ | ------ | ---- | --------------------------- | +| src | string | Yes | Application sandbox path of the source folder.| +| dest | string | Yes | Application sandbox path of the destination folder.| +| mode | number | No | Mode for moving the folder. The default value is **0**.
- **0**: Throw an exception if the destination directory has folders of the same names with the source folder.
- **1**: Throw an exception if the destination directory has files of the same names with the source folder. All files without conflicts in the source folder are moved to the destination folder, and all files without conflicts in the destination folder are retained. The **data** in the error thrown provides information about the conflict files.
- **2**: Forcibly overwrite the files with the same names in the destination folder. The files with the the same names in the destination folder are overwritten forcibly; the files without conflicts in the destination folder are retained.
- **3**: Forcibly overwrite the destination folder. Move the source folder to the destination directory and make the destination folder completely the same as the source folder. All the original files in the destination folder are not retained.| **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1776,12 +1967,16 @@ Moves a folder. This API uses an asynchronous callback to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ------ | ------ | ---- | --------------------------- | - | src | string | Yes | Application sandbox path of the source folder.| - | dest | string | Yes | Application sandbox path of the destination folder.| - | mode | number | No | Whether to overwrite the file of the same name in the destination directory. The default value is **0**.
- **0**: Throw an exception if the destination directory has folders of the same names with the source folder.
- **1**: Throw an exception if the destination directory has files of the same names with the source folder. All files without conflicts in the source folder are moved to the destination folder, and all files without conflicts in the destination folder are retained. The **data** in the error thrown provides information about the conflict files.
- **2**: Forcibly overwrite the files with the same names in the destination folder. The files with the the same names in the destination folder are overwritten forcibly; the files without conflicts in the destination folder are retained.
- **3**: Forcibly overwrite the destination folder. Move the source folder to the destination directory and make the destination folder completely the same as the source folder. All the original files in the destination folder are not retained.| - | callback | AsyncCallback<void> | Yes | Callback invoked when the folder is moved. | +| Name | Type | Mandatory | Description | +| ------ | ------ | ---- | --------------------------- | +| src | string | Yes | Application sandbox path of the source folder.| +| dest | string | Yes | Application sandbox path of the destination folder.| +| mode | number | No | Mode for moving the folder. The default value is **0**.
- **0**: Throw an exception if the destination directory has folders of the same names with the source folder.
- **1**: Throw an exception if the destination directory has files of the same names with the source folder. All files without conflicts in the source folder are moved to the destination folder, and all files without conflicts in the destination folder are retained. The **data** in the error thrown provides information about the conflict files.
- **2**: Forcibly overwrite the files with the same names in the destination folder. The files with the the same names in the destination folder are overwritten forcibly; the files without conflicts in the destination folder are retained.
- **3**: Forcibly overwrite the destination folder. Move the source folder to the destination directory and make the destination folder completely the same as the source folder. All the original files in the destination folder are not retained.| +| callback | AsyncCallback<void> | Yes | Callback invoked when the folder is moved. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1813,17 +2008,21 @@ Moves a file. This API uses a promise to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ------ | ------ | ---- | --------------------------- | - | src | string | Yes | Application sandbox path of the source file.| - | dest | string | Yes | Application sandbox path of the destination file.| - | mode | number | No | Whether to overwrite the file of the same name in the destination directory. The value **0** means to overwrite the file of the same name in the destination directory. The value **1** means to throw an exception if a file of the same name exists in the destination directory. The default value is **0**.| +| Name | Type | Mandatory | Description | +| ------ | ------ | ---- | --------------------------- | +| src | string | Yes | Application sandbox path of the source file.| +| dest | string | Yes | Application sandbox path of the destination file.| +| mode | number | No | Whether to overwrite the file of the same name in the destination directory. The value **0** means to overwrite the file of the same name in the destination directory. The value **1** means to throw an exception if a file of the same name exists in the destination directory. The default value is **0**.| **Return value** - | Type | Description | - | ------------------- | ---------------------------- | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ------------------- | ---------------------------- | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1847,12 +2046,16 @@ Moves a file. This API uses an asynchronous callback to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ------ | ------ | ---- | --------------------------- | - | src | string | Yes | Application sandbox path of the source file.| - | dest | string | Yes | Application sandbox path of the destination file.| - | mode | number | No | Whether to overwrite the file of the same name in the destination directory. The value **0** means to overwrite the file of the same name in the destination directory. The value **1** means to throw an exception if a file of the same name exists in the destination directory. The default value is **0**.| - | callback | AsyncCallback<void> | Yes | Callback invoked when the file is moved. | +| Name | Type | Mandatory | Description | +| ------ | ------ | ---- | --------------------------- | +| src | string | Yes | Application sandbox path of the source file.| +| dest | string | Yes | Application sandbox path of the destination file.| +| mode | number | No | Whether to overwrite the file of the same name in the destination directory. The value **0** means to overwrite the file of the same name in the destination directory. The value **1** means to throw an exception if a file of the same name exists in the destination directory. The default value is **0**.| +| callback | AsyncCallback<void> | Yes | Callback invoked when the file is moved. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1878,11 +2081,15 @@ Moves a file synchronously. **Parameters** - | Name | Type | Mandatory | Description | - | ------ | ------ | ---- | --------------------------- | - | src | string | Yes | Application sandbox path of the source file.| - | dest | string | Yes | Application sandbox path of the destination file.| - | mode | number | No | Whether to overwrite the file of the same name in the destination directory. The value **0** means to overwrite the file of the same name in the destination directory. The value **1** means to throw an exception if a file of the same name exists in the destination directory. The default value is **0**.| +| Name | Type | Mandatory | Description | +| ------ | ------ | ---- | --------------------------- | +| src | string | Yes | Application sandbox path of the source file.| +| dest | string | Yes | Application sandbox path of the destination file.| +| mode | number | No | Whether to overwrite the file of the same name in the destination directory. The value **0** means to overwrite the file of the same name in the destination directory. The value **1** means to throw an exception if a file of the same name exists in the destination directory. The default value is **0**.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1903,15 +2110,19 @@ Creates a temporary directory. This API uses a promise to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ------ | ------ | ---- | --------------------------- | - | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| +| Name | Type | Mandatory | Description | +| ------ | ------ | ---- | --------------------------- | +| prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| **Return value** - | Type | Description | - | --------------------- | ---------- | - | Promise<string> | Promise used to return the unique directory generated.| +| Type | Description | +| --------------------- | ---------- | +| Promise<string> | Promise used to return the unique directory generated.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1923,7 +2134,6 @@ Creates a temporary directory. This API uses a promise to return the result. }); ``` - ## fs.mkdtemp mkdtemp(prefix: string, callback: AsyncCallback<string>): void @@ -1934,10 +2144,14 @@ Creates a temporary directory. This API uses an asynchronous callback to return **Parameters** - | Name | Type | Mandatory | Description | - | -------- | --------------------------- | ---- | --------------------------- | - | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| - | callback | AsyncCallback<string> | Yes | Callback invoked when a temporary directory is created asynchronously. | +| Name | Type | Mandatory | Description | +| -------- | --------------------------- | ---- | --------------------------- | +| prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| +| callback | AsyncCallback<string> | Yes | Callback invoked when a temporary directory is created asynchronously. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -1961,21 +2175,25 @@ Synchronously creates a temporary directory. **Parameters** - | Name | Type | Mandatory | Description | - | ------ | ------ | ---- | --------------------------- | - | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| +| Name | Type | Mandatory | Description | +| ------ | ------ | ---- | --------------------------- | +| prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| **Return value** - | Type | Description | - | ------ | ---------- | - | string | Unique path generated.| +| Type | Description | +| ------ | ---------- | +| string | Unique path generated.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** ```js let res = fs.mkdtempSync(pathDir + "/XXXXXX"); - ``` + ``` ## fs.createStream @@ -1994,9 +2212,13 @@ Creates a stream based on the file path. This API uses a promise to return the r **Return value** - | Type | Description | - | --------------------------------- | --------- | - | Promise<[Stream](#stream)> | Promise used to return the result.| +| Type | Description | +| --------------------------------- | --------- | +| Promise<[Stream](#stream)> | Promise used to return the result.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2026,6 +2248,10 @@ Creates a stream based on the file path. This API uses an asynchronous callback | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| | callback | AsyncCallback<[Stream](#stream)> | Yes | Callback invoked when the stream is created asynchronously. | +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -2056,9 +2282,13 @@ Synchronously creates a stream based on the file path. **Return value** - | Type | Description | - | ------------------ | --------- | - | [Stream](#stream) | Stream opened.| +| Type | Description | +| ------------------ | --------- | +| [Stream](#stream) | Stream opened.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2072,22 +2302,26 @@ Synchronously creates a stream based on the file path. fdopenStream(fd: number, mode: string): Promise<Stream> -Opens a stream based on the file descriptor. This API uses a promise to return the result. +Opens a stream based on the FD. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ---------------------------------------- | - | fd | number | Yes | FD of the file. | - | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ---------------------------------------- | +| fd | number | Yes | FD of the file. | +| mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| **Return value** - | Type | Description | - | --------------------------------- | --------- | - | Promise<[Stream](#stream)> | Promise used to return the result.| +| Type | Description | +| --------------------------------- | --------- | +| Promise<[Stream](#stream)> | Promise used to return the result.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2102,22 +2336,25 @@ Opens a stream based on the file descriptor. This API uses a promise to return t }); ``` - ## fs.fdopenStream fdopenStream(fd: number, mode: string, callback: AsyncCallback<Stream>): void -Opens a stream based on the file descriptor. This API uses an asynchronous callback to return the result. +Opens a stream based on the FD. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** - | Name | Type | Mandatory | Description | - | -------- | ---------------------------------------- | ---- | ---------------------------------------- | - | fd | number | Yes | FD of the file. | - | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| - | callback | AsyncCallback<[Stream](#stream)> | Yes | Callback invoked when the stream is open asynchronously. | +| Name | Type | Mandatory | Description | +| -------- | ---------------------------------------- | ---- | ---------------------------------------- | +| fd | number | Yes | FD of the file. | +| mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| +| callback | AsyncCallback<[Stream](#stream)> | Yes | Callback invoked when the stream is open asynchronously. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2138,22 +2375,26 @@ Opens a stream based on the file descriptor. This API uses an asynchronous callb fdopenStreamSync(fd: number, mode: string): Stream -Synchronously opens a stream based on the file descriptor. +Synchronously opens a stream based on the FD. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ---------------------------------------- | - | fd | number | Yes | FD of the file. | - | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ---------------------------------------- | +| fd | number | Yes | FD of the file. | +| mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| **Return value** - | Type | Description | - | ------------------ | --------- | - | [Stream](#stream) | Stream opened.| +| Type | Description | +| ------------------ | --------- | +| [Stream](#stream) | Stream opened.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2176,17 +2417,21 @@ Creates a **Watcher** object to observe file or directory changes. **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ---------------------------------------- | - | path | string | Yes | Application sandbox path of the file or directory to observe. | - | events | number | Yes | Events to observe. Multiple events can be separated by a bitwise OR operator (|)|.
- **0x1: IN_ACCESS**: A file is accessed.
- **0x2: IN_MODIFY**: The file content is modified.
- **0x4: IN_ATTRIB**: Metadata is changed.
- **0x8: IN_CLOSE_WRITE**: The file opened for writing is closed.
- **0x10: IN_CLOSE_NOWRITE**: The file or directory not opened for writing is closed.
- **0x20: IN_OPEN**: A file or directory is opened.
- **0x40: IN_MOVED_FROM**: A file in the observed directory is moved.
- **0x80: IN_MOVED_TO**: A file is moved to the observed directory.
- **0x100: IN_CREATE**: A file or directory is created in the observed directory.
- **0x200: IN_DELETE**: A file or directory is deleted from the observed directory.
- **0x400: IN_DELETE_SELF**: The observed directory is deleted. After the directory is deleted, the listening stops.
- **0x800: IN_MOVE_SELF**: The observed file or directory is moved. After the file or directory is moved, the listening continues.
- **0xfff: IN_ALL_EVENTS**: All events.| - | listener | WatchEventListener | Yes | Callback invoked when an observed event occurs. The callback will be invoked each time an observed event occurs. | +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ---------------------------------------- | +| path | string | Yes | Application sandbox path of the file or directory to observe. | +| events | number | Yes | Events to observe. Multiple events can be separated by a bitwise OR operator (|)|.
- **0x1: IN_ACCESS**: A file is accessed.
- **0x2: IN_MODIFY**: The file content is modified.
- **0x4: IN_ATTRIB**: Metadata is changed.
- **0x8: IN_CLOSE_WRITE**: The file opened for writing is closed.
- **0x10: IN_CLOSE_NOWRITE**: The file or directory not opened for writing is closed.
- **0x20: IN_OPEN**: A file or directory is opened.
- **0x40: IN_MOVED_FROM**: A file in the observed directory is moved.
- **0x80: IN_MOVED_TO**: A file is moved to the observed directory.
- **0x100: IN_CREATE**: A file or directory is created in the observed directory.
- **0x200: IN_DELETE**: A file or directory is deleted from the observed directory.
- **0x400: IN_DELETE_SELF**: The observed directory is deleted. After the directory is deleted, the listening stops.
- **0x800: IN_MOVE_SELF**: The observed file or directory is moved. After the file or directory is moved, the listening continues.
- **0xfff: IN_ALL_EVENTS**: All events.| +| listener | WatchEventListener | Yes | Callback invoked when an observed event occurs. The callback will be invoked each time an observed event occurs. | **Return value** - | Type | Description | - | ------------------ | --------- | - | [Watcher](#watcher10) | **Watcher** object created.| +| Type | Description | +| ------------------ | --------- | +| [Watcher](#watcher10) | **Watcher** object created.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2218,10 +2463,10 @@ Called when an observed event occurs. **Parameters** - | Name | Type | Mandatory | Description | - | ---- | ------ | ---- | ---------------------------------------- | - | event | WatchEvent | Yes | Event for the callback to invoke. | - +| Name | Type | Mandatory | Description | +| ---- | ------ | ---- | ---------------------------------------- | +| event | WatchEvent | Yes | Event for the callback to invoke. | + ## WatchEvent10+ Defines the event to observe. @@ -2245,7 +2490,7 @@ Represents detailed file information. Before calling any API of the **Stat()** c ### Attributes | Name | Type | Readable | Writable | Description | -| ------ | ------ | ---- | ---- | ---------------------------------------- | +| ------ | ------ | ---- | ---- | ---------------------------------------- | | ino | number | Yes | No | File ID. Different files on the same device have different **ino**s.| | | mode | number | Yes | No | File permissions. The meaning of each bit is as follows:
- **0o400**: The owner has the read permission on a regular file or a directory entry.
- **0o200**: The owner has the permission to write a regular file or create and delete a directory entry.
- **0o100**: The owner has the permission to execute a regular file or search for the specified path in a directory.
- **0o040**: The user group has the read permission on a regular file or a directory entry.
- **0o020**: The user group has the permission to write a regular file or create and delete a directory entry.
- **0o010**: The user group has the permission to execute a regular file or search for the specified path in a directory.
- **0o004**: Other users have the permission to read a regular file or read a directory entry.
- **0o002**: Other users have the permission to write a regular file or create and delete a directory entry.
- **0o001**: Other users have the permission to execute a regular file or search for the specified path in a directory.| | uid | number | Yes | No | ID of the file owner.| @@ -2255,7 +2500,6 @@ Represents detailed file information. Before calling any API of the **Stat()** c | mtime | number | Yes | No | Time of the last modification to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970. | | ctime | number | Yes | No | Time of the last status change of the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970. | - ### isBlockDevice isBlockDevice(): boolean @@ -2266,9 +2510,13 @@ Checks whether this file is a block special file. A block special file supports **Return value** - | Type | Description | - | ------- | ---------------- | - | boolean | Whether the file is a block special file.| +| Type | Description | +| ------- | ---------------- | +| boolean | Whether the file is a block special file.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2287,9 +2535,13 @@ Checks whether this file is a character special file. A character special file s **Return value** - | Type | Description | - | ------- | ----------------- | - | boolean | Whether the file is a character special file.| +| Type | Description | +| ------- | ----------------- | +| boolean | Whether the file is a character special file.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2298,7 +2550,6 @@ Checks whether this file is a character special file. A character special file s let isCharacterDevice = fs.statSync(filePath).isCharacterDevice(); ``` - ### isDirectory isDirectory(): boolean @@ -2309,9 +2560,13 @@ Checks whether this file is a directory. **Return value** - | Type | Description | - | ------- | ------------- | - | boolean | Whether the file is a directory.| +| Type | Description | +| ------- | ------------- | +| boolean | Whether the file is a directory.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2320,7 +2575,6 @@ Checks whether this file is a directory. let isDirectory = fs.statSync(dirPath).isDirectory(); ``` - ### isFIFO isFIFO(): boolean @@ -2331,9 +2585,13 @@ Checks whether this file is a named pipe (or FIFO). Named pipes are used for int **Return value** - | Type | Description | - | ------- | --------------------- | - | boolean | Whether the file is a FIFO.| +| Type | Description | +| ------- | --------------------- | +| boolean | Whether the file is a FIFO.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2342,7 +2600,6 @@ Checks whether this file is a named pipe (or FIFO). Named pipes are used for int let isFIFO = fs.statSync(filePath).isFIFO(); ``` - ### isFile isFile(): boolean @@ -2353,9 +2610,13 @@ Checks whether this file is a regular file. **Return value** - | Type | Description | - | ------- | --------------- | - | boolean | Whether the file is a regular file.| +| Type | Description | +| ------- | --------------- | +| boolean | Whether the file is a regular file.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2364,7 +2625,6 @@ Checks whether this file is a regular file. let isFile = fs.statSync(filePath).isFile(); ``` - ### isSocket isSocket(): boolean @@ -2375,9 +2635,13 @@ Checks whether this file is a socket. **Return value** - | Type | Description | - | ------- | -------------- | - | boolean | Whether the file is a socket.| +| Type | Description | +| ------- | -------------- | +| boolean | Whether the file is a socket.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2386,7 +2650,6 @@ Checks whether this file is a socket. let isSocket = fs.statSync(filePath).isSocket(); ``` - ### isSymbolicLink isSymbolicLink(): boolean @@ -2397,9 +2660,13 @@ Checks whether this file is a symbolic link. **Return value** - | Type | Description | - | ------- | --------------- | - | boolean | Whether the file is a symbolic link.| +| Type | Description | +| ------- | --------------- | +| boolean | Whether the file is a symbolic link.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2412,7 +2679,6 @@ Checks whether this file is a symbolic link. Provides a stream for file operations. Before calling any API of the **Stream** class, use **createStream()** to create a **Stream** instance synchronously or asynchronously. - ### close close(): Promise<void> @@ -2423,9 +2689,13 @@ Closes the stream. This API uses a promise to return the result. **Return value** - | Type | Description | - | ------------------- | ------------- | - | Promise<void> | Promise used to return the stream close result.| +| Type | Description | +| ------------------- | ------------- | +| Promise<void> | Promise used to return the stream close result.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2439,7 +2709,6 @@ Closes the stream. This API uses a promise to return the result. }); ``` - ### close close(callback: AsyncCallback<void>): void @@ -2450,9 +2719,13 @@ Closes the stream. This API uses an asynchronous callback to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | -------- | ------------------------- | ---- | ------------- | - | callback | AsyncCallback<void> | Yes | Callback invoked when the stream is closed asynchronously.| +| Name | Type | Mandatory | Description | +| -------- | ------------------------- | ---- | ------------- | +| callback | AsyncCallback<void> | Yes | Callback invoked when the stream is closed asynchronously.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2476,6 +2749,10 @@ Synchronously closes the stream. **System capability**: SystemCapability.FileManagement.File.FileIO +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -2494,9 +2771,13 @@ Flushes the stream. This API uses a promise to return the result. **Return value** - | Type | Description | - | ------------------- | ------------- | - | Promise<void> | Promise used to return the stream flushing result.| +| Type | Description | +| ------------------- | ------------- | +| Promise<void> | Promise used to return the stream flushing result.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2510,7 +2791,6 @@ Flushes the stream. This API uses a promise to return the result. }); ``` - ### flush flush(callback: AsyncCallback<void>): void @@ -2521,9 +2801,13 @@ Flushes the stream. This API uses an asynchronous callback to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | -------- | ------------------------- | ---- | -------------- | - | callback | AsyncCallback<void> | Yes | Callback invoked when the stream is asynchronously flushed.| +| Name | Type | Mandatory | Description | +| -------- | ------------------------- | ---- | -------------- | +| callback | AsyncCallback<void> | Yes | Callback invoked when the stream is asynchronously flushed.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2547,6 +2831,10 @@ Synchronously flushes the stream. **System capability**: SystemCapability.FileManagement.File.FileIO +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -2565,16 +2853,20 @@ Writes data into the stream. This API uses a promise to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ------------------------------- | ---- | ---------------------------------------- | - | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | - | options | Object | No | The options are as follows:
- **length** (number): length of the data to write. The default value is the buffer length.
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| +| Name | Type | Mandatory | Description | +| ------- | ------------------------------- | ---- | ---------------------------------------- | +| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | +| options | Object | No | The options are as follows:
- **length** (number): length of the data to write. The default value is the buffer length.
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| **Return value** - | Type | Description | - | --------------------- | -------- | - | Promise<number> | Promise used to return the length of the data written.| +| Type | Description | +| --------------------- | -------- | +| Promise<number> | Promise used to return the length of the data written.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2588,7 +2880,6 @@ Writes data into the stream. This API uses a promise to return the result. }); ``` - ### write write(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }, callback: AsyncCallback<number>): void @@ -2599,11 +2890,15 @@ Writes data into the stream. This API uses an asynchronous callback to return th **Parameters** - | Name | Type | Mandatory| Description | - | -------- | ------------------------------- | ---- | ------------------------------------------------------------ | - | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | - | options | Object | No | The options are as follows:
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| - | callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | +| Name | Type | Mandatory| Description | +| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | +| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | +| options | Object | No | The options are as follows:
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| +| callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2631,16 +2926,20 @@ Synchronously writes data into the stream. **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ------------------------------- | ---- | ---------------------------------------- | - | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | - | options | Object | No | The options are as follows:
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| +| Name | Type | Mandatory | Description | +| ------- | ------------------------------- | ---- | ---------------------------------------- | +| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | +| options | Object | No | The options are as follows:
- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.
- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.
- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| **Return value** - | Type | Description | - | ------ | -------- | - | number | Length of the data written in the file.| +| Type | Description | +| ------ | -------- | +| number | Length of the data written in the file.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2660,16 +2959,20 @@ Reads data from the stream. This API uses a promise to return the result. **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ----------- | ---- | ---------------------------------------- | - | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | - | options | Object | No | The options are as follows:
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.
- **offset** (number): position of the data to read in the file. By default, data is read from the current position.| +| Name | Type | Mandatory | Description | +| ------- | ----------- | ---- | ---------------------------------------- | +| buffer | ArrayBuffer | Yes | Buffer used to store the file read. | +| options | Object | No | The options are as follows:
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.
- **offset** (number): position of the data to read in the file. By default, data is read from the current position.| **Return value** - | Type | Description | - | ---------------------------------- | ------ | - | Promise<number> | Promise used to return the data read.| +| Type | Description | +| ---------------------------------- | ------ | +| Promise<number> | Promise used to return the data read.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2685,7 +2988,6 @@ Reads data from the stream. This API uses a promise to return the result. }); ``` - ### read read(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }, callback: AsyncCallback<number>): void @@ -2696,11 +2998,15 @@ Reads data from the stream. This API uses an asynchronous callback to return the **Parameters** - | Name | Type | Mandatory | Description | - | -------- | ---------------------------------------- | ---- | ---------------------------------------- | - | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | - | options | Object | No | The options are as follows:
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.
- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.| - | callback | AsyncCallback<number> | Yes | Callback invoked when data is read asynchronously from the stream. | +| Name | Type | Mandatory | Description | +| -------- | ---------------------------------------- | ---- | ---------------------------------------- | +| buffer | ArrayBuffer | Yes | Buffer used to store the file read. | +| options | Object | No | The options are as follows:
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.
- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.| +| callback | AsyncCallback<number> | Yes | Callback invoked when data is read asynchronously from the stream. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2728,16 +3034,20 @@ Synchronously reads data from the stream. **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ----------- | ---- | ---------------------------------------- | - | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | - | options | Object | No | The options are as follows:
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.
- **offset** (number): position of the data to read in the file. By default, data is read from the current position.
| +| Name | Type | Mandatory | Description | +| ------- | ----------- | ---- | ---------------------------------------- | +| buffer | ArrayBuffer | Yes | Buffer used to store the file read. | +| options | Object | No | The options are as follows:
- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.
- **offset** (number): position of the data to read in the file. By default, data is read from the current position.
| **Return value** - | Type | Description | - | ------ | -------- | - | number | Length of the data read.| +| Type | Description | +| ------ | -------- | +| number | Length of the data read.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2769,15 +3079,19 @@ Applies an exclusive lock or a shared lock on this file in blocking mode. This A **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ----------- | ---- | ---------------------------------------- | - | exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | +| Name | Type | Mandatory | Description | +| ------- | ----------- | ---- | ---------------------------------------- | +| exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | **Return value** - | Type | Description | - | ---------------------------------- | ------ | - | Promise<void> | Promise that returns no value.| +| Type | Description | +| ---------------------------------- | ------ | +| Promise<void> | Promise that returns no value.| + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2800,10 +3114,14 @@ Applies an exclusive lock or a shared lock on this file in blocking mode. This A **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ----------- | ---- | ---------------------------------------- | - | exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | - | callback | AsyncCallback<void> | Yes | Callback invoked when the file is locked. | +| Name | Type | Mandatory | Description | +| ------- | ----------- | ---- | ---------------------------------------- | +| exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | +| callback | AsyncCallback<void> | Yes | Callback invoked when the file is locked. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2828,9 +3146,13 @@ Applies an exclusive lock or a shared lock on this file in non-blocking mode. **Parameters** - | Name | Type | Mandatory | Description | - | ------- | ----------- | ---- | ---------------------------------------- | - | exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | +| Name | Type | Mandatory | Description | +| ------- | ----------- | ---- | ---------------------------------------- | +| exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | + +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). **Example** @@ -2848,6 +3170,10 @@ Unlocks this file synchronously. **System capability**: SystemCapability.FileManagement.File.FileIO +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -2871,6 +3197,10 @@ Starts listening. **System capability**: SystemCapability.FileManagement.File.FileIO +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js @@ -2890,6 +3220,10 @@ Stops listening. **System capability**: SystemCapability.FileManagement.File.FileIO +**Error codes** + +For details about error codes, see "Basic File I/O Error Codes" in [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). + **Example** ```js -- GitLab