未验证 提交 031eae1d 编写于 作者: O openharmony_ci 提交者: Gitee

!17468 [翻译完成】#I6T7HG

Merge pull request !17468 from Annie_wang/PR16877
# @ohos.file.fileUri (File URI)
# @ohos.file.fileuri (File URI)
The **fileUri** module allows the uniform resource identifier (URI) of a file to be obtained based on the file path. With the file URI, you can use the APIs provided by [@ohos.file.fs](js-apis-file-fs.md) to operate the file.
......@@ -9,7 +9,7 @@ The **fileUri** module allows the uniform resource identifier (URI) of a file to
## Modules to Import
```js
import fileUri from "@ohos.file.fileUri";
import fileuri from "@ohos.file.fileuri";
```
Before using this module, you need to obtain the path of the file in the application sandbox. The following is an example:
......@@ -57,5 +57,5 @@ For details about the error codes, see [File Management Error Codes](../errorcod
```js
let filePath = pathDir + "test.txt";
let uri = fileUri.getUriFromPath(filePath);
let uri = fileuri.getUriFromPath(filePath);
```
# @ohos.file.fs (File Management)
The **fs** module provides APIs for file operations, including basic file management, directory management, file information statistics, and stream read and write.
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**
>
> - 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.
>
> - The APIs of this module support processing of error codes. For details, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
> **NOTE**<br>
> 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
......@@ -14,9 +11,13 @@ 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 files or folders, obtain the path of the file or directory in the application sandbox as follows:
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:
**Stage Model**
......@@ -56,7 +57,7 @@ Obtains detailed file information. This API uses a promise to return the result.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------- |
| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file.|
| file | string\|number | Yes | Application sandbox path or file descriptor (FD) of the file.|
**Return value**
......@@ -87,7 +88,7 @@ Obtains detailed file information. This API uses an asynchronous callback to ret
| Name | Type | Mandatory| Description |
| -------- | ---------------------------------- | ---- | ------------------------------ |
| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file. |
| file | string\|number | Yes | Application sandbox path or FD of the file. |
| callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes | Callback invoked to return the file information obtained.|
**Example**
......@@ -114,7 +115,7 @@ Obtains detailed file information synchronously.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------- |
| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file.|
| file | string\|number | Yes | Application sandbox path or FD of the file.|
**Return value**
......@@ -142,13 +143,13 @@ Checks whether a file exists. This API uses a promise to return the result.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the file in the application sandbox. |
| path | string | Yes | Application sandbox path of the file. |
**Return value**
| Type | Description |
| ------------------- | ---------------------------- |
| Promise&lt;boolean&gt; | Promise used to return a Boolean value.|
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** means the file exists; the value **false** means the opposite.|
**Example**
......@@ -176,7 +177,7 @@ Checks whether a file exists. This API uses an asynchronous callback to return t
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the file in the application sandbox. |
| path | string | Yes | Application sandbox path of the file. |
| callback | AsyncCallback&lt;boolean&gt; | Yes | Callback invoked to return the result. |
**Example**
......@@ -206,7 +207,13 @@ Synchronously checks whether a file exists.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the file in the application sandbox. |
| path | string | Yes | Application sandbox path of the file. |
**Return value**
| Type | Description |
| ------------------- | ---------------------------- |
| boolean | Returns **true** if the file exists; returns **false** otherwise.|
**Example**
......@@ -410,7 +417,7 @@ Creates a directory. This API uses a promise to return the result.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the directory in the application sandbox. |
| path | string | Yes | Application sandbox path of the directory. |
**Return value**
......@@ -442,7 +449,7 @@ Creates a directory. This API uses an asynchronous callback to return the result
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the directory in the application sandbox. |
| path | string | Yes | Application sandbox path of the directory. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the directory is created asynchronously. |
**Example**
......@@ -471,7 +478,7 @@ Synchronously creates a directory.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the directory in the application sandbox. |
| path | string | Yes | Application sandbox path of the directory. |
**Example**
......@@ -493,7 +500,7 @@ Opens a file. This API uses a promise to return the result. File uniform resourc
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the file in the application sandbox or URI of the 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.<br>- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.<br>- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (&#124;). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **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.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.|
**Return value**
......@@ -526,7 +533,7 @@ Opens a file. This API uses an asynchronous callback to return the result. File
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the file in the application sandbox or URI of the 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.<br>- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.<br>- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (&#124;). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **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.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.|
**Example**
......@@ -554,7 +561,7 @@ Synchronously opens a file. File URIs are supported.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the file in the application sandbox or URI of the 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.<br>- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.<br>- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (&#124;). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **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.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.|
**Return value**
......@@ -689,7 +696,7 @@ Deletes a directory. This API uses a promise to return the result.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------- |
| path | string | Yes | Path of the directory in the application sandbox.|
| path | string | Yes | Application sandbox path of the directory.|
**Return value**
......@@ -721,7 +728,7 @@ Deletes a directory. This API uses an asynchronous callback to return the result
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | -------------------------- |
| path | string | Yes | Path of the directory in the application sandbox.|
| path | string | Yes | Application sandbox path of the directory.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the directory is deleted asynchronously. |
**Example**
......@@ -750,7 +757,7 @@ Synchronously deletes a directory.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------- |
| path | string | Yes | Path of the directory in the application sandbox.|
| path | string | Yes | Application sandbox path of the directory.|
**Example**
......@@ -772,7 +779,7 @@ Deletes a file. This API uses a promise to return the result.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------- |
| path | string | Yes | Path of the file in the application sandbox.|
| path | string | Yes | Application sandbox path of the file.|
**Return value**
......@@ -804,7 +811,7 @@ Deletes a file. This API uses an asynchronous callback to return the result.
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | -------------------------- |
| path | string | Yes | Path of the file in the application sandbox.|
| path | string | Yes | Application sandbox path of the file.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the file is deleted asynchronously. |
**Example**
......@@ -833,7 +840,7 @@ Synchronously deletes a file.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------- |
| path | string | Yes | Path of the file in the application sandbox.|
| path | string | Yes | Application sandbox path of the file.|
**Example**
......@@ -956,8 +963,8 @@ Truncates a file. This API uses a promise to return the result.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------------- |
| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file. |
| len | number | No | File length, in bytes, after truncation.|
| 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**.|
**Return value**
......@@ -990,8 +997,8 @@ Truncates a file. This API uses an asynchronous callback to return the result.
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | -------------------------------- |
| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file. |
| len | number | No | File length, in bytes, after truncation.|
| 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**.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. |
**Example**
......@@ -1021,8 +1028,8 @@ Synchronously truncates a file.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------------- |
| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file. |
| len | number | No | File length, in bytes, after truncation.|
| 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**.|
**Example**
......@@ -1045,7 +1052,7 @@ Reads the text content of a file. This API uses a promise to return the result.
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filePath | string | Yes | Path of the file in the application sandbox. |
| filePath | string | Yes | Application sandbox path of the file. |
| options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.<br>- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.|
**Return value**
......@@ -1078,7 +1085,7 @@ Reads the text content of a file. This API uses an asynchronous callback to retu
| Name | Type | Mandatory| Description |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| filePath | string | Yes | Path of the file in the application sandbox. |
| filePath | string | Yes | Application sandbox path of the file. |
| options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.<br>- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.|
| callback | AsyncCallback&lt;string&gt; | Yes | Callback invoked to return the content read. |
......@@ -1108,7 +1115,7 @@ Synchronously reads the text of a file.
| Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filePath | string | Yes | Path of the file in the application sandbox. |
| filePath | string | Yes | Application sandbox path of the file. |
| options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.<br>- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.|
**Return value**
......@@ -1137,7 +1144,7 @@ Obtains information about a symbolic link. This API uses a promise to return the
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------------------- |
| path | string | Yes | Path of the symbolic link in the application sandbox.|
| path | string | Yes | Application sandbox path of the file.|
**Return value**
......@@ -1169,7 +1176,7 @@ Obtains information about a symbolic link. This API uses an asynchronous callbac
| Name | Type | Mandatory| Description |
| -------- | ---------------------------------- | ---- | -------------------------------------- |
| path | string | Yes | Path of the symbolic link in the application sandbox.|
| path | string | Yes | Application sandbox path of the file.|
| callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes | Callback invoked to return the symbolic link information obtained. |
**Example**
......@@ -1197,7 +1204,7 @@ Obtains information about a symbolic link synchronously.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------------------- |
| path | string | Yes | Path of the file in the application sandbox.|
| path | string | Yes | Application sandbox path of the file.|
**Return value**
......@@ -1224,8 +1231,8 @@ Renames a file or folder. This API uses a promise to return the result.
| Name | Type | Mandatory| Description |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes | Path of the file to rename in the application sandbox.|
| newPath | string | Yes | Path of the renamed file in the application sandbox. |
| oldPath | string | Yes | Application sandbox path of the file to rename.|
| newPath | string | Yes | Application sandbox path of the renamed file. |
**Return value**
......@@ -1257,8 +1264,8 @@ Renames a file or folder. This API uses an asynchronous callback to return the r
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath | string | Yes | Path of the file to rename in the application sandbox.|
| newPath | string | Yes | Path of the renamed file in the application sandbox. |
| oldPath | string | Yes | Application sandbox path of the file to rename.|
| newPath | string | Yes | Application sandbox path of the renamed file. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the file is asynchronously renamed. |
**Example**
......@@ -1287,8 +1294,8 @@ Renames a file or folder synchronously.
| Name | Type | Mandatory| Description |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes | Path of the file to rename in the application sandbox.|
| newPath | string | Yes | Path of the renamed file in the application sandbox. |
| oldPath | string | Yes | Application sandbox path of the file to rename.|
| newPath | string | Yes | Application sandbox path of the renamed file. |
**Example**
......@@ -1487,8 +1494,8 @@ Creates a symbolic link based on a file path. This API uses a promise to return
| Name | Type | Mandatory| Description |
| ------- | ------ | ---- | ---------------------------- |
| target | string | Yes | Path of the source file in the application sandbox. |
| srcPath | string | Yes | Path of the symbolic link in the application sandbox.|
| target | string | Yes | Application sandbox path of the source file. |
| srcPath | string | Yes | Application sandbox path of the symbolic link.|
**Return value**
......@@ -1520,8 +1527,8 @@ Creates a symbolic link based on a file path. This API uses an asynchronous call
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | -------------------------------- |
| target | string | Yes | Path of the source file in the application sandbox. |
| srcPath | string | Yes | Path of the symbolic link in the application sandbox. |
| target | string | Yes | Application sandbox path of the source file. |
| srcPath | string | Yes | Application sandbox path of the symbolic link. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the symbolic link is created asynchronously.|
**Example**
......@@ -1550,8 +1557,8 @@ Synchronously creates a symbolic link based on a file path.
| Name | Type | Mandatory| Description |
| ------- | ------ | ---- | ---------------------------- |
| target | string | Yes | Path of the source file in the application sandbox. |
| srcPath | string | Yes | Path of the symbolic link in the application sandbox.|
| target | string | Yes | Application sandbox path of the source file. |
| srcPath | string | Yes | Application sandbox path of the symbolic link.|
**Example**
......@@ -1566,9 +1573,9 @@ listFile(path: string, options?: {
recursion?: boolean;
listNum?: number;
filter?: Filter;
}): Promise<string[]>;
}): Promise<string[]>
Lists all files in a folder. This API uses a promise to return the result.<br>This API supports recursive listing of all files (including files in subfolders) and file filtering.
Lists all files in a directory. This API uses a promise to return the result.<br>This API supports recursive listing of all files (including files in subdirectories) and file filtering.
**System capability**: SystemCapability.FileManagement.File.FileIO
......@@ -1576,14 +1583,14 @@ Lists all files in a folder. This API uses a promise to return the result.<br>Th
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| path | string | Yes | Path of the folder in the application sandbox.|
| options | Object | No | File filtering options.|
| path | string | Yes | Application sandbox path of the folder.|
| options | Object | No | File filtering options. The files are not filtered by default.|
**options parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| recursion | boolean | No | Whether to list all files in subfolders recursively. The default value is **false**.|
| recursion | boolean | No | Whether to list all files in subdirectories recursively. The default value is **false**.|
| listNum | number | No | Number of file names to list. The default value **0** means to list all files.|
| filter | [Filter](#filter) | No | File filtering options. Currently, only the match by file name extension, fuzzy search by file name, and filter by file size or latest modification time are supported.|
......@@ -1621,23 +1628,23 @@ listFile(path: string, options?: {
recursion?: boolean;
listNum?: number;
filter?: Filter;
}, callback: AsyncCallback<string[]>): void;
}, callback: AsyncCallback<string[]>): void
Lists all files in a folder. This API uses an asynchronous callback to return the result.<br>This API supports recursive listing of all files (including files in subfolders) and file filtering.
Lists all files in a directory. This API uses an asynchronous callback to return the result.<br>This API supports recursive listing of all files (including files in subdirectories) and file filtering.
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| path | string | Yes | Path of the folder in the application sandbox.|
| options | Object | No | File filtering options.|
| path | string | Yes | Application sandbox path of the folder.|
| options | Object | No | File filtering options. The files are not filtered by default.|
| callback | AsyncCallback&lt;string[]&gt; | Yes | Callback invoked to return the file names listed. |
**options parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| recursion | boolean | No | Whether to list all files in subfolders recursively. The default value is **false**.|
| recursion | boolean | No | Whether to list all files in subdirectories recursively. The default value is **false**.|
| listNum | number | No | Number of file names to list. The default value **0** means to list all files.|
| filter | [Filter](#filter) | No | File filtering options. Currently, only the match by file name extension, fuzzy search by file name, and filter by file size or latest modification time are supported.|
......@@ -1672,22 +1679,22 @@ listFileSync(path: string, options?: {
recursion?: boolean;
listNum?: number;
filter?: Filter;
}): string[];
}): string[]
Lists all files in a folder synchronously. This API supports recursive listing of all files (including files in subfolders) and file filtering.
Lists all files in a directory synchronously. This API supports recursive listing of all files (including files in subdirectories) and file filtering.
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| path | string | Yes | Path of the folder in the application sandbox.|
| options | Object | No | File filtering options.|
| path | string | Yes | Application sandbox path of the folder.|
| options | Object | No | File filtering options. The files are not filtered by default.|
**options parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| recursion | boolean | No | Whether to list all files in subfolders recursively. The default value is **false**.|
| recursion | boolean | No | Whether to list all files in subdirectories recursively. The default value is **false**.|
| listNum | number | No | Number of file names to list. The default value **0** means to list all files.|
| filter | [Filter](#filter) | No | File filtering options. Currently, only the match by file name extension, fuzzy search by file name, and filter by file size or latest modification time are supported.|
......@@ -1716,9 +1723,89 @@ Lists all files in a folder synchronously. This API supports recursive listing o
console.info("filename: %s", filenames[i]);
}
```
## fs.moveDir<sup>10+</sup>
moveDir(src: string, dest: string, mode?: number): Promise\<void>
Moves a folder. This API uses a promise to return the result.
**System capability**: SystemCapability.FileManagement.File.FileIO
**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 | Mode for moving the folder. The default value is **0**.<br>- **0**: Throw an exception if the destination directory has folders of the same names with the source folder.<br>- **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.<br>- **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.<br>- **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&lt;void&gt; | Promise that returns no value.|
**Example**
```js
// move directory from srcPath to destPath/srcPath
let srcPath = pathDir + "/srcDir/";
let destPath = pathDir + "/destDir/";
fs.moveDir(srcPath, destPath, 1).then(() => {
console.info("move directory succeed");
}).catch((err) => {
if (err.code == 13900015) {
for (let i = 0; i < err.data.length; i++) {
console.info("move directory failed with conflicting files: " + err.data[i].srcFile +
" " + err.data[i].destFile);
}
} else {
console.info("move directory failed with error message: " + err.message + ", error code: " + err.code);
}
});
```
## fs.moveDir<sup>10+</sup>
moveDir(src: string, dest: string, mode?: number, callback: AsyncCallback\<void>): void
Moves a folder. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.FileManagement.File.FileIO
**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**.<br>- **0**: Throw an exception if the destination directory has folders of the same names with the source folder.<br>- **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.<br>- **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.<br>- **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&lt;void&gt; | Yes | Callback invoked when the folder is moved. |
**Example**
```js
// move directory from srcPath to destPath/srcPath
let srcPath = pathDir + "/srcDir/";
let destPath = pathDir + "/destDir/";
fs.moveDir(srcPath, destPath, 1, (err) => {
if (err && err.code == 13900015) {
for (let i = 0; i < err.data.length; i++) {
console.info("move directory failed with conflicting files: " + err.data[i].srcFile +
" " + err.data[i].destFile);
}
} else if (err) {
console.info("move directory failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("move directory succeed");
}
});
```
## fs.moveFile
moveFile(src: string, dest: string, mode?: number): Promise\<void>;
moveFile(src: string, dest: string, mode?: number): Promise\<void>
Moves a file. This API uses a promise to return the result.
......@@ -1728,10 +1815,16 @@ Moves a file. This API uses a promise to return the result.
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| src | string | Yes | Path of the file to move in the application sandbox.|
| dest | string | Yes | Destination path of the file in the application sandbox.|
| 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&lt;void&gt; | Promise that returns no value.|
**Example**
```js
......@@ -1746,7 +1839,7 @@ Moves a file. This API uses a promise to return the result.
## fs.moveFile
moveFile(src: string, dest: string, mode?: number, callback: AsyncCallback\<void>): void;
moveFile(src: string, dest: string, mode?: number, callback: AsyncCallback\<void>): void
Moves a file. This API uses an asynchronous callback to return the result.
......@@ -1756,8 +1849,8 @@ Moves a file. This API uses an asynchronous callback to return the result.
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| src | string | Yes | Path of the file to move in the application sandbox.|
| dest | string | Yes | Destination path of the file in the application sandbox.|
| 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&lt;void&gt; | Yes | Callback invoked when the file is moved. |
......@@ -1777,7 +1870,7 @@ Moves a file. This API uses an asynchronous callback to return the result.
## fs.moveFileSync
moveFile(src: string, dest: string, mode?: number): void;
moveFile(src: string, dest: string, mode?: number): void
Moves a file synchronously.
......@@ -1787,8 +1880,8 @@ Moves a file synchronously.
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| src | string | Yes | Path of the file to move in the application sandbox.|
| dest | string | Yes | Destination path of the file in the application sandbox.|
| 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**.|
**Example**
......@@ -1888,7 +1981,7 @@ Synchronously creates a temporary directory.
createStream(path: string, mode: string): Promise&lt;Stream&gt;
Opens a file stream based on the file path. This API uses a promise to return the result.
Creates a stream based on the file path. This API uses a promise to return the result.
**System capability**: SystemCapability.FileManagement.File.FileIO
......@@ -1896,7 +1989,7 @@ Opens a file stream based on the file path. This API uses a promise to return th
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the file in the application sandbox. |
| path | string | Yes | Application sandbox path of the file. |
| mode | string | Yes | - **r**: Open a file for reading. The file must exist.<br>- **r+**: Open a file for both reading and writing. The file must exist.<br>- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **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.<br>- **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).<br>- **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**
......@@ -1921,7 +2014,7 @@ Opens a file stream based on the file path. This API uses a promise to return th
createStream(path: string, mode: string, callback: AsyncCallback&lt;Stream&gt;): void
Opens a file stream based on the file path. This API uses an asynchronous callback to return the result.
Creates a stream based on the file path. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.FileManagement.File.FileIO
......@@ -1929,9 +2022,9 @@ Opens a file stream based on the file path. This API uses an asynchronous callba
| Name | Type | Mandatory| Description |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the file in the application sandbox. |
| path | string | Yes | Application sandbox path of the file. |
| mode | string | Yes | - **r**: Open a file for reading. The file must exist.<br>- **r+**: Open a file for both reading and writing. The file must exist.<br>- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **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.<br>- **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).<br>- **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&lt;[Stream](#stream)&gt; | Yes | Callback invoked when the stream is open asynchronously. |
| callback | AsyncCallback&lt;[Stream](#stream)&gt; | Yes | Callback invoked when the stream is created asynchronously. |
**Example**
......@@ -1950,7 +2043,7 @@ Opens a file stream based on the file path. This API uses an asynchronous callba
createStreamSync(path: string, mode: string): Stream
Synchronously opens a stream based on the file path.
Synchronously creates a stream based on the file path.
**System capability**: SystemCapability.FileManagement.File.FileIO
......@@ -1958,7 +2051,7 @@ Synchronously opens a stream based on the file path.
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path | string | Yes | Path of the file in the application sandbox. |
| path | string | Yes | Application sandbox path of the file. |
| mode | string | Yes | - **r**: Open a file for reading. The file must exist.<br>- **r+**: Open a file for both reading and writing. The file must exist.<br>- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **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.<br>- **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).<br>- **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**
......@@ -1979,7 +2072,7 @@ Synchronously opens a stream based on the file path.
fdopenStream(fd: number, mode: string): Promise&lt;Stream&gt;
Opens a file stream based on the file descriptor. This API uses a promise to return the result.
Opens a stream based on the file descriptor. This API uses a promise to return the result.
**System capability**: SystemCapability.FileManagement.File.FileIO
......@@ -2014,7 +2107,7 @@ Opens a file stream based on the file descriptor. This API uses a promise to ret
fdopenStream(fd: number, mode: string, callback: AsyncCallback&lt;Stream&gt;): void
Opens a file stream based on the file descriptor. This API uses an asynchronous callback to return the result.
Opens a stream based on the file descriptor. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.FileManagement.File.FileIO
......@@ -2085,8 +2178,8 @@ Creates a **Watcher** object to observe file or directory changes.
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ---------------------------------------- |
| path | string | Yes | Path of the file or directory to observe in the application sandbox. |
| events | number | Yes | Events to observe. Multiple events can be separated by a bitwise OR operator (&#124;).<br>- **0x1: IN_ACCESS**: A file is accessed.<br>- **0x2: IN_MODIFY**: The file content is modified.<br>- **0x4: IN_ATTRIB**: Metadata is changed.<br>- **0x8: IN_CLOSE_WRITE**: The file opened for writing is closed.<br>- **0x10: IN_CLOSE_NOWRITE**: The file or directory not opened for writing is closed.<br>- **0x20: IN_OPEN**: A file or directory is opened.<br>- **0x40: IN_MOVED_FROM**: A file in the observed directory is moved.<br>- **0x80: IN_MOVED_TO**: A file is moved to the observed directory.<br>- **0x100: IN_CREATE**: A file or directory is created in the observed directory.<br>- **0x200: IN_DELETE**: A file or directory is deleted form the observed directory.<br>- **0x400: IN_DELETE_SELF**: The observed directory is deleted. After the directory is deleted, the listening stops.<br>- **0x800: IN_MOVE_SELF**: The observed file or directory is moved. After the file or directory is moved, the listening continues.<br>- **0xfff: IN_ALL_EVENTS**: All events.|
| 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 (&#124;)|.<br>- **0x1: IN_ACCESS**: A file is accessed.<br>- **0x2: IN_MODIFY**: The file content is modified.<br>- **0x4: IN_ATTRIB**: Metadata is changed.<br>- **0x8: IN_CLOSE_WRITE**: The file opened for writing is closed.<br>- **0x10: IN_CLOSE_NOWRITE**: The file or directory not opened for writing is closed.<br>- **0x20: IN_OPEN**: A file or directory is opened.<br>- **0x40: IN_MOVED_FROM**: A file in the observed directory is moved.<br>- **0x80: IN_MOVED_TO**: A file is moved to the observed directory.<br>- **0x100: IN_CREATE**: A file or directory is created in the observed directory.<br>- **0x200: IN_DELETE**: A file or directory is deleted from the observed directory.<br>- **0x400: IN_DELETE_SELF**: The observed directory is deleted. After the directory is deleted, the listening stops.<br>- **0x800: IN_MOVE_SELF**: The observed file or directory is moved. After the file or directory is moved, the listening continues.<br>- **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**
......@@ -2317,7 +2410,7 @@ Checks whether this file is a symbolic link.
## Stream
Provides file stream management. Before calling any API of the **Stream** class, use **createStream()** to create a **Stream** instance synchronously or asynchronously.
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
......@@ -2638,7 +2731,7 @@ Synchronously reads data 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:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br> |
| options | Object | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): position of the data to read in the file. By default, data is read from the current position.<br> |
**Return value**
......@@ -2668,7 +2761,7 @@ Represents a **File** object opened by **open()**.
### lock
lock(exclusive?: boolean): Promise\<void>;
lock(exclusive?: boolean): Promise\<void>
Applies an exclusive lock or a shared lock on this file in blocking mode. This API uses a promise to return the result.
......@@ -2699,7 +2792,7 @@ Applies an exclusive lock or a shared lock on this file in blocking mode. This A
### lock
lock(exclusive?: boolean, callback: AsyncCallback\<void>): void;
lock(exclusive?: boolean, callback: AsyncCallback\<void>): void
Applies an exclusive lock or a shared lock on this file in blocking mode. This API uses a promise to return the result.
......@@ -2727,7 +2820,7 @@ Applies an exclusive lock or a shared lock on this file in blocking mode. This A
### tryLock
tryLock(exclusive?: boolean): void;
tryLock(exclusive?: boolean): void
Applies an exclusive lock or a shared lock on this file in non-blocking mode.
......@@ -2749,7 +2842,7 @@ Applies an exclusive lock or a shared lock on this file in non-blocking mode.
### unlock
unlock(): void;
unlock(): void
Unlocks this file synchronously.
......@@ -2808,7 +2901,7 @@ Stops listening.
## OpenMode
Defines the constants of the **mode** parameter used in **open()**. It species the mode for opening a file.
Defines the constants of the **mode** parameter used in **open()**. It specifies the mode for opening a file.
**System capability**: SystemCapability.FileManagement.File.FileIO
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册