# File Management 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**
> 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. ## Modules to Import ```js import fileio from '@ohos.fileio'; ``` ## Guidelines Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows: ```js import featureAbility from '@ohos.ability.featureAbility'; let context = featureAbility.getContext(); let path = ''; context.getFilesDir().then((data) => { path = data; }) ``` ## fileio.stat stat(path: string): Promise<Stat> Obtains file information. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| **Return value** | Type | Description | | ---------------------------- | ---------- | | Promise<[Stat](#stat)> | Promise used to return the file information obtained.| **Example** ```js fileio.stat(path).then(function(stat){ console.info("Got file info:"+ JSON.stringify(stat)); }).catch(function(err){ console.info("Failed to get file info. Error:"+ err); }); ``` ## fileio.stat stat(path:string, callback:AsyncCallback<Stat>): void Obtains file information. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------- | ---- | ------------------------------ | | path | string | Yes | Application sandbox path of the file. | | callback | AsyncCallback<[Stat](#stat)> | Yes | Callback invoked to return the file information obtained.| **Example** ```js fileio.stat(path, function (err, stat) { // Example code in Stat }); ``` ## fileio.statSync statSync(path:string): Stat Synchronously obtains file information. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| **Return value** | Type | Description | | ------------- | ---------- | | [Stat](#stat) | File information obtained.| **Example** ```js let stat = fileio.statSync(path); // Example code in Stat ``` ## fileio.opendir opendir(path: string): Promise<Dir> Opens a file directory. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------ | | path | string | Yes | Application sandbox path of the directory to open.| **Return value** | Type | Description | | -------------------------- | -------- | | Promise<[Dir](#dir)> | Promise used to return the **Dir** object.| **Example** ```js fileio.opendir(path).then(function(dir){ console.info("Directory opened:"+ JSON.stringify(dir)); }).catch(function(err){ console.info("Failed to open the directory. Error:"+ err); }); ``` ## fileio.opendir opendir(path: string, callback: AsyncCallback<Dir>): void Opens a file directory. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | -------------------------------- | ---- | ------------------------------ | | path | string | Yes | Application sandbox path of the directory to open.| | 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 // Use read/readSync/close. }); ``` ## fileio.opendirSync opendirSync(path: string): Dir Synchronously opens a directory. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------ | | 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 let dir = fileio.opendirSync(path); // Example code in Dir struct // Use read/readSync/close. ``` ## fileio.access access(path: string, mode?: number): Promise<void> Checks whether the current process can access a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | 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**.
The options are as follows:
- **0**: check whether the file exists.
- **1**: check whether the current process has the execute permission on the file.
- **2**: check whether the current process has the write permission on the file.
- **4**: check whether the current process has the read permission on the file.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js fileio.access(path).then(function() { console.info("Access successful"); }).catch(function(err){ console.info("Access failed. Error:"+ err); }); ``` ## fileio.access access(path: string, mode: number, callback: AsyncCallback<void>): void Checks whether the current process can access a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ------------------------------------------------------------ | | 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**.
The options are as follows:
- **0**: check whether the file exists.
- **1**: check whether the current process has the execute permission on the file.
- **2**: check whether the current process has the write permission on the file.
- **4**: check whether the current process has the read permission on the file.| | callback | AsyncCallback<void> | Yes | Callback invoked when the file is asynchronously checked. | **Example** ```js fileio.access(path, function (err) { // Do something. }); ``` ## fileio.accessSync accessSync(path: string, mode?: number): void Synchronously checks whether the current process can access the specified file. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | 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**.
The options are as follows:
- **0**: check whether the file exists.
- **1**: check whether the current process has the execute permission on the file.
- **2**: check whether the current process has the write permission on the file.
- **4**: check whether the current process has the read permission on the file.| **Example** ```js try { fileio.accessSync(path); } catch(err) { console.info("accessSync failed. Error:"+ err); } ``` ## fileio.close7+ close(fd: number):Promise<void> Closes a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the file to close.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let fd = fileio.openSync(path); fileio.close(fd).then(function(){ console.info("File closed"); }).catch(function(err){ console.info("Failed to close the file. Error:"+ err); }); ``` ## fileio.close7+ close(fd: number, callback:AsyncCallback<void>): void Closes a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------- | ---- | ------------ | | fd | number | Yes | File descriptor of the file to close.| | callback | AsyncCallback<void> | Yes | Callback invoked when the file is closed asynchronously.| **Example** ```js let fd = fileio.openSync(path); fileio.close(fd, function (err) { // Do something. }); ``` ## fileio.closeSync closeSync(fd: number): void Synchronously closes a file. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the file to close.| **Example** ```js let fd = fileio.openSync(path); fileio.closeSync(fd); ``` ## fileio.copyFile copyFile(src:string | number, dest:string | number, mode?:number):Promise<void> Copies a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | -------------------------- | ---- | ---------------------------------------- | | 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.
**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let src = path; let dest = src + 'tgt'; fileio.copyFile(src, dest).then(function(){ console.info("File copied"); }).catch(function(err){ console.info("Failed to copy the file. Error:"+ err); }); ``` ## fileio.copyFile copyFile(src: string | number, dest: string | number, mode: number, callback: AsyncCallback<void>): void Copies a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | -------------------------- | ---- | ---------------------------------------- | | 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.
**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. | **Example** ```js let src = path; let dest = src + 'tgt'; fileio.copyFile(src, dest, function (err) { // Do something. }); ``` ## fileio.copyFileSync copyFileSync(src: string | number, dest: string | number, mode?: number): void Synchronously copies a file. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | -------------------------- | ---- | ---------------------------------------- | | 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.
**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.| **Example** ```js let src = path; let dest = src + 'tgt'; fileio.copyFileSync(src, dest); ``` ## fileio.mkdir mkdir(path:string, mode?: number): Promise<void> Creates a directory. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | 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**.
- **0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js fileio.mkdir(path).then(function() { console.info("Directory created"); }).catch(function (error){ console.info("Failed to create the directory. Error:"+ error); }); ``` ## fileio.mkdir mkdir(path: string, mode: number, callback: AsyncCallback<void>): void Creates a directory. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ------------------------------------------------------------ | | 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**.
- **0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| | callback | AsyncCallback<void> | Yes | Callback invoked when the directory is created asynchronously. | **Example** ```js fileio.mkdir(path, function(err) { console.info("Directory created"); }); ``` ## fileio.mkdirSync mkdirSync(path: string, mode?: number): void Synchronously creates a directory. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | 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**.
- **0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| **Example** ```js fileio.mkdirSync(path); ``` ## fileio.open7+ open(path: string, flags?: number, mode?: number): Promise<number> Opens a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | 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.
- **0o0**: Open the file in read-only mode.
- **0o1**: Open the file in write-only mode.
- **0o2**: Open the file in read/write mode.
In addition, you can specify the following options, separated using a bitwise OR operator (|). By default, no additional option is specified.
- **0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.
- **0o200**: If **0o100** is added and the file already exists, throw an exception.
- **0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.
- **0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).
- **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.
- **0o200000**: If **path** does not point to a directory, throw an exception.

- **0o400000**: If **path** points to a symbolic link, throw an exception.
- **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**.
- **0o666**: The owner, user group, and other users have the read and write permissions on the file.
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| **Return value** | Type | Description | | --------------------- | ----------- | | Promise<number> | Promise used to return the file descriptor of the file opened.| **Example** ```js fileio.open(path, 0o1, 0o0200).then(function(number){ console.info("File opened"); }).catch(function(err){ console.info("Failed to open the file. Error:"+ err); }); ``` ## fileio.open7+ open(path: string, flags: number, mode: number, callback: AsyncCallback<number>): void Opens a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------- | ---- | ------------------------------------------------------------ | | path | string | Yes | Application sandbox path of the file. | | flags | number | Yes | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.
- **0o0**: Open the file in read-only mode.
- **0o1**: Open the file in write-only mode.
- **0o2**: Open the file in read/write mode.
In addition, you can specify the following options, separated using a bitwise OR operator (|). By default, no additional option is specified.
- **0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.
- **0o200**: If **0o100** is added and the file already exists, throw an exception.
- **0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.
- **0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).
- **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.
- **0o200000**: If **path** does not point to a directory, throw an exception.

- **0o400000**: If **path** points to a symbolic link, throw an exception.
- **0o4010000**: Open the file in synchronous I/O mode.| | mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o666**.
- **0o666**: The owner, user group, and other users have the read and write permissions on the file.
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| | callback | AsyncCallback <void> | Yes | Callback invoked when the file is open asynchronously. | **Example** ```js fileio.open(path, 0, function(err, fd) { // Do something. }); ``` ## fileio.openSync openSync(path:string, flags?:number, mode?:number): number Synchronously opens a file. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | 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.
- **0o0**: Open the file in read-only mode.
- **0o1**: Open the file in write-only mode.
- **0o2**: Open the file in read/write mode.
In addition, you can specify the following options, separated using a bitwise OR operator (|). By default, no additional option is specified.
- **0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.
- **0o200**: If **0o100** is added and the file already exists, throw an exception.
- **0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.
- **0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).
- **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.
- **0o200000**: If **path** does not point to a directory, throw an exception.

- **0o400000**: If **path** points to a symbolic link, throw an exception.
- **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**.
- **0o666**: The owner, user group, and other users have the read and write permissions on the file.
- **0o640**: The owner has the read and write permissions, and the user group has the read permission.
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.
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 let fd = fileio.openSync(path, 0o102, 0o640); ``` ```js let fd = fileio.openSync(path, 0o102, 0o666); fileio.writeSync(fd, 'hello world'); let fd1 = fileio.openSync(path, 0o2002); fileio.writeSync(fd1, 'hello world'); let num = fileio.readSync(fd1, new ArrayBuffer(4096), {position: 0}); console.info("num == " + num); ``` ## fileio.read read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): Promise<ReadOut> Reads data from a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | ------- | ----------- | ---- | ------------------------------------------------------------ | | fd | number | Yes | File descriptor of the file to read. | | buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | | options | Object | No | The options are as follows:
- **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**.
- **length** (number): length of the data to read. The default value is the buffer length minus the offset.
- **position** (number): position of the data to read in the file. By default, data is read from the current position.
Constraints: offset + length <= Buffer size| **Return value** | Type | Description | | ---------------------------------- | ------ | | Promise<[ReadOut](#readout)> | Promise used to return the data read.| **Example** ```js let fd = fileio.openSync(path, 0o2); let buf = new ArrayBuffer(4096); fileio.read(fd, buf).then(function(readOut){ console.info("Read file data"); console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); }).catch(function(err){ console.info("Failed to read file data. Error:"+ err); }); ``` ## fileio.read read(fd: number, buffer: ArrayBuffer, options: { offset?: number; length?: number; position?: number; }, callback: AsyncCallback<ReadOut>): void Reads data from a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ---------------------------------------- | | fd | number | Yes | File descriptor of the file to read. | | buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | | options | Object | No | The options are as follows:
- **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**.
- **length** (number): length of the data to read. The default value is the buffer length minus the offset.
- **position** (number): position of the data to read in the file. By default, data is read from the current position.
Constraints: offset + length <= Buffer size | | callback | AsyncCallback<[ReadOut](#readout)> | Yes | Callback invoked when the data is read asynchronously. | **Example** ```js let fd = fileio.openSync(path, 0o2); let buf = new ArrayBuffer(4096); fileio.read(fd, buf, function (err, readOut) { if (readOut) { console.info("Read file data"); console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); } }); ``` ## fileio.readSync readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): number Synchronously reads data from a file. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ------- | ----------- | ---- | ---------------------------------------- | | fd | number | Yes | File descriptor of the file to read. | | buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | | options | Object | No | The options are as follows:
- **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**.
- **length** (number): length of the data to read. The default value is the buffer length minus the offset.
- **position** (number): position of the data to read in the file. By default, data is read from the current position.
Constraints: offset + length <= Buffer size | **Return value** | Type | Description | | ------ | -------- | | number | Length of the data read.| **Example** ```js let fd = fileio.openSync(path, 0o2); let buf = new ArrayBuffer(4096); let num = fileio.readSync(fd, buf); ``` ## fileio.rmdir7+ rmdir(path: string): Promise<void> Deletes a directory. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the directory.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js fileio.rmdir(path).then(function() { console.info("Directory deleted"); }).catch(function(err){ console.info("Failed to delete the directory. Error:"+ err); }); ``` ## fileio.rmdir7+ rmdir(path: string, callback:AsyncCallback<void>): void Deletes a directory. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the directory.| | callback | AsyncCallback<void> | Yes | Callback invoked when the directory is deleted asynchronously. | **Example** ```js fileio.rmdir(path, function(err){ // Do something. console.info("Directory deleted"); }); ``` ## fileio.rmdirSync7+ rmdirSync(path: string): void Synchronously deletes a directory. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the directory.| **Example** ```js fileio.rmdirSync(path); ``` ## fileio.unlink unlink(path:string): Promise<void> Deletes a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js fileio.unlink(path).then(function(){ console.info("File deleted"); }).catch(function(error){ console.info("Failed to delete the file. Error:"+ error); }); ``` ## fileio.unlink unlink(path:string, callback:AsyncCallback<void>): void Deletes a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| | callback | AsyncCallback<void> | Yes | Callback invoked when the file is deleted asynchronously. | **Example** ```js fileio.unlink(path, function(err) { console.info("File deleted"); }); ``` ## fileio.unlinkSync unlinkSync(path: string): void Synchronously deletes a file. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| **Example** ```js fileio.unlinkSync(path); ``` ## fileio.write write(fd: number, buffer: ArrayBuffer | string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise<number> Writes data into a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ------- | ------------------------------- | ---- | ---------------------------------------- | | 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. | | options | Object | No | The options are as follows:
- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.
- **length** (number): length of the data to write. The default value is the buffer length minus the offset.
- **position** (number): start position to write the data in the file. By default, data is written from the current position.
- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.
Constraints: offset + length <= Buffer size| **Return value** | Type | Description | | --------------------- | -------- | | Promise<number> | Promise used to return the length of the data written.| **Example** ```js let fd = fileio.openSync(path, 0o100 | 0o2, 0o666); fileio.write(fd, "hello, world").then(function(number){ console.info("Data written to the file. Size is:"+ number); }).catch(function(err){ console.info("Failed to write data to the file. Error:"+ err); }); ``` ## fileio.write write(fd: number, buffer: ArrayBuffer | string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback<number>): void Writes data into a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------------- | ---- | ---------------------------------------- | | 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. | | options | Object | No | The options are as follows:
- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.
- **length** (number): length of the data to write. The default value is the buffer length minus the offset.
- **position** (number): start position to write the data in the file. By default, data is written from the current position.
- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.
Constraints: offset + length <= Buffer size| | callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | **Example** ```js let fd = fileio.openSync(path, 0o100 | 0o2, 0o666); fileio.write(fd, "hello, world", function (err, bytesWritten) { if (bytesWritten) { console.info("Data written to the file. Size is:"+ bytesWritten); } }); ``` ## fileio.writeSync writeSync(fd: number, buffer: ArrayBuffer | string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number Synchronously writes data into a file. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ------- | ------------------------------- | ---- | ---------------------------------------- | | 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. | | options | Object | No | The options are as follows:
- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.
- **length** (number): length of the data to write. The default value is the buffer length minus the offset.
- **position** (number): start position to write the data in the file. By default, data is written from the current position.
- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.
Constraints: offset + length <= Buffer size| **Return value** | Type | Description | | ------ | -------- | | number | Length of the data written in the file.| **Example** ```js let fd = fileio.openSync(path, 0o100 | 0o2, 0o666); let num = fileio.writeSync(fd, "hello, world"); ``` ## fileio.hash hash(path: string, algorithm: string): Promise<string> Calculates the hash value of a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | --------- | ------ | ---- | ------------------------------------------------------------ | | 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.| **Return value** | Type | Description | | --------------------- | -------------------------- | | 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); }).catch(function(err){ console.info("Failed to calculate the file hash. Error:"+ err); }); ``` ## fileio.hash hash(path: string, algorithm: string, callback: AsyncCallback<string>): void Calculates the hash value of a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | --------- | --------------------------- | ---- | ------------------------------------------------------------ | | 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.| | 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) { console.info("Calculated file hash:"+ hashStr); } }); ``` ## fileio.chmod7+ chmod(path: string, mode: number):Promise<void> Changes file permissions. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | 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 (|).
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js fileio.chmod(path, 0o700).then(function() { console.info("File permissions changed"); }).catch(function(err){ console.info("Failed to change file permissions. Error:"+ err); }); ``` ## fileio.chmod7+ chmod(path: string, mode: number, callback: AsyncCallback<void>): void Changes file permissions. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ------------------------------------------------------------ | | 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 (|).
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| | callback | AsyncCallback<void> | Yes | Callback invoked when the file permissions are changed asynchronously. | **Example** ```js fileio.chmod(path, 0o700, function (err) { // Do something. }); ``` ## fileio.chmodSync7+ chmodSync(path: string, mode: number): void Synchronously changes file permissions. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | 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 (|).
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| **Example** ```js fileio.chmodSync(path, 0o700); ``` ## fileio.fstat7+ fstat(fd: number): Promise<Stat> Obtains file information based on the file descriptor. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the target file.| **Return value** | Type | Description | | ---------------------------- | ---------- | | Promise<[Stat](#stat)> | Promise used to return the file information.| **Example** ```js let fd = fileio.openSync(path); fileio.fstat(fd).then(function(stat){ console.info("Obtained file info:"+ JSON.stringify(stat)); }).catch(function(err){ console.info("Failed to obtain file info. Error:"+ err); }); ``` ## fileio.fstat7+ fstat(fd: number, callback: AsyncCallback<Stat>): void Obtains file information based on the file descriptor. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------- | ---- | ---------------- | | fd | number | Yes | File descriptor of the target file. | | callback | AsyncCallback<[Stat](#stat)> | Yes | Callback invoked to return the file information obtained.| **Example** ```js let fd = fileio.openSync(path); fileio.fstat(fd, function (err) { // Do something. }); ``` ## fileio.fstatSync7+ fstatSync(fd: number): Stat Synchronously obtains file information based on the file descriptor. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the target file.| **Return value** | Type | Description | | ------------- | ---------- | | [Stat](#stat) | File information obtained.| **Example** ```js let fd = fileio.openSync(path); let stat = fileio.fstatSync(fd); ``` ## fileio.ftruncate7+ ftruncate(fd: number, len?: number): Promise<void> Truncates a file based on the file descriptor. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ---------------- | | fd | number | Yes | File descriptor of the file to truncate. | | len | number | No | File length, in bytes, after truncation.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let fd = fileio.openSync(path); fileio.ftruncate(fd, 5).then(function(err) { console.info("File truncated"); }).catch(function(err){ console.info("Failed to truncate the file. Error:"+ err); }); ``` ## fileio.ftruncate7+ ftruncate(fd: number, len: number, callback:AsyncCallback<void>): void Truncates a file based on the file descriptor. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------- | ---- | ---------------- | | fd | number | Yes | File descriptor of the file to truncate. | | len | number | Yes | File length, in bytes, after truncation.| | callback | AsyncCallback<void> | Yes | Callback that returns no value. | **Example** ```js let fd = fileio.openSync(path); let len = 5; fileio.ftruncate(fd, 5, function(err){ // Do something. }); ``` ## fileio.ftruncateSync7+ ftruncateSync(fd: number, len?: number): void Synchronously truncates a file based on the file descriptor. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ---------------- | | fd | number | Yes | File descriptor of the file to truncate. | | len | number | No | File length, in bytes, after truncation.| **Example** ```js let fd = fileio.openSync(path); let len = 5; fileio.ftruncateSync(fd, len); ``` ## fileio.truncate7+ truncate(path: string, len?: number): Promise<void> Truncates a file based on the file path. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------------- | | path | string | Yes | Application sandbox path of the file to truncate.| | len | number | No | File length, in bytes, after truncation.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let len = 5; fileio.truncate(path, len).then(function(){ console.info("File truncated"); }).catch(function(err){ console.info("Failed to truncate the file. Error:"+ err); }); ``` ## fileio.truncate7+ truncate(path: string, len: number, callback:AsyncCallback<void>): void Truncates a file based on the file path. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | -------------------------------- | | path | string | Yes | Application sandbox path of the file to truncate.| | len | number | Yes | File length, in bytes, after truncation.| | callback | AsyncCallback<void> | Yes | Callback that returns no value. | **Example** ```js let len = 5; fileio.truncate(path, len, function(err){ // Do something. }); ``` ## fileio.truncateSync7+ truncateSync(path: string, len?: number): void Synchronously truncates a file based on the file path. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------------- | | path | string | Yes | Application sandbox path of the file to truncate.| | len | number | No | File length, in bytes, after truncation.| **Example** ```js let len = 5; fileio.truncateSync(path, len); ``` ## fileio.readText7+ readText(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): Promise<string> Reads the text content of a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ------------------------------------------------------------ | | filePath | string | Yes | Application sandbox path of the file to read. | | options | Object | No | The options are as follows:
- **position** (number): position of the data to read in the file. By default, data is read from the current position.
- **length** (number): length of the data to read. The default value is the buffer length minus the offset.
- **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 text:"+ str); }).catch(function(err){ console.info("Failed to read the text. Error:"+ err); }); ``` ## fileio.readText7+ readText(filePath: string, options: { position?: number; length?: number; encoding?: string; }, callback: AsyncCallback<string>): void Reads the text content of a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------- | ---- | ------------------------------------------------------------ | | filePath | string | Yes | Application sandbox path of the file to read. | | options | Object | Yes | The options are as follows:
- **position** (number): position of the data to read in the file. By default, data is read from the current position.
- **length** (number): length of the data to read. The default value is the buffer length minus the offset.
-  **encoding**: format of the string to be encoded. The default value is  **utf-8**, which is the only value supported.| | callback | AsyncCallback<string> | Yes | Callback used to return the content read. | **Example** ```js fileio.readText(path, { position: 1, encoding: 'UTF-8' }, function(err, str){ // Do something. }); ``` ## fileio.readTextSync7+ readTextSync(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): string Synchronously reads the text of a file. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ------------------------------------------------------------ | | filePath | string | Yes | Application sandbox path of the file to read. | | options | Object | No | The options are as follows:
- **position** (number): position of the data to read in the file. By default, data is read from the current position.
- **length** (number): length of the data to read. The default value is the buffer length minus the offset.
- **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.| **Example** ```js let str = fileio.readTextSync(path, {position: 1, length: 3}); ``` ## fileio.lstat7+ lstat(path: string): Promise<Stat> Obtains link information. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------------------- | | path | string | Yes | Application sandbox path of the target file.| **Return value** | Type | Description | | ---------------------------- | ---------- | | Promise<[Stat](#stat)> | Promise used to return the link information obtained. For details, see [Stat](#stat).| **Example** ```js fileio.lstat(path).then(function(stat){ console.info("Got link info:"+ JSON.stringify(stat)); }).catch(function(err){ console.info("Failed to obtain link info. Error:"+ err); }); ``` ## fileio.lstat7+ lstat(path:string, callback:AsyncCallback<Stat>): void Obtains link information. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ---------------------------------- | ---- | -------------------------------------- | | path | string | Yes | Application sandbox path of the target file.| | callback | AsyncCallback<[Stat](#stat)> | Yes | Callback used to return the link information obtained. | **Example** ```js fileio.lstat(path, function (err, stat) { // Do something. }); ``` ## fileio.lstatSync7+ lstatSync(path:string): Stat Synchronously obtains the link information. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------------------- | | path | string | Yes | Application sandbox path of the target file.| **Return value** | Type | Description | | ------------- | ---------- | | [Stat](#stat) | Link information obtained.| **Example** ```js let stat = fileio.lstatSync(path); ``` ## fileio.rename7+ rename(oldPath: string, newPath: string): Promise<void> Renames a file. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | ------- | ------ | ---- | ---------------------------- | | oldPath | string | Yes | Application sandbox path of the file to rename.| | newPath | String | Yes | Application sandbox path of the file renamed. | **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let oldPath = path; let newPath = oldPath + '123'; fileio.rename(oldPath, newPath).then(function() { console.info("File renamed"); }).catch(function(err){ console.info("Failed to rename the file. Error:"+ err); }); ``` ## fileio.rename7+ rename(oldPath: string, newPath: string, callback: AsyncCallback<void>): void Renames a file. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ---------------------------- | | oldPath | string | Yes | Application sandbox path of the file to rename.| | newPath | String | Yes | Application sandbox path of the file renamed. | | Callback | AsyncCallback<void> | Yes | Callback invoked when the file is asynchronously renamed. | **Example** ```js let oldPath = path; let newPath = oldPath + '123'; fileio.rename(oldPath, newPath, function(err){ }); ``` ## fileio.renameSync7+ renameSync(oldPath: string, newPath: string): void Synchronously renames a file. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | ------- | ------ | ---- | ---------------------------- | | oldPath | string | Yes | Application sandbox path of the file to rename.| | newPath | String | Yes | Application sandbox path of the file renamed. | **Example** ```js let oldPath = path; let newPath = oldPath + '123'; fileio.renameSync(oldPath, newPath); ``` ## fileio.fsync7+ fsync(fd: number): Promise<void> Flushes data of a file to disk. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the file to flush.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let fd = fileio.openSync(path); fileio.fsync(fd).then(function(){ console.info("Data flushed"); }).catch(function(err){ console.info("Failed to flush data. Error:"+ err); }); ``` ## fileio.fsync7+ fsync(fd: number, callback: AsyncCallback<void>): void Flushes data of a file to disk. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------- | ---- | --------------- | | fd | number | Yes | File descriptor of the file to flush. | | Callback | AsyncCallback<void> | Yes | Callback invoked when the file is synchronized in asynchronous mode.| **Example** ```js let fd = fileio.openSync(path); fileio.fsync(fd, function(err){ // Do something. }); ``` ## fileio.fsyncSync7+ fsyncSync(fd: number): void Flushes data of a file to disk in synchronous mode. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the file to flush.| **Example** ```js let fd = fileio.openSync(path); fileio.fsyncSync(fd); ``` ## fileio.fdatasync7+ fdatasync(fd: number): Promise<void> Flushes data of a file to disk. This API uses a promise to return the result. **fdatasync()** is similar to **fsync()**, but does not flush modified metadata unless that metadata is needed. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the file to flush.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let fd = fileio.openSync(path); fileio.fdatasync(fd).then(function(err) { console.info("Data flushed"); }).catch(function(err){ console.info("Failed to flush data. Error:"+ err); }); ``` ## fileio.fdatasync7+ fdatasync(fd: number, callback:AsyncCallback<void>): void Flushes data of a file to disk. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------------- | ---- | ----------------- | | fd | number | Yes | File descriptor of the file to synchronize. | | callback | AsyncCallback <void> | Yes | Callback invoked when the file data is synchronized in asynchronous mode.| **Example** ```js let fd = fileio.openSync(path); fileio.fdatasync (fd, function (err) { // Do something. }); ``` ## fileio.fdatasyncSync7+ fdatasyncSync(fd: number): void Synchronizes data in a file in synchronous mode. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the file to flush.| **Example** ```js let fd = fileio.openSync(path); let stat = fileio.fdatasyncSync(fd); ``` ## fileio.symlink7+ symlink(target: string, srcPath: string): Promise<void> Creates a symbolic link based on the file path. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | ------- | ------ | ---- | ---------------------------- | | target | string | Yes | Application sandbox path of the target file. | | srcPath | string | Yes | Application sandbox path of the symbolic link file.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let target = path; let srcPath = target + 'aaa'; fileio.symlink(target, srcPath).then(function() { console.info("Symbolic link created"); }).catch(function(err){ console.info("Failed to create the symbolic link. Error:"+ err); }); ``` ## fileio.symlink7+ symlink(target: string, srcPath: string, callback: AsyncCallback<void>): void Creates a symbolic link based on the file path. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | -------------------------------- | | target | string | Yes | Application sandbox path of the target file. | | srcPath | string | Yes | Application sandbox path of the symbolic link file. | | callback | AsyncCallback<void> | Yes | Callback invoked when the symbolic link is created asynchronously.| **Example** ```js let target = path; let srcPath = target + 'aaa'; fileio.symlink(target, srcPath, function (err) { // Do something. }); ``` ## fileio.symlinkSync7+ symlinkSync(target: string, srcPath: string): void Synchronously creates a symbolic link based on a specified path. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | ------- | ------ | ---- | ---------------------------- | | target | string | Yes | Application sandbox path of the target file. | | srcPath | string | Yes | Application sandbox path of the symbolic link file.| **Example** ```js let target = path; let srcPath = target + 'aaa'; fileio.symlinkSync(target, srcPath); ``` ## fileio.chown7+ chown(path: string, uid: number, gid: number): Promise<void> Changes the file owner based on the file path. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| | uid | number | Yes | New user ID (UID). | | gid | number | Yes | New group ID (GID). | **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let stat = fileio.statSync(path); fileio.chown(path, stat.uid, stat.gid).then(function(){ console.info("File owner changed"); }).catch(function(err){ console.info("Failed to change the file owner. Error:"+ err); }); ``` ## fileio.chown7+ chown(path: string, uid: number, gid: number, callback: AsyncCallback<void>): void Changes the file owner based on the file path. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ------------------------------ | | path | string | Yes | Application sandbox path of the file. | | uid | number | Yes | New UID. | | gid | number | Yes | New GID. | | callback | AsyncCallback<void> | Yes | Callback invoked when the file owner is changed asynchronously.| **Example** ```js let stat = fileio.statSync(path) fileio.chown(path, stat.uid, stat.gid, function (err){ // Do something. }); ``` ## fileio.chownSync7+ chownSync(path: string, uid: number, gid: number): void Synchronously changes the file owner based on its path. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| | uid | number | Yes | New UID. | | gid | number | Yes | New GID. | **Example** ```js let stat = fileio.statSync(path) fileio.chownSync(path, stat.uid, stat.gid); ``` ## fileio.mkdtemp7+ mkdtemp(prefix: string): Promise<string> Creates a temporary directory. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ------ | ------ | ---- | --------------------------- | | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| **Return value** | Type | Description | | --------------------- | ---------- | | Promise<string> | Promise used to return the unique directory generated.| **Example** ```js fileio.mkdtemp(path + "XXXX").then(function(path){ console.info("Temporary directory created:"+ path); }).catch(function(err){ console.info("Failed to create the temporary directory. Error:"+ err); }); ``` ## fileio.mkdtemp7+ mkdtemp(prefix: string, callback: AsyncCallback<string>): void Creates a temporary directory. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | --------------------------- | | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| | callback | AsyncCallback<string> | Yes | Callback invoked when a temporary directory is created asynchronously. | **Example** ```js fileio.mkdtemp(path + "XXXX", function (err, res) { // Do something. }); ``` ## fileio.mkdtempSync7+ mkdtempSync(prefix: string): string Synchronously creates a temporary directory. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ------ | ------ | ---- | --------------------------- | | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| **Return value** | Type | Description | | ------ | ---------- | | string | Unique path generated.| **Example** ```js let res = fileio.mkdtempSync(path + "XXXX"); ``` ## fileio.fchmod7+ fchmod(fd: number, mode: number): Promise<void> Changes file permissions based on the file descriptor. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ---------------------------------------- | | 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 (|).
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let fd = fileio.openSync(path); let mode = 0o700; fileio.fchmod(fd, mode).then(function() { console.info("File permissions changed"); }).catch(function(err){ console.info("Failed to change file permissions. Error:"+ err); }); ``` ## fileio.fchmod7+ fchmod(fd: number, mode: number, callback: AsyncCallback<void>): void Changes file permissions based on the file descriptor. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------------- | ---- | ---------------------------------------- | | 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 (|).
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| | callback | AsyncCallback <void> | Yes | Callback invoked when the file permissions are changed asynchronously. | **Example** ```js let fd = fileio.openSync(path); let mode = 0o700; fileio.fchmod(fd, mode, function (err) { // Do something. }); ``` ## fileio.fchmodSync7+ fchmodSync(fd: number, mode: number): void Synchronously changes the file permissions based on the file descriptor. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ---------------------------------------- | | 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 (|).
- **0o700**: The owner has the read, write, and execute permissions.
-  **0o400**: The owner has the read permission.
- **0o200**: The owner has the write permission.
- **0o100**: The owner has the execute permission.
- **0o070**: The user group has the read, write, and execute permissions.
- **0o040**: The user group has the read permission.
- **0o020**: The user group has the write permission.
- **0o010**: The user group has the execute permission.
- **0o007**: Other users have the read, write, and execute permissions.
- **0o004**: Other users have the read permission.
- **0o002**: Other users have the write permission.
- **0o001**: Other users have the execute permission.| **Example** ```js let fd = fileio.openSync(path); let mode = 0o700; fileio.fchmodSync(fd, mode); ``` ## fileio.createStream7+ createStream(path: string, mode: string): Promise<Stream> Opens a file stream based on the file path. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | path | string | Yes | Application sandbox path of the file. | | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| **Return value** | Type | Description | | --------------------------------- | --------- | | Promise<[Stream](#stream7)> | Promise used to return the result.| **Example** ```js fileio.createStream(path, "r+").then(function(stream){ console.info("Stream opened"); }).catch(function(err){ console.info("Failed to create the stream. Error:"+ err); }); ``` ## fileio.createStream7+ createStream(path: string, mode: string, callback: AsyncCallback<Stream>): void Opens a file stream based on the file path. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | | path | string | Yes | Application sandbox path of the file. | | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| | callback | AsyncCallback<[Stream](#stream7)> | Yes | Callback invoked when the stream is open asynchronously. | **Example** ```js fileio.createStream(path, "r+", function(err, stream){ // Do something. }); ``` ## fileio.createStreamSync7+ createStreamSync(path: string, mode: string): Stream Synchronously opens a stream based on the file path. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | path | string | Yes | Application sandbox path of the file. | | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| **Return value** | Type | Description | | ------------------ | --------- | | [Stream](#stream7) | Stream opened.| **Example** ```js let ss = fileio.createStreamSync(path, "r+"); ``` ## fileio.fdopenStream7+ fdopenStream(fd: number, mode: string): Promise<Stream> Opens a file stream based on the file descriptor. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ---------------------------------------- | | fd | number | Yes | File descriptor of the target file. | | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| **Return value** | Type | Description | | --------------------------------- | --------- | | Promise<[Stream](#stream7)> | Promise used to return the result.| **Example** ```js let fd = fileio.openSync(path); fileio.fdopenStream(fd, "r+").then(function(stream){ console.info("Stream opened"); }).catch(function(err){ console.info("Failed to open the stream. Error:"+ err); }); ``` ## fileio.fdopenStream7+ fdopenStream(fd: number, mode: string, callback: AsyncCallback<Stream>): void Opens a file stream based on the file descriptor. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ---------------------------------------- | | fd | number | Yes | File descriptor of the target file. | | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| | callback | AsyncCallback <[Stream](#stream7)> | Yes | Callback invoked when the stream is open asynchronously. | **Example** ```js let fd = fileio.openSync(path); fileio.fdopenStream(fd, "r+", function (err, stream) { // Do something. }); ``` ## fileio.fdopenStreamSync7+ fdopenStreamSync(fd: number, mode: string): Stream Synchronously opens a stream based on the file descriptor. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ---------------------------------------- | | fd | number | Yes | File descriptor of the target file. | | mode | string | Yes | - **r**: Open a file for reading. The file must exist.
- **r+**: Open a file for both reading and writing. The file must exist.
- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| **Return value** | Type | Description | | ------------------ | --------- | | [Stream](#stream7) | Stream opened.| **Example** ```js let fd = fileio.openSync(path); let ss = fileio.fdopenStreamSync(fd, "r+"); ``` ## fileio.fchown7+ fchown(fd: number, uid: number, gid: number): Promise<void> Changes the file owner based on the file descriptor. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the target file.| | uid | number | Yes | New UID. | | gid | number | Yes | New GID. | **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let fd = fileio.openSync(path); let stat = fileio.statSync(path); fileio.fchown(fd, stat.uid, stat.gid).then(function() { console.info("File owner changed"); }).catch(function(err){ console.info("Failed to change the file owner. Error:"+ err); }); ``` ## fileio.fchown7+ fchown(fd: number, uid: number, gid: number, callback: AsyncCallback<void>): void Changes the file owner based on the file descriptor. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------- | ---- | --------------- | | fd | number | Yes | File descriptor of the target file. | | uid | number | Yes | New UID. | | gid | number | Yes | New GID. | | callback | AsyncCallback<void> | Yes | Callback invoked when the file owner is changed asynchronously.| **Example** ```js let fd = fileio.openSync(path); let stat = fileio.statSync(path); fileio.fchown(fd, stat.uid, stat.gid, function (err){ // Do something. }); ``` ## fileio.fchownSync7+ fchownSync(fd: number, uid: number, gid: number): void Synchronously changes the file owner based on the file descriptor. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ------------ | | fd | number | Yes | File descriptor of the target file.| | uid | number | Yes | New UID. | | gid | number | Yes | New GID. | **Example** ```js let fd = fileio.openSync(path); let stat = fileio.statSync(path); fileio.fchownSync(fd, stat.uid, stat.gid); ``` ## fileio.lchown7+ lchown(path: string, uid: number, gid: number): Promise<void> Changes the file owner (owner of the symbolic link, not the file referred to by the symbolic link) based on the file path. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| | uid | number | Yes | New UID. | | gid | number | Yes | New GID. | **Return value** | Type | Description | | ------------------- | ---------------------------- | | Promise<void> | Promise that returns no value.| **Example** ```js let stat = fileio.statSync(path); fileio.lchown(path, stat.uid, stat.gid).then(function() { console.info("File owner changed"); }).catch(function(err){ console.info("Failed to change the file owner. Error:"+ err); }); ``` ## fileio.lchown7+ lchown(path: string, uid: number, gid: number, callback: AsyncCallback<void>): void Changes the file owner (owner of the symbolic link, not the file referred to by the symbolic link) based on the file path. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------- | ---- | ------------------------------ | | path | string | Yes | Application sandbox path of the file. | | uid | number | Yes | New UID. | | gid | number | Yes | New GID. | | callback | AsyncCallback<void> | Yes | Callback invoked when the file owner is changed asynchronously.| **Example** ```js let stat = fileio.statSync(path); fileio.lchown(path, stat.uid, stat.gid, function (err){ // Do something. }); ``` ## fileio.lchownSync7+ lchownSync(path: string, uid: number, gid: number): void Synchronously changes the file owner based on the file path and changes the owner of the symbolic link (not the referenced file). **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | path | string | Yes | Application sandbox path of the file.| | uid | number | Yes | New UID. | | gid | number | Yes | New GID. | **Example** ```js let stat = fileio.statSync(path); fileio.lchownSync(path, stat.uid, stat.gid); ``` ## fileio.createWatcher7+ createWatcher(filename: string, events: number, callback: AsyncCallback<number>): Watcher Listens for file or directory changes. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------- | ---- | ------------------------------------------------------------ | | filename | string | Yes | Application sandbox path of the file. | | events | Number | Yes | - **1**: The file or directory is renamed.
- **2**: The file or directory is modified.
- **3**: The file or directory is modified and renamed.| | callback | AsyncCallback<number > | Yes | Called each time a change is detected. | **Return value** | Type | Description | | -------------------- | ---------- | | [Watcher](#watcher7) | Promise used to return the **Watcher** instance.| **Example** ```js let filename = path +"/test.txt"; fileio.createWatcher(filename, 1, function(number){ console.info("Monitoring times: "+number); }); ``` ## Readout Obtains the file read result. This class applies only to the **read()** method. **System capability**: SystemCapability.FileManagement.File.FileIO | Name | Type | Readable | Writable | Description | | --------- | ---------- | ---- | ---- | ----------------- | | bytesRead | number | Yes | Yes | Length of the data read. | | offset | number | Yes | Yes | Position of the buffer to which the data will be read in reference to the start address of the buffer.| | buffer | ArrayBufer | Yes | Yes | Buffer for storing the data read. | ## Stat Provides detailed file information. Before calling a method of the **Stat** class, use the [stat()](#fileiostat) method synchronously or asynchronously to create a **Stat** instance. **System capability**: SystemCapability.FileManagement.File.FileIO ### Attributes | Name | Type | Readable | Writable | Description | | ------ | ------ | ---- | ---- | ---------------------------------------- | | 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:
- **0o170000**: mask used to obtain the file type.
- **0o140000**: The file is a socket.
- **0o120000**: The file is a symbolic link.
- **0o100000**: The file is a regular file.
- **0o060000**: The file is a block device.
- **0o040000**: The file is a directory.
- **0o020000**: The file is a character device.
- **0o010000**: The file is a named pipe (FIFO).
- **0o0700**: mask used to obtain the owner permissions.
- **0o0400**: The owner has the permission to read a regular file or a directory entry.
- **0o0200**: The owner has the permission to write a regular file or create and delete a directory entry.
- **0o0100**: The owner has the permission to execute a regular file or search for the specified path in a directory.
- **0o0070**: mask used to obtain the user group permissions.
- **0o0040**: The user group has the permission to read a regular file or a directory entry.
- **0o0020**: The user group has the permission to write a regular file or create and delete a directory entry.
- **0o0010**: The user group has the permission to execute a regular file or search for the specified path in a directory.
- **0o0007**: mask used to obtain the permissions of other users.
- **0o0004**: Other users have the permission to read a regular file or a directory entry.
- **0o0002**: Other users have the permission to write a regular file or create and delete a directory entry.
- **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. | | rdev | number | Yes | No | Minor device number. | | size | number | Yes | No | File size, in bytes. This parameter is valid only for regular files. | | blocks | number | Yes | No | Number of blocks occupied by a file. Each block is 512 bytes. | | atime | number | Yes | No | Time of the last access to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970. | | mtime | number | Yes | No | Time of the last modification to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970. | | ctime | number | Yes | No | Time of the last status change of the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970. | ### isBlockDevice isBlockDevice(): boolean Checks whether this file is a block special file. A block special file supports access by block only, and it is cached when accessed. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | ---------------- | | boolean | Whether the file is a block special file.| **Example** ```js let isBLockDevice = fileio.statSync(path).isBlockDevice(); ``` ### isCharacterDevice isCharacterDevice(): boolean Checks whether this file is a character special file. A character special file supports random access, and it is not cached when accessed. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | ----------------- | | boolean | Whether the file is a character special file.| **Example** ```js let isCharacterDevice = fileio.statSync(path).isCharacterDevice(); ``` ### isDirectory isDirectory(): boolean Checks whether this file is a directory. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | ------------- | | boolean | Whether the file is a directory.| **Example** ```js let isDirectory = fileio.statSync(path).isDirectory(); ``` ### isFIFO isFIFO(): boolean Checks whether this file is a named pipe (or FIFO). Named pipes are used for inter-process communication. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | --------------------- | | boolean | Whether the file is an FIFO.| **Example** ```js let isFIFO = fileio.statSync(path).isFIFO(); ``` ### isFile isFile(): boolean Checks whether this file is a regular file. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | --------------- | | boolean | Whether the file is a regular file.| **Example** ```js let isFile = fileio.statSync(path).isFile(); ``` ### isSocket isSocket(): boolean Checks whether this file is a socket. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | -------------- | | boolean | Whether the file is a socket.| **Example** ```js let isSocket = fileio.statSync(path).isSocket(); ``` ### isSymbolicLink isSymbolicLink(): boolean Checks whether this file is a symbolic link. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | --------------- | | boolean | Whether the file is a symbolic link.| **Example** ```js let isSymbolicLink = fileio.statSync(path).isSymbolicLink(); ``` ## Watcher7+ Listens for the changes of a file. You can call the **Watcher.stop()** method synchronously or asynchronously to stop the listening. ### stop7+ stop(): Promise<void> Stops the **watcher** instance. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Example** ```js let filename = path +"/test.txt"; let watcher = await fileio.createWatcher(filename, 1, function(number){ console.info("Monitoring times: "+number); }); watcher.stop().then(function(){ console.info("Watcher stopped"); }); ``` ### stop7+ stop(callback: AsyncCallback<void>): void Stops the **watcher** instance. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------- | ---- | ---------------------- | | callback | AsyncCallback<void> | Yes | Callback invoked when **watcher** is stopped asynchronously.| **Example** ```js let filename = path +"/test.txt"; let watcher = await fileio.createWatcher(filename, 1, function(number){ console.info("Monitoring times: "+number); }); watcher.stop(function(){ console.info("Watcher stopped"); }) ``` ## Stream Provides file stream management. Before calling a method of the **Stream** class, use the **createStream()** method synchronously or asynchronously to create a **Stream** instance. ### close7+ close(): Promise<void> Closes the stream. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------------------- | ------------- | | Promise<void> | Promise used to return the stream close result.| **Example** ```js let ss= fileio.createStreamSync(path, "r+"); ss.close().then(function(){ console.info("File stream closed"); }).catch(function(err){ console.info("Failed to close the file stream. Error:"+ err); }); ``` ### close7+ close(callback: AsyncCallback<void>): void Closes the stream. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------- | ---- | ------------- | | callback | AsyncCallback<void> | Yes | Callback invoked when the stream is closed asynchronously.| **Example** ```js let ss= fileio.createStreamSync(path, "r+"); ss.close(function (err) { // Do something }); ``` ### closeSync closeSync(): void Synchronously closes the stream. **System capability**: SystemCapability.FileManagement.File.FileIO **Example** ```js let ss= fileio.createStreamSync(path, "r+"); ss.closeSync(); ``` ### flush7+ flush(): Promise<void> Flushes the stream. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------------------- | ------------- | | Promise<void> | Promise used to return the stream flushing result.| **Example** ```js let ss= fileio.createStreamSync(path, "r+"); ss.flush().then(function (){ console.info("Stream flushed"); }).catch(function(err){ console.info("Failed to flush the stream. Error:"+ err); }); ``` ### flush7+ flush(callback: AsyncCallback<void>): void Flushes the stream. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------- | ---- | -------------- | | callback | AsyncCallback<void> | Yes | Callback invoked when the stream is asynchronously flushed.| **Example** ```js let ss= fileio.createStreamSync(path, "r+"); ss.flush(function (err) { // Do something }); ``` ### flushSync7+ flushSync(): void Synchronously flushes the stream. **System capability**: SystemCapability.FileManagement.File.FileIO **Example** ```js let ss= fileio.createStreamSync(path, "r+"); ss.flushSync(); ``` ### write7+ write(buffer: ArrayBuffer | string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise<number> Writes data into the stream. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ------- | ------------------------------- | ---- | ---------------------------------------- | | buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | | options | Object | No | The options are as follows:
- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.
- **length** (number): length of the data to write. The default value is the buffer length minus the offset.
- **position** (number): start position to write the data in the file. By default, data is written from the current position.
- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.
Constraints: offset + length <= Buffer size | **Return value** | Type | Description | | --------------------- | -------- | | Promise<number> | Promise used to return the length of the data written.| **Example** ```js let ss= fileio.createStreamSync(path, "r+"); ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){ console.info("Data written to the stream. Size is:"+ number); }).catch(function(err){ console.info("Failed to write data to the stream. Error:"+ err); }); ``` ### write7+ write(buffer: ArrayBuffer | string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback<number>): void Writes data into the stream. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------- | ---- | ------------------------------------------------------------ | | buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | | options | Object | No | The options are as follows:
- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.
- **length** (number): length of the data to write. The default value is the buffer length minus the offset.
- **position** (number): start position to write the data in the file. By default, data is written from the current position.
- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.
Constraints: offset + length <= Buffer size| | callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | **Example** ```js let ss= fileio.createStreamSync(path, "r+"); ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) { if (bytesWritten) { // Do something console.info("Data written to the stream. Size is:"+ bytesWritten); } }); ``` ### writeSync7+ writeSync(buffer: ArrayBuffer | string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number Synchronously writes data into the stream. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ------- | ------------------------------- | ---- | ---------------------------------------- | | buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | | options | Object | No | The options are as follows:
- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.
- **length** (number): length of the data to write. The default value is the buffer length minus the offset.
- **position** (number): start position to write the data in the file. By default, data is written from the current position.
- **encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.
Constraints: offset + length <= Buffer size | **Return value** | Type | Description | | ------ | -------- | | number | Length of the data written in the file.| **Example** ```js let ss= fileio.createStreamSync(path,"r+"); let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}); ``` ### read7+ read(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): Promise<ReadOut> Reads data from the stream. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ------- | ----------- | ---- | ---------------------------------------- | | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | | options | Object | No | The options are as follows:
- **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**.
- **length** (number): length of the data to read. The default value is the buffer length minus the offset.
- **position** (number): position of the data to read in the file. By default, data is read from the current position.
Constraints: offset + length <= Buffer size | **Return value** | Type | Description | | ---------------------------------- | ------ | | Promise<[ReadOut](#readout)> | Promise used to return the data read.| **Example** ```js let ss = fileio.createStreamSync(path, "r+"); ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut){ console.info("Read data successfully"); console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); }).catch(function(err){ console.info("Failed to read data. Error:"+ err); }); ``` ### read7+ read(buffer: ArrayBuffer, options: { position?: number; offset?: number; length?: number; }, callback: AsyncCallback<ReadOut>): void Reads data from the stream. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ---------------------------------------- | | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | | options | Object | No | The options are as follows:
- **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**.
- **length** (number): length of the data to read. The default value is the buffer length minus the offset.
- **position** (number): position of the data to read in the file. By default, data is read from the current position.
Constraints: offset + length <= Buffer size | | callback | AsyncCallback<[ReadOut](#readout)> | Yes | Callback invoked when data is read asynchronously from the stream. | **Example** ```js let ss = fileio.createStreamSync(path, "r+"); ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) { if (readOut) { console.info("Read data successfully"); console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); } }); ``` ### readSync7+ readSync(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): number Synchronously reads data from the stream. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | ------- | ----------- | ---- | ---------------------------------------- | | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | | options | Object | No | The options are as follows:
- **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**.
- **length** (number): length of the data to read. The default value is the buffer length minus the offset.
- **position** (number): position of the data to read in the file. By default, data is read from the current position.
Constraints: offset + length <= Buffer size | **Return value** | Type | Description | | ------ | -------- | | number | Length of the data read.| **Example** ```js let ss = fileio.createStreamSync(path, "r+"); let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}); ``` ## Dir Manages directories. Before calling a method of the **Dir** class, use the **opendir()** method synchronously or asynchronously to create a **Dir** instance. ### read read(): Promise<Dirent> Reads the next directory entry. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | -------------------------------- | ------------- | | Promise<[Dirent](#dirent)> | Promise used to return the directory entry read.| **Example** ```js let dir = fileio.opendirSync(path); dir.read().then(function (dirent){ console.log("Read the next directory entry:"+JSON.stringify(dirent)); }).catch(function(err){ console.info("Failed to read the next directory entry. Error:"+ err); }); ``` ### read read(callback: AsyncCallback<Dirent>): void Reads the next directory entry. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.FileManagement.File.FileIO **Parameters** | Name | Type | Mandatory | Description | | -------- | -------------------------------------- | ---- | ---------------- | | callback | AsyncCallback<[Dirent](#dirent)> | Yes | Callback invoked when the next directory entry is asynchronously read.| **Example** ```js let dir = fileio.opendirSync(path); dir.read(function (err, dirent) { if (dirent) { // Do something console.log("Read the next directory entry:"+JSON.stringify(dirent)); } }); ``` ### readSync readSync(): Dirent Synchronously reads the next directory entry. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ----------------- | -------- | | [Dirent](#dirent) | Directory entry read.| **Example** ```js let dir = fileio.opendirSync(path); let dirent = dir.readSync(); ``` ### close7+ close(): Promise<void> Closes a directory. This API uses a promise to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir. **System capability**: SystemCapability.FileManagement.File.FileIO **Example** ```js let dir = fileio.opendirSync(path); dir.close().then(function(err){ console.info("close dir successfully"); }); ``` ### close7+ close(callback: AsyncCallback<void>): void Closes a directory. This API uses an asynchronous callback to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir. **System capability**: SystemCapability.FileManagement.File.FileIO **Example** ```js let dir = fileio.opendirSync(path); dir.close(function(err){ console.info("close dir successfully"); }); ``` ### closeSync closeSync(): void Closes a directory. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir. **System capability**: SystemCapability.FileManagement.File.FileIO **Example** ```js let dir = fileio.opendirSync(path); dir.closeSync(); ``` ## Dirent Provides information about files and directories. Before calling a method of the **Dirent** class, use the [dir.read()](#read) method synchronously or asynchronously to create a **Dirent** instance. **System capability**: SystemCapability.FileManagement.File.FileIO ### Attributes | Name | Type | Readable | Writable | Description | | ---- | ------ | ---- | ---- | ------- | | name | string | Yes | No | Directory entry name.| ### isBlockDevice isBlockDevice(): boolean Checks whether this directory entry is a block special file. A block special file supports access by block only, and it is cached when accessed. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | ---------------- | | boolean | Whether the directory entry is a block special file.| **Example** ```js let dir = fileio.opendirSync(path); let isBLockDevice = dir.readSync().isBlockDevice(); ``` ### isCharacterDevice isCharacterDevice(): boolean Checks whether a directory entry is a character special file. A character special file supports random access, and it is not cached when accessed. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | ----------------- | | boolean | Whether the directory entry is a character special file.| **Example** ```js let dir = fileio.opendirSync(path); let isCharacterDevice = dir.readSync().isCharacterDevice(); ``` ### isDirectory isDirectory(): boolean Checks whether a directory entry is a directory. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | ------------- | | boolean | Whether the directory entry is a directory.| **Example** ```js let dir = fileio.opendirSync(path); let isDirectory = dir.readSync().isDirectory(); ``` ### isFIFO isFIFO(): boolean Checks whether this directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | --------------- | | boolean | Whether the directory entry is a FIFO.| **Example** ```js let dir = fileio.opendirSync(path); let isFIFO = dir.readSync().isFIFO(); ``` ### isFile isFile(): boolean Checks whether a directory entry is a regular file. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | --------------- | | boolean | Whether the directory entry is a regular file.| **Example** ```js let dir = fileio.opendirSync(path); let isFile = dir.readSync().isFile(); ``` ### isSocket isSocket(): boolean Checks whether a directory entry is a socket. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | -------------- | | boolean | Whether the directory entry is a socket.| **Example** ```js let dir = fileio.opendirSync(path); let isSocket = dir.readSync().isSocket(); ``` ### isSymbolicLink isSymbolicLink(): boolean Checks whether a directory entry is a symbolic link. **System capability**: SystemCapability.FileManagement.File.FileIO **Return value** | Type | Description | | ------- | --------------- | | boolean | Whether the directory entry is a symbolic link.| **Example** ```js let dir = fileio.opendirSync(path); let isSymbolicLink = dir.readSync().isSymbolicLink(); ```