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
importfileUrifrom"@ohos.file.fileUri";
importfileurifrom"@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
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
importfsfrom'@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.
| 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 (|). 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
| 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 (|). 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.
| 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 (|). 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.
| 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
| 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<string> | Yes | Callback invoked to return the content read. |
...
...
@@ -1108,7 +1115,7 @@ Synchronously reads the text of a file.
| 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
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.
| 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.|
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.
| 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.|
| 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
| 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.|
| 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<void> | Yes | Callback invoked when the folder is moved. |
**Example**
```js
// move directory from srcPath to destPath/srcPath
letsrcPath=pathDir+"/srcDir/";
letdestPath=pathDir+"/destDir/";
fs.moveDir(srcPath,destPath,1,(err)=>{
if(err&&err.code==13900015){
for(leti=0;i<err.data.length;i++){
console.info("move directory failed with conflicting files: "+err.data[i].srcFile+
""+err.data[i].destFile);
}
}elseif(err){
console.info("move directory failed with error message: "+err.message+", error code: "+err.code);
| 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**.|
| 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<void> | 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.
| 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.
| 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
| 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<[Stream](#stream)> | Yes | Callback invoked when the stream is open asynchronously. |
| callback | AsyncCallback<[Stream](#stream)> | 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
| 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.
| 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 (|).<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 (|)|.<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.
| 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