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>
> 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.
Provides file storage and management capabilities, including basic file management, file directory management, file information statistics, and stream read and write.
## Modules to Import
...
...
@@ -46,6 +47,7 @@ Obtains file information. This API uses a promise to return the result.
| Promise<[Stat](#stat)> | Promise used to return the file information obtained.|
@@ -147,6 +157,7 @@ Opens a file directory. This API uses an asynchronous callback to return the res
| callback | AsyncCallback<[Dir](#dir)> | Yes | Callback invoked when the directory is open asynchronously. |
**Example**
```js
fileio.opendir(path,function(err,dir){
// Example code in Dir struct
...
...
@@ -171,11 +182,13 @@ Synchronously opens a directory.
| path | string | Yes | Application sandbox path of the directory to open.|
**Return value**
| Type | Description |
| ----------- | -------- |
| [Dir](#dir) | A **Dir** instance corresponding to the directory.|
**Example**
```js
letdir=fileio.opendirSync(path);
// Example code in Dir struct
...
...
@@ -199,11 +212,13 @@ Checks whether the current process can access a file. This API uses a promise to
| mode | number | No | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (|). The default value is **0**.<br>The options are as follows:<br>- **0**: check whether the file exists.<br>- **1**: check whether the current process has the execute permission on the file.<br>- **2**: check whether the current process has the write permission on the file.<br>- **4**: check whether the current process has the read permission on the file.|
| path | string | Yes | Application sandbox path of the file. |
| mode | number | No | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (|). The default value is **0**.<br>The options are as follows:<br>- **0**: check whether the file exists.<br>- **1**: check whether the current process has the execute permission on the file.<br>- **2**: check whether the current process has the write permission on the file.<br>- **4**: check whether the current process has the read permission on the file.|
**Example**
```js
try{
fileio.accessSync(path);
...
...
@@ -269,16 +288,19 @@ Closes a file. This API uses a promise to return the result.
| src | string \| number | Yes | Path or file descriptor of the file to copy. |
...
...
@@ -391,11 +422,13 @@ Copies a file. This API uses a promise to return the result.
| 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.|
| src | string \| number | Yes | Path or file descriptor of the file to copy. |
...
...
@@ -445,6 +481,7 @@ Synchronously copies a 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**
```js
fileio.copyFileSync(src,dest);
```
...
...
@@ -459,17 +496,20 @@ Creates a directory. This API uses a promise to return the result.
| path | string | Yes | Application sandbox path of the directory. |
| mode | number | No | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o775**.<br>- **0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<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.|
| path | string | Yes | Application sandbox path of the directory. |
| mode | number | No | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o775**.<br>- **0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<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.|
**Example**
```js
fileio.mkdirSync(path);
```
...
...
@@ -531,6 +575,7 @@ Opens a file. This API uses a promise to return the result.
| path | string | Yes | Application sandbox path of the file. |
...
...
@@ -538,11 +583,13 @@ Opens a file. This API uses a promise to return the result.
| 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.|
**Return value**
| Type | Description |
| --------------------- | ----------- |
| Promise<number> | Promise used to return the file descriptor of the file opened.|
| path | string | Yes | Application sandbox path of the file. |
...
...
@@ -592,11 +642,13 @@ Synchronously opens a file.
| 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>- **0o640**: The owner has the read and write permissions, and the user group has the read permission.<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.<br>The file permissions on newly created files are affected by umask, which is set as the process starts. Currently, the modification of umask is not open.|
**Return value**
| Type | Description |
| ------ | ----------- |
| number | File descriptor of the file opened.|
**Example**
```js
letfd=fileio.openSync(path,0o102,0o640);
```
...
...
@@ -623,6 +675,7 @@ Reads data from a file. This API uses a promise to return the result.
| fd | number | Yes | File descriptor of the file to read. |
...
...
@@ -701,11 +758,13 @@ Synchronously reads data from a file.
| options | Object | No | The options are as follows:<br>- **offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
**Return value**
| Type | Description |
| ------ | -------- |
| number | Length of the data read.|
**Example**
```js
letfd=fileio.openSync(path,0o2);
letbuf=newArrayBuffer(4096);
...
...
@@ -722,16 +781,19 @@ Deletes a directory. This API uses a promise to return the result.
| fd | number | Yes | File descriptor of the file to write. |
...
...
@@ -873,11 +947,13 @@ Writes data into a file. This API uses a promise to return the result.
| 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**
| Type | Description |
| --------------------- | -------- |
| Promise<number> | Promise used to return the length of the data written.|
| fd | number | Yes | File descriptor of the file to write. |
...
...
@@ -941,11 +1020,13 @@ Synchronously writes data into a file.
| 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**
| Type | Description |
| ------ | -------- |
| number | Length of the data written in the file.|
**Example**
```js
letfd=fileio.openSync(path,0o100|0o2,0o666);
letnum=fileio.writeSync(fd,"hello, world");
...
...
@@ -961,17 +1042,20 @@ Calculates the hash value of a file. This API uses a promise to return the resul
| path | string | Yes | Application sandbox path of the file. |
| algorithm | string | Yes | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
| Promise<string> | Promise used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
**Example**
```js
fileio.hash(path,"sha256").then(function(str){
console.info("Calculated file hash:"+str);
...
...
@@ -990,6 +1074,7 @@ Calculates the hash value of a file. This API uses an asynchronous callback to r
| path | string | Yes | Application sandbox path of the file. |
...
...
@@ -997,6 +1082,7 @@ Calculates the hash value of a file. This API uses an asynchronous callback to r
| callback | AsyncCallback<string> | Yes | Callback used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
**Example**
```js
fileio.hash(path,"sha256",function(err,hashStr){
if(hashStr){
...
...
@@ -1015,17 +1101,20 @@ Changes file permissions. This API uses a promise to return the result.
| path | string | Yes | Application sandbox path of the 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.|
| path | string | Yes | Application sandbox path of the 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.|
**Example**
```js
fileio.chmodSync(path,mode);
```
...
...
@@ -1087,16 +1180,19 @@ Obtains file information based on the file descriptor. This API uses a promise t
| filePath | string | Yes | Application sandbox path of the file to read. |
| options | Object | No | The options are as follows:<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **encoding** (string): format of the data (string) to be encoded. The default value is **utf-8**, which is the only value supported.|
**Return value**
| Type | Description |
| --------------------- | ---------- |
| Promise<string> | Promise used to return the content read.|
**Example**
```js
fileio.readText(path).then(function(str){
console.info("Read file text:"+str);
...
...
@@ -1345,6 +1463,7 @@ Reads the text content of a file. This API uses an asynchronous callback to retu
| filePath | string | Yes | Application sandbox path of the file to read. |
| options | Object | No | The options are as follows:<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **encoding** (string): format of the data (string) to be encoded. The default value is **utf-8**, which is the only value supported.|
**Return value**
| Type | Description |
| ------ | -------------------- |
| string | Promise used to return the content of the file read.|
| buffer | ArrayBuffer | Yes | Buffer used to store the file data read. |
| options | Object | No | The options are as follows:<br>- **offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>Constraints: offset + length <= Buffer size|
**Return value**
| Type | Description |
| ---------------------------------- | ------ |
| Promise<[ReadOut](#readout)> | Promise used to return the data read.|
| 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.|
| 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.|
**Example**
```js
fileio.fchmodSync(fd,mode);
```
...
...
@@ -2041,17 +2227,20 @@ Opens a file stream based on the file path. This API uses a promise to return th
| 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**
| Type | Description |
| --------------------------------- | --------- |
| Promise<[Stream](#stream7)> | Promise used to return the result.|
| 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**
| Type | Description |
| ------------------ | --------- |
| [Stream](#stream7) | Stream opened.|
**Example**
```js
letss=fileio.createStreamSync(path,"r+");
```
...
...
@@ -2118,17 +2312,20 @@ Opens a file stream based on the file descriptor. This API uses a promise to ret
| 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).|
**Return value**
| Type | Description |
| --------------------------------- | --------- |
| Promise<[Stream](#stream7)> | Promise used to return the result.|
| 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).|
**Return value**
| Type | Description |
| ------------------ | --------- |
| [Stream](#stream7) | Stream opened.|
**Example**
```js
letfd=fileio.openSync(path);
letss=fileio.fdopenStreamSync(fd,"r+");
...
...
@@ -2198,6 +2400,7 @@ Changes the file owner based on the file descriptor. This API uses a promise to
| 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**
| Type | Description |
| --------------------- | -------- |
| Promise<number> | Promise used to return the length of the data written.|
| 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**
| Type | Description |
| ------ | -------- |
| number | Length of the data written in the file.|
| buffer | ArrayBuffer | Yes | Buffer used to store the file read. |
| options | Object | No | The options are as follows:<br>- **offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
**Return value**
| Type | Description |
| ---------------------------------- | ------ |
| Promise<[ReadOut](#readout)> | Promise used to return the data read.|
The fileManager module provides APIs for accessing and managing user files. It interworks with the underlying file management services to implement media library and external card management, and provides capabilities for applications to query and create user files.
>**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.
>- The APIs of this module are system APIs and cannot be called by third-party applications. Currently, these APIs can be called only by **filepicker**.
Provides service APIs for accessing and managing user files. It interworks with the underlying file management services to implement media library and external card management, and provides capabilities for applications to query and create user files.
The secuityLabel module provides APIs to manage file data security levels, including obtaining and setting file data security levels.
> **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.
Manages file data security levels, including obtaining and setting file data security levels.
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
The statfs module provides APIs for obtaining file system information, including the total number of bytes and the number of idle bytes of the file system.
This module provides information related to the file system. It provides JS APIs to obtain the total number of bytes and the number of idle bytes of the file system.
> **NOTE**<br>
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```js
importstatfsfrom'@ohos.statfs';
```
## Guidelines
Before using the APIs provided by this module to perform operations on a file or directory, obtain the path of the application sandbox. For details, see [getOrCreateLocalDir of the Context module](js-apis-Context.md).
Application sandbox path of a file or directory = Application directory + File name or directory name
For example, if the application directory obtained by using **getOrCreateLocalDir** is **dir** and the file name is **xxx.txt**, the application sandbox path of the file is as follows:
```js
letpath=dir+"xxx.txt";
```
## statfs.getFreeBytes
getFreeBytes(path:string):Promise<number>
...
...
@@ -72,8 +59,12 @@ Obtains the number of free bytes of the specified file system in asynchronous mo
The storageStatistics module provides APIs for obtaining storage space information, including the space of built-in and plug-in memory cards, space occupied by different types of data, and space of application data.
> **NOTE**<br/>
>
> - The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - API version 9 is a canary version for trial use. The APIs of this version may be unstable.
Obtains storage space information, including the space of built-in and plug-in memory cards, space occupied by different types of data, and space of application data.
> - The APIs of this module are no longer maintained since API version 6. It is recommended that you use [`@ohos.fileio`](js-apis-fileio.md) instead.
> **NOTE**<br>
> - The APIs of this module are no longer maintained since API version 6. You are advised to use [`@ohos.fileio`](js-apis-fileio.md).
>
> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
...
...
@@ -26,21 +24,21 @@ Moves a specified file to a given location.
| success | Function | No | Called when the source file is moved to the specified location successfully. This function returns the URI of the destination location. |
| fail | Function | No | Called when the operation fails. |
| complete | Function | No | Called when the execution is complete. |
| srcUri | string | Yes| Uniform resource identifier (URI) of the file to move. <br/>The URI can contain a maximum of 128 characters, excluding the following characters: "\*+,:;<=>?[]\|\x7F |
| dstUri | string | Yes| URI of the location to which the file is to move. <br/>The URI can contain a maximum of 128 characters, excluding the following characters: "\*+,:;<=>?[]\|\x7F |
| success | Function | No| Called when the file is moved to the specified location. This API returns the URI of the destination location.|
| fail | Function | No| Called when the file fails to be moved.|
| complete | Function | No| Called when the execution is complete.|
One of the following error codes will be returned if the operation fails.
| success | Function | No | Called when the source file is copied and saved to the specified location successfully. This function returns the URI of the destination location. |
| fail | Function | No | Called when the operation fails. |
| complete | Function | No | Called when the execution is complete. |
| srcUri | string | Yes| URI of the file to copy.|
| dstUri | string | Yes| URI of the location to which the copy is to be saved.<br>The directory of application resources and URI of the **tmp** type are not supported.|
| success | Function | No| Called when the file is copied and saved to the specified location. This API returns the URI of the destination location.|
| fail | Function | No| Called when the file fails to be copied.|
| complete | Function | No| Called when the execution is complete.|
One of the following error codes will be returned if the operation fails.
| uri | string | Yes | URI of the directory. |
| success | Function | No | Called when the operation is successful. |
| fail | Function | No | Called when the operation fails. |
| complete | Function | No | Called when the execution is complete. |
| uri | string | Yes| URI of the directory. <br/>The URI can contain a maximum of 128 characters, excluding the following characters: "\*+,:;<=>?[]\|\x7F |
| success | Function | No| Called when the file list is obtained.|
| fail | Function | No| Called when the file list fails to be obtained.|
| complete | Function | No| Called when the execution is complete.|
| fileList | Array<FileInfo> | File list. The format of each file is as follows:<br>{<br>uri:'file1',<br>lastModifiedTime:1589965924479,<br>length:10240,<br>type: 'file'<br>} |
**Table1** FileInfo
**Table1** FileInfo
| Name | Type | Description |
| Name| Type| Description|
| -------- | -------- | -------- |
| uri | string | File URI. |
| lastModifiedTime | number | Timestamp when the file is stored the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT. |
| length | number | File size, in bytes. |
| type | string | File type. Available values are as follows:<br/>- **dir**: directory<br/>- **file**: file |
| uri | string | URI of the file.|
| lastModifiedTime | number | Timestamp when the file is saved the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT.|
| length | number | File size, in bytes.|
| type | string | File type. Available values are as follows:<br>- **dir**: directory<br>- **file**: file |
One of the following error codes will be returned if the operation fails.
| recursive | boolean | No | Whether to recursively obtain the file list under a subdirectory. The default value is **false**. |
| success | Function | No | Called when the operation is successful. |
| fail | Function | No | Called when the operation fails. |
| complete | Function | No | Called when the execution is complete. |
| uri | string | Yes| URI of the file.|
| recursive | boolean | No| Whether to recursively obtain the file list under a subdirectory. The default value is **false**.|
| success | Function | No| Called when the file information is obtained.|
| fail | Function | No| Called when the file information fails to be obtained.|
| complete | Function | No| Called when the execution is complete.|
Return values of the **success** callback
**Return value of success()**
| Name | Type | Description |
| Name| Type| Description|
| -------- | -------- | -------- |
| uri | string | File URI. |
| length | number | File size, in bytes. |
| lastModifiedTime | number | Timestamp when the file is stored the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT. |
| type | string | File type. The values are as follows:<br/>- **dir**: directory<br/>- **file**: file |
| subFiles | Array | File list. |
| uri | string | URI of the file.|
| length | number | File size, in bytes.|
| lastModifiedTime | number | Timestamp when the file is saved the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT.|
| type | string | File type. Available values are as follows:<br>- **dir**: directory<br>- **file**: file |
| subFiles | Array | List of files.|
One of the following error codes will be returned if the operation fails.
| position | number | No | Offset to the position where the writing starts. The default value is **0**. |
| append | boolean | No | Whether to enable the append mode. The default value is **false**. If the value is **true**, the **position** parameter will become invalid. |
| success | Function | No | Called when the operation is successful. |
| fail | Function | No | Called when the operation fails. |
| complete | Function | No | Called when the execution is complete. |
| uri | string | Yes| URI of a local file. If it does not exist, a file will be created.|
| buffer | Uint8Array | Yes| Buffer from which the data is derived.|
| position | number | No| Offset to the position where the writing starts. The default value is **0**.|
| append | boolean | No| Whether to enable the append mode. The default value is **false**. If the value is **true**, the **position** parameter will become invalid.|
| success | Function | No| Called when buffer data is written into the file. |
| fail | Function | No| Called when buffer data fails to be written into the file.|
| complete | Function | No| Called when the execution is complete.|
One of the following error codes will be returned if the operation fails.
**Return value of fail()**
| Error Code | Description |
| Error Code| Description|
| -------- | -------- |
| 202 | Invalid parameter. |
| 300 | I/O error. |
| 202 | Incorrect parameters are detected.|
| 300 | An I/O error occurs.|
**Example**
...
...
@@ -347,7 +345,7 @@ export default {
writeArrayBuffer() {
file.writeArrayBuffer({
uri: 'internal://app/test',
buffer: new Uint8Array(8), // The buffer is of the Uint8Array type.
buffer: new Uint8Array(8), // The buffer is of the Uint8Array type.
success: function() {
console.log('call writeArrayBuffer success.');
},
...
...
@@ -364,36 +362,36 @@ export default {
readText(Object): void
Reads text from a specified file.
Reads text from a file. Only text files can be read and written.
| uri | string | Yes | URI of a local file. |
| encoding | string | No | Encoding format. The default format is UTF-8. |
| position | number | No | Position where the reading starts. The default value is the start position of the file. |
| length | number | No | Length of the text to be read (in bytes). The default value is **4096**. |
| success | Function | No | Called when the operation is successful. |
| fail | Function | No | Called when the operation fails. |
| complete | Function | No | Called when the execution is complete. |
| uri | string | Yes| URI of a local file.|
| encoding | string | No| Encoding format. The default format is **UTF-8**.|
| position | number | No| Position where the reading starts. The default value is the start position of the file.|
| length | number | No| Length of the text to read, in bytes. The default value is **4096**.|
| success | Function | No| Called when the text is read successfully.|
| fail | Function | No| Called when the text failed to be read.|
| complete | Function | No| Called when the execution is complete.|
Return values of the **success** callback
**Return value of success()**
| Name | Type | Description |
| Name| Type| Description|
| -------- | -------- | -------- |
| text | string | Text read from the specified file. |
| text | string | Text read from the specified file.|
One of the following error codes will be returned if the operation fails.
| uri | string | Yes | URI of a local file. |
| position | number | No | Position where the reading starts. The default value is the start position of the file. |
| length | number | No | Length of data to read. If this parameter is not set, the reading proceeds until the end of the file. |
| success | Function | No | Called when the operation is successful. |
| fail | Function | No | Called when the operation fails. |
| complete | Function | No | Called when the execution is complete. |
| uri | string | Yes| URI of a local file.|
| position | number | No| Position where the reading starts. The default value is the start position of the file.|
| length | number | No| Length of data to read. If this parameter is not set, the reading proceeds until the end of the file.|
| success | Function | No| Called when the buffer data is read successfully.|
| fail | Function | No| Called when the buffer data fails to be read.|
| complete | Function | No| Called when the execution is complete.|
The volumeManager module provides APIs for volume and disk management, including obtaining volume information, mounting or unmounting volumes, partitioning disks, and formatting volumes.
> **NOTE**<br>
>
> - The initial APIs of this module are supported since API version 9.
> - API version 9 is a canary version for trial use. The APIs of this version may be unstable.
> - The APIs of this module are system APIs and cannot be called by third-party applications.
The APIs of this module can be used to perform volume and disk management, including obtaining volume information, mounting and unmounting volumes, partitioning disks, and formatting volumes.