The **fileio** module provides APIs for file storage and management, including basic file management, directory management, file information statistics, and stream read and write.
> **NOTE**<br>
> **NOTE**
>
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs provided by this module are deprecated since API version 9. You are advised to use [@ohos.file.fs](./js-apis-file-fs.md).
## Modules to Import
...
...
@@ -21,6 +22,7 @@ Stage Model
```js
importAbilityfrom'@ohos.application.Ability';
classMainAbilityextendsAbility{
onWindowStageCreate(windowStage){
letcontext=this.context;
...
...
@@ -29,19 +31,20 @@ class MainAbility extends Ability {
}
```
For details about how to obtain the stage model context, see [Stage Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-ability-context.md#abilitycontext).
For details about how to obtain the stage model context, see [AbilityContext](js-apis-ability-context.md#abilitycontext).
For details about how to obtain the context of the FA model, see [FA Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-Context.md#context).
For details about how to obtain the FA model context, see [Contex](js-apis-inner-app-context.md#context).
## fileio.stat
...
...
@@ -67,10 +70,10 @@ Obtains file information. This API uses a promise to return the result.
| src | string \| number | Yes | Path or file descriptor of the file to copy. |
| dest | string \| number | Yes | Path or file descriptor of the new file. |
| src | string\|number | Yes | Path or file descriptor of the file to copy. |
| dest | string\|number | Yes | Path or file descriptor of the new file. |
| mode | number | No | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
**Return value**
...
...
@@ -410,17 +413,17 @@ Copies a file. This API uses a promise to return the result.
```js
letsrcPath=pathDir+"srcDir/test.txt";
letdstPath=pathDir+"dstDir/test.txt";
fileio.copyFile(srcPath,dstPath).then(function(){
fileio.copyFile(srcPath,dstPath).then(function(){
console.info("File copied");
}).catch(function(err){
console.info("Failed to copy the file. Error:"+err);
| src | string \| number | Yes | Path or file descriptor of the file to copy. |
| dest | string \| number | Yes | Path or file descriptor of the new file. |
| src | string\|number | Yes | Path or file descriptor of the file to copy. |
| dest | string\|number | Yes | Path or file descriptor of the new file. |
| mode | number | No | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely 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. |
...
...
@@ -448,7 +451,7 @@ Copies a file. This API uses an asynchronous callback to return the result.
| src | string \| number | Yes | Path or file descriptor of the file to copy. |
| dest | string \| number | Yes | Path or file descriptor of the new file. |
| src | string\|number | Yes | Path or file descriptor of the file to copy. |
| dest | string\|number | Yes | Path or file descriptor of the new file. |
| mode | number | No | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
**Example**
...
...
@@ -496,10 +499,10 @@ Creates a directory. This API uses a promise to return the result.
```js
letdirPath=pathDir+'/testDir';
fileio.mkdir(dirPath).then(function(){
fileio.mkdir(dirPath).then(function(){
console.info("Directory created");
}).catch(function(error){
console.info("Failed to create the directory. Error:"+error);
}).catch(function(error){
console.info("mkdir failed with error:"+error);
});
```
...
...
@@ -524,7 +527,7 @@ Creates a directory. This API uses an asynchronous callback to return the result
```js
letdirPath=pathDir+'/testDir';
fileio.mkdir(dirPath,function(err){
fileio.mkdir(dirPath,function(err){
console.info("Directory created");
});
```
...
...
@@ -579,10 +582,10 @@ Opens a file. This API uses a promise to return the result.
console.info("Failed to open the file. Error:"+err);
}).catch(function(err){
console.info("open file failed with error:"+err);
});
```
...
...
@@ -602,13 +605,13 @@ Opens a file. This API uses an asynchronous callback to return the result.
| path | string | Yes | Application sandbox path of the file. |
| flags | number | No | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **0o0**: Open the file in read-only mode.<br>- **0o1**: Open the file in write-only mode.<br>- **0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (|). By default, no additional option is specified.<br>- **0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>- **0o200**: If **0o100** is added and the file already exists, throw an exception.<br>- **0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>- **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>- **0o200000**: If **path** does not point to a directory, throw an exception.<br><br>- **0o400000**: If **path** points to a symbolic link, throw an exception.<br>- **0o4010000**: Open the file in synchronous I/O mode.|
| mode | number | No | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o666**.<br>- **0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission.|
| callback | AsyncCallback <void> | Yes | Callback invoked when the file is open asynchronously. |
| callback | AsyncCallback<number> | Yes | Callback invoked when the file is open asynchronously. |
| fd | number | Yes | File descriptor of the file to write. |
| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. |
| 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:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
**Return value**
...
...
@@ -957,22 +943,17 @@ Writes data into a file. This API uses a promise to return the result.
| fd | number | Yes | File descriptor of the file to write. |
| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. |
| 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:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
| callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. |
...
...
@@ -994,7 +975,7 @@ Writes data into a file. This API uses an asynchronous callback to return the re
| fd | number | Yes | File descriptor of the file to write. |
| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. |
| 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:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
**Return value**
...
...
@@ -1061,10 +1037,10 @@ Calculates the hash value of a file. This API uses a promise to return the resul
| fd | number | Yes | File descriptor of the target file. |
| mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|).<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission.|
| callback | AsyncCallback <void> | Yes | Callback invoked when the file permissions are changed asynchronously. |
| callback | AsyncCallback<void> | Yes | Callback invoked when the file permissions are changed asynchronously. |
**Example**
...
...
@@ -2240,10 +2204,10 @@ Opens a file stream based on the file path. This API uses a promise to return th
| fd | number | Yes | File descriptor of the target 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 open asynchronously. |
**Example**
...
...
@@ -2422,10 +2386,10 @@ Changes the file owner based on the file descriptor. This API uses a promise to
| filePath | string | Yes | Application sandbox path of the file. |
| events | Number | Yes | - **1**: The file or directory is renamed.<br>- **2**: The file or directory is modified.<br>- **3**: The file or directory is modified and renamed.|
| callback | AsyncCallback<number > | Yes | Called each time a change is detected. |
| events | number | Yes | - **1**: The file or directory is renamed.<br>- **2**: The file or directory is modified.<br>- **3**: The file or directory is modified and renamed.|
| callback | AsyncCallback<number> | Yes | Called each time a change is detected. |
**Return value**
...
...
@@ -2599,8 +2563,8 @@ Listens for file or directory changes. This API uses an asynchronous callback to
```js
letfilePath=pathDir+"/test.txt";
fileio.createWatcher(filePath,1,function(number){
console.info("Monitoring times: "+number);
fileio.createWatcher(filePath,1,function(number){
console.info("Monitoring times: "+number);
});
```
...
...
@@ -2616,7 +2580,7 @@ Obtains the file read result. This class applies only to the **read()** method.
| dev | number | Yes | No | Major device number. |
| ino | number | Yes | No | File ID. Different files on the same device have different **ino**s. |
| mode | number | Yes | No | File type and permissions. The first four bits indicate the file type, and the last 12 bits indicate the permissions. The bit fields are described as follows:<br>- **0o170000**: mask used to obtain the file type.<br>- **0o140000**: The file is a socket.<br>- **0o120000**: The file is a symbolic link.<br>- **0o100000**: The file is a regular file.<br>- **0o060000**: The file is a block device.<br>- **0o040000**: The file is a directory.<br>- **0o020000**: The file is a character device.<br>- **0o010000**: The file is a named pipe (FIFO).<br>- **0o0700**: mask used to obtain the owner permissions.<br>- **0o0400**: The owner has the permission to read a regular file or a directory entry.<br>- **0o0200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>- **0o0100**: The owner has the permission to execute a regular file or search for the specified path in a directory.<br>- **0o0070**: mask used to obtain the user group permissions.<br>- **0o0040**: The user group has the permission to read a regular file or a directory entry.<br>- **0o0020**: The user group has the permission to write a regular file or create and delete a directory entry.<br>- **0o0010**: The user group has the permission to execute a regular file or search for the specified path in a directory.<br>- **0o0007**: mask used to obtain the permissions of other users.<br>- **0o0004**: Other users have the permission to read a regular file or a directory entry.<br>- **0o0002**: Other users have the permission to write a regular file or create and delete a directory entry.<br>- **0o0001**: Other users have the permission to execute a regular file or search for the specified path in a directory.|
| mode | number | Yes | No | File type and permissions. The first four bits indicate the file type, and the last 12 bits indicate the permissions. The bit fields are described as follows:<br>- **0o170000**: mask used to obtain the file type.<br>- **0o140000**: The file is a socket.<br>- **0o120000**: The file is a symbolic link.<br>- **0o100000**: The file is a regular file.<br>- **0o060000**: The file is a block device.<br>- **0o040000**: The file is a directory.<br>- **0o020000**: The file is a character device.<br>-**0o010000**: The file is a named pipe (FIFO).<br>- **0o0700**: mask used to obtain the owner permissions.<br>- **0o0400**: The owner has the permission to read a regular file or a directory entry.<br>- **0o0200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>- **0o0100**: The owner has the permission to execute a regular file or search for the specified path in a directory.<br>- **0o0070**: mask used to obtain the user group permissions.<br>- **0o0040**: The user group has the permission to read a regular file or a directory entry.<br>- **0o0020**: The user group has the permission to write a regular file or create and delete a directory entry.<br>- **0o0010**: The user group has the permission to execute a regular file or search for the specified path in a directory.<br>- **0o0007**: mask used to obtain the permissions of other users.<br>- **0o0004**: Other users have the permission to read a regular file or a directory entry.<br>- **0o0002**: Other users have the permission to write a regular file or create and delete a directory entry.<br>- **0o0001**: Other users have the permission to execute a regular file or search for the specified path in a directory.|
| nlink | number | Yes | No | Number of hard links in the file. |
| uid | number | Yes | No | User ID, that is ID of the file owner. |
| gid | number | Yes | No | Group ID, that is, ID of the user group of the file. |
...
...
@@ -2814,10 +2778,10 @@ Stops the **watcher** instance. This API uses a promise to return the result.
| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. |
| 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:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
**Return value**
...
...
@@ -3024,22 +2983,17 @@ Writes data into the stream. This API uses a promise to return the result.
| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. |
| 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:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
| callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. |
...
...
@@ -3061,7 +3015,7 @@ Writes data into the stream. This API uses an asynchronous callback to return th
| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. |
| 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:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
**Return value**
...
...
@@ -3104,11 +3053,7 @@ Synchronously writes data into the stream.