1e41f4b71Sopenharmony_ci# @ohos.file.fs (File Management) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **fs** module provides APIs for file operations, including accessing and managing files and directories, obtaining file information statistics, and reading and writing data using a stream. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Modules to Import 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Guidelines 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciBefore using the APIs provided by this module to perform operations on a file or directory, obtain the application sandbox path of the file or directory as follows: 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci ```ts 20e41f4b71Sopenharmony_ci import { UIAbility } from '@kit.AbilityKit'; 21e41f4b71Sopenharmony_ci import { window } from '@kit.ArkUI'; 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci export default class EntryAbility extends UIAbility { 24e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 25e41f4b71Sopenharmony_ci let context = this.context; 26e41f4b71Sopenharmony_ci let pathDir = context.filesDir; 27e41f4b71Sopenharmony_ci } 28e41f4b71Sopenharmony_ci } 29e41f4b71Sopenharmony_ci ``` 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciBefore using this module to perform operations on a file or directory, you need to obtain the application sandbox path. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciThe **path** parameter specifies the application sandbox path. For details about how to obtain **path**, see [Obtaining Application File Paths](../../application-models/application-context-stage.md#obtaining-application-file-paths). 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ciA uniform resource identifier (URI) is a string pointing to a resource. For APIs that support only **path**, you can construct a **fileUri** object and obtain the **path** property to convert the URI to **path**, and then use the APIs. For details about the URI definition and how to convert a URI to a path, see [File URI](../../../application-dev/reference/apis-core-file-kit/js-apis-file-fileuri.md). 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci## fs.stat 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_cistat(file: string | number): Promise<Stat> 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ciObtains detailed file attribute information. This API uses a promise to return the result. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**Parameters** 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 50e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 51e41f4b71Sopenharmony_ci| file | string \| number | Yes | Application sandbox path or file descriptor (FD) of the file.| 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**Return value** 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci| Type | Description | 56e41f4b71Sopenharmony_ci| ---------------------------- | ---------- | 57e41f4b71Sopenharmony_ci| Promise<[Stat](#stat)> | Promise used to return detailed file information.| 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci**Error codes** 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**Example** 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci ```ts 66e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 67e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 68e41f4b71Sopenharmony_ci fs.stat(filePath).then((stat: fs.Stat) => { 69e41f4b71Sopenharmony_ci console.info("get file info succeed, the size of file is " + stat.size); 70e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 71e41f4b71Sopenharmony_ci console.error("get file info failed with error message: " + err.message + ", error code: " + err.code); 72e41f4b71Sopenharmony_ci }); 73e41f4b71Sopenharmony_ci ``` 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci## fs.stat 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_cistat(file: string | number, callback: AsyncCallback<Stat>): void 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ciObtains detailed file information. This API uses an asynchronous callback to return the result. 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci**Parameters** 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 88e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | ---- | ------------------------------ | 89e41f4b71Sopenharmony_ci| file | string \| number | Yes | Application sandbox path or FD of the file. | 90e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Stat](#stat)> | Yes | Callback used to return the file information obtained.| 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci**Error codes** 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**Example** 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci ```ts 99e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 100e41f4b71Sopenharmony_ci fs.stat(pathDir, (err: BusinessError, stat: fs.Stat) => { 101e41f4b71Sopenharmony_ci if (err) { 102e41f4b71Sopenharmony_ci console.error("get file info failed with error message: " + err.message + ", error code: " + err.code); 103e41f4b71Sopenharmony_ci } else { 104e41f4b71Sopenharmony_ci console.info("get file info succeed, the size of file is " + stat.size); 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci }); 107e41f4b71Sopenharmony_ci ``` 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci## fs.statSync 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_cistatSync(file: string | number): Stat 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ciObtains detailed file information. This API returns the result synchronously. 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci**Parameters** 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 122e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 123e41f4b71Sopenharmony_ci| file | string \| number | Yes | Application sandbox path or FD of the file.| 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci**Return value** 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci| Type | Description | 128e41f4b71Sopenharmony_ci| ------------- | ---------- | 129e41f4b71Sopenharmony_ci| [Stat](#stat) | File information obtained.| 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci**Error codes** 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci**Example** 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci ```ts 138e41f4b71Sopenharmony_ci let stat = fs.statSync(pathDir); 139e41f4b71Sopenharmony_ci console.info("get file info succeed, the size of file is " + stat.size); 140e41f4b71Sopenharmony_ci ``` 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci## fs.access 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ciaccess(path: string, mode?: AccessModeType): Promise<boolean> 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ciChecks whether a file exists. This API uses a promise to return the result. 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci**Parameters** 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 155e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 156e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file to check. | 157e41f4b71Sopenharmony_ci| mode<sup>12+</sup> | [AccessModeType](#accessmodetype12) | No | Permission on the file to verify. | 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci**Return value** 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci| Type | Description | 162e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 163e41f4b71Sopenharmony_ci| Promise<boolean> | Promise used to return a Boolean value, which indicating whether the file exists.| 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci**Error codes** 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci**Example** 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci ```ts 172e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 173e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 174e41f4b71Sopenharmony_ci fs.access(filePath).then((res: boolean) => { 175e41f4b71Sopenharmony_ci if (res) { 176e41f4b71Sopenharmony_ci console.info("file exists"); 177e41f4b71Sopenharmony_ci } else { 178e41f4b71Sopenharmony_ci console.info("file not exists"); 179e41f4b71Sopenharmony_ci } 180e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 181e41f4b71Sopenharmony_ci console.error("access failed with error message: " + err.message + ", error code: " + err.code); 182e41f4b71Sopenharmony_ci }); 183e41f4b71Sopenharmony_ci ``` 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci## fs.access 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ciaccess(path: string, callback: AsyncCallback<boolean>): void 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciChecks whether a file exists. This API uses an asynchronous callback to return the result. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci**Parameters** 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 198e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 199e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file. | 200e41f4b71Sopenharmony_ci| callback | AsyncCallback<boolean> | Yes | Callback used to return the result. The value **true** means the file exists; the value **false** means the opposite.| 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci**Error codes** 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci**Example** 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci ```ts 209e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 210e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 211e41f4b71Sopenharmony_ci fs.access(filePath, (err: BusinessError, res: boolean) => { 212e41f4b71Sopenharmony_ci if (err) { 213e41f4b71Sopenharmony_ci console.error("access failed with error message: " + err.message + ", error code: " + err.code); 214e41f4b71Sopenharmony_ci } else { 215e41f4b71Sopenharmony_ci if (res) { 216e41f4b71Sopenharmony_ci console.info("file exists"); 217e41f4b71Sopenharmony_ci } else { 218e41f4b71Sopenharmony_ci console.info("file not exists"); 219e41f4b71Sopenharmony_ci } 220e41f4b71Sopenharmony_ci } 221e41f4b71Sopenharmony_ci }); 222e41f4b71Sopenharmony_ci ``` 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci## fs.accessSync 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ciaccessSync(path: string, mode?: AccessModeType): boolean 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ciChecks whether a file exists. This API returns the result synchronously. 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci**Parameters** 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 237e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 238e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file to check. | 239e41f4b71Sopenharmony_ci| mode<sup>12+</sup> | [AccessModeType](#accessmodetype12) | No | Permission on the file to verify. | 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci**Return value** 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci| Type | Description | 244e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 245e41f4b71Sopenharmony_ci| boolean | Returns **true** if the file exists; returns **false** otherwise.| 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci**Error codes** 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci**Example** 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci ```ts 254e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 255e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 256e41f4b71Sopenharmony_ci try { 257e41f4b71Sopenharmony_ci let res = fs.accessSync(filePath); 258e41f4b71Sopenharmony_ci if (res) { 259e41f4b71Sopenharmony_ci console.info("file exists"); 260e41f4b71Sopenharmony_ci } else { 261e41f4b71Sopenharmony_ci console.info("file not exists"); 262e41f4b71Sopenharmony_ci } 263e41f4b71Sopenharmony_ci } catch(error) { 264e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 265e41f4b71Sopenharmony_ci console.error("accessSync failed with error message: " + err.message + ", error code: " + err.code); 266e41f4b71Sopenharmony_ci } 267e41f4b71Sopenharmony_ci ``` 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci## fs.close 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ciclose(file: number | File): Promise<void> 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ciCloses a file. This API uses a promise to return the result. 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci**Parameters** 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 283e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------ | 284e41f4b71Sopenharmony_ci| file | number \| [File](#file) | Yes | **File** object or FD of the file to close. Once closed, the **File** object or FD cannot be used for read/write operations.| 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci**Return value** 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci| Type | Description | 289e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 290e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci**Error codes** 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci**Example** 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci ```ts 299e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 300e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 301e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 302e41f4b71Sopenharmony_ci fs.close(file).then(() => { 303e41f4b71Sopenharmony_ci console.info("File closed"); 304e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 305e41f4b71Sopenharmony_ci console.error("close file failed with error message: " + err.message + ", error code: " + err.code); 306e41f4b71Sopenharmony_ci }); 307e41f4b71Sopenharmony_ci ``` 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci## fs.close 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ciclose(file: number | File, callback: AsyncCallback<void>): void 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ciCloses a file. This API uses an asynchronous callback to return the result. 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ci**Parameters** 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 322e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------ | 323e41f4b71Sopenharmony_ci| file | number \| [File](#file) | Yes | **File** object or FD of the file to close. Once closed, the **File** object or FD cannot be used for read/write operations.| 324e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback invoked immediately after the file is closed.| 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci**Error codes** 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci**Example** 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci ```ts 333e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 334e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 335e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 336e41f4b71Sopenharmony_ci fs.close(file, (err: BusinessError) => { 337e41f4b71Sopenharmony_ci if (err) { 338e41f4b71Sopenharmony_ci console.error("close file failed with error message: " + err.message + ", error code: " + err.code); 339e41f4b71Sopenharmony_ci } else { 340e41f4b71Sopenharmony_ci console.info("File closed"); 341e41f4b71Sopenharmony_ci } 342e41f4b71Sopenharmony_ci }); 343e41f4b71Sopenharmony_ci ``` 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci## fs.closeSync 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_cicloseSync(file: number | File): void 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ciCloses a file. This API returns the result synchronously. 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci**Parameters** 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 358e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------ | 359e41f4b71Sopenharmony_ci| file | number \| [File](#file) | Yes | **File** object or FD of the file to close. Once closed, the **File** object or FD cannot be used for read/write operations.| 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci**Error codes** 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci**Example** 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci ```ts 368e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 369e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 370e41f4b71Sopenharmony_ci fs.closeSync(file); 371e41f4b71Sopenharmony_ci ``` 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci## fs.copy<sup>11+</sup> 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_cicopy(srcUri: string, destUri: string, options?: CopyOptions): Promise\<void> 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ciCopies a file or folder. This API uses a promise to return the result. 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ciFile copy across devices is supported. This API forcibly overwrites the file or folder with the same name in the destination directory. The file or directory URI is supported. 380e41f4b71Sopenharmony_ciA maximum of 10 cross-device copy tasks are allowed at the same time, and the number of files to be copied at a time cannot exceed 500. 381e41f4b71Sopenharmony_ci 382e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 383e41f4b71Sopenharmony_ci 384e41f4b71Sopenharmony_ci**Parameters** 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 387e41f4b71Sopenharmony_ci| ---- | -------------------------- | ---- | ---------------------------------------- | 388e41f4b71Sopenharmony_ci| srcUri | string | Yes | URI of the file or folder to copy. | 389e41f4b71Sopenharmony_ci| destUri | string | Yes | URI of the destination file or folder. | 390e41f4b71Sopenharmony_ci| options | [CopyOptions](#copyoptions11)| No| Callback invoked to provide the copy progress| 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci**Return value** 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci| Type | Description | 395e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 396e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value.| 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci**Error codes** 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci**Example** 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci```ts 405e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit'; 406e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 407e41f4b71Sopenharmony_ciimport { fileUri } from '@kit.CoreFileKit'; 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_cilet srcDirPathLocal: string = pathDir + "/src"; 410e41f4b71Sopenharmony_cilet dstDirPathLocal: string = pathDir + "/dest"; 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_cilet srcDirUriLocal: string = fileUri.getUriFromPath(srcDirPathLocal); 413e41f4b71Sopenharmony_cilet dstDirUriLocal: string = fileUri.getUriFromPath(dstDirPathLocal); 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_cilet progressListener: fs.ProgressListener = (progress: fs.Progress) => { 416e41f4b71Sopenharmony_ci console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`); 417e41f4b71Sopenharmony_ci}; 418e41f4b71Sopenharmony_cilet copyoption: fs.CopyOptions = { 419e41f4b71Sopenharmony_ci "progressListener" : progressListener 420e41f4b71Sopenharmony_ci} 421e41f4b71Sopenharmony_citry { 422e41f4b71Sopenharmony_ci fs.copy(srcDirUriLocal, dstDirUriLocal, copyoption).then(()=>{ 423e41f4b71Sopenharmony_ci console.info("Succeeded in copying. "); 424e41f4b71Sopenharmony_ci }).catch((err: BusinessError)=>{ 425e41f4b71Sopenharmony_ci console.error(`Failed to copy: ${JSON.stringify(err)}`); 426e41f4b71Sopenharmony_ci }) 427e41f4b71Sopenharmony_ci} catch(err) { 428e41f4b71Sopenharmony_ci console.error(`Failed to copy: ${JSON.stringify(err)}`); 429e41f4b71Sopenharmony_ci} 430e41f4b71Sopenharmony_ci``` 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci## fs.copy<sup>11+</sup> 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_cicopy(srcUri: string, destUri: string, callback: AsyncCallback\<void>): void 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ciCopies a file or folder. This API uses an asynchronous callback to return the result. 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ciFile copy across devices is supported. This API forcibly overwrites the file or folder with the same name in the destination directory. The file or directory URI is supported. 439e41f4b71Sopenharmony_ciA maximum of 10 cross-device copy tasks are allowed at the same time, and the number of files to be copied at a time cannot exceed 500. 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci**Parameters** 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 446e41f4b71Sopenharmony_ci| ---- | -------------------------- | ---- | ---------------------------------------- | 447e41f4b71Sopenharmony_ci| srcUri | string | Yes | URI of the file or folder to copy. | 448e41f4b71Sopenharmony_ci| destUri | string | Yes | URI of the destination file or folder. | 449e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void>| Yes| Callback used to return the result.| 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci**Error codes** 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci**Example** 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci```ts 458e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 459e41f4b71Sopenharmony_ciimport { fileUri } from '@kit.CoreFileKit'; 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_cilet srcDirPathLocal: string = pathDir + "/src"; 462e41f4b71Sopenharmony_cilet dstDirPathLocal: string = pathDir + "/dest"; 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_cilet srcDirUriLocal: string = fileUri.getUriFromPath(srcDirPathLocal); 465e41f4b71Sopenharmony_cilet dstDirUriLocal: string = fileUri.getUriFromPath(dstDirPathLocal); 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_citry { 468e41f4b71Sopenharmony_ci fs.copy(srcDirUriLocal, dstDirUriLocal, (err: BusinessError) => { 469e41f4b71Sopenharmony_ci if (err) { 470e41f4b71Sopenharmony_ci console.error(`Failed to copy: ${JSON.stringify(err)}`); 471e41f4b71Sopenharmony_ci return; 472e41f4b71Sopenharmony_ci } 473e41f4b71Sopenharmony_ci console.info("Succeeded in copying. "); 474e41f4b71Sopenharmony_ci }) 475e41f4b71Sopenharmony_ci} catch(err) { 476e41f4b71Sopenharmony_ci console.error(`Failed to copy: ${JSON.stringify(err)}`); 477e41f4b71Sopenharmony_ci} 478e41f4b71Sopenharmony_ci``` 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_ci## fs.copy<sup>11+</sup> 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_cicopy(srcUri: string, destUri: string, options: CopyOptions, callback: AsyncCallback\<void>): void 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ciCopies a file or folder. This API uses an asynchronous callback to return the result. 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ciFile copy across devices is supported. This API forcibly overwrites the file or folder with the same name in the destination directory. The file or directory URI is supported. 487e41f4b71Sopenharmony_ciA maximum of 10 cross-device copy tasks are allowed at the same time, and the number of files to be copied at a time cannot exceed 500. 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci**Parameters** 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 494e41f4b71Sopenharmony_ci| ---- | -------------------------- | ---- | ---------------------------------------- | 495e41f4b71Sopenharmony_ci| srcUri | string | Yes | URI of the file or folder to copy. | 496e41f4b71Sopenharmony_ci| destUri | string | Yes | URI of the destination file or folder. | 497e41f4b71Sopenharmony_ci| options | [CopyOptions](#copyoptions11) |Yes| Callback used to return the copy progress. | 498e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void>| Yes| Callback used to return the result.| 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_ci**Error codes** 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci**Example** 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci```ts 507e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit'; 508e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 509e41f4b71Sopenharmony_ciimport { fileUri } from '@kit.CoreFileKit'; 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_cilet srcDirPathLocal: string = pathDir + "/src"; 512e41f4b71Sopenharmony_cilet dstDirPathLocal: string = pathDir + "/dest"; 513e41f4b71Sopenharmony_ci 514e41f4b71Sopenharmony_cilet srcDirUriLocal: string = fileUri.getUriFromPath(srcDirPathLocal); 515e41f4b71Sopenharmony_cilet dstDirUriLocal: string = fileUri.getUriFromPath(dstDirPathLocal); 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_citry { 518e41f4b71Sopenharmony_ci let progressListener: fs.ProgressListener = (progress: fs.Progress) => { 519e41f4b71Sopenharmony_ci console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`); 520e41f4b71Sopenharmony_ci }; 521e41f4b71Sopenharmony_ci let copyoption: fs.CopyOptions = { 522e41f4b71Sopenharmony_ci "progressListener" : progressListener 523e41f4b71Sopenharmony_ci } 524e41f4b71Sopenharmony_ci fs.copy(srcDirUriLocal, dstDirUriLocal, copyoption, (err: BusinessError) => { 525e41f4b71Sopenharmony_ci if (err) { 526e41f4b71Sopenharmony_ci console.error(`Failed to copy: ${JSON.stringify(err)}`); 527e41f4b71Sopenharmony_ci return; 528e41f4b71Sopenharmony_ci } 529e41f4b71Sopenharmony_ci console.info("Succeeded in copying. "); 530e41f4b71Sopenharmony_ci }) 531e41f4b71Sopenharmony_ci} catch(err) { 532e41f4b71Sopenharmony_ci console.error(`Failed to copy: ${JSON.stringify(err)}`); 533e41f4b71Sopenharmony_ci} 534e41f4b71Sopenharmony_ci``` 535e41f4b71Sopenharmony_ci 536e41f4b71Sopenharmony_ci## fs.copyFile 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_cicopyFile(src: string | number, dest: string | number, mode?: number): Promise<void> 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ciCopies a file. This API uses a promise to return the result. 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ci**Parameters** 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 549e41f4b71Sopenharmony_ci| ---- | -------------------------- | ---- | ---------------------------------------- | 550e41f4b71Sopenharmony_ci| src | string \| number | Yes | Path or FD of the file to copy. | 551e41f4b71Sopenharmony_ci| dest | string \| number | Yes | Destination path of the file or FD of the file created. | 552e41f4b71Sopenharmony_ci| mode | number | No | Whether to overwrite the file with the same name in the destination directory. The default value is **0**, which is the only value supported.<br>**0**: overwrite the file with the same name and truncate the part that is not overwritten.| 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_ci**Return value** 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci| Type | Description | 557e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 558e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci**Error codes** 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_ci**Example** 565e41f4b71Sopenharmony_ci 566e41f4b71Sopenharmony_ci ```ts 567e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 568e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/test.txt"; 569e41f4b71Sopenharmony_ci let dstPath = pathDir + "/dstDir/test.txt"; 570e41f4b71Sopenharmony_ci fs.copyFile(srcPath, dstPath, 0).then(() => { 571e41f4b71Sopenharmony_ci console.info("copy file succeed"); 572e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 573e41f4b71Sopenharmony_ci console.error("copy file failed with error message: " + err.message + ", error code: " + err.code); 574e41f4b71Sopenharmony_ci }); 575e41f4b71Sopenharmony_ci ``` 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci## fs.copyFile 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_cicopyFile(src: string | number, dest: string | number, mode: number, callback: AsyncCallback<void>): void 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ciCopies a file with the specified mode. This API uses an asynchronous callback to return the result. 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci**Parameters** 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 590e41f4b71Sopenharmony_ci| -------- | -------------------------- | ---- | ---------------------------------------- | 591e41f4b71Sopenharmony_ci| src | string \| number | Yes | Path or FD of the file to copy. | 592e41f4b71Sopenharmony_ci| dest | string \| number | Yes | Destination path of the file or FD of the file created. | 593e41f4b71Sopenharmony_ci| mode | number | Yes | Whether to overwrite the file with the same name in the destination directory. The default value is **0**, which is the only value supported.<br>**0**: overwrite the file with the same name and truncate the part that is not overwritten.| 594e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback invoked immediately after the file is copied. | 595e41f4b71Sopenharmony_ci 596e41f4b71Sopenharmony_ci**Error codes** 597e41f4b71Sopenharmony_ci 598e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 599e41f4b71Sopenharmony_ci 600e41f4b71Sopenharmony_ci**Example** 601e41f4b71Sopenharmony_ci 602e41f4b71Sopenharmony_ci ```ts 603e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 604e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/test.txt"; 605e41f4b71Sopenharmony_ci let dstPath = pathDir + "/dstDir/test.txt"; 606e41f4b71Sopenharmony_ci fs.copyFile(srcPath, dstPath, 0, (err: BusinessError) => { 607e41f4b71Sopenharmony_ci if (err) { 608e41f4b71Sopenharmony_ci console.error("copy file failed with error message: " + err.message + ", error code: " + err.code); 609e41f4b71Sopenharmony_ci } else { 610e41f4b71Sopenharmony_ci console.info("copy file succeed"); 611e41f4b71Sopenharmony_ci } 612e41f4b71Sopenharmony_ci }); 613e41f4b71Sopenharmony_ci ``` 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ci## fs.copyFile 616e41f4b71Sopenharmony_ci 617e41f4b71Sopenharmony_cicopyFile(src: string | number, dest: string | number, callback: AsyncCallback<void>): void 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ciCopies a file. This API overwrites the file with the same name in the destination directory and truncates the part that is not overwritten. This API uses an asynchronous callback to return the result. 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_ci**Parameters** 626e41f4b71Sopenharmony_ci 627e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 628e41f4b71Sopenharmony_ci| -------- | -------------------------- | ---- | ---------------------------------------- | 629e41f4b71Sopenharmony_ci| src | string \| number | Yes | Path or FD of the file to copy. | 630e41f4b71Sopenharmony_ci| dest | string \| number | Yes | Destination path of the file or FD of the file created. | 631e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback invoked immediately after the file is copied. | 632e41f4b71Sopenharmony_ci 633e41f4b71Sopenharmony_ci**Error codes** 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ci**Example** 638e41f4b71Sopenharmony_ci 639e41f4b71Sopenharmony_ci ```ts 640e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 641e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/test.txt"; 642e41f4b71Sopenharmony_ci let dstPath = pathDir + "/dstDir/test.txt"; 643e41f4b71Sopenharmony_ci fs.copyFile(srcPath, dstPath, (err: BusinessError) => { 644e41f4b71Sopenharmony_ci if (err) { 645e41f4b71Sopenharmony_ci console.error("copy file failed with error message: " + err.message + ", error code: " + err.code); 646e41f4b71Sopenharmony_ci } else { 647e41f4b71Sopenharmony_ci console.info("copy file succeed"); 648e41f4b71Sopenharmony_ci } 649e41f4b71Sopenharmony_ci }); 650e41f4b71Sopenharmony_ci ``` 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci## fs.copyFileSync 654e41f4b71Sopenharmony_ci 655e41f4b71Sopenharmony_cicopyFileSync(src: string | number, dest: string | number, mode?: number): void 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ciCopies a file. This API returns the result synchronously. 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ci**Parameters** 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 666e41f4b71Sopenharmony_ci| ---- | -------------------------- | ---- | ---------------------------------------- | 667e41f4b71Sopenharmony_ci| src | string \| number | Yes | Path or FD of the file to copy. | 668e41f4b71Sopenharmony_ci| dest | string \| number | Yes | Destination path of the file or FD of the file created. | 669e41f4b71Sopenharmony_ci| mode | number | No | Whether to overwrite the file with the same name in the destination directory. The default value is **0**, which is the only value supported.<br>**0**: overwrite the file with the same name and truncate the part that is not overwritten.| 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ci**Error codes** 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 674e41f4b71Sopenharmony_ci 675e41f4b71Sopenharmony_ci**Example** 676e41f4b71Sopenharmony_ci 677e41f4b71Sopenharmony_ci ```ts 678e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/test.txt"; 679e41f4b71Sopenharmony_ci let dstPath = pathDir + "/dstDir/test.txt"; 680e41f4b71Sopenharmony_ci fs.copyFileSync(srcPath, dstPath); 681e41f4b71Sopenharmony_ci ``` 682e41f4b71Sopenharmony_ci 683e41f4b71Sopenharmony_ci## fs.copyDir<sup>10+</sup> 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_cicopyDir(src: string, dest: string, mode?: number): Promise\<void> 686e41f4b71Sopenharmony_ci 687e41f4b71Sopenharmony_ciCopies a folder. This API uses a promise to return the result. 688e41f4b71Sopenharmony_ci 689e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 690e41f4b71Sopenharmony_ci 691e41f4b71Sopenharmony_ci**Parameters** 692e41f4b71Sopenharmony_ci 693e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 694e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 695e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the folder to copy.| 696e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination folder.| 697e41f4b71Sopenharmony_ci| mode | number | No | Copy mode. The default value is **0**.<br>- **0**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a folder with the same name in the destination folder and there are files with the same name in the conflicting folder. All the non-conflicting files in the source folder will be moved to the destination folder, and the non-conflicting files in the destination folder will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **1**: Forcibly overwrite the files with the same name in the destination folder. If there is a folder with the same name in the destination directory and there are files with the same name in the conflicting folder, all the files with the same name in the destination folder will be overwritten and the non-conflicting files will be retained.| 698e41f4b71Sopenharmony_ci 699e41f4b71Sopenharmony_ci**Return value** 700e41f4b71Sopenharmony_ci 701e41f4b71Sopenharmony_ci| Type | Description | 702e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 703e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 704e41f4b71Sopenharmony_ci 705e41f4b71Sopenharmony_ci**Error codes** 706e41f4b71Sopenharmony_ci 707e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 708e41f4b71Sopenharmony_ci 709e41f4b71Sopenharmony_ci**Example** 710e41f4b71Sopenharmony_ci 711e41f4b71Sopenharmony_ci ```ts 712e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 713e41f4b71Sopenharmony_ci // Copy srcPath to destPath. 714e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/"; 715e41f4b71Sopenharmony_ci let destPath = pathDir + "/destDir/"; 716e41f4b71Sopenharmony_ci fs.copyDir(srcPath, destPath, 0).then(() => { 717e41f4b71Sopenharmony_ci console.info("copy directory succeed"); 718e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 719e41f4b71Sopenharmony_ci console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code); 720e41f4b71Sopenharmony_ci }); 721e41f4b71Sopenharmony_ci ``` 722e41f4b71Sopenharmony_ci 723e41f4b71Sopenharmony_ci## fs.copyDir<sup>10+</sup> 724e41f4b71Sopenharmony_ci 725e41f4b71Sopenharmony_cicopyDir(src: string, dest: string, mode: number, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void 726e41f4b71Sopenharmony_ci 727e41f4b71Sopenharmony_ciCopies a folder with the specified mode. This API uses an asynchronous callback to return the result. 728e41f4b71Sopenharmony_ci 729e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 730e41f4b71Sopenharmony_ci 731e41f4b71Sopenharmony_ci**Parameters** 732e41f4b71Sopenharmony_ci 733e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 734e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 735e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the folder to copy.| 736e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination folder.| 737e41f4b71Sopenharmony_ci| mode | number | Yes | Copy mode. The default value is **0**.<br>- **0**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a folder with the same name in the destination folder and there are files with the same name in the conflicting folder. All the non-conflicting files in the source folder will be moved to the destination folder, and the non-conflicting files in the destination folder will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **1**: Forcibly overwrite the files with the same name in the destination folder. If there is a folder with the same name in the destination directory and there are files with the same name in the conflicting folder, all the files with the same name in the destination folder will be overwritten and the non-conflicting files will be retained.| 738e41f4b71Sopenharmony_ci| callback | AsyncCallback<void, Array<[ConflictFiles](#conflictfiles10)>> | Yes | Callback used to return the result. | 739e41f4b71Sopenharmony_ci 740e41f4b71Sopenharmony_ci**Error codes** 741e41f4b71Sopenharmony_ci 742e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 743e41f4b71Sopenharmony_ci 744e41f4b71Sopenharmony_ci**Example** 745e41f4b71Sopenharmony_ci 746e41f4b71Sopenharmony_ci ```ts 747e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 748e41f4b71Sopenharmony_ci import { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit'; 749e41f4b71Sopenharmony_ci // Copy srcPath to destPath. 750e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/"; 751e41f4b71Sopenharmony_ci let destPath = pathDir + "/destDir/"; 752e41f4b71Sopenharmony_ci fs.copyDir(srcPath, destPath, 0, (err: BusinessError<Array<ConflictFiles>>) => { 753e41f4b71Sopenharmony_ci if (err && err.code == 13900015 && err.data?.length !== undefined) { 754e41f4b71Sopenharmony_ci for (let i = 0; i < err.data.length; i++) { 755e41f4b71Sopenharmony_ci console.error("copy directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile); 756e41f4b71Sopenharmony_ci } 757e41f4b71Sopenharmony_ci } else if (err) { 758e41f4b71Sopenharmony_ci console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code); 759e41f4b71Sopenharmony_ci } else { 760e41f4b71Sopenharmony_ci console.info("copy directory succeed"); 761e41f4b71Sopenharmony_ci } 762e41f4b71Sopenharmony_ci }); 763e41f4b71Sopenharmony_ci ``` 764e41f4b71Sopenharmony_ci 765e41f4b71Sopenharmony_ci## fs.copyDir<sup>10+</sup> 766e41f4b71Sopenharmony_ci 767e41f4b71Sopenharmony_cicopyDir(src: string, dest: string, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void 768e41f4b71Sopenharmony_ci 769e41f4b71Sopenharmony_ciCopies a folder. This API uses an asynchronous callback to return the result. 770e41f4b71Sopenharmony_ci 771e41f4b71Sopenharmony_ciAn exception will be thrown if there is a folder with the same name in the destination directory and there are files with the same name in the conflicting folder. All the non-conflicting files in the source folder will be moved to the destination folder, and the non-conflicting files in the destination folder will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format. 772e41f4b71Sopenharmony_ci 773e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 774e41f4b71Sopenharmony_ci 775e41f4b71Sopenharmony_ci**Parameters** 776e41f4b71Sopenharmony_ci 777e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 778e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 779e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the folder to copy.| 780e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination folder.| 781e41f4b71Sopenharmony_ci| callback | AsyncCallback<void, Array<[ConflictFiles](#conflictfiles10)>> | Yes | Callback used to return the result. | 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ci**Error codes** 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 786e41f4b71Sopenharmony_ci 787e41f4b71Sopenharmony_ci**Example** 788e41f4b71Sopenharmony_ci 789e41f4b71Sopenharmony_ci ```ts 790e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 791e41f4b71Sopenharmony_ci import { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit'; 792e41f4b71Sopenharmony_ci // Copy srcPath to destPath. 793e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/"; 794e41f4b71Sopenharmony_ci let destPath = pathDir + "/destDir/"; 795e41f4b71Sopenharmony_ci fs.copyDir(srcPath, destPath, (err: BusinessError<Array<ConflictFiles>>) => { 796e41f4b71Sopenharmony_ci if (err && err.code == 13900015 && err.data?.length !== undefined) { 797e41f4b71Sopenharmony_ci for (let i = 0; i < err.data.length; i++) { 798e41f4b71Sopenharmony_ci console.error("copy directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile); 799e41f4b71Sopenharmony_ci } 800e41f4b71Sopenharmony_ci } else if (err) { 801e41f4b71Sopenharmony_ci console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code); 802e41f4b71Sopenharmony_ci } else { 803e41f4b71Sopenharmony_ci console.info("copy directory succeed"); 804e41f4b71Sopenharmony_ci } 805e41f4b71Sopenharmony_ci }); 806e41f4b71Sopenharmony_ci ``` 807e41f4b71Sopenharmony_ci 808e41f4b71Sopenharmony_ci## fs.copyDirSync<sup>10+</sup> 809e41f4b71Sopenharmony_ci 810e41f4b71Sopenharmony_cicopyDirSync(src: string, dest: string, mode?: number): void 811e41f4b71Sopenharmony_ci 812e41f4b71Sopenharmony_ciCopies a folder. This API returns the result synchronously. 813e41f4b71Sopenharmony_ci 814e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 815e41f4b71Sopenharmony_ci 816e41f4b71Sopenharmony_ci**Parameters** 817e41f4b71Sopenharmony_ci 818e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 819e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 820e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the folder to copy.| 821e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination folder.| 822e41f4b71Sopenharmony_ci| mode | number | No | Copy mode. The default value is **0**.<br>- **0**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a folder with the same name in the destination folder and there are files with the same name in the conflicting folder. All the non-conflicting files in the source folder will be moved to the destination folder, and the non-conflicting files in the destination folder will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **1**: Forcibly overwrite the files with the same name in the destination folder. If there is a folder with the same name in the destination directory and there are files with the same name in the conflicting folder, all the files with the same name in the destination folder will be overwritten and the non-conflicting files will be retained.| 823e41f4b71Sopenharmony_ci 824e41f4b71Sopenharmony_ci**Error codes** 825e41f4b71Sopenharmony_ci 826e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 827e41f4b71Sopenharmony_ci 828e41f4b71Sopenharmony_ci**Example** 829e41f4b71Sopenharmony_ci 830e41f4b71Sopenharmony_ci ```ts 831e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 832e41f4b71Sopenharmony_ci // Copy srcPath to destPath. 833e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/"; 834e41f4b71Sopenharmony_ci let destPath = pathDir + "/destDir/"; 835e41f4b71Sopenharmony_ci try { 836e41f4b71Sopenharmony_ci fs.copyDirSync(srcPath, destPath, 0); 837e41f4b71Sopenharmony_ci console.info("copy directory succeed"); 838e41f4b71Sopenharmony_ci } catch (error) { 839e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 840e41f4b71Sopenharmony_ci console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code); 841e41f4b71Sopenharmony_ci } 842e41f4b71Sopenharmony_ci ``` 843e41f4b71Sopenharmony_ci 844e41f4b71Sopenharmony_ci## fs.dup<sup>10+</sup> 845e41f4b71Sopenharmony_ci 846e41f4b71Sopenharmony_cidup(fd: number): File 847e41f4b71Sopenharmony_ci 848e41f4b71Sopenharmony_ciOpens a **File** object based on an FD. 849e41f4b71Sopenharmony_ci 850e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 851e41f4b71Sopenharmony_ci 852e41f4b71Sopenharmony_ci**Parameters** 853e41f4b71Sopenharmony_ci 854e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 855e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 856e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file.| 857e41f4b71Sopenharmony_ci 858e41f4b71Sopenharmony_ci**Return value** 859e41f4b71Sopenharmony_ci 860e41f4b71Sopenharmony_ci| Type | Description | 861e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 862e41f4b71Sopenharmony_ci| [File](#file) | File object opened.| 863e41f4b71Sopenharmony_ci 864e41f4b71Sopenharmony_ci**Error codes** 865e41f4b71Sopenharmony_ci 866e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 867e41f4b71Sopenharmony_ci 868e41f4b71Sopenharmony_ci**Example** 869e41f4b71Sopenharmony_ci 870e41f4b71Sopenharmony_ci ```ts 871e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 872e41f4b71Sopenharmony_ci let file1 = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 873e41f4b71Sopenharmony_ci let fd: number = file1.fd; 874e41f4b71Sopenharmony_ci let file2 = fs.dup(fd); 875e41f4b71Sopenharmony_ci console.info("The name of the file2 is " + file2.name); 876e41f4b71Sopenharmony_ci fs.closeSync(file1); 877e41f4b71Sopenharmony_ci fs.closeSync(file2); 878e41f4b71Sopenharmony_ci ``` 879e41f4b71Sopenharmony_ci 880e41f4b71Sopenharmony_ci## fs.connectDfs<sup>12+</sup> 881e41f4b71Sopenharmony_ci 882e41f4b71Sopenharmony_ciconnectDfs(networkId: string, listeners: DfsListeners): Promise<void> 883e41f4b71Sopenharmony_ci 884e41f4b71Sopenharmony_ciMounts the user directories of a device to the sandbox directory. If the device is abnormal, [onStatus](#onstatus12) in **DfsListeners** will be called to notify the application. 885e41f4b71Sopenharmony_ci 886e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 887e41f4b71Sopenharmony_ci 888e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 889e41f4b71Sopenharmony_ci 890e41f4b71Sopenharmony_ci**Parameters** 891e41f4b71Sopenharmony_ci 892e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 893e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 894e41f4b71Sopenharmony_ci| networkId | string | Yes | Network ID of the device. The device network ID can be obtained from [deviceBasicInfo](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#devicebasicinfo) using the related [distributedDeviceManager](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md) API. | 895e41f4b71Sopenharmony_ci| listeners | [DfsListeners](#fsdfslisteners12) | Yes | Listeners for distributed file system status. | 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_ci**Return value** 898e41f4b71Sopenharmony_ci 899e41f4b71Sopenharmony_ci| Type | Description | 900e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- | 901e41f4b71Sopenharmony_ci| Promise<void>| Promise that returns no value. | 902e41f4b71Sopenharmony_ci 903e41f4b71Sopenharmony_ci**Error codes** 904e41f4b71Sopenharmony_ci 905e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 906e41f4b71Sopenharmony_ci 907e41f4b71Sopenharmony_ci**Example** 908e41f4b71Sopenharmony_ci 909e41f4b71Sopenharmony_ci ```ts 910e41f4b71Sopenharmony_ci import { fileIo as fs } from '@kit.CoreFileKit'; 911e41f4b71Sopenharmony_ci import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 912e41f4b71Sopenharmony_ci let dmInstance = distributedDeviceManager.createDeviceManager("com.example.filesync"); 913e41f4b71Sopenharmony_ci let deviceInfoList: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 914e41f4b71Sopenharmony_ci let networkId = deviceInfoList[0].networkId; 915e41f4b71Sopenharmony_ci let listeners: fs.DfsListeners = { 916e41f4b71Sopenharmony_ci onStatus(networkId, status) { 917e41f4b71Sopenharmony_ci console.info('onStatus'); 918e41f4b71Sopenharmony_ci } 919e41f4b71Sopenharmony_ci } 920e41f4b71Sopenharmony_ci fs.connectDfs(networkId, listeners).then(() => { 921e41f4b71Sopenharmony_ci console.info("Success to connectDfs"); 922e41f4b71Sopenharmony_ci }).catch((err) => { 923e41f4b71Sopenharmony_ci console.error('connectDfs failed with error message: ${JSON.stringify(err)}'); 924e41f4b71Sopenharmony_ci }); 925e41f4b71Sopenharmony_ci ``` 926e41f4b71Sopenharmony_ci 927e41f4b71Sopenharmony_ci## fs.disconnectDfs<sup>12+</sup> 928e41f4b71Sopenharmony_ci 929e41f4b71Sopenharmony_cidisconnectDfs(networkId: string): Promise<void> 930e41f4b71Sopenharmony_ci 931e41f4b71Sopenharmony_ciUnmounts the user directories of a device. 932e41f4b71Sopenharmony_ci 933e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 934e41f4b71Sopenharmony_ci 935e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 936e41f4b71Sopenharmony_ci 937e41f4b71Sopenharmony_ci**Parameters** 938e41f4b71Sopenharmony_ci 939e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 940e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 941e41f4b71Sopenharmony_ci| networkId | string | Yes | Network ID of the device. The device network ID can be obtained from [deviceBasicInfo](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#devicebasicinfo) using the related [distributedDeviceManager](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md) API. | 942e41f4b71Sopenharmony_ci 943e41f4b71Sopenharmony_ci**Return value** 944e41f4b71Sopenharmony_ci 945e41f4b71Sopenharmony_ci| Type | Description | 946e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- | 947e41f4b71Sopenharmony_ci| Promise<void>| Promise that returns no value. | 948e41f4b71Sopenharmony_ci 949e41f4b71Sopenharmony_ci**Error codes** 950e41f4b71Sopenharmony_ci 951e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ci**Example** 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ci ```ts 956e41f4b71Sopenharmony_ci import { fileIo as fs } from '@kit.CoreFileKit'; 957e41f4b71Sopenharmony_ci import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 958e41f4b71Sopenharmony_ci let dmInstance = distributedDeviceManager.createDeviceManager("com.example.filesync"); 959e41f4b71Sopenharmony_ci let deviceInfoList: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 960e41f4b71Sopenharmony_ci let networkId = deviceInfoList[0].networkId; 961e41f4b71Sopenharmony_ci fs.disconnectDfs(networkId).then(() => { 962e41f4b71Sopenharmony_ci console.info("Success to disconnectDfs"); 963e41f4b71Sopenharmony_ci }).catch((err) => { 964e41f4b71Sopenharmony_ci console.error('disconnectDfs failed with error message: ${JSON.stringify(err)}') 965e41f4b71Sopenharmony_ci }) 966e41f4b71Sopenharmony_ci ``` 967e41f4b71Sopenharmony_ci 968e41f4b71Sopenharmony_ci## fs.setxattr<sup>12+</sup> 969e41f4b71Sopenharmony_ci 970e41f4b71Sopenharmony_cisetxattr(path: string, key: string, value: string): Promise<void> 971e41f4b71Sopenharmony_ci 972e41f4b71Sopenharmony_ciSets an extended attribute for a file. 973e41f4b71Sopenharmony_ci 974e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 975e41f4b71Sopenharmony_ci 976e41f4b71Sopenharmony_ci**Parameters** 977e41f4b71Sopenharmony_ci 978e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 979e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 980e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file. | 981e41f4b71Sopenharmony_ci| key | string | Yes | Key of the extended attribute to set. | 982e41f4b71Sopenharmony_ci| value | string | Yes | Value of the extended attribute to set. | 983e41f4b71Sopenharmony_ci 984e41f4b71Sopenharmony_ci**Return value** 985e41f4b71Sopenharmony_ci 986e41f4b71Sopenharmony_ci| Type | Description | 987e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- | 988e41f4b71Sopenharmony_ci| Promise<void>| Promise that returns no value. | 989e41f4b71Sopenharmony_ci 990e41f4b71Sopenharmony_ci**Error codes** 991e41f4b71Sopenharmony_ci 992e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 993e41f4b71Sopenharmony_ci 994e41f4b71Sopenharmony_ci**Example** 995e41f4b71Sopenharmony_ci 996e41f4b71Sopenharmony_ci ```ts 997e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 998e41f4b71Sopenharmony_ci 999e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1000e41f4b71Sopenharmony_ci let attrKey = "user.comment"; 1001e41f4b71Sopenharmony_ci let attrValue = "Test file."; 1002e41f4b71Sopenharmony_ci 1003e41f4b71Sopenharmony_ci fs.setxattr(filePath, attrKey, attrValue).then(() => { 1004e41f4b71Sopenharmony_ci console.info("Set extended attribute successfully."); 1005e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1006e41f4b71Sopenharmony_ci console.error("Failed to set extended attribute with error message: " + err.message + ", error code: " + err.code); 1007e41f4b71Sopenharmony_ci }); 1008e41f4b71Sopenharmony_ci 1009e41f4b71Sopenharmony_ci ``` 1010e41f4b71Sopenharmony_ci## fs.setxattrSync<sup>12+</sup> 1011e41f4b71Sopenharmony_ci 1012e41f4b71Sopenharmony_cisetxattrSync(path: string, key: string, value: string): void; 1013e41f4b71Sopenharmony_ci 1014e41f4b71Sopenharmony_ciSets an extended attribute for a file. This API returns the result synchronously. 1015e41f4b71Sopenharmony_ci 1016e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1017e41f4b71Sopenharmony_ci 1018e41f4b71Sopenharmony_ci**Parameters** 1019e41f4b71Sopenharmony_ci 1020e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1021e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1022e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file. | 1023e41f4b71Sopenharmony_ci| key | string | Yes | Key of the extended attribute to set. | 1024e41f4b71Sopenharmony_ci| value | string | Yes | Value of the extended attribute to set. | 1025e41f4b71Sopenharmony_ci 1026e41f4b71Sopenharmony_ci**Error codes** 1027e41f4b71Sopenharmony_ci 1028e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1029e41f4b71Sopenharmony_ci 1030e41f4b71Sopenharmony_ci**Example** 1031e41f4b71Sopenharmony_ci 1032e41f4b71Sopenharmony_ci ```ts 1033e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1034e41f4b71Sopenharmony_ci 1035e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1036e41f4b71Sopenharmony_ci let attrKey = "user.comment"; 1037e41f4b71Sopenharmony_ci let attrValue = "Test file."; 1038e41f4b71Sopenharmony_ci 1039e41f4b71Sopenharmony_ci try { 1040e41f4b71Sopenharmony_ci fs.setxattrSync(filePath, attrKey, attrValue); 1041e41f4b71Sopenharmony_ci console.info("Set extended attribute successfully."); 1042e41f4b71Sopenharmony_ci } catch (err) { 1043e41f4b71Sopenharmony_ci console.error("Failed to set extended attribute with error message: " + err.message + ", error code: " + err.code); 1044e41f4b71Sopenharmony_ci } 1045e41f4b71Sopenharmony_ci 1046e41f4b71Sopenharmony_ci ``` 1047e41f4b71Sopenharmony_ci 1048e41f4b71Sopenharmony_ci## fs.getxattr<sup>12+</sup> 1049e41f4b71Sopenharmony_ci 1050e41f4b71Sopenharmony_cigetxattr(path: string, key: string): Promise<string> 1051e41f4b71Sopenharmony_ci 1052e41f4b71Sopenharmony_ciObtains an extended attribute of a file. This API uses a promise to return the result. 1053e41f4b71Sopenharmony_ci 1054e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1055e41f4b71Sopenharmony_ci 1056e41f4b71Sopenharmony_ci**Parameters** 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1059e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1060e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file. | 1061e41f4b71Sopenharmony_ci| key | string | Yes | Key of the extended attribute to obtain. | 1062e41f4b71Sopenharmony_ci 1063e41f4b71Sopenharmony_ci**Return value** 1064e41f4b71Sopenharmony_ci 1065e41f4b71Sopenharmony_ci| Type | Description | 1066e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- | 1067e41f4b71Sopenharmony_ci| Promise<string>| Promise used to return the value of the extended attribute obtained. | 1068e41f4b71Sopenharmony_ci 1069e41f4b71Sopenharmony_ci**Error codes** 1070e41f4b71Sopenharmony_ci 1071e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1072e41f4b71Sopenharmony_ci 1073e41f4b71Sopenharmony_ci**Example** 1074e41f4b71Sopenharmony_ci 1075e41f4b71Sopenharmony_ci ```ts 1076e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1077e41f4b71Sopenharmony_ci 1078e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1079e41f4b71Sopenharmony_ci let attrKey = "user.comment"; 1080e41f4b71Sopenharmony_ci 1081e41f4b71Sopenharmony_ci fs.getxattr(filePath, attrKey).then((attrValue: string) => { 1082e41f4b71Sopenharmony_ci console.info("Get extended attribute succeed, the value is: " + attrValue); 1083e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1084e41f4b71Sopenharmony_ci console.error("Failed to get extended attribute with error message: " + err.message + ", error code: " + err.code); 1085e41f4b71Sopenharmony_ci }); 1086e41f4b71Sopenharmony_ci 1087e41f4b71Sopenharmony_ci ``` 1088e41f4b71Sopenharmony_ci 1089e41f4b71Sopenharmony_ci## fs.getxattrSync<sup>12+</sup> 1090e41f4b71Sopenharmony_ci 1091e41f4b71Sopenharmony_cigetxattrSync(path: string, key: string): string; 1092e41f4b71Sopenharmony_ci 1093e41f4b71Sopenharmony_ciObtains an extended attribute of a file. This API returns the result synchronously. 1094e41f4b71Sopenharmony_ci 1095e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1096e41f4b71Sopenharmony_ci 1097e41f4b71Sopenharmony_ci**Parameters** 1098e41f4b71Sopenharmony_ci 1099e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1100e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1101e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file. | 1102e41f4b71Sopenharmony_ci| key | string | Yes | Key of the extended attribute to obtain. | 1103e41f4b71Sopenharmony_ci 1104e41f4b71Sopenharmony_ci**Return value** 1105e41f4b71Sopenharmony_ci 1106e41f4b71Sopenharmony_ci| Type | Description | 1107e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- | 1108e41f4b71Sopenharmony_ci| key| Value of the extended attribute obtained. | 1109e41f4b71Sopenharmony_ci 1110e41f4b71Sopenharmony_ci**Error codes** 1111e41f4b71Sopenharmony_ci 1112e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1113e41f4b71Sopenharmony_ci 1114e41f4b71Sopenharmony_ci**Example** 1115e41f4b71Sopenharmony_ci 1116e41f4b71Sopenharmony_ci ```ts 1117e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1118e41f4b71Sopenharmony_ci 1119e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1120e41f4b71Sopenharmony_ci let attrKey = "user.comment"; 1121e41f4b71Sopenharmony_ci 1122e41f4b71Sopenharmony_ci try { 1123e41f4b71Sopenharmony_ci let attrValue = fs.getxattrSync(filePath, attrKey); 1124e41f4b71Sopenharmony_ci console.info("Get extended attribute succeed, the value is: " + attrValue); 1125e41f4b71Sopenharmony_ci } catch (err) { 1126e41f4b71Sopenharmony_ci console.error("Failed to get extended attribute with error message: " + err.message + ", error code: " + err.code); 1127e41f4b71Sopenharmony_ci } 1128e41f4b71Sopenharmony_ci 1129e41f4b71Sopenharmony_ci ``` 1130e41f4b71Sopenharmony_ci 1131e41f4b71Sopenharmony_ci## fs.mkdir 1132e41f4b71Sopenharmony_ci 1133e41f4b71Sopenharmony_cimkdir(path: string): Promise<void> 1134e41f4b71Sopenharmony_ci 1135e41f4b71Sopenharmony_ciCreates a directory. This API uses a promise to return the result. 1136e41f4b71Sopenharmony_ci 1137e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1138e41f4b71Sopenharmony_ci 1139e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1140e41f4b71Sopenharmony_ci 1141e41f4b71Sopenharmony_ci**Parameters** 1142e41f4b71Sopenharmony_ci 1143e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1144e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1145e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the directory. | 1146e41f4b71Sopenharmony_ci 1147e41f4b71Sopenharmony_ci**Return value** 1148e41f4b71Sopenharmony_ci 1149e41f4b71Sopenharmony_ci| Type | Description | 1150e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 1151e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 1152e41f4b71Sopenharmony_ci 1153e41f4b71Sopenharmony_ci**Error codes** 1154e41f4b71Sopenharmony_ci 1155e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1156e41f4b71Sopenharmony_ci 1157e41f4b71Sopenharmony_ci**Example** 1158e41f4b71Sopenharmony_ci 1159e41f4b71Sopenharmony_ci ```ts 1160e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1161e41f4b71Sopenharmony_ci let dirPath = pathDir + "/testDir"; 1162e41f4b71Sopenharmony_ci fs.mkdir(dirPath).then(() => { 1163e41f4b71Sopenharmony_ci console.info("Directory created"); 1164e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1165e41f4b71Sopenharmony_ci console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code); 1166e41f4b71Sopenharmony_ci }); 1167e41f4b71Sopenharmony_ci ``` 1168e41f4b71Sopenharmony_ci 1169e41f4b71Sopenharmony_ci## fs.mkdir<sup>11+</sup> 1170e41f4b71Sopenharmony_ci 1171e41f4b71Sopenharmony_cimkdir(path: string, recursion: boolean): Promise\<void> 1172e41f4b71Sopenharmony_ci 1173e41f4b71Sopenharmony_ciCreates a directory. This API uses a promise to return the result. If **recursion** is set to **true**, a multi-level directory is created. 1174e41f4b71Sopenharmony_ci 1175e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1176e41f4b71Sopenharmony_ci 1177e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1178e41f4b71Sopenharmony_ci 1179e41f4b71Sopenharmony_ci**Parameters** 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1182e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1183e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the directory. | 1184e41f4b71Sopenharmony_ci| recursion | boolean | Yes | Whether to create a multi-level directory.<br> The value **true** means to create a multi-level directory. The value **false** means to create a single-level directory. | 1185e41f4b71Sopenharmony_ci 1186e41f4b71Sopenharmony_ci**Return value** 1187e41f4b71Sopenharmony_ci 1188e41f4b71Sopenharmony_ci| Type | Description | 1189e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 1190e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 1191e41f4b71Sopenharmony_ci 1192e41f4b71Sopenharmony_ci**Error codes** 1193e41f4b71Sopenharmony_ci 1194e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1195e41f4b71Sopenharmony_ci 1196e41f4b71Sopenharmony_ci**Example** 1197e41f4b71Sopenharmony_ci 1198e41f4b71Sopenharmony_ci ```ts 1199e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1200e41f4b71Sopenharmony_ci let dirPath = pathDir + "/testDir1/testDir2/testDir3"; 1201e41f4b71Sopenharmony_ci fs.mkdir(dirPath, true).then(() => { 1202e41f4b71Sopenharmony_ci console.info("Directory created"); 1203e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1204e41f4b71Sopenharmony_ci console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code); 1205e41f4b71Sopenharmony_ci }); 1206e41f4b71Sopenharmony_ci ``` 1207e41f4b71Sopenharmony_ci 1208e41f4b71Sopenharmony_ci## fs.mkdir 1209e41f4b71Sopenharmony_ci 1210e41f4b71Sopenharmony_cimkdir(path: string, callback: AsyncCallback<void>): void 1211e41f4b71Sopenharmony_ci 1212e41f4b71Sopenharmony_ciCreates a directory. This API uses an asynchronous callback to return the result. 1213e41f4b71Sopenharmony_ci 1214e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1215e41f4b71Sopenharmony_ci 1216e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1217e41f4b71Sopenharmony_ci 1218e41f4b71Sopenharmony_ci**Parameters** 1219e41f4b71Sopenharmony_ci 1220e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1221e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 1222e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the directory. | 1223e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1224e41f4b71Sopenharmony_ci 1225e41f4b71Sopenharmony_ci**Error codes** 1226e41f4b71Sopenharmony_ci 1227e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1228e41f4b71Sopenharmony_ci 1229e41f4b71Sopenharmony_ci**Example** 1230e41f4b71Sopenharmony_ci 1231e41f4b71Sopenharmony_ci ```ts 1232e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1233e41f4b71Sopenharmony_ci let dirPath = pathDir + "/testDir"; 1234e41f4b71Sopenharmony_ci fs.mkdir(dirPath, (err: BusinessError) => { 1235e41f4b71Sopenharmony_ci if (err) { 1236e41f4b71Sopenharmony_ci console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code); 1237e41f4b71Sopenharmony_ci } else { 1238e41f4b71Sopenharmony_ci console.info("Directory created"); 1239e41f4b71Sopenharmony_ci } 1240e41f4b71Sopenharmony_ci }); 1241e41f4b71Sopenharmony_ci ``` 1242e41f4b71Sopenharmony_ci 1243e41f4b71Sopenharmony_ci## fs.mkdir<sup>11+</sup> 1244e41f4b71Sopenharmony_ci 1245e41f4b71Sopenharmony_cimkdir(path: string, recursion: boolean, callback: AsyncCallback<void>): void 1246e41f4b71Sopenharmony_ci 1247e41f4b71Sopenharmony_ciCreates a directory. This API uses an asynchronous callback to return the result. If **recursion** is set to **true**, a multi-level directory is created. 1248e41f4b71Sopenharmony_ci 1249e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1250e41f4b71Sopenharmony_ci 1251e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1252e41f4b71Sopenharmony_ci 1253e41f4b71Sopenharmony_ci**Parameters** 1254e41f4b71Sopenharmony_ci 1255e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1256e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 1257e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the directory. | 1258e41f4b71Sopenharmony_ci| recursion | boolean | Yes | Whether to create a multi-level directory.<br> The value **true** means to create a multi-level directory. The value **false** means to create a single-level directory. | 1259e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1260e41f4b71Sopenharmony_ci 1261e41f4b71Sopenharmony_ci**Error codes** 1262e41f4b71Sopenharmony_ci 1263e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1264e41f4b71Sopenharmony_ci 1265e41f4b71Sopenharmony_ci**Example** 1266e41f4b71Sopenharmony_ci 1267e41f4b71Sopenharmony_ci ```ts 1268e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1269e41f4b71Sopenharmony_ci let dirPath = pathDir + "/testDir1/testDir2/testDir3"; 1270e41f4b71Sopenharmony_ci fs.mkdir(dirPath, true, (err: BusinessError) => { 1271e41f4b71Sopenharmony_ci if (err) { 1272e41f4b71Sopenharmony_ci console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code); 1273e41f4b71Sopenharmony_ci } else { 1274e41f4b71Sopenharmony_ci console.info("Directory created"); 1275e41f4b71Sopenharmony_ci } 1276e41f4b71Sopenharmony_ci }); 1277e41f4b71Sopenharmony_ci ``` 1278e41f4b71Sopenharmony_ci 1279e41f4b71Sopenharmony_ci## fs.mkdirSync 1280e41f4b71Sopenharmony_ci 1281e41f4b71Sopenharmony_cimkdirSync(path: string): void 1282e41f4b71Sopenharmony_ci 1283e41f4b71Sopenharmony_ciCreates a directory. This API returns the result synchronously. 1284e41f4b71Sopenharmony_ci 1285e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1286e41f4b71Sopenharmony_ci 1287e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1288e41f4b71Sopenharmony_ci 1289e41f4b71Sopenharmony_ci**Parameters** 1290e41f4b71Sopenharmony_ci 1291e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1292e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1293e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the directory. | 1294e41f4b71Sopenharmony_ci 1295e41f4b71Sopenharmony_ci**Error codes** 1296e41f4b71Sopenharmony_ci 1297e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1298e41f4b71Sopenharmony_ci 1299e41f4b71Sopenharmony_ci**Example** 1300e41f4b71Sopenharmony_ci 1301e41f4b71Sopenharmony_ci ```ts 1302e41f4b71Sopenharmony_ci let dirPath = pathDir + "/testDir"; 1303e41f4b71Sopenharmony_ci fs.mkdirSync(dirPath); 1304e41f4b71Sopenharmony_ci ``` 1305e41f4b71Sopenharmony_ci 1306e41f4b71Sopenharmony_ci## fs.mkdirSync<sup>11+</sup> 1307e41f4b71Sopenharmony_ci 1308e41f4b71Sopenharmony_cimkdirSync(path: string, recursion: boolean): void 1309e41f4b71Sopenharmony_ci 1310e41f4b71Sopenharmony_ciCreates a directory. This API returns the result synchronously. If **recursion** is set to **true**, a multi-level directory is created. 1311e41f4b71Sopenharmony_ci 1312e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1313e41f4b71Sopenharmony_ci 1314e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1315e41f4b71Sopenharmony_ci 1316e41f4b71Sopenharmony_ci**Parameters** 1317e41f4b71Sopenharmony_ci 1318e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1319e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1320e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the directory. | 1321e41f4b71Sopenharmony_ci| recursion | boolean | Yes | Whether to create a multi-level directory.<br> The value **true** means to create a multi-level directory. The value **false** means to create a single-level directory. | 1322e41f4b71Sopenharmony_ci 1323e41f4b71Sopenharmony_ci**Error codes** 1324e41f4b71Sopenharmony_ci 1325e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1326e41f4b71Sopenharmony_ci 1327e41f4b71Sopenharmony_ci**Example** 1328e41f4b71Sopenharmony_ci 1329e41f4b71Sopenharmony_ci ```ts 1330e41f4b71Sopenharmony_ci let dirPath = pathDir + "/testDir1/testDir2/testDir3"; 1331e41f4b71Sopenharmony_ci fs.mkdirSync(dirPath, true); 1332e41f4b71Sopenharmony_ci ``` 1333e41f4b71Sopenharmony_ci 1334e41f4b71Sopenharmony_ci## fs.open 1335e41f4b71Sopenharmony_ci 1336e41f4b71Sopenharmony_ciopen(path: string, mode?: number): Promise<File> 1337e41f4b71Sopenharmony_ci 1338e41f4b71Sopenharmony_ciOpens a file. This API uses a promise to return the result. This API supports the use of a URI. 1339e41f4b71Sopenharmony_ci 1340e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1341e41f4b71Sopenharmony_ci 1342e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1343e41f4b71Sopenharmony_ci 1344e41f4b71Sopenharmony_ci**Parameters** 1345e41f4b71Sopenharmony_ci 1346e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1347e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1348e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path or file URI of the file to open. When a URI is used, the file can only be opened. | 1349e41f4b71Sopenharmony_ci| mode | number | No | [Mode](#openmode) for opening the file. You must specify one of the following options. By default, the file is opened in read-only mode.<br>- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.<br>- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it. (This value does not support the use of a URI.)<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is opened in write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the opened file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception. The write permission is not allowed.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.| 1350e41f4b71Sopenharmony_ci 1351e41f4b71Sopenharmony_ci**Return value** 1352e41f4b71Sopenharmony_ci 1353e41f4b71Sopenharmony_ci| Type | Description | 1354e41f4b71Sopenharmony_ci| --------------------- | ----------- | 1355e41f4b71Sopenharmony_ci| Promise<[File](#file)> | Promise used to return the **File** object.| 1356e41f4b71Sopenharmony_ci 1357e41f4b71Sopenharmony_ci**Error codes** 1358e41f4b71Sopenharmony_ci 1359e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1360e41f4b71Sopenharmony_ci 1361e41f4b71Sopenharmony_ci**Example** 1362e41f4b71Sopenharmony_ci 1363e41f4b71Sopenharmony_ci ```ts 1364e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1365e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1366e41f4b71Sopenharmony_ci fs.open(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).then((file: fs.File) => { 1367e41f4b71Sopenharmony_ci console.info("file fd: " + file.fd); 1368e41f4b71Sopenharmony_ci fs.closeSync(file); 1369e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1370e41f4b71Sopenharmony_ci console.error("open file failed with error message: " + err.message + ", error code: " + err.code); 1371e41f4b71Sopenharmony_ci }); 1372e41f4b71Sopenharmony_ci ``` 1373e41f4b71Sopenharmony_ci 1374e41f4b71Sopenharmony_ci 1375e41f4b71Sopenharmony_ci## fs.open 1376e41f4b71Sopenharmony_ci 1377e41f4b71Sopenharmony_ciopen(path: string, mode: number, callback: AsyncCallback<File>): void 1378e41f4b71Sopenharmony_ci 1379e41f4b71Sopenharmony_ciOpens a file with the specified mode. This API uses an asynchronous callback to return the result. 1380e41f4b71Sopenharmony_ci 1381e41f4b71Sopenharmony_ciFile URIs are supported. 1382e41f4b71Sopenharmony_ci 1383e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1384e41f4b71Sopenharmony_ci 1385e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1386e41f4b71Sopenharmony_ci 1387e41f4b71Sopenharmony_ci**Parameters** 1388e41f4b71Sopenharmony_ci 1389e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1390e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 1391e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path or file URI of the file to open. The URI can be used to open the file only. | 1392e41f4b71Sopenharmony_ci| mode | number | Yes | [Mode](#openmode) for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.<br>- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it. (This value does not take effect if a URI is used.)<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is opened in write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the opened file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception. The write permission is not allowed.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.| 1393e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1394e41f4b71Sopenharmony_ci 1395e41f4b71Sopenharmony_ci**Error codes** 1396e41f4b71Sopenharmony_ci 1397e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1398e41f4b71Sopenharmony_ci 1399e41f4b71Sopenharmony_ci**Example** 1400e41f4b71Sopenharmony_ci 1401e41f4b71Sopenharmony_ci ```ts 1402e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1403e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1404e41f4b71Sopenharmony_ci fs.open(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE, (err: BusinessError, file: fs.File) => { 1405e41f4b71Sopenharmony_ci if (err) { 1406e41f4b71Sopenharmony_ci console.error("open failed with error message: " + err.message + ", error code: " + err.code); 1407e41f4b71Sopenharmony_ci } else { 1408e41f4b71Sopenharmony_ci console.info("file fd: " + file.fd); 1409e41f4b71Sopenharmony_ci } 1410e41f4b71Sopenharmony_ci fs.closeSync(file); 1411e41f4b71Sopenharmony_ci }); 1412e41f4b71Sopenharmony_ci ``` 1413e41f4b71Sopenharmony_ci 1414e41f4b71Sopenharmony_ci## fs.open 1415e41f4b71Sopenharmony_ci 1416e41f4b71Sopenharmony_ciopen(path: string, callback: AsyncCallback<File>): void 1417e41f4b71Sopenharmony_ci 1418e41f4b71Sopenharmony_ciOpens a file. This API uses an asynchronous callback to return the result. File URIs are supported. 1419e41f4b71Sopenharmony_ci 1420e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1421e41f4b71Sopenharmony_ci 1422e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1423e41f4b71Sopenharmony_ci 1424e41f4b71Sopenharmony_ci**Parameters** 1425e41f4b71Sopenharmony_ci 1426e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1427e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 1428e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path or URI of the file. | 1429e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1430e41f4b71Sopenharmony_ci 1431e41f4b71Sopenharmony_ci**Error codes** 1432e41f4b71Sopenharmony_ci 1433e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1434e41f4b71Sopenharmony_ci 1435e41f4b71Sopenharmony_ci**Example** 1436e41f4b71Sopenharmony_ci 1437e41f4b71Sopenharmony_ci ```ts 1438e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1439e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1440e41f4b71Sopenharmony_ci fs.open(filePath, (err: BusinessError, file: fs.File) => { 1441e41f4b71Sopenharmony_ci if (err) { 1442e41f4b71Sopenharmony_ci console.error("open failed with error message: " + err.message + ", error code: " + err.code); 1443e41f4b71Sopenharmony_ci } else { 1444e41f4b71Sopenharmony_ci console.info("file fd: " + file.fd); 1445e41f4b71Sopenharmony_ci } 1446e41f4b71Sopenharmony_ci fs.closeSync(file); 1447e41f4b71Sopenharmony_ci }); 1448e41f4b71Sopenharmony_ci ``` 1449e41f4b71Sopenharmony_ci 1450e41f4b71Sopenharmony_ci## fs.openSync 1451e41f4b71Sopenharmony_ci 1452e41f4b71Sopenharmony_ciopenSync(path: string, mode?: number): File 1453e41f4b71Sopenharmony_ci 1454e41f4b71Sopenharmony_ciOpens a file. This API returns the result synchronously. File URIs are supported. 1455e41f4b71Sopenharmony_ci 1456e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1457e41f4b71Sopenharmony_ci 1458e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1459e41f4b71Sopenharmony_ci 1460e41f4b71Sopenharmony_ci**Parameters** 1461e41f4b71Sopenharmony_ci 1462e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1463e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 1464e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path or file URI of the file to open. The URI can be used to open the file only. | 1465e41f4b71Sopenharmony_ci| mode | number | No | [Mode](#openmode) for opening the file. You must specify one of the following options. By default, the file is opened in read-only mode.<br>- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.<br>- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it. (This value does not take effect if a URI is used.)<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is opened in write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the opened file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception. The write permission is not allowed.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.| 1466e41f4b71Sopenharmony_ci 1467e41f4b71Sopenharmony_ci**Return value** 1468e41f4b71Sopenharmony_ci 1469e41f4b71Sopenharmony_ci| Type | Description | 1470e41f4b71Sopenharmony_ci| ------ | ----------- | 1471e41f4b71Sopenharmony_ci| [File](#file) | File object opened.| 1472e41f4b71Sopenharmony_ci 1473e41f4b71Sopenharmony_ci**Error codes** 1474e41f4b71Sopenharmony_ci 1475e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1476e41f4b71Sopenharmony_ci 1477e41f4b71Sopenharmony_ci**Example** 1478e41f4b71Sopenharmony_ci 1479e41f4b71Sopenharmony_ci ```ts 1480e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1481e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1482e41f4b71Sopenharmony_ci console.info("file fd: " + file.fd); 1483e41f4b71Sopenharmony_ci fs.closeSync(file); 1484e41f4b71Sopenharmony_ci ``` 1485e41f4b71Sopenharmony_ci 1486e41f4b71Sopenharmony_ci## fs.read 1487e41f4b71Sopenharmony_ci 1488e41f4b71Sopenharmony_ciread(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise<number> 1489e41f4b71Sopenharmony_ci 1490e41f4b71Sopenharmony_ciReads data from a file. This API uses a promise to return the result. 1491e41f4b71Sopenharmony_ci 1492e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1493e41f4b71Sopenharmony_ci 1494e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1495e41f4b71Sopenharmony_ci 1496e41f4b71Sopenharmony_ci**Parameters** 1497e41f4b71Sopenharmony_ci 1498e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1499e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ------------------------------------------------------------ | 1500e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 1501e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | 1502e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11) | No | The options are as follows:<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| 1503e41f4b71Sopenharmony_ci 1504e41f4b71Sopenharmony_ci**Return value** 1505e41f4b71Sopenharmony_ci 1506e41f4b71Sopenharmony_ci| Type | Description | 1507e41f4b71Sopenharmony_ci| ---------------------------------- | ------ | 1508e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the length of the data read, in bytes.| 1509e41f4b71Sopenharmony_ci 1510e41f4b71Sopenharmony_ci**Error codes** 1511e41f4b71Sopenharmony_ci 1512e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1513e41f4b71Sopenharmony_ci 1514e41f4b71Sopenharmony_ci**Example** 1515e41f4b71Sopenharmony_ci 1516e41f4b71Sopenharmony_ci ```ts 1517e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1518e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 1519e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1520e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 1521e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(4096); 1522e41f4b71Sopenharmony_ci fs.read(file.fd, arrayBuffer).then((readLen: number) => { 1523e41f4b71Sopenharmony_ci console.info("Read file data successfully"); 1524e41f4b71Sopenharmony_ci let buf = buffer.from(arrayBuffer, 0, readLen); 1525e41f4b71Sopenharmony_ci console.info(`The content of file: ${buf.toString()}`); 1526e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1527e41f4b71Sopenharmony_ci console.error("read file data failed with error message: " + err.message + ", error code: " + err.code); 1528e41f4b71Sopenharmony_ci }).finally(() => { 1529e41f4b71Sopenharmony_ci fs.closeSync(file); 1530e41f4b71Sopenharmony_ci }); 1531e41f4b71Sopenharmony_ci ``` 1532e41f4b71Sopenharmony_ci 1533e41f4b71Sopenharmony_ci## fs.read 1534e41f4b71Sopenharmony_ci 1535e41f4b71Sopenharmony_ciread(fd: number, buffer: ArrayBuffer, options?: ReadOptions, callback: AsyncCallback<number>): void 1536e41f4b71Sopenharmony_ci 1537e41f4b71Sopenharmony_ciReads data from a file. This API uses an asynchronous callback to return the result. 1538e41f4b71Sopenharmony_ci 1539e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1540e41f4b71Sopenharmony_ci 1541e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1542e41f4b71Sopenharmony_ci 1543e41f4b71Sopenharmony_ci**Parameters** 1544e41f4b71Sopenharmony_ci 1545e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1546e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 1547e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 1548e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | 1549e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11) | No | The options are as follows:<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| 1550e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the length of the data read, in bytes. | 1551e41f4b71Sopenharmony_ci 1552e41f4b71Sopenharmony_ci**Error codes** 1553e41f4b71Sopenharmony_ci 1554e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1555e41f4b71Sopenharmony_ci 1556e41f4b71Sopenharmony_ci**Example** 1557e41f4b71Sopenharmony_ci 1558e41f4b71Sopenharmony_ci ```ts 1559e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1560e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 1561e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1562e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 1563e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(4096); 1564e41f4b71Sopenharmony_ci fs.read(file.fd, arrayBuffer, (err: BusinessError, readLen: number) => { 1565e41f4b71Sopenharmony_ci if (err) { 1566e41f4b71Sopenharmony_ci console.error("read failed with error message: " + err.message + ", error code: " + err.code); 1567e41f4b71Sopenharmony_ci } else { 1568e41f4b71Sopenharmony_ci console.info("Read file data successfully"); 1569e41f4b71Sopenharmony_ci let buf = buffer.from(arrayBuffer, 0, readLen); 1570e41f4b71Sopenharmony_ci console.info(`The content of file: ${buf.toString()}`); 1571e41f4b71Sopenharmony_ci } 1572e41f4b71Sopenharmony_ci fs.closeSync(file); 1573e41f4b71Sopenharmony_ci }); 1574e41f4b71Sopenharmony_ci ``` 1575e41f4b71Sopenharmony_ci 1576e41f4b71Sopenharmony_ci## fs.readSync 1577e41f4b71Sopenharmony_ci 1578e41f4b71Sopenharmony_cireadSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number 1579e41f4b71Sopenharmony_ci 1580e41f4b71Sopenharmony_ciReads data from a file. This API returns the result synchronously. 1581e41f4b71Sopenharmony_ci 1582e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1583e41f4b71Sopenharmony_ci 1584e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1585e41f4b71Sopenharmony_ci 1586e41f4b71Sopenharmony_ci**Parameters** 1587e41f4b71Sopenharmony_ci 1588e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1589e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ---------------------------------------- | 1590e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 1591e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | 1592e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11) | No | The options are as follows:<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| 1593e41f4b71Sopenharmony_ci 1594e41f4b71Sopenharmony_ci**Return value** 1595e41f4b71Sopenharmony_ci 1596e41f4b71Sopenharmony_ci| Type | Description | 1597e41f4b71Sopenharmony_ci| ------ | -------- | 1598e41f4b71Sopenharmony_ci| number | Length of the data read, in bytes.| 1599e41f4b71Sopenharmony_ci 1600e41f4b71Sopenharmony_ci**Error codes** 1601e41f4b71Sopenharmony_ci 1602e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1603e41f4b71Sopenharmony_ci 1604e41f4b71Sopenharmony_ci**Example** 1605e41f4b71Sopenharmony_ci 1606e41f4b71Sopenharmony_ci ```ts 1607e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1608e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 1609e41f4b71Sopenharmony_ci let buf = new ArrayBuffer(4096); 1610e41f4b71Sopenharmony_ci fs.readSync(file.fd, buf); 1611e41f4b71Sopenharmony_ci fs.closeSync(file); 1612e41f4b71Sopenharmony_ci ``` 1613e41f4b71Sopenharmony_ci 1614e41f4b71Sopenharmony_ci## fs.rmdir 1615e41f4b71Sopenharmony_ci 1616e41f4b71Sopenharmony_cirmdir(path: string): Promise<void> 1617e41f4b71Sopenharmony_ci 1618e41f4b71Sopenharmony_ciDeletes a directory. This API uses a promise to return the result. 1619e41f4b71Sopenharmony_ci 1620e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1621e41f4b71Sopenharmony_ci 1622e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1623e41f4b71Sopenharmony_ci 1624e41f4b71Sopenharmony_ci**Parameters** 1625e41f4b71Sopenharmony_ci 1626e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1627e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 1628e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the directory.| 1629e41f4b71Sopenharmony_ci 1630e41f4b71Sopenharmony_ci**Return value** 1631e41f4b71Sopenharmony_ci 1632e41f4b71Sopenharmony_ci| Type | Description | 1633e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 1634e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 1635e41f4b71Sopenharmony_ci 1636e41f4b71Sopenharmony_ci**Error codes** 1637e41f4b71Sopenharmony_ci 1638e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1639e41f4b71Sopenharmony_ci 1640e41f4b71Sopenharmony_ci**Example** 1641e41f4b71Sopenharmony_ci 1642e41f4b71Sopenharmony_ci ```ts 1643e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1644e41f4b71Sopenharmony_ci let dirPath = pathDir + "/testDir"; 1645e41f4b71Sopenharmony_ci fs.rmdir(dirPath).then(() => { 1646e41f4b71Sopenharmony_ci console.info("Directory deleted"); 1647e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1648e41f4b71Sopenharmony_ci console.error("rmdir failed with error message: " + err.message + ", error code: " + err.code); 1649e41f4b71Sopenharmony_ci }); 1650e41f4b71Sopenharmony_ci ``` 1651e41f4b71Sopenharmony_ci 1652e41f4b71Sopenharmony_ci## fs.rmdir 1653e41f4b71Sopenharmony_ci 1654e41f4b71Sopenharmony_cirmdir(path: string, callback: AsyncCallback<void>): void 1655e41f4b71Sopenharmony_ci 1656e41f4b71Sopenharmony_ciDeletes a directory. This API uses an asynchronous callback to return the result. 1657e41f4b71Sopenharmony_ci 1658e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1659e41f4b71Sopenharmony_ci 1660e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1661e41f4b71Sopenharmony_ci 1662e41f4b71Sopenharmony_ci**Parameters** 1663e41f4b71Sopenharmony_ci 1664e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1665e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | -------------------------- | 1666e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the directory.| 1667e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1668e41f4b71Sopenharmony_ci 1669e41f4b71Sopenharmony_ci**Error codes** 1670e41f4b71Sopenharmony_ci 1671e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1672e41f4b71Sopenharmony_ci 1673e41f4b71Sopenharmony_ci**Example** 1674e41f4b71Sopenharmony_ci 1675e41f4b71Sopenharmony_ci ```ts 1676e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1677e41f4b71Sopenharmony_ci let dirPath = pathDir + "/testDir"; 1678e41f4b71Sopenharmony_ci fs.rmdir(dirPath, (err: BusinessError) => { 1679e41f4b71Sopenharmony_ci if (err) { 1680e41f4b71Sopenharmony_ci console.error("rmdir failed with error message: " + err.message + ", error code: " + err.code); 1681e41f4b71Sopenharmony_ci } else { 1682e41f4b71Sopenharmony_ci console.info("Directory deleted"); 1683e41f4b71Sopenharmony_ci } 1684e41f4b71Sopenharmony_ci }); 1685e41f4b71Sopenharmony_ci ``` 1686e41f4b71Sopenharmony_ci 1687e41f4b71Sopenharmony_ci## fs.rmdirSync 1688e41f4b71Sopenharmony_ci 1689e41f4b71Sopenharmony_cirmdirSync(path: string): void 1690e41f4b71Sopenharmony_ci 1691e41f4b71Sopenharmony_ciDeletes a directory. This API returns the result synchronously. 1692e41f4b71Sopenharmony_ci 1693e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1694e41f4b71Sopenharmony_ci 1695e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1696e41f4b71Sopenharmony_ci 1697e41f4b71Sopenharmony_ci**Parameters** 1698e41f4b71Sopenharmony_ci 1699e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1700e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 1701e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the directory.| 1702e41f4b71Sopenharmony_ci 1703e41f4b71Sopenharmony_ci**Error codes** 1704e41f4b71Sopenharmony_ci 1705e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1706e41f4b71Sopenharmony_ci 1707e41f4b71Sopenharmony_ci**Example** 1708e41f4b71Sopenharmony_ci 1709e41f4b71Sopenharmony_ci ```ts 1710e41f4b71Sopenharmony_ci let dirPath = pathDir + "/testDir"; 1711e41f4b71Sopenharmony_ci fs.rmdirSync(dirPath); 1712e41f4b71Sopenharmony_ci ``` 1713e41f4b71Sopenharmony_ci 1714e41f4b71Sopenharmony_ci## fs.unlink 1715e41f4b71Sopenharmony_ci 1716e41f4b71Sopenharmony_ciunlink(path: string): Promise<void> 1717e41f4b71Sopenharmony_ci 1718e41f4b71Sopenharmony_ciDeletes a single file. This API uses a promise to return the result. 1719e41f4b71Sopenharmony_ci 1720e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1721e41f4b71Sopenharmony_ci 1722e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1723e41f4b71Sopenharmony_ci 1724e41f4b71Sopenharmony_ci**Parameters** 1725e41f4b71Sopenharmony_ci 1726e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1727e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 1728e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file.| 1729e41f4b71Sopenharmony_ci 1730e41f4b71Sopenharmony_ci**Return value** 1731e41f4b71Sopenharmony_ci 1732e41f4b71Sopenharmony_ci| Type | Description | 1733e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 1734e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 1735e41f4b71Sopenharmony_ci 1736e41f4b71Sopenharmony_ci**Error codes** 1737e41f4b71Sopenharmony_ci 1738e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1739e41f4b71Sopenharmony_ci 1740e41f4b71Sopenharmony_ci**Example** 1741e41f4b71Sopenharmony_ci 1742e41f4b71Sopenharmony_ci ```ts 1743e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1744e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1745e41f4b71Sopenharmony_ci fs.unlink(filePath).then(() => { 1746e41f4b71Sopenharmony_ci console.info("File deleted"); 1747e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1748e41f4b71Sopenharmony_ci console.error("remove file failed with error message: " + err.message + ", error code: " + err.code); 1749e41f4b71Sopenharmony_ci }); 1750e41f4b71Sopenharmony_ci ``` 1751e41f4b71Sopenharmony_ci 1752e41f4b71Sopenharmony_ci## fs.unlink 1753e41f4b71Sopenharmony_ci 1754e41f4b71Sopenharmony_ciunlink(path: string, callback: AsyncCallback<void>): void 1755e41f4b71Sopenharmony_ci 1756e41f4b71Sopenharmony_ciDeletes a file. This API uses an asynchronous callback to return the result. 1757e41f4b71Sopenharmony_ci 1758e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1759e41f4b71Sopenharmony_ci 1760e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1761e41f4b71Sopenharmony_ci 1762e41f4b71Sopenharmony_ci**Parameters** 1763e41f4b71Sopenharmony_ci 1764e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1765e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | -------------------------- | 1766e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file.| 1767e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback invoked immediately after the file is deleted. | 1768e41f4b71Sopenharmony_ci 1769e41f4b71Sopenharmony_ci**Error codes** 1770e41f4b71Sopenharmony_ci 1771e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1772e41f4b71Sopenharmony_ci 1773e41f4b71Sopenharmony_ci**Example** 1774e41f4b71Sopenharmony_ci 1775e41f4b71Sopenharmony_ci ```ts 1776e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1777e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1778e41f4b71Sopenharmony_ci fs.unlink(filePath, (err: BusinessError) => { 1779e41f4b71Sopenharmony_ci if (err) { 1780e41f4b71Sopenharmony_ci console.error("remove file failed with error message: " + err.message + ", error code: " + err.code); 1781e41f4b71Sopenharmony_ci } else { 1782e41f4b71Sopenharmony_ci console.info("File deleted"); 1783e41f4b71Sopenharmony_ci } 1784e41f4b71Sopenharmony_ci }); 1785e41f4b71Sopenharmony_ci ``` 1786e41f4b71Sopenharmony_ci 1787e41f4b71Sopenharmony_ci## fs.unlinkSync 1788e41f4b71Sopenharmony_ci 1789e41f4b71Sopenharmony_ciunlinkSync(path: string): void 1790e41f4b71Sopenharmony_ci 1791e41f4b71Sopenharmony_ciDeletes a file. This API returns the result synchronously. 1792e41f4b71Sopenharmony_ci 1793e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1794e41f4b71Sopenharmony_ci 1795e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1796e41f4b71Sopenharmony_ci 1797e41f4b71Sopenharmony_ci**Parameters** 1798e41f4b71Sopenharmony_ci 1799e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1800e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 1801e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file.| 1802e41f4b71Sopenharmony_ci 1803e41f4b71Sopenharmony_ci**Error codes** 1804e41f4b71Sopenharmony_ci 1805e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1806e41f4b71Sopenharmony_ci 1807e41f4b71Sopenharmony_ci**Example** 1808e41f4b71Sopenharmony_ci 1809e41f4b71Sopenharmony_ci ```ts 1810e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1811e41f4b71Sopenharmony_ci fs.unlinkSync(filePath); 1812e41f4b71Sopenharmony_ci ``` 1813e41f4b71Sopenharmony_ci 1814e41f4b71Sopenharmony_ci 1815e41f4b71Sopenharmony_ci## fs.write 1816e41f4b71Sopenharmony_ci 1817e41f4b71Sopenharmony_ciwrite(fd: number, buffer: ArrayBuffer | string, options?: WriteOptions): Promise<number> 1818e41f4b71Sopenharmony_ci 1819e41f4b71Sopenharmony_ciWrites data to a file. This API uses a promise to return the result. 1820e41f4b71Sopenharmony_ci 1821e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1822e41f4b71Sopenharmony_ci 1823e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1824e41f4b71Sopenharmony_ci 1825e41f4b71Sopenharmony_ci**Parameters** 1826e41f4b71Sopenharmony_ci 1827e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1828e41f4b71Sopenharmony_ci| ------- | ------------------------------- | ---- | ---------------------------------------- | 1829e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 1830e41f4b71Sopenharmony_ci| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | 1831e41f4b71Sopenharmony_ci| options | [WriteOptions](#writeoptions11) | No | The options are as follows:<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported currently.| 1832e41f4b71Sopenharmony_ci 1833e41f4b71Sopenharmony_ci**Return value** 1834e41f4b71Sopenharmony_ci 1835e41f4b71Sopenharmony_ci| Type | Description | 1836e41f4b71Sopenharmony_ci| --------------------- | -------- | 1837e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the length of the data written, in bytes.| 1838e41f4b71Sopenharmony_ci 1839e41f4b71Sopenharmony_ci**Error codes** 1840e41f4b71Sopenharmony_ci 1841e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1842e41f4b71Sopenharmony_ci 1843e41f4b71Sopenharmony_ci**Example** 1844e41f4b71Sopenharmony_ci 1845e41f4b71Sopenharmony_ci ```ts 1846e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1847e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1848e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1849e41f4b71Sopenharmony_ci let str: string = "hello, world"; 1850e41f4b71Sopenharmony_ci fs.write(file.fd, str).then((writeLen: number) => { 1851e41f4b71Sopenharmony_ci console.info("write data to file succeed and size is:" + writeLen); 1852e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1853e41f4b71Sopenharmony_ci console.error("write data to file failed with error message: " + err.message + ", error code: " + err.code); 1854e41f4b71Sopenharmony_ci }).finally(() => { 1855e41f4b71Sopenharmony_ci fs.closeSync(file); 1856e41f4b71Sopenharmony_ci }); 1857e41f4b71Sopenharmony_ci ``` 1858e41f4b71Sopenharmony_ci 1859e41f4b71Sopenharmony_ci## fs.write 1860e41f4b71Sopenharmony_ci 1861e41f4b71Sopenharmony_ciwrite(fd: number, buffer: ArrayBuffer | string, options?: WriteOptions, callback: AsyncCallback<number>): void 1862e41f4b71Sopenharmony_ci 1863e41f4b71Sopenharmony_ciWrites data to a file. This API uses an asynchronous callback to return the result. 1864e41f4b71Sopenharmony_ci 1865e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1866e41f4b71Sopenharmony_ci 1867e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1868e41f4b71Sopenharmony_ci 1869e41f4b71Sopenharmony_ci**Parameters** 1870e41f4b71Sopenharmony_ci 1871e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1872e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ---------------------------------------- | 1873e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 1874e41f4b71Sopenharmony_ci| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | 1875e41f4b71Sopenharmony_ci| options | [WriteOptions](#writeoptions11) | No | The options are as follows:<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported currently.| 1876e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the result. | 1877e41f4b71Sopenharmony_ci 1878e41f4b71Sopenharmony_ci**Error codes** 1879e41f4b71Sopenharmony_ci 1880e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1881e41f4b71Sopenharmony_ci 1882e41f4b71Sopenharmony_ci**Example** 1883e41f4b71Sopenharmony_ci 1884e41f4b71Sopenharmony_ci ```ts 1885e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1886e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1887e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1888e41f4b71Sopenharmony_ci let str: string = "hello, world"; 1889e41f4b71Sopenharmony_ci fs.write(file.fd, str, (err: BusinessError, writeLen: number) => { 1890e41f4b71Sopenharmony_ci if (err) { 1891e41f4b71Sopenharmony_ci console.error("write data to file failed with error message:" + err.message + ", error code: " + err.code); 1892e41f4b71Sopenharmony_ci } else { 1893e41f4b71Sopenharmony_ci console.info("write data to file succeed and size is:" + writeLen); 1894e41f4b71Sopenharmony_ci } 1895e41f4b71Sopenharmony_ci fs.closeSync(file); 1896e41f4b71Sopenharmony_ci }); 1897e41f4b71Sopenharmony_ci ``` 1898e41f4b71Sopenharmony_ci 1899e41f4b71Sopenharmony_ci## fs.writeSync 1900e41f4b71Sopenharmony_ci 1901e41f4b71Sopenharmony_ciwriteSync(fd: number, buffer: ArrayBuffer | string, options?: WriteOptions): number 1902e41f4b71Sopenharmony_ci 1903e41f4b71Sopenharmony_ciWrites data to a file. This API returns the result synchronously. 1904e41f4b71Sopenharmony_ci 1905e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1906e41f4b71Sopenharmony_ci 1907e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1908e41f4b71Sopenharmony_ci 1909e41f4b71Sopenharmony_ci**Parameters** 1910e41f4b71Sopenharmony_ci 1911e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1912e41f4b71Sopenharmony_ci| ------- | ------------------------------- | ---- | ---------------------------------------- | 1913e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 1914e41f4b71Sopenharmony_ci| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | 1915e41f4b71Sopenharmony_ci| options | [WriteOptions](#writeoptions11) | No | The options are as follows:<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported currently.| 1916e41f4b71Sopenharmony_ci 1917e41f4b71Sopenharmony_ci**Return value** 1918e41f4b71Sopenharmony_ci 1919e41f4b71Sopenharmony_ci| Type | Description | 1920e41f4b71Sopenharmony_ci| ------ | -------- | 1921e41f4b71Sopenharmony_ci| number | Length of the data written, in bytes.| 1922e41f4b71Sopenharmony_ci 1923e41f4b71Sopenharmony_ci**Error codes** 1924e41f4b71Sopenharmony_ci 1925e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1926e41f4b71Sopenharmony_ci 1927e41f4b71Sopenharmony_ci**Example** 1928e41f4b71Sopenharmony_ci 1929e41f4b71Sopenharmony_ci ```ts 1930e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1931e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 1932e41f4b71Sopenharmony_ci let str: string = "hello, world"; 1933e41f4b71Sopenharmony_ci let writeLen = fs.writeSync(file.fd, str); 1934e41f4b71Sopenharmony_ci console.info("write data to file succeed and size is:" + writeLen); 1935e41f4b71Sopenharmony_ci fs.closeSync(file); 1936e41f4b71Sopenharmony_ci ``` 1937e41f4b71Sopenharmony_ci 1938e41f4b71Sopenharmony_ci## fs.truncate 1939e41f4b71Sopenharmony_ci 1940e41f4b71Sopenharmony_citruncate(file: string | number, len?: number): Promise<void> 1941e41f4b71Sopenharmony_ci 1942e41f4b71Sopenharmony_ciTruncates the file content. This API uses a promise to return the result. 1943e41f4b71Sopenharmony_ci 1944e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1945e41f4b71Sopenharmony_ci 1946e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1947e41f4b71Sopenharmony_ci 1948e41f4b71Sopenharmony_ci**Parameters** 1949e41f4b71Sopenharmony_ci 1950e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1951e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------- | 1952e41f4b71Sopenharmony_ci| file | string \| number | Yes | Application sandbox path or FD of the file. | 1953e41f4b71Sopenharmony_ci| len | number | No | File length, in bytes, after truncation. The default value is **0**.| 1954e41f4b71Sopenharmony_ci 1955e41f4b71Sopenharmony_ci**Return value** 1956e41f4b71Sopenharmony_ci 1957e41f4b71Sopenharmony_ci| Type | Description | 1958e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 1959e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 1960e41f4b71Sopenharmony_ci 1961e41f4b71Sopenharmony_ci**Error codes** 1962e41f4b71Sopenharmony_ci 1963e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1964e41f4b71Sopenharmony_ci 1965e41f4b71Sopenharmony_ci**Example** 1966e41f4b71Sopenharmony_ci 1967e41f4b71Sopenharmony_ci ```ts 1968e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 1969e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 1970e41f4b71Sopenharmony_ci let len: number = 5; 1971e41f4b71Sopenharmony_ci fs.truncate(filePath, len).then(() => { 1972e41f4b71Sopenharmony_ci console.info("File truncated"); 1973e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1974e41f4b71Sopenharmony_ci console.error("truncate file failed with error message: " + err.message + ", error code: " + err.code); 1975e41f4b71Sopenharmony_ci }); 1976e41f4b71Sopenharmony_ci ``` 1977e41f4b71Sopenharmony_ci 1978e41f4b71Sopenharmony_ci## fs.truncate 1979e41f4b71Sopenharmony_ci 1980e41f4b71Sopenharmony_citruncate(file: string | number, len?: number, callback: AsyncCallback<void>): void 1981e41f4b71Sopenharmony_ci 1982e41f4b71Sopenharmony_ciTruncates the file content. This API uses an asynchronous callback to return the result. 1983e41f4b71Sopenharmony_ci 1984e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 1985e41f4b71Sopenharmony_ci 1986e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 1987e41f4b71Sopenharmony_ci 1988e41f4b71Sopenharmony_ci**Parameters** 1989e41f4b71Sopenharmony_ci 1990e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1991e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | -------------------------------- | 1992e41f4b71Sopenharmony_ci| file | string \| number | Yes | Application sandbox path or FD of the file. | 1993e41f4b71Sopenharmony_ci| len | number | No | File length, in bytes, after truncation. The default value is **0**.| 1994e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback that returns no value. | 1995e41f4b71Sopenharmony_ci 1996e41f4b71Sopenharmony_ci**Error codes** 1997e41f4b71Sopenharmony_ci 1998e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 1999e41f4b71Sopenharmony_ci 2000e41f4b71Sopenharmony_ci**Example** 2001e41f4b71Sopenharmony_ci 2002e41f4b71Sopenharmony_ci ```ts 2003e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2004e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2005e41f4b71Sopenharmony_ci let len: number = 5; 2006e41f4b71Sopenharmony_ci fs.truncate(filePath, len, (err: BusinessError) => { 2007e41f4b71Sopenharmony_ci if (err) { 2008e41f4b71Sopenharmony_ci console.error("truncate failed with error message: " + err.message + ", error code: " + err.code); 2009e41f4b71Sopenharmony_ci } else { 2010e41f4b71Sopenharmony_ci console.info("truncate succeed"); 2011e41f4b71Sopenharmony_ci } 2012e41f4b71Sopenharmony_ci }); 2013e41f4b71Sopenharmony_ci ``` 2014e41f4b71Sopenharmony_ci 2015e41f4b71Sopenharmony_ci## fs.truncateSync 2016e41f4b71Sopenharmony_ci 2017e41f4b71Sopenharmony_citruncateSync(file: string | number, len?: number): void 2018e41f4b71Sopenharmony_ci 2019e41f4b71Sopenharmony_ciTruncates the file content. This API returns the result synchronously. 2020e41f4b71Sopenharmony_ci 2021e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2022e41f4b71Sopenharmony_ci 2023e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2024e41f4b71Sopenharmony_ci 2025e41f4b71Sopenharmony_ci**Parameters** 2026e41f4b71Sopenharmony_ci 2027e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 2028e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------- | 2029e41f4b71Sopenharmony_ci| file | string \| number | Yes | Application sandbox path or FD of the file. | 2030e41f4b71Sopenharmony_ci| len | number | No | File length, in bytes, after truncation. The default value is **0**.| 2031e41f4b71Sopenharmony_ci 2032e41f4b71Sopenharmony_ci**Error codes** 2033e41f4b71Sopenharmony_ci 2034e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2035e41f4b71Sopenharmony_ci 2036e41f4b71Sopenharmony_ci**Example** 2037e41f4b71Sopenharmony_ci 2038e41f4b71Sopenharmony_ci ```ts 2039e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2040e41f4b71Sopenharmony_ci let len: number = 5; 2041e41f4b71Sopenharmony_ci fs.truncateSync(filePath, len); 2042e41f4b71Sopenharmony_ci ``` 2043e41f4b71Sopenharmony_ci 2044e41f4b71Sopenharmony_ci## fs.readLines<sup>11+</sup> 2045e41f4b71Sopenharmony_ci 2046e41f4b71Sopenharmony_cireadLines(filePath: string, options?: Options): Promise<ReaderIterator> 2047e41f4b71Sopenharmony_ci 2048e41f4b71Sopenharmony_ciReads a file text line by line. This API uses a promise to return the result. Only the files in UTF-8 format are supported. 2049e41f4b71Sopenharmony_ci 2050e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2051e41f4b71Sopenharmony_ci 2052e41f4b71Sopenharmony_ci**Parameters** 2053e41f4b71Sopenharmony_ci 2054e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2055e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ | 2056e41f4b71Sopenharmony_ci| filePath | string | Yes | Application sandbox path of the file. | 2057e41f4b71Sopenharmony_ci| options | [Options](#options11) | No | Options for reading the text. The options are as follows:<br>- **encoding** (string): format of the data to be encoded.<br>It is valid only when the data is of the string type.<br>The default value is **'utf-8'**, which is the only value supported.| 2058e41f4b71Sopenharmony_ci 2059e41f4b71Sopenharmony_ci**Return value** 2060e41f4b71Sopenharmony_ci 2061e41f4b71Sopenharmony_ci| Type | Description | 2062e41f4b71Sopenharmony_ci| --------------------- | ---------- | 2063e41f4b71Sopenharmony_ci| Promise<[ReaderIterator](#readeriterator11)> | Promise used to return a **ReaderIterator** object.| 2064e41f4b71Sopenharmony_ci 2065e41f4b71Sopenharmony_ci**Error codes** 2066e41f4b71Sopenharmony_ci 2067e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2068e41f4b71Sopenharmony_ci 2069e41f4b71Sopenharmony_ci**Example** 2070e41f4b71Sopenharmony_ci 2071e41f4b71Sopenharmony_ci ```ts 2072e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2073e41f4b71Sopenharmony_ci import { fileIo as fs, Options } from '@kit.CoreFileKit'; 2074e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2075e41f4b71Sopenharmony_ci let options: Options = { 2076e41f4b71Sopenharmony_ci encoding: 'utf-8' 2077e41f4b71Sopenharmony_ci }; 2078e41f4b71Sopenharmony_ci fs.readLines(filePath, options).then((readerIterator: fs.ReaderIterator) => { 2079e41f4b71Sopenharmony_ci for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) { 2080e41f4b71Sopenharmony_ci console.info("content: " + it.value); 2081e41f4b71Sopenharmony_ci } 2082e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2083e41f4b71Sopenharmony_ci console.error("readLines failed with error message: " + err.message + ", error code: " + err.code); 2084e41f4b71Sopenharmony_ci }); 2085e41f4b71Sopenharmony_ci ``` 2086e41f4b71Sopenharmony_ci 2087e41f4b71Sopenharmony_ci## fs.readLines<sup>11+</sup> 2088e41f4b71Sopenharmony_ci 2089e41f4b71Sopenharmony_cireadLines(filePath: string, options?: Options, callback: AsyncCallback<ReaderIterator>): void 2090e41f4b71Sopenharmony_ci 2091e41f4b71Sopenharmony_ciReads a file text line by line. This API uses an asynchronous callback to return the result. Only the files in UTF-8 format are supported. 2092e41f4b71Sopenharmony_ci 2093e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2094e41f4b71Sopenharmony_ci 2095e41f4b71Sopenharmony_ci**Parameters** 2096e41f4b71Sopenharmony_ci 2097e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2098e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ | 2099e41f4b71Sopenharmony_ci| filePath | string | Yes | Application sandbox path of the file. | 2100e41f4b71Sopenharmony_ci| options | [Options](#options11) | No | Options for reading the text. The options are as follows:<br>- **encoding** (string): format of the data to be encoded.<br>It is valid only when the data is of the string type.<br>The default value is **'utf-8'**, which is the only value supported.| 2101e41f4b71Sopenharmony_ci| callback | AsyncCallback<[ReaderIterator](#readeriterator11)> | Yes | Callback used to return a **ReaderIterator** object. | 2102e41f4b71Sopenharmony_ci 2103e41f4b71Sopenharmony_ci**Error codes** 2104e41f4b71Sopenharmony_ci 2105e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2106e41f4b71Sopenharmony_ci 2107e41f4b71Sopenharmony_ci**Example** 2108e41f4b71Sopenharmony_ci 2109e41f4b71Sopenharmony_ci ```ts 2110e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2111e41f4b71Sopenharmony_ci import { fileIo as fs, Options } from '@kit.CoreFileKit'; 2112e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2113e41f4b71Sopenharmony_ci let options: Options = { 2114e41f4b71Sopenharmony_ci encoding: 'utf-8' 2115e41f4b71Sopenharmony_ci }; 2116e41f4b71Sopenharmony_ci fs.readLines(filePath, options, (err: BusinessError, readerIterator: fs.ReaderIterator) => { 2117e41f4b71Sopenharmony_ci if (err) { 2118e41f4b71Sopenharmony_ci console.error("readLines failed with error message: " + err.message + ", error code: " + err.code); 2119e41f4b71Sopenharmony_ci } else { 2120e41f4b71Sopenharmony_ci for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) { 2121e41f4b71Sopenharmony_ci console.info("content: " + it.value); 2122e41f4b71Sopenharmony_ci } 2123e41f4b71Sopenharmony_ci } 2124e41f4b71Sopenharmony_ci }); 2125e41f4b71Sopenharmony_ci ``` 2126e41f4b71Sopenharmony_ci 2127e41f4b71Sopenharmony_ci## fs.readLinesSync<sup>11+</sup> 2128e41f4b71Sopenharmony_ci 2129e41f4b71Sopenharmony_cireadLinesSync(filePath: string, options?: Options): ReaderIterator 2130e41f4b71Sopenharmony_ci 2131e41f4b71Sopenharmony_ciReads the text content of a file line by line. This API returns the result synchronously. 2132e41f4b71Sopenharmony_ci 2133e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2134e41f4b71Sopenharmony_ci 2135e41f4b71Sopenharmony_ci**Parameters** 2136e41f4b71Sopenharmony_ci 2137e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2138e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ | 2139e41f4b71Sopenharmony_ci| filePath | string | Yes | Application sandbox path of the file. | 2140e41f4b71Sopenharmony_ci| options | [Options](#options11) | No | Options for reading the text. The options are as follows:<br>- **encoding** (string): format of the data to be encoded.<br>It is valid only when the data is of the string type.<br>The default value is **'utf-8'**, which is the only value supported.| 2141e41f4b71Sopenharmony_ci 2142e41f4b71Sopenharmony_ci**Return value** 2143e41f4b71Sopenharmony_ci 2144e41f4b71Sopenharmony_ci| Type | Description | 2145e41f4b71Sopenharmony_ci| --------------------- | ---------- | 2146e41f4b71Sopenharmony_ci| [ReaderIterator](#readeriterator11) | **ReaderIterator** object.| 2147e41f4b71Sopenharmony_ci 2148e41f4b71Sopenharmony_ci**Error codes** 2149e41f4b71Sopenharmony_ci 2150e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2151e41f4b71Sopenharmony_ci 2152e41f4b71Sopenharmony_ci**Example** 2153e41f4b71Sopenharmony_ci 2154e41f4b71Sopenharmony_ci ```ts 2155e41f4b71Sopenharmony_ci import { fileIo as fs, Options } from '@kit.CoreFileKit'; 2156e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2157e41f4b71Sopenharmony_ci let options: Options = { 2158e41f4b71Sopenharmony_ci encoding: 'utf-8' 2159e41f4b71Sopenharmony_ci }; 2160e41f4b71Sopenharmony_ci let readerIterator = fs.readLinesSync(filePath, options); 2161e41f4b71Sopenharmony_ci for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) { 2162e41f4b71Sopenharmony_ci console.info("content: " + it.value); 2163e41f4b71Sopenharmony_ci } 2164e41f4b71Sopenharmony_ci ``` 2165e41f4b71Sopenharmony_ci 2166e41f4b71Sopenharmony_ci## ReaderIterator<sup>11+</sup> 2167e41f4b71Sopenharmony_ci 2168e41f4b71Sopenharmony_ciProvides a **ReaderIterator** object. Before calling APIs of **ReaderIterator**, you need to use **readLines()** to create a **ReaderIterator** instance. 2169e41f4b71Sopenharmony_ci 2170e41f4b71Sopenharmony_ci### next<sup>11+</sup> 2171e41f4b71Sopenharmony_ci 2172e41f4b71Sopenharmony_cinext(): ReaderIteratorResult 2173e41f4b71Sopenharmony_ci 2174e41f4b71Sopenharmony_ciObtains the **ReaderIterator** result. 2175e41f4b71Sopenharmony_ci 2176e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2177e41f4b71Sopenharmony_ci 2178e41f4b71Sopenharmony_ci**Return value** 2179e41f4b71Sopenharmony_ci 2180e41f4b71Sopenharmony_ci| Type | Description | 2181e41f4b71Sopenharmony_ci| --------------------- | ---------- | 2182e41f4b71Sopenharmony_ci| ReaderIteratorResult | **ReaderIteratorResult** object obtained.| 2183e41f4b71Sopenharmony_ci 2184e41f4b71Sopenharmony_ci**Error codes** 2185e41f4b71Sopenharmony_ci 2186e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2187e41f4b71Sopenharmony_ci 2188e41f4b71Sopenharmony_ci**Example** 2189e41f4b71Sopenharmony_ci 2190e41f4b71Sopenharmony_ci ```ts 2191e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2192e41f4b71Sopenharmony_ci import { fileIo as fs, Options } from '@kit.CoreFileKit'; 2193e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2194e41f4b71Sopenharmony_ci let options: Options = { 2195e41f4b71Sopenharmony_ci encoding: 'utf-8' 2196e41f4b71Sopenharmony_ci }; 2197e41f4b71Sopenharmony_ci fs.readLines(filePath, options).then((readerIterator: fs.ReaderIterator) => { 2198e41f4b71Sopenharmony_ci for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) { 2199e41f4b71Sopenharmony_ci console.info("content: " + it.value); 2200e41f4b71Sopenharmony_ci } 2201e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2202e41f4b71Sopenharmony_ci console.error("readLines failed with error message: " + err.message + ", error code: " + err.code); 2203e41f4b71Sopenharmony_ci }); 2204e41f4b71Sopenharmony_ci ``` 2205e41f4b71Sopenharmony_ci 2206e41f4b71Sopenharmony_ci## ReaderIteratorResult 2207e41f4b71Sopenharmony_ci 2208e41f4b71Sopenharmony_ciRepresents the information obtained by the **ReadIterator** object. 2209e41f4b71Sopenharmony_ci 2210e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2211e41f4b71Sopenharmony_ci 2212e41f4b71Sopenharmony_ci| Name | Type | Description | 2213e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | 2214e41f4b71Sopenharmony_ci| done | boolean | Whether the iteration is complete. | 2215e41f4b71Sopenharmony_ci| value | string | File text content read line by line.| 2216e41f4b71Sopenharmony_ci 2217e41f4b71Sopenharmony_ci## fs.readText 2218e41f4b71Sopenharmony_ci 2219e41f4b71Sopenharmony_cireadText(filePath: string, options?: ReadTextOptions): Promise<string> 2220e41f4b71Sopenharmony_ci 2221e41f4b71Sopenharmony_ciReads the text content of a file. This API uses a promise to return the result. 2222e41f4b71Sopenharmony_ci 2223e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2224e41f4b71Sopenharmony_ci 2225e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2226e41f4b71Sopenharmony_ci 2227e41f4b71Sopenharmony_ci**Parameters** 2228e41f4b71Sopenharmony_ci 2229e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2230e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ | 2231e41f4b71Sopenharmony_ci| filePath | string | Yes | Application sandbox path of the file. | 2232e41f4b71Sopenharmony_ci| options | [ReadTextOptions](#readtextoptions11) | No | The options are as follows:<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.<br>- **encoding** (string): format of the data to be encoded.<br>It is valid only when the data is of the string type. The default value is **'utf-8'**, which is the only value supported.| 2233e41f4b71Sopenharmony_ci 2234e41f4b71Sopenharmony_ci**Return value** 2235e41f4b71Sopenharmony_ci 2236e41f4b71Sopenharmony_ci| Type | Description | 2237e41f4b71Sopenharmony_ci| --------------------- | ---------- | 2238e41f4b71Sopenharmony_ci| Promise<string> | Promise used to return the file content read.| 2239e41f4b71Sopenharmony_ci 2240e41f4b71Sopenharmony_ci**Error codes** 2241e41f4b71Sopenharmony_ci 2242e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2243e41f4b71Sopenharmony_ci 2244e41f4b71Sopenharmony_ci**Example** 2245e41f4b71Sopenharmony_ci 2246e41f4b71Sopenharmony_ci ```ts 2247e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2248e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2249e41f4b71Sopenharmony_ci fs.readText(filePath).then((str: string) => { 2250e41f4b71Sopenharmony_ci console.info("readText succeed:" + str); 2251e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2252e41f4b71Sopenharmony_ci console.error("readText failed with error message: " + err.message + ", error code: " + err.code); 2253e41f4b71Sopenharmony_ci }); 2254e41f4b71Sopenharmony_ci ``` 2255e41f4b71Sopenharmony_ci 2256e41f4b71Sopenharmony_ci## fs.readText 2257e41f4b71Sopenharmony_ci 2258e41f4b71Sopenharmony_cireadText(filePath: string, options?: ReadTextOptions, callback: AsyncCallback<string>): void 2259e41f4b71Sopenharmony_ci 2260e41f4b71Sopenharmony_ciReads the text content of a file. This API uses an asynchronous callback to return the result. 2261e41f4b71Sopenharmony_ci 2262e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2263e41f4b71Sopenharmony_ci 2264e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2265e41f4b71Sopenharmony_ci 2266e41f4b71Sopenharmony_ci**Parameters** 2267e41f4b71Sopenharmony_ci 2268e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2269e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 2270e41f4b71Sopenharmony_ci| filePath | string | Yes | Application sandbox path of the file. | 2271e41f4b71Sopenharmony_ci| options | [ReadTextOptions](#readtextoptions11) | No | The options are as follows:<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.<br>- **encoding**: format of the data to be encoded. The default value is **'utf-8'**, which is the only value supported.| 2272e41f4b71Sopenharmony_ci| callback | AsyncCallback<string> | Yes | Callback used to return the content read. | 2273e41f4b71Sopenharmony_ci 2274e41f4b71Sopenharmony_ci**Error codes** 2275e41f4b71Sopenharmony_ci 2276e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2277e41f4b71Sopenharmony_ci 2278e41f4b71Sopenharmony_ci**Example** 2279e41f4b71Sopenharmony_ci 2280e41f4b71Sopenharmony_ci ```ts 2281e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2282e41f4b71Sopenharmony_ci import { fileIo as fs, ReadTextOptions } from '@kit.CoreFileKit'; 2283e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2284e41f4b71Sopenharmony_ci let readTextOption: ReadTextOptions = { 2285e41f4b71Sopenharmony_ci offset: 1, 2286e41f4b71Sopenharmony_ci length: 0, 2287e41f4b71Sopenharmony_ci encoding: 'utf-8' 2288e41f4b71Sopenharmony_ci }; 2289e41f4b71Sopenharmony_ci let stat = fs.statSync(filePath); 2290e41f4b71Sopenharmony_ci readTextOption.length = stat.size; 2291e41f4b71Sopenharmony_ci fs.readText(filePath, readTextOption, (err: BusinessError, str: string) => { 2292e41f4b71Sopenharmony_ci if (err) { 2293e41f4b71Sopenharmony_ci console.error("readText failed with error message: " + err.message + ", error code: " + err.code); 2294e41f4b71Sopenharmony_ci } else { 2295e41f4b71Sopenharmony_ci console.info("readText succeed:" + str); 2296e41f4b71Sopenharmony_ci } 2297e41f4b71Sopenharmony_ci }); 2298e41f4b71Sopenharmony_ci ``` 2299e41f4b71Sopenharmony_ci 2300e41f4b71Sopenharmony_ci## fs.readTextSync 2301e41f4b71Sopenharmony_ci 2302e41f4b71Sopenharmony_cireadTextSync(filePath: string, options?: ReadTextOptions): string 2303e41f4b71Sopenharmony_ci 2304e41f4b71Sopenharmony_ciReads the text of a file. This API returns the result synchronously. 2305e41f4b71Sopenharmony_ci 2306e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2307e41f4b71Sopenharmony_ci 2308e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2309e41f4b71Sopenharmony_ci 2310e41f4b71Sopenharmony_ci**Parameters** 2311e41f4b71Sopenharmony_ci 2312e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2313e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ | 2314e41f4b71Sopenharmony_ci| filePath | string | Yes | Application sandbox path of the file. | 2315e41f4b71Sopenharmony_ci| options | [ReadTextOptions](#readtextoptions11) | No | The options are as follows:<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.<br>- **encoding** (string): format of the data to be encoded.<br>It is valid only when the data is of the string type. The default value is **'utf-8'**, which is the only value supported.| 2316e41f4b71Sopenharmony_ci 2317e41f4b71Sopenharmony_ci**Return value** 2318e41f4b71Sopenharmony_ci 2319e41f4b71Sopenharmony_ci| Type | Description | 2320e41f4b71Sopenharmony_ci| ------ | -------------------- | 2321e41f4b71Sopenharmony_ci| string | Content of the file read.| 2322e41f4b71Sopenharmony_ci 2323e41f4b71Sopenharmony_ci**Error codes** 2324e41f4b71Sopenharmony_ci 2325e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2326e41f4b71Sopenharmony_ci 2327e41f4b71Sopenharmony_ci**Example** 2328e41f4b71Sopenharmony_ci 2329e41f4b71Sopenharmony_ci ```ts 2330e41f4b71Sopenharmony_ci import { fileIo as fs, ReadTextOptions } from '@kit.CoreFileKit'; 2331e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2332e41f4b71Sopenharmony_ci let readTextOptions: ReadTextOptions = { 2333e41f4b71Sopenharmony_ci offset: 1, 2334e41f4b71Sopenharmony_ci length: 0, 2335e41f4b71Sopenharmony_ci encoding: 'utf-8' 2336e41f4b71Sopenharmony_ci }; 2337e41f4b71Sopenharmony_ci let stat = fs.statSync(filePath); 2338e41f4b71Sopenharmony_ci readTextOptions.length = stat.size; 2339e41f4b71Sopenharmony_ci let str = fs.readTextSync(filePath, readTextOptions); 2340e41f4b71Sopenharmony_ci console.info("readText succeed:" + str); 2341e41f4b71Sopenharmony_ci ``` 2342e41f4b71Sopenharmony_ci 2343e41f4b71Sopenharmony_ci## fs.lstat 2344e41f4b71Sopenharmony_ci 2345e41f4b71Sopenharmony_cilstat(path: string): Promise<Stat> 2346e41f4b71Sopenharmony_ci 2347e41f4b71Sopenharmony_ciObtains information about a symbolic link that is used to refer to a file or directory. This API uses a promise to return the result. 2348e41f4b71Sopenharmony_ci 2349e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2350e41f4b71Sopenharmony_ci 2351e41f4b71Sopenharmony_ci**Parameters** 2352e41f4b71Sopenharmony_ci 2353e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 2354e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------- | 2355e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the symbolic link.| 2356e41f4b71Sopenharmony_ci 2357e41f4b71Sopenharmony_ci**Return value** 2358e41f4b71Sopenharmony_ci 2359e41f4b71Sopenharmony_ci| Type | Description | 2360e41f4b71Sopenharmony_ci| ---------------------------- | ---------- | 2361e41f4b71Sopenharmony_ci| Promise<[Stat](#stat)> | Promise used to return the symbolic link information obtained. For details, see **stat**.| 2362e41f4b71Sopenharmony_ci 2363e41f4b71Sopenharmony_ci**Error codes** 2364e41f4b71Sopenharmony_ci 2365e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2366e41f4b71Sopenharmony_ci 2367e41f4b71Sopenharmony_ci**Example** 2368e41f4b71Sopenharmony_ci 2369e41f4b71Sopenharmony_ci ```ts 2370e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2371e41f4b71Sopenharmony_ci let filePath = pathDir + "/linkToFile"; 2372e41f4b71Sopenharmony_ci fs.lstat(filePath).then((stat: fs.Stat) => { 2373e41f4b71Sopenharmony_ci console.info("lstat succeed, the size of file is " + stat.size); 2374e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2375e41f4b71Sopenharmony_ci console.error("lstat failed with error message: " + err.message + ", error code: " + err.code); 2376e41f4b71Sopenharmony_ci }); 2377e41f4b71Sopenharmony_ci ``` 2378e41f4b71Sopenharmony_ci 2379e41f4b71Sopenharmony_ci## fs.lstat 2380e41f4b71Sopenharmony_ci 2381e41f4b71Sopenharmony_cilstat(path: string, callback: AsyncCallback<Stat>): void 2382e41f4b71Sopenharmony_ci 2383e41f4b71Sopenharmony_ciObtains information about a symbolic link that is used to refer to a file or directory. This API uses an asynchronous callback to return the result. 2384e41f4b71Sopenharmony_ci 2385e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2386e41f4b71Sopenharmony_ci 2387e41f4b71Sopenharmony_ci**Parameters** 2388e41f4b71Sopenharmony_ci 2389e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2390e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | ---- | -------------------------------------- | 2391e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the symbolic link.| 2392e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Stat](#stat)> | Yes | Callback used to return the symbolic link information obtained. | 2393e41f4b71Sopenharmony_ci 2394e41f4b71Sopenharmony_ci**Error codes** 2395e41f4b71Sopenharmony_ci 2396e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2397e41f4b71Sopenharmony_ci 2398e41f4b71Sopenharmony_ci**Example** 2399e41f4b71Sopenharmony_ci 2400e41f4b71Sopenharmony_ci ```ts 2401e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2402e41f4b71Sopenharmony_ci let filePath = pathDir + "/linkToFile"; 2403e41f4b71Sopenharmony_ci fs.lstat(filePath, (err: BusinessError, stat: fs.Stat) => { 2404e41f4b71Sopenharmony_ci if (err) { 2405e41f4b71Sopenharmony_ci console.error("lstat failed with error message: " + err.message + ", error code: " + err.code); 2406e41f4b71Sopenharmony_ci } else { 2407e41f4b71Sopenharmony_ci console.info("lstat succeed, the size of file is" + stat.size); 2408e41f4b71Sopenharmony_ci } 2409e41f4b71Sopenharmony_ci }); 2410e41f4b71Sopenharmony_ci ``` 2411e41f4b71Sopenharmony_ci 2412e41f4b71Sopenharmony_ci## fs.lstatSync 2413e41f4b71Sopenharmony_ci 2414e41f4b71Sopenharmony_cilstatSync(path: string): Stat 2415e41f4b71Sopenharmony_ci 2416e41f4b71Sopenharmony_ciObtains information about a symbolic link that is used to refer to a file or directory. This API returns the result synchronously. 2417e41f4b71Sopenharmony_ci 2418e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2419e41f4b71Sopenharmony_ci 2420e41f4b71Sopenharmony_ci**Parameters** 2421e41f4b71Sopenharmony_ci 2422e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 2423e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------- | 2424e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the symbolic link.| 2425e41f4b71Sopenharmony_ci 2426e41f4b71Sopenharmony_ci**Return value** 2427e41f4b71Sopenharmony_ci 2428e41f4b71Sopenharmony_ci| Type | Description | 2429e41f4b71Sopenharmony_ci| ------------- | ---------- | 2430e41f4b71Sopenharmony_ci| [Stat](#stat) | File information obtained.| 2431e41f4b71Sopenharmony_ci 2432e41f4b71Sopenharmony_ci**Error codes** 2433e41f4b71Sopenharmony_ci 2434e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2435e41f4b71Sopenharmony_ci 2436e41f4b71Sopenharmony_ci**Example** 2437e41f4b71Sopenharmony_ci 2438e41f4b71Sopenharmony_ci ```ts 2439e41f4b71Sopenharmony_ci let filePath = pathDir + "/linkToFile"; 2440e41f4b71Sopenharmony_ci let fileStat = fs.lstatSync(filePath); 2441e41f4b71Sopenharmony_ci console.info("lstat succeed, the size of file is" + fileStat.size); 2442e41f4b71Sopenharmony_ci ``` 2443e41f4b71Sopenharmony_ci 2444e41f4b71Sopenharmony_ci## fs.rename 2445e41f4b71Sopenharmony_ci 2446e41f4b71Sopenharmony_cirename(oldPath: string, newPath: string): Promise<void> 2447e41f4b71Sopenharmony_ci 2448e41f4b71Sopenharmony_ciRenames a file or folder. This API uses a promise to return the result. 2449e41f4b71Sopenharmony_ci 2450e41f4b71Sopenharmony_ci> **NOTE** 2451e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 2452e41f4b71Sopenharmony_ci 2453e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2454e41f4b71Sopenharmony_ci 2455e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2456e41f4b71Sopenharmony_ci 2457e41f4b71Sopenharmony_ci**Parameters** 2458e41f4b71Sopenharmony_ci 2459e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2460e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------- | 2461e41f4b71Sopenharmony_ci| oldPath | string | Yes | Application sandbox path of the file or folder to rename.| 2462e41f4b71Sopenharmony_ci| newPath | string | Yes | Application sandbox path of the renamed file or folder. | 2463e41f4b71Sopenharmony_ci 2464e41f4b71Sopenharmony_ci**Return value** 2465e41f4b71Sopenharmony_ci 2466e41f4b71Sopenharmony_ci| Type | Description | 2467e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 2468e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 2469e41f4b71Sopenharmony_ci 2470e41f4b71Sopenharmony_ci**Error codes** 2471e41f4b71Sopenharmony_ci 2472e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2473e41f4b71Sopenharmony_ci 2474e41f4b71Sopenharmony_ci**Example** 2475e41f4b71Sopenharmony_ci 2476e41f4b71Sopenharmony_ci ```ts 2477e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2478e41f4b71Sopenharmony_ci let srcFile = pathDir + "/test.txt"; 2479e41f4b71Sopenharmony_ci let dstFile = pathDir + "/new.txt"; 2480e41f4b71Sopenharmony_ci fs.rename(srcFile, dstFile).then(() => { 2481e41f4b71Sopenharmony_ci console.info("File renamed"); 2482e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2483e41f4b71Sopenharmony_ci console.error("rename failed with error message: " + err.message + ", error code: " + err.code); 2484e41f4b71Sopenharmony_ci }); 2485e41f4b71Sopenharmony_ci ``` 2486e41f4b71Sopenharmony_ci 2487e41f4b71Sopenharmony_ci## fs.rename 2488e41f4b71Sopenharmony_ci 2489e41f4b71Sopenharmony_cirename(oldPath: string, newPath: string, callback: AsyncCallback<void>): void 2490e41f4b71Sopenharmony_ci 2491e41f4b71Sopenharmony_ciRenames a file or folder. This API uses an asynchronous callback to return the result. 2492e41f4b71Sopenharmony_ci 2493e41f4b71Sopenharmony_ci> **NOTE** 2494e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 2495e41f4b71Sopenharmony_ci 2496e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2497e41f4b71Sopenharmony_ci 2498e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2499e41f4b71Sopenharmony_ci 2500e41f4b71Sopenharmony_ci**Parameters** 2501e41f4b71Sopenharmony_ci 2502e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2503e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------------- | 2504e41f4b71Sopenharmony_ci| oldPath | string | Yes | Application sandbox path of the file or folder to rename.| 2505e41f4b71Sopenharmony_ci| newPath | string | Yes | Application sandbox path of the renamed file or folder. | 2506e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 2507e41f4b71Sopenharmony_ci 2508e41f4b71Sopenharmony_ci**Error codes** 2509e41f4b71Sopenharmony_ci 2510e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2511e41f4b71Sopenharmony_ci 2512e41f4b71Sopenharmony_ci**Example** 2513e41f4b71Sopenharmony_ci 2514e41f4b71Sopenharmony_ci ```ts 2515e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2516e41f4b71Sopenharmony_ci let srcFile = pathDir + "/test.txt"; 2517e41f4b71Sopenharmony_ci let dstFile = pathDir + "/new.txt"; 2518e41f4b71Sopenharmony_ci fs.rename(srcFile, dstFile, (err: BusinessError) => { 2519e41f4b71Sopenharmony_ci if (err) { 2520e41f4b71Sopenharmony_ci console.error("rename failed with error message: " + err.message + ", error code: " + err.code); 2521e41f4b71Sopenharmony_ci } else { 2522e41f4b71Sopenharmony_ci console.info("File renamed"); 2523e41f4b71Sopenharmony_ci } 2524e41f4b71Sopenharmony_ci }); 2525e41f4b71Sopenharmony_ci ``` 2526e41f4b71Sopenharmony_ci 2527e41f4b71Sopenharmony_ci## fs.renameSync 2528e41f4b71Sopenharmony_ci 2529e41f4b71Sopenharmony_cirenameSync(oldPath: string, newPath: string): void 2530e41f4b71Sopenharmony_ci 2531e41f4b71Sopenharmony_ciRenames a file or folder. This API returns the result synchronously. 2532e41f4b71Sopenharmony_ci 2533e41f4b71Sopenharmony_ci> **NOTE** 2534e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 2535e41f4b71Sopenharmony_ci 2536e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2537e41f4b71Sopenharmony_ci 2538e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2539e41f4b71Sopenharmony_ci 2540e41f4b71Sopenharmony_ci**Parameters** 2541e41f4b71Sopenharmony_ci 2542e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2543e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------- | 2544e41f4b71Sopenharmony_ci| oldPath | string | Yes | Application sandbox path of the file or folder to rename.| 2545e41f4b71Sopenharmony_ci| newPath | string | Yes | Application sandbox path of the renamed file or folder. | 2546e41f4b71Sopenharmony_ci 2547e41f4b71Sopenharmony_ci**Error codes** 2548e41f4b71Sopenharmony_ci 2549e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2550e41f4b71Sopenharmony_ci 2551e41f4b71Sopenharmony_ci**Example** 2552e41f4b71Sopenharmony_ci 2553e41f4b71Sopenharmony_ci ```ts 2554e41f4b71Sopenharmony_ci let srcFile = pathDir + "/test.txt"; 2555e41f4b71Sopenharmony_ci let dstFile = pathDir + "/new.txt"; 2556e41f4b71Sopenharmony_ci fs.renameSync(srcFile, dstFile); 2557e41f4b71Sopenharmony_ci ``` 2558e41f4b71Sopenharmony_ci 2559e41f4b71Sopenharmony_ci## fs.fsync 2560e41f4b71Sopenharmony_ci 2561e41f4b71Sopenharmony_cifsync(fd: number): Promise<void> 2562e41f4b71Sopenharmony_ci 2563e41f4b71Sopenharmony_ciSynchronizes the cached data of a file to storage. This API uses a promise to return the result. 2564e41f4b71Sopenharmony_ci 2565e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2566e41f4b71Sopenharmony_ci 2567e41f4b71Sopenharmony_ci**Parameters** 2568e41f4b71Sopenharmony_ci 2569e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2570e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------ | 2571e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file.| 2572e41f4b71Sopenharmony_ci 2573e41f4b71Sopenharmony_ci**Return value** 2574e41f4b71Sopenharmony_ci 2575e41f4b71Sopenharmony_ci| Type | Description | 2576e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 2577e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 2578e41f4b71Sopenharmony_ci 2579e41f4b71Sopenharmony_ci**Error codes** 2580e41f4b71Sopenharmony_ci 2581e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2582e41f4b71Sopenharmony_ci 2583e41f4b71Sopenharmony_ci**Example** 2584e41f4b71Sopenharmony_ci 2585e41f4b71Sopenharmony_ci ```ts 2586e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2587e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2588e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 2589e41f4b71Sopenharmony_ci fs.fsync(file.fd).then(() => { 2590e41f4b71Sopenharmony_ci console.info("Data flushed"); 2591e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2592e41f4b71Sopenharmony_ci console.error("sync data failed with error message: " + err.message + ", error code: " + err.code); 2593e41f4b71Sopenharmony_ci }).finally(() => { 2594e41f4b71Sopenharmony_ci fs.closeSync(file); 2595e41f4b71Sopenharmony_ci }); 2596e41f4b71Sopenharmony_ci ``` 2597e41f4b71Sopenharmony_ci 2598e41f4b71Sopenharmony_ci## fs.fsync 2599e41f4b71Sopenharmony_ci 2600e41f4b71Sopenharmony_cifsync(fd: number, callback: AsyncCallback<void>): void 2601e41f4b71Sopenharmony_ci 2602e41f4b71Sopenharmony_ciSynchronizes the cached data of a file to storage. This API uses an asynchronous callback to return the result. 2603e41f4b71Sopenharmony_ci 2604e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2605e41f4b71Sopenharmony_ci 2606e41f4b71Sopenharmony_ci**Parameters** 2607e41f4b71Sopenharmony_ci 2608e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2609e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | --------------- | 2610e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 2611e41f4b71Sopenharmony_ci| Callback | AsyncCallback<void> | Yes | Callback used to return the result.| 2612e41f4b71Sopenharmony_ci 2613e41f4b71Sopenharmony_ci**Error codes** 2614e41f4b71Sopenharmony_ci 2615e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2616e41f4b71Sopenharmony_ci 2617e41f4b71Sopenharmony_ci**Example** 2618e41f4b71Sopenharmony_ci 2619e41f4b71Sopenharmony_ci ```ts 2620e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2621e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2622e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 2623e41f4b71Sopenharmony_ci fs.fsync(file.fd, (err: BusinessError) => { 2624e41f4b71Sopenharmony_ci if (err) { 2625e41f4b71Sopenharmony_ci console.error("fsync failed with error message: " + err.message + ", error code: " + err.code); 2626e41f4b71Sopenharmony_ci } else { 2627e41f4b71Sopenharmony_ci console.info("fsync succeed"); 2628e41f4b71Sopenharmony_ci } 2629e41f4b71Sopenharmony_ci fs.closeSync(file); 2630e41f4b71Sopenharmony_ci }); 2631e41f4b71Sopenharmony_ci ``` 2632e41f4b71Sopenharmony_ci 2633e41f4b71Sopenharmony_ci 2634e41f4b71Sopenharmony_ci## fs.fsyncSync 2635e41f4b71Sopenharmony_ci 2636e41f4b71Sopenharmony_cifsyncSync(fd: number): void 2637e41f4b71Sopenharmony_ci 2638e41f4b71Sopenharmony_ciSynchronizes the cached data of a file to storage. This API returns the result synchronously. 2639e41f4b71Sopenharmony_ci 2640e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2641e41f4b71Sopenharmony_ci 2642e41f4b71Sopenharmony_ci**Parameters** 2643e41f4b71Sopenharmony_ci 2644e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2645e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------ | 2646e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file.| 2647e41f4b71Sopenharmony_ci 2648e41f4b71Sopenharmony_ci**Error codes** 2649e41f4b71Sopenharmony_ci 2650e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2651e41f4b71Sopenharmony_ci 2652e41f4b71Sopenharmony_ci**Example** 2653e41f4b71Sopenharmony_ci 2654e41f4b71Sopenharmony_ci ```ts 2655e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2656e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 2657e41f4b71Sopenharmony_ci fs.fsyncSync(file.fd); 2658e41f4b71Sopenharmony_ci fs.closeSync(file); 2659e41f4b71Sopenharmony_ci ``` 2660e41f4b71Sopenharmony_ci 2661e41f4b71Sopenharmony_ci## fs.fdatasync 2662e41f4b71Sopenharmony_ci 2663e41f4b71Sopenharmony_cifdatasync(fd: number): Promise<void> 2664e41f4b71Sopenharmony_ci 2665e41f4b71Sopenharmony_ciSynchronizes the data of a file. 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. 2666e41f4b71Sopenharmony_ci 2667e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2668e41f4b71Sopenharmony_ci 2669e41f4b71Sopenharmony_ci**Parameters** 2670e41f4b71Sopenharmony_ci 2671e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2672e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------ | 2673e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file.| 2674e41f4b71Sopenharmony_ci 2675e41f4b71Sopenharmony_ci**Return value** 2676e41f4b71Sopenharmony_ci 2677e41f4b71Sopenharmony_ci| Type | Description | 2678e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 2679e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 2680e41f4b71Sopenharmony_ci 2681e41f4b71Sopenharmony_ci**Error codes** 2682e41f4b71Sopenharmony_ci 2683e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2684e41f4b71Sopenharmony_ci 2685e41f4b71Sopenharmony_ci**Example** 2686e41f4b71Sopenharmony_ci 2687e41f4b71Sopenharmony_ci ```ts 2688e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2689e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2690e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 2691e41f4b71Sopenharmony_ci fs.fdatasync(file.fd).then(() => { 2692e41f4b71Sopenharmony_ci console.info("Data flushed"); 2693e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2694e41f4b71Sopenharmony_ci console.error("sync data failed with error message: " + err.message + ", error code: " + err.code); 2695e41f4b71Sopenharmony_ci }).finally(() => { 2696e41f4b71Sopenharmony_ci fs.closeSync(file); 2697e41f4b71Sopenharmony_ci }); 2698e41f4b71Sopenharmony_ci ``` 2699e41f4b71Sopenharmony_ci 2700e41f4b71Sopenharmony_ci## fs.fdatasync 2701e41f4b71Sopenharmony_ci 2702e41f4b71Sopenharmony_cifdatasync(fd: number, callback: AsyncCallback<void>): void 2703e41f4b71Sopenharmony_ci 2704e41f4b71Sopenharmony_ciSynchronizes the data of a file. This API uses an asynchronous callback to return the result. **fdatasync()** is similar to **fsync()**, but does not flush modified metadata unless that metadata is needed. 2705e41f4b71Sopenharmony_ci 2706e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2707e41f4b71Sopenharmony_ci 2708e41f4b71Sopenharmony_ci**Parameters** 2709e41f4b71Sopenharmony_ci 2710e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2711e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ----------------- | 2712e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 2713e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 2714e41f4b71Sopenharmony_ci 2715e41f4b71Sopenharmony_ci**Error codes** 2716e41f4b71Sopenharmony_ci 2717e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2718e41f4b71Sopenharmony_ci 2719e41f4b71Sopenharmony_ci**Example** 2720e41f4b71Sopenharmony_ci 2721e41f4b71Sopenharmony_ci ```ts 2722e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2723e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2724e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 2725e41f4b71Sopenharmony_ci fs.fdatasync (file.fd, (err: BusinessError) => { 2726e41f4b71Sopenharmony_ci if (err) { 2727e41f4b71Sopenharmony_ci console.error("fdatasync failed with error message: " + err.message + ", error code: " + err.code); 2728e41f4b71Sopenharmony_ci } else { 2729e41f4b71Sopenharmony_ci console.info("fdatasync succeed"); 2730e41f4b71Sopenharmony_ci } 2731e41f4b71Sopenharmony_ci fs.closeSync(file); 2732e41f4b71Sopenharmony_ci }); 2733e41f4b71Sopenharmony_ci ``` 2734e41f4b71Sopenharmony_ci 2735e41f4b71Sopenharmony_ci## fs.fdatasyncSync 2736e41f4b71Sopenharmony_ci 2737e41f4b71Sopenharmony_cifdatasyncSync(fd: number): void 2738e41f4b71Sopenharmony_ci 2739e41f4b71Sopenharmony_ciSynchronizes the data of a file. This API returns the result synchronously. **fdatasync()** is similar to **fsync()**, but does not flush modified metadata unless that metadata is needed. 2740e41f4b71Sopenharmony_ci 2741e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2742e41f4b71Sopenharmony_ci 2743e41f4b71Sopenharmony_ci**Parameters** 2744e41f4b71Sopenharmony_ci 2745e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2746e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------ | 2747e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file.| 2748e41f4b71Sopenharmony_ci 2749e41f4b71Sopenharmony_ci**Error codes** 2750e41f4b71Sopenharmony_ci 2751e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2752e41f4b71Sopenharmony_ci 2753e41f4b71Sopenharmony_ci**Example** 2754e41f4b71Sopenharmony_ci 2755e41f4b71Sopenharmony_ci ```ts 2756e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 2757e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 2758e41f4b71Sopenharmony_ci fs.fdatasyncSync(file.fd); 2759e41f4b71Sopenharmony_ci fs.closeSync(file); 2760e41f4b71Sopenharmony_ci ``` 2761e41f4b71Sopenharmony_ci 2762e41f4b71Sopenharmony_ci## fs.symlink 2763e41f4b71Sopenharmony_ci 2764e41f4b71Sopenharmony_cisymlink(target: string, srcPath: string): Promise<void> 2765e41f4b71Sopenharmony_ci 2766e41f4b71Sopenharmony_ciCreates a symbolic link based on a file path. This API uses a promise to return the result. 2767e41f4b71Sopenharmony_ci 2768e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2769e41f4b71Sopenharmony_ci 2770e41f4b71Sopenharmony_ci**Parameters** 2771e41f4b71Sopenharmony_ci 2772e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2773e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------- | 2774e41f4b71Sopenharmony_ci| target | string | Yes | Application sandbox path of the source file. | 2775e41f4b71Sopenharmony_ci| srcPath | string | Yes | Application sandbox path of the symbolic link.| 2776e41f4b71Sopenharmony_ci 2777e41f4b71Sopenharmony_ci**Return value** 2778e41f4b71Sopenharmony_ci 2779e41f4b71Sopenharmony_ci| Type | Description | 2780e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 2781e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 2782e41f4b71Sopenharmony_ci 2783e41f4b71Sopenharmony_ci**Error codes** 2784e41f4b71Sopenharmony_ci 2785e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2786e41f4b71Sopenharmony_ci 2787e41f4b71Sopenharmony_ci**Example** 2788e41f4b71Sopenharmony_ci 2789e41f4b71Sopenharmony_ci ```ts 2790e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2791e41f4b71Sopenharmony_ci let srcFile = pathDir + "/test.txt"; 2792e41f4b71Sopenharmony_ci let dstFile = pathDir + "/test"; 2793e41f4b71Sopenharmony_ci fs.symlink(srcFile, dstFile).then(() => { 2794e41f4b71Sopenharmony_ci console.info("Symbolic link created"); 2795e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2796e41f4b71Sopenharmony_ci console.error("symlink failed with error message: " + err.message + ", error code: " + err.code); 2797e41f4b71Sopenharmony_ci }); 2798e41f4b71Sopenharmony_ci ``` 2799e41f4b71Sopenharmony_ci 2800e41f4b71Sopenharmony_ci 2801e41f4b71Sopenharmony_ci## fs.symlink 2802e41f4b71Sopenharmony_cisymlink(target: string, srcPath: string, callback: AsyncCallback<void>): void 2803e41f4b71Sopenharmony_ci 2804e41f4b71Sopenharmony_ciCreates a symbolic link based on a file path. This API uses an asynchronous callback to return the result. 2805e41f4b71Sopenharmony_ci 2806e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2807e41f4b71Sopenharmony_ci 2808e41f4b71Sopenharmony_ci**Parameters** 2809e41f4b71Sopenharmony_ci 2810e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2811e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | -------------------------------- | 2812e41f4b71Sopenharmony_ci| target | string | Yes | Application sandbox path of the source file. | 2813e41f4b71Sopenharmony_ci| srcPath | string | Yes | Application sandbox path of the symbolic link. | 2814e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 2815e41f4b71Sopenharmony_ci 2816e41f4b71Sopenharmony_ci**Error codes** 2817e41f4b71Sopenharmony_ci 2818e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2819e41f4b71Sopenharmony_ci 2820e41f4b71Sopenharmony_ci**Example** 2821e41f4b71Sopenharmony_ci 2822e41f4b71Sopenharmony_ci ```ts 2823e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2824e41f4b71Sopenharmony_ci let srcFile = pathDir + "/test.txt"; 2825e41f4b71Sopenharmony_ci let dstFile = pathDir + "/test"; 2826e41f4b71Sopenharmony_ci fs.symlink(srcFile, dstFile, (err: BusinessError) => { 2827e41f4b71Sopenharmony_ci if (err) { 2828e41f4b71Sopenharmony_ci console.error("symlink failed with error message: " + err.message + ", error code: " + err.code); 2829e41f4b71Sopenharmony_ci } else { 2830e41f4b71Sopenharmony_ci console.info("Symbolic link created"); 2831e41f4b71Sopenharmony_ci } 2832e41f4b71Sopenharmony_ci }); 2833e41f4b71Sopenharmony_ci ``` 2834e41f4b71Sopenharmony_ci 2835e41f4b71Sopenharmony_ci## fs.symlinkSync 2836e41f4b71Sopenharmony_ci 2837e41f4b71Sopenharmony_cisymlinkSync(target: string, srcPath: string): void 2838e41f4b71Sopenharmony_ci 2839e41f4b71Sopenharmony_ciCreates a symbolic link based on a file path. This API returns the result synchronously. 2840e41f4b71Sopenharmony_ci 2841e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2842e41f4b71Sopenharmony_ci 2843e41f4b71Sopenharmony_ci**Parameters** 2844e41f4b71Sopenharmony_ci 2845e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2846e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------- | 2847e41f4b71Sopenharmony_ci| target | string | Yes | Application sandbox path of the source file. | 2848e41f4b71Sopenharmony_ci| srcPath | string | Yes | Application sandbox path of the symbolic link.| 2849e41f4b71Sopenharmony_ci 2850e41f4b71Sopenharmony_ci**Error codes** 2851e41f4b71Sopenharmony_ci 2852e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2853e41f4b71Sopenharmony_ci 2854e41f4b71Sopenharmony_ci**Example** 2855e41f4b71Sopenharmony_ci 2856e41f4b71Sopenharmony_ci ```ts 2857e41f4b71Sopenharmony_ci let srcFile = pathDir + "/test.txt"; 2858e41f4b71Sopenharmony_ci let dstFile = pathDir + "/test"; 2859e41f4b71Sopenharmony_ci fs.symlinkSync(srcFile, dstFile); 2860e41f4b71Sopenharmony_ci ``` 2861e41f4b71Sopenharmony_ci 2862e41f4b71Sopenharmony_ci## fs.listFile 2863e41f4b71Sopenharmony_cilistFile(path: string, options?: ListFileOptions): Promise<string[]> 2864e41f4b71Sopenharmony_ci 2865e41f4b71Sopenharmony_ciLists all files, including the files in subfolders, in a directory. This API uses a promise to return the result.<br>You can also set a filter to list the files you want. 2866e41f4b71Sopenharmony_ci 2867e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2868e41f4b71Sopenharmony_ci 2869e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2870e41f4b71Sopenharmony_ci 2871e41f4b71Sopenharmony_ci**Parameters** 2872e41f4b71Sopenharmony_ci 2873e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2874e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 2875e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the folder.| 2876e41f4b71Sopenharmony_ci| options | [ListFileOptions](#listfileoptions11) | No | Options for filtering files. The files are not filtered by default.| 2877e41f4b71Sopenharmony_ci 2878e41f4b71Sopenharmony_ci 2879e41f4b71Sopenharmony_ci**Return value** 2880e41f4b71Sopenharmony_ci 2881e41f4b71Sopenharmony_ci| Type | Description | 2882e41f4b71Sopenharmony_ci| --------------------- | ---------- | 2883e41f4b71Sopenharmony_ci| Promise<string[]> | Promise used to return the file names listed.| 2884e41f4b71Sopenharmony_ci 2885e41f4b71Sopenharmony_ci**Error codes** 2886e41f4b71Sopenharmony_ci 2887e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2888e41f4b71Sopenharmony_ci 2889e41f4b71Sopenharmony_ci**Example** 2890e41f4b71Sopenharmony_ci 2891e41f4b71Sopenharmony_ci ```ts 2892e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2893e41f4b71Sopenharmony_ci import { fileIo as fs, Filter, ListFileOptions } from '@kit.CoreFileKit'; 2894e41f4b71Sopenharmony_ci let listFileOption: ListFileOptions = { 2895e41f4b71Sopenharmony_ci recursion: false, 2896e41f4b71Sopenharmony_ci listNum: 0, 2897e41f4b71Sopenharmony_ci filter: { 2898e41f4b71Sopenharmony_ci suffix: [".png", ".jpg", ".jpeg"], 2899e41f4b71Sopenharmony_ci displayName: ["*abc", "efg*"], 2900e41f4b71Sopenharmony_ci fileSizeOver: 1024 2901e41f4b71Sopenharmony_ci } 2902e41f4b71Sopenharmony_ci } 2903e41f4b71Sopenharmony_ci fs.listFile(pathDir, listFileOption).then((filenames: Array<string>) => { 2904e41f4b71Sopenharmony_ci console.info("listFile succeed"); 2905e41f4b71Sopenharmony_ci for (let i = 0; i < filenames.length; i++) { 2906e41f4b71Sopenharmony_ci console.info("fileName: %s", filenames[i]); 2907e41f4b71Sopenharmony_ci } 2908e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2909e41f4b71Sopenharmony_ci console.error("list file failed with error message: " + err.message + ", error code: " + err.code); 2910e41f4b71Sopenharmony_ci }); 2911e41f4b71Sopenharmony_ci ``` 2912e41f4b71Sopenharmony_ci 2913e41f4b71Sopenharmony_ci## fs.listFile 2914e41f4b71Sopenharmony_cilistFile(path: string, options?: ListFileOptions, callback: AsyncCallback<string[]>): void 2915e41f4b71Sopenharmony_ci 2916e41f4b71Sopenharmony_ciLists all files, including the files in subfolders, in a directory. This API uses an asynchronous callback to return the result.<br>You can also set a filter to list the files you want. 2917e41f4b71Sopenharmony_ci 2918e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2919e41f4b71Sopenharmony_ci 2920e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2921e41f4b71Sopenharmony_ci 2922e41f4b71Sopenharmony_ci**Parameters** 2923e41f4b71Sopenharmony_ci 2924e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2925e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 2926e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the folder.| 2927e41f4b71Sopenharmony_ci| options | [ListFileOptions](#listfileoptions11) | No | Options for filtering files. The files are not filtered by default.| 2928e41f4b71Sopenharmony_ci| callback | AsyncCallback<string[]> | Yes | Callback used to return the file names listed. | 2929e41f4b71Sopenharmony_ci 2930e41f4b71Sopenharmony_ci 2931e41f4b71Sopenharmony_ci**Error codes** 2932e41f4b71Sopenharmony_ci 2933e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2934e41f4b71Sopenharmony_ci 2935e41f4b71Sopenharmony_ci**Example** 2936e41f4b71Sopenharmony_ci 2937e41f4b71Sopenharmony_ci ```ts 2938e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 2939e41f4b71Sopenharmony_ci import { fileIo as fs, Filter, ListFileOptions } from '@kit.CoreFileKit'; 2940e41f4b71Sopenharmony_ci let listFileOption: ListFileOptions = { 2941e41f4b71Sopenharmony_ci recursion: false, 2942e41f4b71Sopenharmony_ci listNum: 0, 2943e41f4b71Sopenharmony_ci filter: { 2944e41f4b71Sopenharmony_ci suffix: [".png", ".jpg", ".jpeg"], 2945e41f4b71Sopenharmony_ci displayName: ["*abc", "efg*"], 2946e41f4b71Sopenharmony_ci fileSizeOver: 1024 2947e41f4b71Sopenharmony_ci } 2948e41f4b71Sopenharmony_ci }; 2949e41f4b71Sopenharmony_ci fs.listFile(pathDir, listFileOption, (err: BusinessError, filenames: Array<string>) => { 2950e41f4b71Sopenharmony_ci if (err) { 2951e41f4b71Sopenharmony_ci console.error("list file failed with error message: " + err.message + ", error code: " + err.code); 2952e41f4b71Sopenharmony_ci } else { 2953e41f4b71Sopenharmony_ci console.info("listFile succeed"); 2954e41f4b71Sopenharmony_ci for (let i = 0; i < filenames.length; i++) { 2955e41f4b71Sopenharmony_ci console.info("filename: %s", filenames[i]); 2956e41f4b71Sopenharmony_ci } 2957e41f4b71Sopenharmony_ci } 2958e41f4b71Sopenharmony_ci }); 2959e41f4b71Sopenharmony_ci ``` 2960e41f4b71Sopenharmony_ci 2961e41f4b71Sopenharmony_ci## fs.listFileSync 2962e41f4b71Sopenharmony_ci 2963e41f4b71Sopenharmony_cilistFileSync(path: string, options?: ListFileOptions): string[] 2964e41f4b71Sopenharmony_ci 2965e41f4b71Sopenharmony_ciLists all files, including the files in subfolders, in a directory. This API returns the result synchronously.<br>You can also set a filter to list the files you want. 2966e41f4b71Sopenharmony_ci 2967e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 2968e41f4b71Sopenharmony_ci 2969e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 2970e41f4b71Sopenharmony_ci 2971e41f4b71Sopenharmony_ci**Parameters** 2972e41f4b71Sopenharmony_ci 2973e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2974e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 2975e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the folder.| 2976e41f4b71Sopenharmony_ci| options | [ListFileOptions](#listfileoptions11) | No | Options for filtering files. The files are not filtered by default.| 2977e41f4b71Sopenharmony_ci 2978e41f4b71Sopenharmony_ci 2979e41f4b71Sopenharmony_ci**Return value** 2980e41f4b71Sopenharmony_ci 2981e41f4b71Sopenharmony_ci| Type | Description | 2982e41f4b71Sopenharmony_ci| --------------------- | ---------- | 2983e41f4b71Sopenharmony_ci| string[] | List of the files obtained.| 2984e41f4b71Sopenharmony_ci 2985e41f4b71Sopenharmony_ci**Error codes** 2986e41f4b71Sopenharmony_ci 2987e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 2988e41f4b71Sopenharmony_ci 2989e41f4b71Sopenharmony_ci**Example** 2990e41f4b71Sopenharmony_ci 2991e41f4b71Sopenharmony_ci ```ts 2992e41f4b71Sopenharmony_ci import { fileIo as fs, Filter, ListFileOptions} from '@kit.CoreFileKit'; 2993e41f4b71Sopenharmony_ci let listFileOption: ListFileOptions = { 2994e41f4b71Sopenharmony_ci recursion: false, 2995e41f4b71Sopenharmony_ci listNum: 0, 2996e41f4b71Sopenharmony_ci filter: { 2997e41f4b71Sopenharmony_ci suffix: [".png", ".jpg", ".jpeg"], 2998e41f4b71Sopenharmony_ci displayName: ["*abc", "efg*"], 2999e41f4b71Sopenharmony_ci fileSizeOver: 1024 3000e41f4b71Sopenharmony_ci } 3001e41f4b71Sopenharmony_ci }; 3002e41f4b71Sopenharmony_ci let filenames = fs.listFileSync(pathDir, listFileOption); 3003e41f4b71Sopenharmony_ci console.info("listFile succeed"); 3004e41f4b71Sopenharmony_ci for (let i = 0; i < filenames.length; i++) { 3005e41f4b71Sopenharmony_ci console.info("filename: %s", filenames[i]); 3006e41f4b71Sopenharmony_ci } 3007e41f4b71Sopenharmony_ci ``` 3008e41f4b71Sopenharmony_ci 3009e41f4b71Sopenharmony_ci## fs.lseek<sup>11+</sup> 3010e41f4b71Sopenharmony_ci 3011e41f4b71Sopenharmony_cilseek(fd: number, offset: number, whence?: WhenceType): number 3012e41f4b71Sopenharmony_ci 3013e41f4b71Sopenharmony_ciSets the offset of a file. 3014e41f4b71Sopenharmony_ci 3015e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3016e41f4b71Sopenharmony_ci 3017e41f4b71Sopenharmony_ci**Parameters** 3018e41f4b71Sopenharmony_ci 3019e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3020e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3021e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file.| 3022e41f4b71Sopenharmony_ci| offset | number | Yes | Offset to set, in bytes.| 3023e41f4b71Sopenharmony_ci| whence | [WhenceType](#whencetype11) | No | Where to start the offset.| 3024e41f4b71Sopenharmony_ci 3025e41f4b71Sopenharmony_ci**Return value** 3026e41f4b71Sopenharmony_ci 3027e41f4b71Sopenharmony_ci| Type | Description | 3028e41f4b71Sopenharmony_ci| --------------------- | ---------- | 3029e41f4b71Sopenharmony_ci| number | Position of the current offset as measured from the beginning of the file.| 3030e41f4b71Sopenharmony_ci 3031e41f4b71Sopenharmony_ci**Error codes** 3032e41f4b71Sopenharmony_ci 3033e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3034e41f4b71Sopenharmony_ci 3035e41f4b71Sopenharmony_ci**Example** 3036e41f4b71Sopenharmony_ci 3037e41f4b71Sopenharmony_ci ```ts 3038e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3039e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 3040e41f4b71Sopenharmony_ci console.info('The current offset is at ' + fs.lseek(file.fd, 5, fs.WhenceType.SEEK_SET)); 3041e41f4b71Sopenharmony_ci fs.closeSync(file); 3042e41f4b71Sopenharmony_ci ``` 3043e41f4b71Sopenharmony_ci 3044e41f4b71Sopenharmony_ci## fs.moveDir<sup>10+</sup> 3045e41f4b71Sopenharmony_ci 3046e41f4b71Sopenharmony_cimoveDir(src: string, dest: string, mode?: number): Promise\<void> 3047e41f4b71Sopenharmony_ci 3048e41f4b71Sopenharmony_ciMoves a folder. This API uses a promise to return the result. 3049e41f4b71Sopenharmony_ci 3050e41f4b71Sopenharmony_ci> **NOTE** 3051e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 3052e41f4b71Sopenharmony_ci 3053e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3054e41f4b71Sopenharmony_ci 3055e41f4b71Sopenharmony_ci**Parameters** 3056e41f4b71Sopenharmony_ci 3057e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3058e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3059e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the folder to move.| 3060e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination folder.| 3061e41f4b71Sopenharmony_ci| mode | number | No | Mode for moving the folder. The default value is **0**.<br>- **0**: Throw an exception if a directory conflict occurs.<br> Throw an exception if there is a non-empty folder with the same name in the destination directory.<br>- **1**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a folder with the same name in the destination folder and there are files with the same name in the conflicting folder. All the non-conflicting files in the source folder will be moved to the destination folder, and the non-conflicting files in the destination folder will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **2**: Forcibly overwrite the conflicting files in the destination directory. If there is a folder with the same name in the destination directory and there are files with the same name in the conflicting folder, all the files with the same name in the destination folder will be overwritten and the non-conflicting files will be retained.<br>- **3**: Forcibly overwrite the conflicting folder.<br> Move the source folder to the destination directory and overwrite the conflicting folder completely. That is, if there is a folder with the same name in the destination directory, all the original files in that folder will not be retained.| 3062e41f4b71Sopenharmony_ci 3063e41f4b71Sopenharmony_ci**Return value** 3064e41f4b71Sopenharmony_ci 3065e41f4b71Sopenharmony_ci| Type | Description | 3066e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 3067e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 3068e41f4b71Sopenharmony_ci 3069e41f4b71Sopenharmony_ci**Error codes** 3070e41f4b71Sopenharmony_ci 3071e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3072e41f4b71Sopenharmony_ci 3073e41f4b71Sopenharmony_ci**Example** 3074e41f4b71Sopenharmony_ci 3075e41f4b71Sopenharmony_ci ```ts 3076e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3077e41f4b71Sopenharmony_ci // move directory from srcPath to destPath 3078e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/"; 3079e41f4b71Sopenharmony_ci let destPath = pathDir + "/destDir/"; 3080e41f4b71Sopenharmony_ci fs.moveDir(srcPath, destPath, 1).then(() => { 3081e41f4b71Sopenharmony_ci console.info("move directory succeed"); 3082e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3083e41f4b71Sopenharmony_ci console.error("move directory failed with error message: " + err.message + ", error code: " + err.code); 3084e41f4b71Sopenharmony_ci }); 3085e41f4b71Sopenharmony_ci ``` 3086e41f4b71Sopenharmony_ci 3087e41f4b71Sopenharmony_ci## fs.moveDir<sup>10+</sup> 3088e41f4b71Sopenharmony_ci 3089e41f4b71Sopenharmony_cimoveDir(src: string, dest: string, mode: number, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void 3090e41f4b71Sopenharmony_ci 3091e41f4b71Sopenharmony_ciMoves a folder with the specified mode. This API uses an asynchronous callback to return the result. 3092e41f4b71Sopenharmony_ci 3093e41f4b71Sopenharmony_ci> **NOTE** 3094e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 3095e41f4b71Sopenharmony_ci 3096e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3097e41f4b71Sopenharmony_ci 3098e41f4b71Sopenharmony_ci**Parameters** 3099e41f4b71Sopenharmony_ci 3100e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3101e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3102e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the folder to move.| 3103e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination folder.| 3104e41f4b71Sopenharmony_ci| mode | number | Yes | Mode for moving the folder. The default value is **0**.<br>- **0**: Throw an exception if a directory conflict occurs.<br> Throw an exception if there is a folder with the same name in the destination directory.<br>- **1**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a folder with the same name in the destination folder and there are files with the same name in the conflicting folder. All the non-conflicting files in the source folder will be moved to the destination folder, and the non-conflicting files in the destination folder will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **2**: Forcibly overwrite the conflicting files in the destination directory. If there is a folder with the same name in the destination directory and there are files with the same name in the conflicting folder, all the files with the same name in the destination folder will be overwritten and the non-conflicting files will be retained.<br>- **3**: Forcibly overwrite the conflicting folder.<br> Move the source folder to the destination directory and overwrite the conflicting folder completely. That is, if there is a folder with the same name in the destination directory, all the original files in that folder will not be retained.| 3105e41f4b71Sopenharmony_ci| callback | AsyncCallback<void, Array<[ConflictFiles](#conflictfiles10)>> | Yes | Callback used to return the result. | 3106e41f4b71Sopenharmony_ci 3107e41f4b71Sopenharmony_ci**Error codes** 3108e41f4b71Sopenharmony_ci 3109e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3110e41f4b71Sopenharmony_ci 3111e41f4b71Sopenharmony_ci**Example** 3112e41f4b71Sopenharmony_ci 3113e41f4b71Sopenharmony_ci ```ts 3114e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3115e41f4b71Sopenharmony_ci import { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit'; 3116e41f4b71Sopenharmony_ci // move directory from srcPath to destPath 3117e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/"; 3118e41f4b71Sopenharmony_ci let destPath = pathDir + "/destDir/"; 3119e41f4b71Sopenharmony_ci fs.moveDir(srcPath, destPath, 1, (err: BusinessError<Array<ConflictFiles>>) => { 3120e41f4b71Sopenharmony_ci if (err && err.code == 13900015 && err.data?.length !== undefined) { 3121e41f4b71Sopenharmony_ci for (let i = 0; i < err.data.length; i++) { 3122e41f4b71Sopenharmony_ci console.error("move directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile); 3123e41f4b71Sopenharmony_ci } 3124e41f4b71Sopenharmony_ci } else if (err) { 3125e41f4b71Sopenharmony_ci console.error("move directory failed with error message: " + err.message + ", error code: " + err.code); 3126e41f4b71Sopenharmony_ci } else { 3127e41f4b71Sopenharmony_ci console.info("move directory succeed"); 3128e41f4b71Sopenharmony_ci } 3129e41f4b71Sopenharmony_ci }); 3130e41f4b71Sopenharmony_ci ``` 3131e41f4b71Sopenharmony_ci 3132e41f4b71Sopenharmony_ci ## fs.moveDir<sup>10+</sup> 3133e41f4b71Sopenharmony_ci 3134e41f4b71Sopenharmony_cimoveDir(src: string, dest: string, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void 3135e41f4b71Sopenharmony_ci 3136e41f4b71Sopenharmony_ciMoves a folder. This API uses an asynchronous callback to return the result. 3137e41f4b71Sopenharmony_ci 3138e41f4b71Sopenharmony_ciAn exception will be thrown if there is a folder with the same name in the destination directory. 3139e41f4b71Sopenharmony_ci 3140e41f4b71Sopenharmony_ci> **NOTE** 3141e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 3142e41f4b71Sopenharmony_ci 3143e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3144e41f4b71Sopenharmony_ci 3145e41f4b71Sopenharmony_ci**Parameters** 3146e41f4b71Sopenharmony_ci 3147e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3148e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3149e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the folder to move.| 3150e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination folder.| 3151e41f4b71Sopenharmony_ci| callback | AsyncCallback<void, Array<[ConflictFiles](#conflictfiles10)>> | Yes | Callback used to return the result. | 3152e41f4b71Sopenharmony_ci 3153e41f4b71Sopenharmony_ci**Error codes** 3154e41f4b71Sopenharmony_ci 3155e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3156e41f4b71Sopenharmony_ci 3157e41f4b71Sopenharmony_ci**Example** 3158e41f4b71Sopenharmony_ci 3159e41f4b71Sopenharmony_ci ```ts 3160e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3161e41f4b71Sopenharmony_ci import { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit'; 3162e41f4b71Sopenharmony_ci // move directory from srcPath to destPath 3163e41f4b71Sopenharmony_ci let srcPath = pathDir + "/srcDir/"; 3164e41f4b71Sopenharmony_ci let destPath = pathDir + "/destDir/"; 3165e41f4b71Sopenharmony_ci fs.moveDir(srcPath, destPath, (err: BusinessError<Array<ConflictFiles>>) => { 3166e41f4b71Sopenharmony_ci if (err && err.code == 13900015 && err.data?.length !== undefined) { 3167e41f4b71Sopenharmony_ci for (let i = 0; i < err.data.length; i++) { 3168e41f4b71Sopenharmony_ci console.error("move directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile); 3169e41f4b71Sopenharmony_ci } 3170e41f4b71Sopenharmony_ci } else if (err) { 3171e41f4b71Sopenharmony_ci console.error("move directory failed with error message: " + err.message + ", error code: " + err.code); 3172e41f4b71Sopenharmony_ci } else { 3173e41f4b71Sopenharmony_ci console.info("move directory succeed"); 3174e41f4b71Sopenharmony_ci } 3175e41f4b71Sopenharmony_ci }); 3176e41f4b71Sopenharmony_ci ``` 3177e41f4b71Sopenharmony_ci 3178e41f4b71Sopenharmony_ci## fs.moveDirSync<sup>10+</sup> 3179e41f4b71Sopenharmony_ci 3180e41f4b71Sopenharmony_cimoveDirSync(src: string, dest: string, mode?: number): void 3181e41f4b71Sopenharmony_ci 3182e41f4b71Sopenharmony_ciMoves a folder. This API returns the result synchronously. 3183e41f4b71Sopenharmony_ci 3184e41f4b71Sopenharmony_ci> **NOTE** 3185e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 3186e41f4b71Sopenharmony_ci 3187e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3188e41f4b71Sopenharmony_ci 3189e41f4b71Sopenharmony_ci**Parameters** 3190e41f4b71Sopenharmony_ci 3191e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3192e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3193e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the folder to move.| 3194e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination folder.| 3195e41f4b71Sopenharmony_ci| mode | number | No | Mode for moving the folder. The default value is **0**.<br>- **0**: Throw an exception if a directory conflict occurs.<br> Throw an exception if there is a folder with the same name in the destination directory.<br>- **1**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a folder with the same name in the destination folder and there are files with the same name in the conflicting folder. All the non-conflicting files in the source folder will be moved to the destination folder, and the non-conflicting files in the destination folder will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **2**: Forcibly overwrite the conflicting files in the destination directory. If there is a folder with the same name in the destination directory and there are files with the same name in the conflicting folder, all the files with the same name in the destination folder will be overwritten and the non-conflicting files will be retained.<br>- **3**: Forcibly overwrite the conflicting folder.<br> Move the source folder to the destination directory and overwrite the conflicting folder completely. That is, if there is a folder with the same name in the destination directory, all the original files in that folder will not be retained.| 3196e41f4b71Sopenharmony_ci 3197e41f4b71Sopenharmony_ci**Error codes** 3198e41f4b71Sopenharmony_ci 3199e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3200e41f4b71Sopenharmony_ci 3201e41f4b71Sopenharmony_ci**Example** 3202e41f4b71Sopenharmony_ci 3203e41f4b71Sopenharmony_ci ```ts 3204e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3205e41f4b71Sopenharmony_ciimport { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit'; 3206e41f4b71Sopenharmony_ci// move directory from srcPath to destPath 3207e41f4b71Sopenharmony_cilet srcPath = pathDir + "/srcDir/"; 3208e41f4b71Sopenharmony_cilet destPath = pathDir + "/destDir/"; 3209e41f4b71Sopenharmony_citry { 3210e41f4b71Sopenharmony_ci fs.moveDirSync(srcPath, destPath, 1); 3211e41f4b71Sopenharmony_ci console.info("move directory succeed"); 3212e41f4b71Sopenharmony_ci} catch (error) { 3213e41f4b71Sopenharmony_ci let err: BusinessError<Array<ConflictFiles>> = error as BusinessError<Array<ConflictFiles>>; 3214e41f4b71Sopenharmony_ci if (err.code == 13900015 && err.data?.length !== undefined) { 3215e41f4b71Sopenharmony_ci for (let i = 0; i < err.data.length; i++) { 3216e41f4b71Sopenharmony_ci console.error("move directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile); 3217e41f4b71Sopenharmony_ci } 3218e41f4b71Sopenharmony_ci } else { 3219e41f4b71Sopenharmony_ci console.error("move directory failed with error message: " + err.message + ", error code: " + err.code); 3220e41f4b71Sopenharmony_ci } 3221e41f4b71Sopenharmony_ci} 3222e41f4b71Sopenharmony_ci ``` 3223e41f4b71Sopenharmony_ci 3224e41f4b71Sopenharmony_ci## fs.moveFile 3225e41f4b71Sopenharmony_ci 3226e41f4b71Sopenharmony_cimoveFile(src: string, dest: string, mode?: number): Promise\<void> 3227e41f4b71Sopenharmony_ci 3228e41f4b71Sopenharmony_ciMoves a file. This API uses a promise to return the result. 3229e41f4b71Sopenharmony_ci 3230e41f4b71Sopenharmony_ci> **NOTE** 3231e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 3232e41f4b71Sopenharmony_ci 3233e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3234e41f4b71Sopenharmony_ci 3235e41f4b71Sopenharmony_ci**Parameters** 3236e41f4b71Sopenharmony_ci 3237e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3238e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3239e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the file to move. | 3240e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination file.| 3241e41f4b71Sopenharmony_ci| mode | number | No | Whether to overwrite the file with the same name in the destination directory.<br> The value **0** means to overwrite the file with the same name in the destination directory; the value **1** means to throw an exception.<br> The default value is **0**.| 3242e41f4b71Sopenharmony_ci 3243e41f4b71Sopenharmony_ci**Return value** 3244e41f4b71Sopenharmony_ci 3245e41f4b71Sopenharmony_ci| Type | Description | 3246e41f4b71Sopenharmony_ci| ------------------- | ---------------------------- | 3247e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 3248e41f4b71Sopenharmony_ci 3249e41f4b71Sopenharmony_ci**Error codes** 3250e41f4b71Sopenharmony_ci 3251e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3252e41f4b71Sopenharmony_ci 3253e41f4b71Sopenharmony_ci**Example** 3254e41f4b71Sopenharmony_ci 3255e41f4b71Sopenharmony_ci ```ts 3256e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3257e41f4b71Sopenharmony_ci let srcPath = pathDir + "/source.txt"; 3258e41f4b71Sopenharmony_ci let destPath = pathDir + "/dest.txt"; 3259e41f4b71Sopenharmony_ci fs.moveFile(srcPath, destPath, 0).then(() => { 3260e41f4b71Sopenharmony_ci console.info("move file succeed"); 3261e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3262e41f4b71Sopenharmony_ci console.error("move file failed with error message: " + err.message + ", error code: " + err.code); 3263e41f4b71Sopenharmony_ci }); 3264e41f4b71Sopenharmony_ci ``` 3265e41f4b71Sopenharmony_ci 3266e41f4b71Sopenharmony_ci## fs.moveFile 3267e41f4b71Sopenharmony_ci 3268e41f4b71Sopenharmony_cimoveFile(src: string, dest: string, mode: number, callback: AsyncCallback\<void>): void 3269e41f4b71Sopenharmony_ci 3270e41f4b71Sopenharmony_ciMoves a file with the specified mode. This API uses an asynchronous callback to return the result. 3271e41f4b71Sopenharmony_ci 3272e41f4b71Sopenharmony_ci> **NOTE** 3273e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 3274e41f4b71Sopenharmony_ci 3275e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3276e41f4b71Sopenharmony_ci 3277e41f4b71Sopenharmony_ci**Parameters** 3278e41f4b71Sopenharmony_ci 3279e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3280e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3281e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the source file.| 3282e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination file.| 3283e41f4b71Sopenharmony_ci| mode | number | Yes | Whether to overwrite the file with the same name in the destination directory.<br> The value **0** means to overwrite the file with the same name in the destination directory; the value **1** means to throw an exception.<br> The default value is **0**.| 3284e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 3285e41f4b71Sopenharmony_ci 3286e41f4b71Sopenharmony_ci**Error codes** 3287e41f4b71Sopenharmony_ci 3288e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3289e41f4b71Sopenharmony_ci 3290e41f4b71Sopenharmony_ci**Example** 3291e41f4b71Sopenharmony_ci 3292e41f4b71Sopenharmony_ci ```ts 3293e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3294e41f4b71Sopenharmony_ci let srcPath = pathDir + "/source.txt"; 3295e41f4b71Sopenharmony_ci let destPath = pathDir + "/dest.txt"; 3296e41f4b71Sopenharmony_ci fs.moveFile(srcPath, destPath, 0, (err: BusinessError) => { 3297e41f4b71Sopenharmony_ci if (err) { 3298e41f4b71Sopenharmony_ci console.error("move file failed with error message: " + err.message + ", error code: " + err.code); 3299e41f4b71Sopenharmony_ci } else { 3300e41f4b71Sopenharmony_ci console.info("move file succeed"); 3301e41f4b71Sopenharmony_ci } 3302e41f4b71Sopenharmony_ci }); 3303e41f4b71Sopenharmony_ci ``` 3304e41f4b71Sopenharmony_ci 3305e41f4b71Sopenharmony_ci## fs.moveFile 3306e41f4b71Sopenharmony_ci 3307e41f4b71Sopenharmony_cimoveFile(src: string, dest: string, callback: AsyncCallback\<void>): void 3308e41f4b71Sopenharmony_ci 3309e41f4b71Sopenharmony_ciMoves a file and forcibly overwrites the file with the same name in the destination directory. This API uses an asynchronous callback to return the result. 3310e41f4b71Sopenharmony_ci 3311e41f4b71Sopenharmony_ci> **NOTE** 3312e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 3313e41f4b71Sopenharmony_ci 3314e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3315e41f4b71Sopenharmony_ci 3316e41f4b71Sopenharmony_ci**Parameters** 3317e41f4b71Sopenharmony_ci 3318e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3319e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3320e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the source file.| 3321e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination file.| 3322e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 3323e41f4b71Sopenharmony_ci 3324e41f4b71Sopenharmony_ci**Error codes** 3325e41f4b71Sopenharmony_ci 3326e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3327e41f4b71Sopenharmony_ci 3328e41f4b71Sopenharmony_ci**Example** 3329e41f4b71Sopenharmony_ci 3330e41f4b71Sopenharmony_ci ```ts 3331e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3332e41f4b71Sopenharmony_ci let srcPath = pathDir + "/source.txt"; 3333e41f4b71Sopenharmony_ci let destPath = pathDir + "/dest.txt"; 3334e41f4b71Sopenharmony_ci fs.moveFile(srcPath, destPath, (err: BusinessError) => { 3335e41f4b71Sopenharmony_ci if (err) { 3336e41f4b71Sopenharmony_ci console.error("move file failed with error message: " + err.message + ", error code: " + err.code); 3337e41f4b71Sopenharmony_ci } else { 3338e41f4b71Sopenharmony_ci console.info("move file succeed"); 3339e41f4b71Sopenharmony_ci } 3340e41f4b71Sopenharmony_ci }); 3341e41f4b71Sopenharmony_ci ``` 3342e41f4b71Sopenharmony_ci 3343e41f4b71Sopenharmony_ci## fs.moveFileSync 3344e41f4b71Sopenharmony_ci 3345e41f4b71Sopenharmony_cimoveFileSync(src: string, dest: string, mode?: number): void 3346e41f4b71Sopenharmony_ci 3347e41f4b71Sopenharmony_ciMoves a file. This API returns the result synchronously. 3348e41f4b71Sopenharmony_ci 3349e41f4b71Sopenharmony_ci> **NOTE** 3350e41f4b71Sopenharmony_ci> This API is not applicable to the files or folders in a distributed directory. 3351e41f4b71Sopenharmony_ci 3352e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3353e41f4b71Sopenharmony_ci 3354e41f4b71Sopenharmony_ci**Parameters** 3355e41f4b71Sopenharmony_ci 3356e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3357e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3358e41f4b71Sopenharmony_ci| src | string | Yes | Application sandbox path of the source file.| 3359e41f4b71Sopenharmony_ci| dest | string | Yes | Application sandbox path of the destination file.| 3360e41f4b71Sopenharmony_ci| mode | number | No | Whether to overwrite the file with the same name in the destination directory.<br> The value **0** means to overwrite the file with the same name in the destination directory; the value **1** means to throw an exception.<br> The default value is **0**.| 3361e41f4b71Sopenharmony_ci 3362e41f4b71Sopenharmony_ci**Error codes** 3363e41f4b71Sopenharmony_ci 3364e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3365e41f4b71Sopenharmony_ci 3366e41f4b71Sopenharmony_ci**Example** 3367e41f4b71Sopenharmony_ci 3368e41f4b71Sopenharmony_ci ```ts 3369e41f4b71Sopenharmony_ci let srcPath = pathDir + "/source.txt"; 3370e41f4b71Sopenharmony_ci let destPath = pathDir + "/dest.txt"; 3371e41f4b71Sopenharmony_ci fs.moveFileSync(srcPath, destPath, 0); 3372e41f4b71Sopenharmony_ci console.info("move file succeed"); 3373e41f4b71Sopenharmony_ci ``` 3374e41f4b71Sopenharmony_ci 3375e41f4b71Sopenharmony_ci## fs.mkdtemp 3376e41f4b71Sopenharmony_ci 3377e41f4b71Sopenharmony_cimkdtemp(prefix: string): Promise<string> 3378e41f4b71Sopenharmony_ci 3379e41f4b71Sopenharmony_ciCreates a temporary directory. This API uses a promise to return the result. The folder name is created by replacing a string (specified by **prefix**) with six randomly generated characters. 3380e41f4b71Sopenharmony_ci 3381e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3382e41f4b71Sopenharmony_ci 3383e41f4b71Sopenharmony_ci**Parameters** 3384e41f4b71Sopenharmony_ci 3385e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3386e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3387e41f4b71Sopenharmony_ci| prefix | string | Yes | String to be replaced with six randomly generated characters to create a unique temporary directory.| 3388e41f4b71Sopenharmony_ci 3389e41f4b71Sopenharmony_ci**Return value** 3390e41f4b71Sopenharmony_ci 3391e41f4b71Sopenharmony_ci| Type | Description | 3392e41f4b71Sopenharmony_ci| --------------------- | ---------- | 3393e41f4b71Sopenharmony_ci| Promise<string> | Promise used to return the directory created.| 3394e41f4b71Sopenharmony_ci 3395e41f4b71Sopenharmony_ci**Error codes** 3396e41f4b71Sopenharmony_ci 3397e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3398e41f4b71Sopenharmony_ci 3399e41f4b71Sopenharmony_ci**Example** 3400e41f4b71Sopenharmony_ci 3401e41f4b71Sopenharmony_ci ```ts 3402e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3403e41f4b71Sopenharmony_ci fs.mkdtemp(pathDir + "/XXXXXX").then((dir: string) => { 3404e41f4b71Sopenharmony_ci console.info("mkdtemp succeed:" + dir); 3405e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3406e41f4b71Sopenharmony_ci console.error("mkdtemp failed with error message: " + err.message + ", error code: " + err.code); 3407e41f4b71Sopenharmony_ci }); 3408e41f4b71Sopenharmony_ci ``` 3409e41f4b71Sopenharmony_ci 3410e41f4b71Sopenharmony_ci## fs.mkdtemp 3411e41f4b71Sopenharmony_ci 3412e41f4b71Sopenharmony_cimkdtemp(prefix: string, callback: AsyncCallback<string>): void 3413e41f4b71Sopenharmony_ci 3414e41f4b71Sopenharmony_ciCreates a temporary directory. This API uses an asynchronous callback to return the result. The folder name is created by replacing a string (specified by **prefix**) with six randomly generated characters. 3415e41f4b71Sopenharmony_ci 3416e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3417e41f4b71Sopenharmony_ci 3418e41f4b71Sopenharmony_ci**Parameters** 3419e41f4b71Sopenharmony_ci 3420e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3421e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | --------------------------- | 3422e41f4b71Sopenharmony_ci| prefix | string | Yes | String to be replaced with six randomly generated characters to create a unique temporary directory.| 3423e41f4b71Sopenharmony_ci| callback | AsyncCallback<string> | Yes | Callback used to return the result. | 3424e41f4b71Sopenharmony_ci 3425e41f4b71Sopenharmony_ci**Error codes** 3426e41f4b71Sopenharmony_ci 3427e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3428e41f4b71Sopenharmony_ci 3429e41f4b71Sopenharmony_ci**Example** 3430e41f4b71Sopenharmony_ci 3431e41f4b71Sopenharmony_ci ```ts 3432e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3433e41f4b71Sopenharmony_ci fs.mkdtemp(pathDir + "/XXXXXX", (err: BusinessError, res: string) => { 3434e41f4b71Sopenharmony_ci if (err) { 3435e41f4b71Sopenharmony_ci console.error("mkdtemp failed with error message: " + err.message + ", error code: " + err.code); 3436e41f4b71Sopenharmony_ci } else { 3437e41f4b71Sopenharmony_ci console.info("mkdtemp succeed"); 3438e41f4b71Sopenharmony_ci } 3439e41f4b71Sopenharmony_ci }); 3440e41f4b71Sopenharmony_ci ``` 3441e41f4b71Sopenharmony_ci 3442e41f4b71Sopenharmony_ci## fs.mkdtempSync 3443e41f4b71Sopenharmony_ci 3444e41f4b71Sopenharmony_cimkdtempSync(prefix: string): string 3445e41f4b71Sopenharmony_ci 3446e41f4b71Sopenharmony_ciCreates a temporary directory. This API returns the result synchronously. The folder name is created by replacing a string (specified by **prefix**) with six randomly generated characters. 3447e41f4b71Sopenharmony_ci 3448e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3449e41f4b71Sopenharmony_ci 3450e41f4b71Sopenharmony_ci**Parameters** 3451e41f4b71Sopenharmony_ci 3452e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3453e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 3454e41f4b71Sopenharmony_ci| prefix | string | Yes | String to be replaced with six randomly generated characters to create a unique temporary directory.| 3455e41f4b71Sopenharmony_ci 3456e41f4b71Sopenharmony_ci**Return value** 3457e41f4b71Sopenharmony_ci 3458e41f4b71Sopenharmony_ci| Type | Description | 3459e41f4b71Sopenharmony_ci| ------ | ---------- | 3460e41f4b71Sopenharmony_ci| string | Unique path generated.| 3461e41f4b71Sopenharmony_ci 3462e41f4b71Sopenharmony_ci**Error codes** 3463e41f4b71Sopenharmony_ci 3464e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3465e41f4b71Sopenharmony_ci 3466e41f4b71Sopenharmony_ci**Example** 3467e41f4b71Sopenharmony_ci 3468e41f4b71Sopenharmony_ci ```ts 3469e41f4b71Sopenharmony_ci let res = fs.mkdtempSync(pathDir + "/XXXXXX"); 3470e41f4b71Sopenharmony_ci ``` 3471e41f4b71Sopenharmony_ci 3472e41f4b71Sopenharmony_ci## fs.utimes<sup>11+</sup> 3473e41f4b71Sopenharmony_ci 3474e41f4b71Sopenharmony_ciutimes(path: string, mtime: number): void 3475e41f4b71Sopenharmony_ci 3476e41f4b71Sopenharmony_ciUpdates the latest access timestamp of a file. 3477e41f4b71Sopenharmony_ci 3478e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3479e41f4b71Sopenharmony_ci 3480e41f4b71Sopenharmony_ci**Parameters** 3481e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3482e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ | 3483e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file.| 3484e41f4b71Sopenharmony_ci| mtime | number | Yes | New timestamp. The value is the number of milliseconds elapsed since the Epoch time (00:00:00 UTC on January 1, 1970). Only the last access time of a file can be modified.| 3485e41f4b71Sopenharmony_ci 3486e41f4b71Sopenharmony_ci**Error codes** 3487e41f4b71Sopenharmony_ci 3488e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3489e41f4b71Sopenharmony_ci 3490e41f4b71Sopenharmony_ci**Example** 3491e41f4b71Sopenharmony_ci 3492e41f4b71Sopenharmony_ci ```ts 3493e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3494e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 3495e41f4b71Sopenharmony_ci fs.writeSync(file.fd, 'test data'); 3496e41f4b71Sopenharmony_ci fs.closeSync(file); 3497e41f4b71Sopenharmony_ci fs.utimes(filePath, new Date().getTime()); 3498e41f4b71Sopenharmony_ci ``` 3499e41f4b71Sopenharmony_ci 3500e41f4b71Sopenharmony_ci## fs.createRandomAccessFile<sup>10+</sup> 3501e41f4b71Sopenharmony_ci 3502e41f4b71Sopenharmony_cicreateRandomAccessFile(file: string | File, mode?: number): Promise<RandomAccessFile> 3503e41f4b71Sopenharmony_ci 3504e41f4b71Sopenharmony_ciCreates a **RandomAccessFile** instance based on a file path or file object. This API uses a promise to return the result. 3505e41f4b71Sopenharmony_ci 3506e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3507e41f4b71Sopenharmony_ci 3508e41f4b71Sopenharmony_ci**Parameters** 3509e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3510e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ | 3511e41f4b71Sopenharmony_ci| file | string \| [File](#file) | Yes | Application sandbox path of the file or an opened file object.| 3512e41f4b71Sopenharmony_ci| mode | number | No | [Mode](#openmode) for creating the **RandomAccessFile** instance. This parameter is valid only when the application sandbox path of the file is passed in. One of the following options must be specified:<br>- **OpenMode.READ_ONLY(0o0)**: Create the file in read-only mode. This is the default value.<br>- **OpenMode.WRITE_ONLY(0o1)**: Create the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Create the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the **RandomAccessFile** object already exists and is created in write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Create the file in append mode. New data will be added to the end of the **RandomAccessFile** object. <br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the created file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception. The write permission is not allowed.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Create a **RandomAccessFile** instance in synchronous I/O mode.| 3513e41f4b71Sopenharmony_ci 3514e41f4b71Sopenharmony_ci**Return value** 3515e41f4b71Sopenharmony_ci 3516e41f4b71Sopenharmony_ci| Type | Description | 3517e41f4b71Sopenharmony_ci| --------------------------------- | --------- | 3518e41f4b71Sopenharmony_ci| Promise<[RandomAccessFile](#randomaccessfile)> | Promise used to return the **RandomAccessFile** instance created.| 3519e41f4b71Sopenharmony_ci 3520e41f4b71Sopenharmony_ci**Error codes** 3521e41f4b71Sopenharmony_ci 3522e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3523e41f4b71Sopenharmony_ci 3524e41f4b71Sopenharmony_ci**Example** 3525e41f4b71Sopenharmony_ci 3526e41f4b71Sopenharmony_ci ```ts 3527e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3528e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3529e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 3530e41f4b71Sopenharmony_ci fs.createRandomAccessFile(file).then((randomAccessFile: fs.RandomAccessFile) => { 3531e41f4b71Sopenharmony_ci console.info("randomAccessFile fd: " + randomAccessFile.fd); 3532e41f4b71Sopenharmony_ci randomAccessFile.close(); 3533e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3534e41f4b71Sopenharmony_ci console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code); 3535e41f4b71Sopenharmony_ci }).finally(() => { 3536e41f4b71Sopenharmony_ci fs.closeSync(file); 3537e41f4b71Sopenharmony_ci }); 3538e41f4b71Sopenharmony_ci ``` 3539e41f4b71Sopenharmony_ci 3540e41f4b71Sopenharmony_ci## fs.createRandomAccessFile<sup>10+</sup> 3541e41f4b71Sopenharmony_ci 3542e41f4b71Sopenharmony_cicreateRandomAccessFile(file: string | File, callback: AsyncCallback<RandomAccessFile>): void 3543e41f4b71Sopenharmony_ci 3544e41f4b71Sopenharmony_ciCreates a **RandomAccessFile** object in read-only mode based on a file path or file object. This API uses an asynchronous callback to return the result. 3545e41f4b71Sopenharmony_ci 3546e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3547e41f4b71Sopenharmony_ci 3548e41f4b71Sopenharmony_ci**Parameters** 3549e41f4b71Sopenharmony_ci 3550e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3551e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ | 3552e41f4b71Sopenharmony_ci| file | string \| [File](#file) | Yes | Application sandbox path of the file or an opened file object.| 3553e41f4b71Sopenharmony_ci| callback | AsyncCallback<[RandomAccessFile](#randomaccessfile)> | Yes | Callback used to return the **RandomAccessFile** instance created. | 3554e41f4b71Sopenharmony_ci 3555e41f4b71Sopenharmony_ci**Error codes** 3556e41f4b71Sopenharmony_ci 3557e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3558e41f4b71Sopenharmony_ci 3559e41f4b71Sopenharmony_ci**Example** 3560e41f4b71Sopenharmony_ci ```ts 3561e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3562e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3563e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 3564e41f4b71Sopenharmony_ci fs.createRandomAccessFile(file, (err: BusinessError, randomAccessFile: fs.RandomAccessFile) => { 3565e41f4b71Sopenharmony_ci if (err) { 3566e41f4b71Sopenharmony_ci console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code); 3567e41f4b71Sopenharmony_ci } else { 3568e41f4b71Sopenharmony_ci console.info("randomAccessFile fd: " + randomAccessFile.fd); 3569e41f4b71Sopenharmony_ci randomAccessFile.close(); 3570e41f4b71Sopenharmony_ci } 3571e41f4b71Sopenharmony_ci fs.closeSync(file); 3572e41f4b71Sopenharmony_ci }); 3573e41f4b71Sopenharmony_ci ``` 3574e41f4b71Sopenharmony_ci 3575e41f4b71Sopenharmony_ci ## fs.createRandomAccessFile<sup>10+</sup> 3576e41f4b71Sopenharmony_ci 3577e41f4b71Sopenharmony_cicreateRandomAccessFile(file: string | File, mode: number, callback: AsyncCallback<RandomAccessFile>): void 3578e41f4b71Sopenharmony_ci 3579e41f4b71Sopenharmony_ciCreates a **RandomAccessFile** instance based on a file path or file object. This API uses an asynchronous callback to return the result. 3580e41f4b71Sopenharmony_ci 3581e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3582e41f4b71Sopenharmony_ci 3583e41f4b71Sopenharmony_ci**Parameters** 3584e41f4b71Sopenharmony_ci 3585e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3586e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ | 3587e41f4b71Sopenharmony_ci| file | string \| [File](#file) | Yes | Application sandbox path of the file or an opened file object.| 3588e41f4b71Sopenharmony_ci| mode | number | Yes | [Mode](#openmode) for creating the **RandomAccessFile** instance. This parameter is valid only when the application sandbox path of the file is passed in. One of the following options must be specified:<br>- **OpenMode.READ_ONLY(0o0)**: Create the file in read-only mode. This is the default value.<br>- **OpenMode.WRITE_ONLY(0o1)**: Create the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Create the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the **RandomAccessFile** object already exists and is created in write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Create the file in append mode. New data will be added to the end of the **RandomAccessFile** object. <br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the created file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception. The write permission is not allowed.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Create a **RandomAccessFile** instance in synchronous I/O mode.| 3589e41f4b71Sopenharmony_ci| callback | AsyncCallback<[RandomAccessFile](#randomaccessfile)> | Yes | Callback used to return the **RandomAccessFile** instance created. | 3590e41f4b71Sopenharmony_ci 3591e41f4b71Sopenharmony_ci**Error codes** 3592e41f4b71Sopenharmony_ci 3593e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3594e41f4b71Sopenharmony_ci 3595e41f4b71Sopenharmony_ci**Example** 3596e41f4b71Sopenharmony_ci ```ts 3597e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3598e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3599e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 3600e41f4b71Sopenharmony_ci fs.createRandomAccessFile(file, fs.OpenMode.READ_ONLY, (err: BusinessError, randomAccessFile: fs.RandomAccessFile) => { 3601e41f4b71Sopenharmony_ci if (err) { 3602e41f4b71Sopenharmony_ci console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code); 3603e41f4b71Sopenharmony_ci } else { 3604e41f4b71Sopenharmony_ci console.info("randomAccessFile fd: " + randomAccessFile.fd); 3605e41f4b71Sopenharmony_ci randomAccessFile.close(); 3606e41f4b71Sopenharmony_ci } 3607e41f4b71Sopenharmony_ci fs.closeSync(file); 3608e41f4b71Sopenharmony_ci }); 3609e41f4b71Sopenharmony_ci ``` 3610e41f4b71Sopenharmony_ci 3611e41f4b71Sopenharmony_ci## fs.createRandomAccessFile<sup>12+</sup> 3612e41f4b71Sopenharmony_ci 3613e41f4b71Sopenharmony_cicreateRandomAccessFile(file: string | File, mode?: number, options?: RandomAccessFileOptions): Promise<RandomAccessFile> 3614e41f4b71Sopenharmony_ci 3615e41f4b71Sopenharmony_ciCreates a **RandomAccessFile** instance based on a file path or file object. This API uses a promise to return the result. 3616e41f4b71Sopenharmony_ci 3617e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3618e41f4b71Sopenharmony_ci 3619e41f4b71Sopenharmony_ci**Parameters** 3620e41f4b71Sopenharmony_ci 3621e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3622e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ | 3623e41f4b71Sopenharmony_ci| file | string \| [File](#file) | Yes | Application sandbox path of the file or an opened file object.| 3624e41f4b71Sopenharmony_ci| mode | number | No | [Mode](#openmode) for creating the **RandomAccessFile** instance. This parameter is valid only when the application sandbox path of the file is passed in. One of the following options must be specified:<br>- **OpenMode.READ_ONLY(0o0)**: Create the file in read-only mode. This is the default value.<br>- **OpenMode.WRITE_ONLY(0o1)**: Create the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Create the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the **RandomAccessFile** object already exists and is created in write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Create the file in append mode. New data will be added to the end of the **RandomAccessFile** object. <br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the created file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception. The write permission is not allowed.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Create a **RandomAccessFile** instance in synchronous I/O mode.| 3625e41f4b71Sopenharmony_ci|options|[RandomAccessFileOptions](#randomaccessfileoptions12)|No|The options are as follows:<br>- **start** (number): start position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **end** (number): end position of the data to read in the file. This parameter is optional. The default value is the end of the file.| 3626e41f4b71Sopenharmony_ci 3627e41f4b71Sopenharmony_ci**Return value** 3628e41f4b71Sopenharmony_ci 3629e41f4b71Sopenharmony_ci| Type | Description | 3630e41f4b71Sopenharmony_ci| --------------------------------- | --------- | 3631e41f4b71Sopenharmony_ci| Promise<[RandomAccessFile](#randomaccessfile)> | Promise used to return the **RandomAccessFile** instance created.| 3632e41f4b71Sopenharmony_ci 3633e41f4b71Sopenharmony_ci**Error codes** 3634e41f4b71Sopenharmony_ci 3635e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3636e41f4b71Sopenharmony_ci 3637e41f4b71Sopenharmony_ci```ts 3638e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3639e41f4b71Sopenharmony_cilet filePath = pathDir + "/test.txt"; 3640e41f4b71Sopenharmony_cifs.createRandomAccessFile(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE, { start: 10, end: 100 }) 3641e41f4b71Sopenharmony_ci .then((randomAccessFile: fs.RandomAccessFile) => { 3642e41f4b71Sopenharmony_ci console.info("randomAccessFile fd: " + randomAccessFile.fd); 3643e41f4b71Sopenharmony_ci randomAccessFile.close(); 3644e41f4b71Sopenharmony_ci }) 3645e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 3646e41f4b71Sopenharmony_ci console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code); 3647e41f4b71Sopenharmony_ci }); 3648e41f4b71Sopenharmony_ci``` 3649e41f4b71Sopenharmony_ci 3650e41f4b71Sopenharmony_ci 3651e41f4b71Sopenharmony_ci## fs.createRandomAccessFileSync<sup>10+</sup> 3652e41f4b71Sopenharmony_ci 3653e41f4b71Sopenharmony_cicreateRandomAccessFileSync(file: string | File, mode?: number): RandomAccessFile 3654e41f4b71Sopenharmony_ci 3655e41f4b71Sopenharmony_ciCreates a **RandomAccessFile** instance based on a file path or file object. 3656e41f4b71Sopenharmony_ci 3657e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3658e41f4b71Sopenharmony_ci 3659e41f4b71Sopenharmony_ci**Parameters** 3660e41f4b71Sopenharmony_ci 3661e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3662e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ | 3663e41f4b71Sopenharmony_ci| file | string \| [File](#file) | Yes | Application sandbox path of the file or an opened file object.| 3664e41f4b71Sopenharmony_ci| mode | number | No | [Mode](#openmode) for creating the **RandomAccessFile** instance. This parameter is valid only when the application sandbox path of the file is passed in. One of the following options must be specified:<br>- **OpenMode.READ_ONLY(0o0)**: Create the file in read-only mode. This is the default value.<br>- **OpenMode.WRITE_ONLY(0o1)**: Create the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Create the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the **RandomAccessFile** object already exists and is created in write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Create the file in append mode. New data will be added to the end of the **RandomAccessFile** object. <br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the created file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception. The write permission is not allowed.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Create a **RandomAccessFile** instance in synchronous I/O mode.| 3665e41f4b71Sopenharmony_ci 3666e41f4b71Sopenharmony_ci**Return value** 3667e41f4b71Sopenharmony_ci 3668e41f4b71Sopenharmony_ci| Type | Description | 3669e41f4b71Sopenharmony_ci| ------------------ | --------- | 3670e41f4b71Sopenharmony_ci| [RandomAccessFile](#randomaccessfile) | **RandomAccessFile** instance created.| 3671e41f4b71Sopenharmony_ci 3672e41f4b71Sopenharmony_ci**Error codes** 3673e41f4b71Sopenharmony_ci 3674e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3675e41f4b71Sopenharmony_ci 3676e41f4b71Sopenharmony_ci**Example** 3677e41f4b71Sopenharmony_ci 3678e41f4b71Sopenharmony_ci ```ts 3679e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3680e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 3681e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(file); 3682e41f4b71Sopenharmony_ci randomAccessFile.close(); 3683e41f4b71Sopenharmony_ci ``` 3684e41f4b71Sopenharmony_ci 3685e41f4b71Sopenharmony_ci## fs.createRandomAccessFileSync<sup>12+</sup> 3686e41f4b71Sopenharmony_ci 3687e41f4b71Sopenharmony_cicreateRandomAccessFileSync(file: string | File, mode?: number, 3688e41f4b71Sopenharmony_ci options?: RandomAccessFileOptions): RandomAccessFile; 3689e41f4b71Sopenharmony_ci 3690e41f4b71Sopenharmony_ciCreates a **RandomAccessFile** instance based on a file path or file object. 3691e41f4b71Sopenharmony_ci 3692e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3693e41f4b71Sopenharmony_ci 3694e41f4b71Sopenharmony_ci**Parameters** 3695e41f4b71Sopenharmony_ci 3696e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3697e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ | 3698e41f4b71Sopenharmony_ci| file | string \| [File](#file) | Yes | Application sandbox path of the file or an opened file object.| 3699e41f4b71Sopenharmony_ci| mode | number | No | [Mode](#openmode) for creating the **RandomAccessFile** instance. This parameter is valid only when the application sandbox path of the file is passed in. One of the following options must be specified:<br>- **OpenMode.READ_ONLY(0o0)**: Create the file in read-only mode. This is the default value.<br>- **OpenMode.WRITE_ONLY(0o1)**: Create the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Create the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the **RandomAccessFile** object already exists and is created in write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Create the file in append mode. New data will be added to the end of the **RandomAccessFile** object. <br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the created file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception. The write permission is not allowed.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Create a **RandomAccessFile** instance in synchronous I/O mode.| 3700e41f4b71Sopenharmony_ci|options|[RandomAccessFileOptions](#randomaccessfileoptions12)|No|The options are as follows:<br>- **start** (number): start position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **end** (number): end position of the data to read in the file. This parameter is optional. The default value is the end of the file.| 3701e41f4b71Sopenharmony_ci 3702e41f4b71Sopenharmony_ci**Return value** 3703e41f4b71Sopenharmony_ci 3704e41f4b71Sopenharmony_ci| Type | Description | 3705e41f4b71Sopenharmony_ci| ------------------ | --------- | 3706e41f4b71Sopenharmony_ci| [RandomAccessFile](#randomaccessfile) | **RandomAccessFile** instance created.| 3707e41f4b71Sopenharmony_ci 3708e41f4b71Sopenharmony_ci**Error codes** 3709e41f4b71Sopenharmony_ci 3710e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3711e41f4b71Sopenharmony_ci 3712e41f4b71Sopenharmony_ci**Example** 3713e41f4b71Sopenharmony_ci 3714e41f4b71Sopenharmony_ci ```ts 3715e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3716e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE, 3717e41f4b71Sopenharmony_ci { start: 10, end: 100 }); 3718e41f4b71Sopenharmony_ci randomAccessFile.close(); 3719e41f4b71Sopenharmony_ci ``` 3720e41f4b71Sopenharmony_ci 3721e41f4b71Sopenharmony_ci## fs.createStream 3722e41f4b71Sopenharmony_ci 3723e41f4b71Sopenharmony_cicreateStream(path: string, mode: string): Promise<Stream> 3724e41f4b71Sopenharmony_ci 3725e41f4b71Sopenharmony_ciCreates a stream based on a file path. This API uses a promise to return the result. 3726e41f4b71Sopenharmony_ci 3727e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3728e41f4b71Sopenharmony_ci 3729e41f4b71Sopenharmony_ci**Parameters** 3730e41f4b71Sopenharmony_ci 3731e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 3732e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 3733e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file. | 3734e41f4b71Sopenharmony_ci| mode | string | Yes | - **r**: Open a file for reading. The file must exist.<br>- **r+**: Open a file for both reading and writing. The file must exist.<br>- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| 3735e41f4b71Sopenharmony_ci 3736e41f4b71Sopenharmony_ci**Return value** 3737e41f4b71Sopenharmony_ci 3738e41f4b71Sopenharmony_ci| Type | Description | 3739e41f4b71Sopenharmony_ci| --------------------------------- | --------- | 3740e41f4b71Sopenharmony_ci| Promise<[Stream](#stream)> | Promise used to return the stream opened.| 3741e41f4b71Sopenharmony_ci 3742e41f4b71Sopenharmony_ci**Error codes** 3743e41f4b71Sopenharmony_ci 3744e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3745e41f4b71Sopenharmony_ci 3746e41f4b71Sopenharmony_ci**Example** 3747e41f4b71Sopenharmony_ci 3748e41f4b71Sopenharmony_ci ```ts 3749e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3750e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3751e41f4b71Sopenharmony_ci fs.createStream(filePath, "a+").then((stream: fs.Stream) => { 3752e41f4b71Sopenharmony_ci stream.closeSync(); 3753e41f4b71Sopenharmony_ci console.info("Stream created"); 3754e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3755e41f4b71Sopenharmony_ci console.error("createStream failed with error message: " + err.message + ", error code: " + err.code); 3756e41f4b71Sopenharmony_ci }); 3757e41f4b71Sopenharmony_ci ``` 3758e41f4b71Sopenharmony_ci 3759e41f4b71Sopenharmony_ci 3760e41f4b71Sopenharmony_ci## fs.createStream 3761e41f4b71Sopenharmony_ci 3762e41f4b71Sopenharmony_cicreateStream(path: string, mode: string, callback: AsyncCallback<Stream>): void 3763e41f4b71Sopenharmony_ci 3764e41f4b71Sopenharmony_ciCreates a stream based on a file path. This API uses an asynchronous callback to return the result. 3765e41f4b71Sopenharmony_ci 3766e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3767e41f4b71Sopenharmony_ci 3768e41f4b71Sopenharmony_ci**Parameters** 3769e41f4b71Sopenharmony_ci 3770e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3771e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 3772e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file. | 3773e41f4b71Sopenharmony_ci| mode | string | Yes | - **r**: Open a file for reading. The file must exist.<br>- **r+**: Open a file for both reading and writing. The file must exist.<br>- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| 3774e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Stream](#stream)> | Yes | Callback used to return the result. | 3775e41f4b71Sopenharmony_ci 3776e41f4b71Sopenharmony_ci**Error codes** 3777e41f4b71Sopenharmony_ci 3778e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3779e41f4b71Sopenharmony_ci 3780e41f4b71Sopenharmony_ci**Example** 3781e41f4b71Sopenharmony_ci 3782e41f4b71Sopenharmony_ci ```ts 3783e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3784e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3785e41f4b71Sopenharmony_ci fs.createStream(filePath, "r+", (err: BusinessError, stream: fs.Stream) => { 3786e41f4b71Sopenharmony_ci if (err) { 3787e41f4b71Sopenharmony_ci console.error("create stream failed with error message: " + err.message + ", error code: " + err.code); 3788e41f4b71Sopenharmony_ci } else { 3789e41f4b71Sopenharmony_ci console.info("Stream created"); 3790e41f4b71Sopenharmony_ci } 3791e41f4b71Sopenharmony_ci stream.closeSync(); 3792e41f4b71Sopenharmony_ci }) 3793e41f4b71Sopenharmony_ci ``` 3794e41f4b71Sopenharmony_ci 3795e41f4b71Sopenharmony_ci## fs.createStreamSync 3796e41f4b71Sopenharmony_ci 3797e41f4b71Sopenharmony_cicreateStreamSync(path: string, mode: string): Stream 3798e41f4b71Sopenharmony_ci 3799e41f4b71Sopenharmony_ciCreates a stream based on a file path. This API returns the result synchronously. 3800e41f4b71Sopenharmony_ci 3801e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3802e41f4b71Sopenharmony_ci 3803e41f4b71Sopenharmony_ci**Parameters** 3804e41f4b71Sopenharmony_ci 3805e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 3806e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 3807e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file. | 3808e41f4b71Sopenharmony_ci| mode | string | Yes | - **r**: Open a file for reading. The file must exist.<br>- **r+**: Open a file for both reading and writing. The file must exist.<br>- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| 3809e41f4b71Sopenharmony_ci 3810e41f4b71Sopenharmony_ci**Return value** 3811e41f4b71Sopenharmony_ci 3812e41f4b71Sopenharmony_ci| Type | Description | 3813e41f4b71Sopenharmony_ci| ------------------ | --------- | 3814e41f4b71Sopenharmony_ci| [Stream](#stream) | Stream opened.| 3815e41f4b71Sopenharmony_ci 3816e41f4b71Sopenharmony_ci**Error codes** 3817e41f4b71Sopenharmony_ci 3818e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3819e41f4b71Sopenharmony_ci 3820e41f4b71Sopenharmony_ci**Example** 3821e41f4b71Sopenharmony_ci 3822e41f4b71Sopenharmony_ci ```ts 3823e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3824e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 3825e41f4b71Sopenharmony_ci console.info("Stream created"); 3826e41f4b71Sopenharmony_ci stream.closeSync(); 3827e41f4b71Sopenharmony_ci ``` 3828e41f4b71Sopenharmony_ci 3829e41f4b71Sopenharmony_ci 3830e41f4b71Sopenharmony_ci## fs.fdopenStream 3831e41f4b71Sopenharmony_ci 3832e41f4b71Sopenharmony_cifdopenStream(fd: number, mode: string): Promise<Stream> 3833e41f4b71Sopenharmony_ci 3834e41f4b71Sopenharmony_ciOpens a stream based on an FD. This API uses a promise to return the result. 3835e41f4b71Sopenharmony_ci 3836e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3837e41f4b71Sopenharmony_ci 3838e41f4b71Sopenharmony_ci**Parameters** 3839e41f4b71Sopenharmony_ci 3840e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3841e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 3842e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 3843e41f4b71Sopenharmony_ci| mode | string | Yes | - **r**: Open a file for reading. The file must exist.<br>- **r+**: Open a file for both reading and writing. The file must exist.<br>- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| 3844e41f4b71Sopenharmony_ci 3845e41f4b71Sopenharmony_ci**Return value** 3846e41f4b71Sopenharmony_ci 3847e41f4b71Sopenharmony_ci| Type | Description | 3848e41f4b71Sopenharmony_ci| --------------------------------- | --------- | 3849e41f4b71Sopenharmony_ci| Promise<[Stream](#stream)> | Promise used to return the stream opened.| 3850e41f4b71Sopenharmony_ci 3851e41f4b71Sopenharmony_ci**Error codes** 3852e41f4b71Sopenharmony_ci 3853e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3854e41f4b71Sopenharmony_ci 3855e41f4b71Sopenharmony_ci**Example** 3856e41f4b71Sopenharmony_ci 3857e41f4b71Sopenharmony_ci ```ts 3858e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3859e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3860e41f4b71Sopenharmony_ci let file = fs.openSync(filePath); 3861e41f4b71Sopenharmony_ci fs.fdopenStream(file.fd, "r+").then((stream: fs.Stream) => { 3862e41f4b71Sopenharmony_ci console.info("Stream opened"); 3863e41f4b71Sopenharmony_ci stream.closeSync(); 3864e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3865e41f4b71Sopenharmony_ci console.error("openStream failed with error message: " + err.message + ", error code: " + err.code); 3866e41f4b71Sopenharmony_ci }).finally(() => { 3867e41f4b71Sopenharmony_ci fs.closeSync(file); 3868e41f4b71Sopenharmony_ci }); 3869e41f4b71Sopenharmony_ci ``` 3870e41f4b71Sopenharmony_ci 3871e41f4b71Sopenharmony_ci## fs.fdopenStream 3872e41f4b71Sopenharmony_ci 3873e41f4b71Sopenharmony_cifdopenStream(fd: number, mode: string, callback: AsyncCallback<Stream>): void 3874e41f4b71Sopenharmony_ci 3875e41f4b71Sopenharmony_ciOpens a stream based on an FD. This API uses an asynchronous callback to return the result. 3876e41f4b71Sopenharmony_ci 3877e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3878e41f4b71Sopenharmony_ci 3879e41f4b71Sopenharmony_ci**Parameters** 3880e41f4b71Sopenharmony_ci 3881e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3882e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3883e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 3884e41f4b71Sopenharmony_ci| mode | string | Yes | - **r**: Open a file for reading. The file must exist.<br>- **r+**: Open a file for both reading and writing. The file must exist.<br>- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| 3885e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Stream](#stream)> | Yes | Callback used to return the result. | 3886e41f4b71Sopenharmony_ci 3887e41f4b71Sopenharmony_ci**Error codes** 3888e41f4b71Sopenharmony_ci 3889e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3890e41f4b71Sopenharmony_ci 3891e41f4b71Sopenharmony_ci**Example** 3892e41f4b71Sopenharmony_ci 3893e41f4b71Sopenharmony_ci ```ts 3894e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 3895e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3896e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 3897e41f4b71Sopenharmony_ci fs.fdopenStream(file.fd, "r+", (err: BusinessError, stream: fs.Stream) => { 3898e41f4b71Sopenharmony_ci if (err) { 3899e41f4b71Sopenharmony_ci console.error("fdopen stream failed with error message: " + err.message + ", error code: " + err.code); 3900e41f4b71Sopenharmony_ci } else { 3901e41f4b71Sopenharmony_ci console.info("fdopen stream succeed"); 3902e41f4b71Sopenharmony_ci fs.closeSync(file); 3903e41f4b71Sopenharmony_ci } 3904e41f4b71Sopenharmony_ci stream.closeSync(); 3905e41f4b71Sopenharmony_ci }); 3906e41f4b71Sopenharmony_ci ``` 3907e41f4b71Sopenharmony_ci 3908e41f4b71Sopenharmony_ci## fs.fdopenStreamSync 3909e41f4b71Sopenharmony_ci 3910e41f4b71Sopenharmony_cifdopenStreamSync(fd: number, mode: string): Stream 3911e41f4b71Sopenharmony_ci 3912e41f4b71Sopenharmony_ciOpens a stream based on an FD. This API returns the result synchronously. 3913e41f4b71Sopenharmony_ci 3914e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3915e41f4b71Sopenharmony_ci 3916e41f4b71Sopenharmony_ci**Parameters** 3917e41f4b71Sopenharmony_ci 3918e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3919e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 3920e41f4b71Sopenharmony_ci| fd | number | Yes | FD of the file. | 3921e41f4b71Sopenharmony_ci| mode | string | Yes | - **r**: Open a file for reading. The file must exist.<br>- **r+**: Open a file for both reading and writing. The file must exist.<br>- **w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>- **a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>- **a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).| 3922e41f4b71Sopenharmony_ci 3923e41f4b71Sopenharmony_ci**Return value** 3924e41f4b71Sopenharmony_ci 3925e41f4b71Sopenharmony_ci| Type | Description | 3926e41f4b71Sopenharmony_ci| ------------------ | --------- | 3927e41f4b71Sopenharmony_ci| [Stream](#stream) | Stream opened.| 3928e41f4b71Sopenharmony_ci 3929e41f4b71Sopenharmony_ci**Error codes** 3930e41f4b71Sopenharmony_ci 3931e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3932e41f4b71Sopenharmony_ci 3933e41f4b71Sopenharmony_ci**Example** 3934e41f4b71Sopenharmony_ci 3935e41f4b71Sopenharmony_ci ```ts 3936e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 3937e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY | fs.OpenMode.CREATE); 3938e41f4b71Sopenharmony_ci let stream = fs.fdopenStreamSync(file.fd, "r+"); 3939e41f4b71Sopenharmony_ci fs.closeSync(file); 3940e41f4b71Sopenharmony_ci stream.closeSync(); 3941e41f4b71Sopenharmony_ci ``` 3942e41f4b71Sopenharmony_ci 3943e41f4b71Sopenharmony_ci## fs.createReadStream<sup>12+</sup> 3944e41f4b71Sopenharmony_ci 3945e41f4b71Sopenharmony_cicreateReadStream(path: string, options?: ReadStreamOptions ): ReadStream; 3946e41f4b71Sopenharmony_ci 3947e41f4b71Sopenharmony_ciCreates a readable stream. This API returns the result synchronously. 3948e41f4b71Sopenharmony_ci 3949e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3950e41f4b71Sopenharmony_ci 3951e41f4b71Sopenharmony_ci**Parameters** 3952e41f4b71Sopenharmony_ci 3953e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3954e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 3955e41f4b71Sopenharmony_ci| path | string | Yes | Path of the file. | 3956e41f4b71Sopenharmony_ci| options | [ReadStreamOptions](#readstreamoptions12) | No | The options are as follows:<br>- **start** (number): start position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **end** (number): end position of the data to read in the file. This parameter is optional. The default value is the end of the file.| 3957e41f4b71Sopenharmony_ci 3958e41f4b71Sopenharmony_ci**Return value** 3959e41f4b71Sopenharmony_ci 3960e41f4b71Sopenharmony_ci| Type | Description | 3961e41f4b71Sopenharmony_ci| ------------------ | --------- | 3962e41f4b71Sopenharmony_ci| [ReadStream](#readstream12) | Readable stream created.| 3963e41f4b71Sopenharmony_ci 3964e41f4b71Sopenharmony_ci**Error codes** 3965e41f4b71Sopenharmony_ci 3966e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 3967e41f4b71Sopenharmony_ci 3968e41f4b71Sopenharmony_ci**Example** 3969e41f4b71Sopenharmony_ci 3970e41f4b71Sopenharmony_ci ```ts 3971e41f4b71Sopenharmony_ci // Create a readable stream. 3972e41f4b71Sopenharmony_ci const rs = fs.createReadStream(`${pathDir}/read.txt`); 3973e41f4b71Sopenharmony_ci // Create a writeable stream. 3974e41f4b71Sopenharmony_ci const ws = fs.createWriteStream(`${pathDir}/write.txt`); 3975e41f4b71Sopenharmony_ci // Copy files in paused mode. 3976e41f4b71Sopenharmony_ci rs.on('readable', () => { 3977e41f4b71Sopenharmony_ci const data = rs.read(); 3978e41f4b71Sopenharmony_ci if (!data) { 3979e41f4b71Sopenharmony_ci return; 3980e41f4b71Sopenharmony_ci } 3981e41f4b71Sopenharmony_ci ws.write(data); 3982e41f4b71Sopenharmony_ci }); 3983e41f4b71Sopenharmony_ci ``` 3984e41f4b71Sopenharmony_ci 3985e41f4b71Sopenharmony_ci## fs.createWriteStream<sup>12+</sup> 3986e41f4b71Sopenharmony_ci 3987e41f4b71Sopenharmony_cicreateWriteStream(path: string, options?: WriteStreamOptions): WriteStream; 3988e41f4b71Sopenharmony_ci 3989e41f4b71Sopenharmony_ciCreates a writeable stream. This API returns the result synchronously. 3990e41f4b71Sopenharmony_ci 3991e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 3992e41f4b71Sopenharmony_ci 3993e41f4b71Sopenharmony_ci**Parameters** 3994e41f4b71Sopenharmony_ci 3995e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 3996e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 3997e41f4b71Sopenharmony_ci| path | string | Yes | Path of the file. | 3998e41f4b71Sopenharmony_ci| options | [WriteStreamOptions](#writestreamoptions12) | No | The options are as follows:<br>- **start** (number): start position to write the data in the file. This parameter is optional. The default value is the current position.<br>- **mode** (number): [mode](#openmode) for creating the writeable stream. This parameter is optional. The default value is the write-only mode.| 3999e41f4b71Sopenharmony_ci 4000e41f4b71Sopenharmony_ci**Return value** 4001e41f4b71Sopenharmony_ci 4002e41f4b71Sopenharmony_ci| Type | Description | 4003e41f4b71Sopenharmony_ci| ------------------ | --------- | 4004e41f4b71Sopenharmony_ci| [WriteStream](#writestream12) | Writable stream created.| 4005e41f4b71Sopenharmony_ci 4006e41f4b71Sopenharmony_ci**Error codes** 4007e41f4b71Sopenharmony_ci 4008e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4009e41f4b71Sopenharmony_ci 4010e41f4b71Sopenharmony_ci**Example** 4011e41f4b71Sopenharmony_ci 4012e41f4b71Sopenharmony_ci ```ts 4013e41f4b71Sopenharmony_ci // Create a readable stream. 4014e41f4b71Sopenharmony_ci const rs = fs.createReadStream(`${pathDir}/read.txt`); 4015e41f4b71Sopenharmony_ci // Create a writeable stream. 4016e41f4b71Sopenharmony_ci const ws = fs.createWriteStream(`${pathDir}/write.txt`); 4017e41f4b71Sopenharmony_ci // Copy files in paused mode. 4018e41f4b71Sopenharmony_ci rs.on('readable', () => { 4019e41f4b71Sopenharmony_ci const data = rs.read(); 4020e41f4b71Sopenharmony_ci if (!data) { 4021e41f4b71Sopenharmony_ci return; 4022e41f4b71Sopenharmony_ci } 4023e41f4b71Sopenharmony_ci ws.write(data); 4024e41f4b71Sopenharmony_ci }); 4025e41f4b71Sopenharmony_ci ``` 4026e41f4b71Sopenharmony_ci 4027e41f4b71Sopenharmony_ci## fs.createWatcher<sup>10+</sup> 4028e41f4b71Sopenharmony_ci 4029e41f4b71Sopenharmony_cicreateWatcher(path: string, events: number, listener: WatchEventListener): Watcher 4030e41f4b71Sopenharmony_ci 4031e41f4b71Sopenharmony_ciCreates a **Watcher** object to observe file or directory changes. 4032e41f4b71Sopenharmony_ci 4033e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4034e41f4b71Sopenharmony_ci 4035e41f4b71Sopenharmony_ci**Parameters** 4036e41f4b71Sopenharmony_ci 4037e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4038e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 4039e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file or directory to observe. | 4040e41f4b71Sopenharmony_ci| events | number | Yes | Events to observe. Multiple events can be separated by a bitwise OR operator (\|).<br>- **0x1: IN_ACCESS**: A file is accessed.<br>- **0x2: IN_MODIFY**: The file content is modified.<br>- **0x4: IN_ATTRIB**: The file metadata is modified.<br>- **0x8: IN_CLOSE_WRITE**: A file is opened, written with data, and then closed.<br>- **0x10: IN_CLOSE_NOWRITE**: A file or directory is opened and then closed without data written.<br>- **0x20: IN_OPEN**: A file or directory is opened.<br>- **0x40: IN_MOVED_FROM**: A file in the observed directory is moved.<br>- **0x80: IN_MOVED_TO**: A file is moved to the observed directory.<br>- **0x100: IN_CREATE**: A file or directory is created in the observed directory.<br>- **0x200: IN_DELETE**: A file or directory is deleted from the observed directory.<br>- **0x400: IN_DELETE_SELF**: The observed directory is deleted. After the directory is deleted, the listening stops.<br>- **0x800: IN_MOVE_SELF**: The observed file or folder is moved. After the file or folder is moved, the listening continues.<br>- **0xfff: IN_ALL_EVENTS**: All events.| 4041e41f4b71Sopenharmony_ci| listener | [WatchEventListener](#watcheventlistener10) | Yes | Callback invoked when an observed event occurs. The callback will be invoked each time an observed event occurs. | 4042e41f4b71Sopenharmony_ci 4043e41f4b71Sopenharmony_ci**Return value** 4044e41f4b71Sopenharmony_ci 4045e41f4b71Sopenharmony_ci| Type | Description | 4046e41f4b71Sopenharmony_ci| ------------------ | --------- | 4047e41f4b71Sopenharmony_ci| [Watcher](#watcher10) | **Watcher** object created.| 4048e41f4b71Sopenharmony_ci 4049e41f4b71Sopenharmony_ci**Error codes** 4050e41f4b71Sopenharmony_ci 4051e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4052e41f4b71Sopenharmony_ci 4053e41f4b71Sopenharmony_ci**Example** 4054e41f4b71Sopenharmony_ci 4055e41f4b71Sopenharmony_ci ```ts 4056e41f4b71Sopenharmony_ci import { fileIo as fs, WatchEvent } from '@kit.CoreFileKit'; 4057e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4058e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 4059e41f4b71Sopenharmony_ci let watcher = fs.createWatcher(filePath, 0x2 | 0x10, (watchEvent: WatchEvent) => { 4060e41f4b71Sopenharmony_ci if (watchEvent.event == 0x2) { 4061e41f4b71Sopenharmony_ci console.info(watchEvent.fileName + 'was modified'); 4062e41f4b71Sopenharmony_ci } else if (watchEvent.event == 0x10) { 4063e41f4b71Sopenharmony_ci console.info(watchEvent.fileName + 'was closed'); 4064e41f4b71Sopenharmony_ci } 4065e41f4b71Sopenharmony_ci }); 4066e41f4b71Sopenharmony_ci watcher.start(); 4067e41f4b71Sopenharmony_ci fs.writeSync(file.fd, 'test'); 4068e41f4b71Sopenharmony_ci fs.closeSync(file); 4069e41f4b71Sopenharmony_ci watcher.stop(); 4070e41f4b71Sopenharmony_ci ``` 4071e41f4b71Sopenharmony_ci 4072e41f4b71Sopenharmony_ci## WatchEventListener<sup>10+</sup> 4073e41f4b71Sopenharmony_ci 4074e41f4b71Sopenharmony_ci(event: WatchEvent): void 4075e41f4b71Sopenharmony_ci 4076e41f4b71Sopenharmony_ciCalled when an observed event occurs. 4077e41f4b71Sopenharmony_ci 4078e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4079e41f4b71Sopenharmony_ci 4080e41f4b71Sopenharmony_ci**Parameters** 4081e41f4b71Sopenharmony_ci 4082e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4083e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 4084e41f4b71Sopenharmony_ci| event | [WatchEvent](#watchevent10) | Yes | Event for the callback to invoke. | 4085e41f4b71Sopenharmony_ci 4086e41f4b71Sopenharmony_ci## WatchEvent<sup>10+</sup> 4087e41f4b71Sopenharmony_ci 4088e41f4b71Sopenharmony_ciDefines the event to observe. 4089e41f4b71Sopenharmony_ci 4090e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4091e41f4b71Sopenharmony_ci 4092e41f4b71Sopenharmony_ci### Properties 4093e41f4b71Sopenharmony_ci 4094e41f4b71Sopenharmony_ci| Name | Type | Read-Only | Writable | Description | 4095e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------- | 4096e41f4b71Sopenharmony_ci| fileName | string | Yes | No | Name of the file for which the event occurs.| 4097e41f4b71Sopenharmony_ci| event | number | Yes | No | Events to observe. Multiple events can be separated by a bitwise OR operator (\|).<br>- **0x1: IN_ACCESS**: A file is accessed.<br>- **0x2: IN_MODIFY**: The file content is modified.<br>- **0x4: IN_ATTRIB**: The file metadata is modified.<br>- **0x8: IN_CLOSE_WRITE**: A file is opened, written with data, and then closed.<br>- **0x10: IN_CLOSE_NOWRITE**: A file or directory is opened and then closed without data written.<br>- **0x20: IN_OPEN**: A file or directory is opened.<br>- **0x40: IN_MOVED_FROM**: A file in the observed directory is moved.<br>- **0x80: IN_MOVED_TO**: A file is moved to the observed directory.<br>- **0x100: IN_CREATE**: A file or directory is created in the observed directory.<br>- **0x200: IN_DELETE**: A file or directory is deleted from the observed directory.<br>- **0x400: IN_DELETE_SELF**: The observed directory is deleted. After the directory is deleted, the listening stops.<br>- **0x800: IN_MOVE_SELF**: The observed file or folder is moved. After the file or folder is moved, the listening continues.<br>- **0xfff: IN_ALL_EVENTS**: All events.| 4098e41f4b71Sopenharmony_ci| cookie | number | Yes | No | Cookie bound with the event. Currently, only the **IN_MOVED_FROM** and **IN_MOVED_TO** events are supported. The **IN_MOVED_FROM** and **IN_MOVED_TO** events of the same file have the same **cookie** value.| 4099e41f4b71Sopenharmony_ci 4100e41f4b71Sopenharmony_ci## Progress<sup>11+</sup> 4101e41f4b71Sopenharmony_ci 4102e41f4b71Sopenharmony_ciDefines the copy progress information. 4103e41f4b71Sopenharmony_ci 4104e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4105e41f4b71Sopenharmony_ci 4106e41f4b71Sopenharmony_ci| Name | Type | Read-Only | Writable | Description | 4107e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------- | 4108e41f4b71Sopenharmony_ci| processedSize | number | Yes | No | Size of the copied data.| 4109e41f4b71Sopenharmony_ci| totalSize | number | Yes | No | Total size of the data to be copied.| 4110e41f4b71Sopenharmony_ci 4111e41f4b71Sopenharmony_ci## TaskSignal<sup>12+</sup> 4112e41f4b71Sopenharmony_ci 4113e41f4b71Sopenharmony_ciProvides APIs for interrupting a copy task. 4114e41f4b71Sopenharmony_ci 4115e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4116e41f4b71Sopenharmony_ci 4117e41f4b71Sopenharmony_ci### cancel<sup>12+</sup> 4118e41f4b71Sopenharmony_ci 4119e41f4b71Sopenharmony_cicancel(): void 4120e41f4b71Sopenharmony_ci 4121e41f4b71Sopenharmony_ciCancels a copy task. 4122e41f4b71Sopenharmony_ci 4123e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4124e41f4b71Sopenharmony_ci 4125e41f4b71Sopenharmony_ci**Error codes** 4126e41f4b71Sopenharmony_ci 4127e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4128e41f4b71Sopenharmony_ci 4129e41f4b71Sopenharmony_ci**Example** 4130e41f4b71Sopenharmony_ci 4131e41f4b71Sopenharmony_ci```ts 4132e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4133e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit'; 4134e41f4b71Sopenharmony_ciimport { fileuri } from '@kit.CoreFileKit'; 4135e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext; 4136e41f4b71Sopenharmony_cilet pathDir: string = context.filesDir; 4137e41f4b71Sopenharmony_cilet srcDirPathLocal: string = pathDir + "/src"; 4138e41f4b71Sopenharmony_cilet dstDirPathLocal: string = pathDir + "/dest"; 4139e41f4b71Sopenharmony_cilet srcDirUriLocal: string = fileuri.getUriFromPath(srcDirPathLocal); 4140e41f4b71Sopenharmony_cilet dstDirUriLocal: string = fileuri.getUriFromPath(dstDirPathLocal); 4141e41f4b71Sopenharmony_cilet progressListener: fs.ProgressListener = (progress: fs.Progress) => { 4142e41f4b71Sopenharmony_ci console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`); 4143e41f4b71Sopenharmony_ci if (progress.processedSize / progress.totalSize > 0.5) { 4144e41f4b71Sopenharmony_ci options.copySignal.cancel(); 4145e41f4b71Sopenharmony_ci } 4146e41f4b71Sopenharmony_ci}; 4147e41f4b71Sopenharmony_cilet options: fs.CopyOptions = { 4148e41f4b71Sopenharmony_ci "progressListener" : progressListener, 4149e41f4b71Sopenharmony_ci "copySignal" : new fs.TaskSignal, 4150e41f4b71Sopenharmony_ci} 4151e41f4b71Sopenharmony_ciconsole.info("copyFileWithCancel success.", + options.copySignal.onCancel()); 4152e41f4b71Sopenharmony_citry { 4153e41f4b71Sopenharmony_ci fs.copy(srcDirPathLocal, dstDirUriLocal, options, (err: BusinessError) => { 4154e41f4b71Sopenharmony_ci if (err) { 4155e41f4b71Sopenharmony_ci console.info("copyFileWithCancel fail."); 4156e41f4b71Sopenharmony_ci return; 4157e41f4b71Sopenharmony_ci } 4158e41f4b71Sopenharmony_ci console.info("copyFileWithCancel success."); 4159e41f4b71Sopenharmony_ci }) 4160e41f4b71Sopenharmony_ci} catch (err) { 4161e41f4b71Sopenharmony_ci console.error("copyFileWithCancel failed with invalid param."); 4162e41f4b71Sopenharmony_ci} 4163e41f4b71Sopenharmony_ci 4164e41f4b71Sopenharmony_ci``` 4165e41f4b71Sopenharmony_ci 4166e41f4b71Sopenharmony_ci### onCancel<sup>12+</sup> 4167e41f4b71Sopenharmony_ci 4168e41f4b71Sopenharmony_cionCancel(): Promise<string> 4169e41f4b71Sopenharmony_ci 4170e41f4b71Sopenharmony_ciSubscribes to the event reported when a copy task is canceled. 4171e41f4b71Sopenharmony_ci 4172e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4173e41f4b71Sopenharmony_ci 4174e41f4b71Sopenharmony_ci**Return value** 4175e41f4b71Sopenharmony_ci 4176e41f4b71Sopenharmony_ci| Type | Description | 4177e41f4b71Sopenharmony_ci| --------------------- | ---------- | 4178e41f4b71Sopenharmony_ci| Promise<string> | Promise used to return the path of the last file copied.| 4179e41f4b71Sopenharmony_ci 4180e41f4b71Sopenharmony_ci**Error codes** 4181e41f4b71Sopenharmony_ci 4182e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4183e41f4b71Sopenharmony_ci 4184e41f4b71Sopenharmony_ci**Example** 4185e41f4b71Sopenharmony_ci 4186e41f4b71Sopenharmony_ci```ts 4187e41f4b71Sopenharmony_cilet copySignal: fs.TaskSignal = new TaskSignal(); 4188e41f4b71Sopenharmony_cicopySignal.onCancel().then(() => { 4189e41f4b71Sopenharmony_ci console.info("copyFileWithCancel success."); 4190e41f4b71Sopenharmony_ci}); 4191e41f4b71Sopenharmony_ci``` 4192e41f4b71Sopenharmony_ci 4193e41f4b71Sopenharmony_ci## CopyOptions<sup>11+</sup> 4194e41f4b71Sopenharmony_ci 4195e41f4b71Sopenharmony_ciDefines the callback for listening for the copy progress. 4196e41f4b71Sopenharmony_ci 4197e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4198e41f4b71Sopenharmony_ci 4199e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 4200e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------- | 4201e41f4b71Sopenharmony_ci| progressListener | [ProgressListener](#progresslistener11) | Yes | Yes | Listener used to observe the copy progress.| 4202e41f4b71Sopenharmony_ci| copySignal | [TaskSignal](#tasksignal12) | Yes | Yes | Signal used to cancel a copy task.| 4203e41f4b71Sopenharmony_ci 4204e41f4b71Sopenharmony_ci## ProgressListener<sup>11+</sup> 4205e41f4b71Sopenharmony_ci 4206e41f4b71Sopenharmony_ciListener used to observe the copy progress. 4207e41f4b71Sopenharmony_ci 4208e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4209e41f4b71Sopenharmony_ci 4210e41f4b71Sopenharmony_ci| Type| Description| 4211e41f4b71Sopenharmony_ci| ----| ------| 4212e41f4b71Sopenharmony_ci|(progress: [Progress](#progress11)) => void| Listener used to observe the copy progress.| 4213e41f4b71Sopenharmony_ci 4214e41f4b71Sopenharmony_ci**Example** 4215e41f4b71Sopenharmony_ci 4216e41f4b71Sopenharmony_ci ```ts 4217e41f4b71Sopenharmony_ci let copySignal: fs.TaskSignal = new TaskSignal(); 4218e41f4b71Sopenharmony_ci let progressListener: fs.ProgressListener = (progress: fs.Progress) => { 4219e41f4b71Sopenharmony_ci console.info(`processedSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`); 4220e41f4b71Sopenharmony_ci }; 4221e41f4b71Sopenharmony_ci let copyOption: fs.CopyOptions = { 4222e41f4b71Sopenharmony_ci "progressListener" : progressListener, 4223e41f4b71Sopenharmony_ci "copySignal" : copySignal, 4224e41f4b71Sopenharmony_ci } 4225e41f4b71Sopenharmony_ci ``` 4226e41f4b71Sopenharmony_ci 4227e41f4b71Sopenharmony_ci## Stat 4228e41f4b71Sopenharmony_ci 4229e41f4b71Sopenharmony_ciRepresents detailed file information. Before calling any API of the **Stat()** class, use [stat()](#fsstat) to create a **Stat** instance. 4230e41f4b71Sopenharmony_ci 4231e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4232e41f4b71Sopenharmony_ci 4233e41f4b71Sopenharmony_ci### Properties 4234e41f4b71Sopenharmony_ci 4235e41f4b71Sopenharmony_ci| Name | Type | Read-Only | Writable | Description | 4236e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ---------------------------------------- | 4237e41f4b71Sopenharmony_ci| ino | bigint | Yes | No | File identifier, which varies with files on the same device.| | 4238e41f4b71Sopenharmony_ci| mode | number | Yes | No | File permissions. The meaning of each bit is as follows:<br>**NOTE**<br>The following values are in octal format. The return values are in decimal format. You need to convert the values.<br>- **0o400**: The owner has the permission to read a regular file or a directory entry.<br>- **0o200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>- **0o100**: The owner has the permission to execute a regular file or search for the specified path in a directory.<br>- **0o040**: The user group has the permission to read a regular file or a directory entry.<br>- **0o020**: The user group has the permission to write a regular file or create and delete a directory entry.<br>- **0o010**: The user group has the permission to execute a regular file or search for the specified path in a directory.<br>- **0o004**: Other users have the permission to read a regular file, and other user groups have the permission to read a directory entry.<br>- **0o002**: Other users have the permission to write a regular file, and other user groups have the permission to create or delete a directory entry.<br>- **0o001**: Other users have the permission to execute a regular file, and other user groups have the permission to search for the specified path in a directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 4239e41f4b71Sopenharmony_ci| uid | number | Yes | No | ID of the file owner.| 4240e41f4b71Sopenharmony_ci| gid | number | Yes | No | ID of the user group of the file.| 4241e41f4b71Sopenharmony_ci| size | number | Yes | No | File size, in bytes. This parameter is valid only for regular files.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 4242e41f4b71Sopenharmony_ci| atime | number | Yes | No | Time when the file was last accessed. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 4243e41f4b71Sopenharmony_ci| mtime | number | Yes | No | Time when the file content was last modified. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 4244e41f4b71Sopenharmony_ci| ctime | number | Yes | No | Time when the file metadata was last modified. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970. | 4245e41f4b71Sopenharmony_ci| location<sup>11+</sup> | [LocaltionType](#locationtype11)| Yes|No| File location, which indicates whether the file is stored in a local device or in the cloud. 4246e41f4b71Sopenharmony_ci 4247e41f4b71Sopenharmony_ci### isBlockDevice 4248e41f4b71Sopenharmony_ci 4249e41f4b71Sopenharmony_ciisBlockDevice(): boolean 4250e41f4b71Sopenharmony_ci 4251e41f4b71Sopenharmony_ciChecks whether this file is a block special file. A block special file supports access by block only, and it is cached when accessed. 4252e41f4b71Sopenharmony_ci 4253e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4254e41f4b71Sopenharmony_ci 4255e41f4b71Sopenharmony_ci**Return value** 4256e41f4b71Sopenharmony_ci 4257e41f4b71Sopenharmony_ci| Type | Description | 4258e41f4b71Sopenharmony_ci| ------- | ---------------- | 4259e41f4b71Sopenharmony_ci| boolean | Whether the file is a block special file.| 4260e41f4b71Sopenharmony_ci 4261e41f4b71Sopenharmony_ci**Error codes** 4262e41f4b71Sopenharmony_ci 4263e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4264e41f4b71Sopenharmony_ci 4265e41f4b71Sopenharmony_ci**Example** 4266e41f4b71Sopenharmony_ci 4267e41f4b71Sopenharmony_ci ```ts 4268e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4269e41f4b71Sopenharmony_ci let isBLockDevice = fs.statSync(filePath).isBlockDevice(); 4270e41f4b71Sopenharmony_ci ``` 4271e41f4b71Sopenharmony_ci 4272e41f4b71Sopenharmony_ci### isCharacterDevice 4273e41f4b71Sopenharmony_ci 4274e41f4b71Sopenharmony_ciisCharacterDevice(): boolean 4275e41f4b71Sopenharmony_ci 4276e41f4b71Sopenharmony_ciChecks whether this file is a character special file. A character special file supports random access, and it is not cached when accessed. 4277e41f4b71Sopenharmony_ci 4278e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4279e41f4b71Sopenharmony_ci 4280e41f4b71Sopenharmony_ci**Return value** 4281e41f4b71Sopenharmony_ci 4282e41f4b71Sopenharmony_ci| Type | Description | 4283e41f4b71Sopenharmony_ci| ------- | ----------------- | 4284e41f4b71Sopenharmony_ci| boolean | Whether the file is a character special file.| 4285e41f4b71Sopenharmony_ci 4286e41f4b71Sopenharmony_ci**Error codes** 4287e41f4b71Sopenharmony_ci 4288e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4289e41f4b71Sopenharmony_ci 4290e41f4b71Sopenharmony_ci**Example** 4291e41f4b71Sopenharmony_ci 4292e41f4b71Sopenharmony_ci ```ts 4293e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4294e41f4b71Sopenharmony_ci let isCharacterDevice = fs.statSync(filePath).isCharacterDevice(); 4295e41f4b71Sopenharmony_ci ``` 4296e41f4b71Sopenharmony_ci 4297e41f4b71Sopenharmony_ci### isDirectory 4298e41f4b71Sopenharmony_ci 4299e41f4b71Sopenharmony_ciisDirectory(): boolean 4300e41f4b71Sopenharmony_ci 4301e41f4b71Sopenharmony_ciChecks whether this file is a directory. 4302e41f4b71Sopenharmony_ci 4303e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 4304e41f4b71Sopenharmony_ci 4305e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4306e41f4b71Sopenharmony_ci 4307e41f4b71Sopenharmony_ci**Return value** 4308e41f4b71Sopenharmony_ci 4309e41f4b71Sopenharmony_ci| Type | Description | 4310e41f4b71Sopenharmony_ci| ------- | ------------- | 4311e41f4b71Sopenharmony_ci| boolean | Whether the file is a directory.| 4312e41f4b71Sopenharmony_ci 4313e41f4b71Sopenharmony_ci**Error codes** 4314e41f4b71Sopenharmony_ci 4315e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4316e41f4b71Sopenharmony_ci 4317e41f4b71Sopenharmony_ci**Example** 4318e41f4b71Sopenharmony_ci 4319e41f4b71Sopenharmony_ci ```ts 4320e41f4b71Sopenharmony_ci let dirPath = pathDir + "/test"; 4321e41f4b71Sopenharmony_ci let isDirectory = fs.statSync(dirPath).isDirectory(); 4322e41f4b71Sopenharmony_ci ``` 4323e41f4b71Sopenharmony_ci 4324e41f4b71Sopenharmony_ci### isFIFO 4325e41f4b71Sopenharmony_ci 4326e41f4b71Sopenharmony_ciisFIFO(): boolean 4327e41f4b71Sopenharmony_ci 4328e41f4b71Sopenharmony_ciChecks whether this file is a named pipe (or FIFO). Named pipes are used for inter-process communication. 4329e41f4b71Sopenharmony_ci 4330e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4331e41f4b71Sopenharmony_ci 4332e41f4b71Sopenharmony_ci**Return value** 4333e41f4b71Sopenharmony_ci 4334e41f4b71Sopenharmony_ci| Type | Description | 4335e41f4b71Sopenharmony_ci| ------- | --------------------- | 4336e41f4b71Sopenharmony_ci| boolean | Whether the file is an FIFO.| 4337e41f4b71Sopenharmony_ci 4338e41f4b71Sopenharmony_ci**Error codes** 4339e41f4b71Sopenharmony_ci 4340e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4341e41f4b71Sopenharmony_ci 4342e41f4b71Sopenharmony_ci**Example** 4343e41f4b71Sopenharmony_ci 4344e41f4b71Sopenharmony_ci ```ts 4345e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4346e41f4b71Sopenharmony_ci let isFIFO = fs.statSync(filePath).isFIFO(); 4347e41f4b71Sopenharmony_ci ``` 4348e41f4b71Sopenharmony_ci 4349e41f4b71Sopenharmony_ci### isFile 4350e41f4b71Sopenharmony_ci 4351e41f4b71Sopenharmony_ciisFile(): boolean 4352e41f4b71Sopenharmony_ci 4353e41f4b71Sopenharmony_ciChecks whether this file is a regular file. 4354e41f4b71Sopenharmony_ci 4355e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 4356e41f4b71Sopenharmony_ci 4357e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4358e41f4b71Sopenharmony_ci 4359e41f4b71Sopenharmony_ci**Return value** 4360e41f4b71Sopenharmony_ci 4361e41f4b71Sopenharmony_ci| Type | Description | 4362e41f4b71Sopenharmony_ci| ------- | --------------- | 4363e41f4b71Sopenharmony_ci| boolean | Whether the file is a regular file.| 4364e41f4b71Sopenharmony_ci 4365e41f4b71Sopenharmony_ci**Error codes** 4366e41f4b71Sopenharmony_ci 4367e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4368e41f4b71Sopenharmony_ci 4369e41f4b71Sopenharmony_ci**Example** 4370e41f4b71Sopenharmony_ci 4371e41f4b71Sopenharmony_ci ```ts 4372e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4373e41f4b71Sopenharmony_ci let isFile = fs.statSync(filePath).isFile(); 4374e41f4b71Sopenharmony_ci ``` 4375e41f4b71Sopenharmony_ci 4376e41f4b71Sopenharmony_ci### isSocket 4377e41f4b71Sopenharmony_ci 4378e41f4b71Sopenharmony_ciisSocket(): boolean 4379e41f4b71Sopenharmony_ci 4380e41f4b71Sopenharmony_ciChecks whether this file is a socket. 4381e41f4b71Sopenharmony_ci 4382e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4383e41f4b71Sopenharmony_ci 4384e41f4b71Sopenharmony_ci**Return value** 4385e41f4b71Sopenharmony_ci 4386e41f4b71Sopenharmony_ci| Type | Description | 4387e41f4b71Sopenharmony_ci| ------- | -------------- | 4388e41f4b71Sopenharmony_ci| boolean | Whether the file is a socket.| 4389e41f4b71Sopenharmony_ci 4390e41f4b71Sopenharmony_ci**Error codes** 4391e41f4b71Sopenharmony_ci 4392e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4393e41f4b71Sopenharmony_ci 4394e41f4b71Sopenharmony_ci**Example** 4395e41f4b71Sopenharmony_ci 4396e41f4b71Sopenharmony_ci ```ts 4397e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4398e41f4b71Sopenharmony_ci let isSocket = fs.statSync(filePath).isSocket(); 4399e41f4b71Sopenharmony_ci ``` 4400e41f4b71Sopenharmony_ci 4401e41f4b71Sopenharmony_ci### isSymbolicLink 4402e41f4b71Sopenharmony_ci 4403e41f4b71Sopenharmony_ciisSymbolicLink(): boolean 4404e41f4b71Sopenharmony_ci 4405e41f4b71Sopenharmony_ciChecks whether this file is a symbolic link. 4406e41f4b71Sopenharmony_ci 4407e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4408e41f4b71Sopenharmony_ci 4409e41f4b71Sopenharmony_ci**Return value** 4410e41f4b71Sopenharmony_ci 4411e41f4b71Sopenharmony_ci| Type | Description | 4412e41f4b71Sopenharmony_ci| ------- | --------------- | 4413e41f4b71Sopenharmony_ci| boolean | Whether the file is a symbolic link.| 4414e41f4b71Sopenharmony_ci 4415e41f4b71Sopenharmony_ci**Error codes** 4416e41f4b71Sopenharmony_ci 4417e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4418e41f4b71Sopenharmony_ci 4419e41f4b71Sopenharmony_ci**Example** 4420e41f4b71Sopenharmony_ci 4421e41f4b71Sopenharmony_ci ```ts 4422e41f4b71Sopenharmony_ci let filePath = pathDir + "/test"; 4423e41f4b71Sopenharmony_ci let isSymbolicLink = fs.statSync(filePath).isSymbolicLink(); 4424e41f4b71Sopenharmony_ci ``` 4425e41f4b71Sopenharmony_ci 4426e41f4b71Sopenharmony_ci## Stream 4427e41f4b71Sopenharmony_ci 4428e41f4b71Sopenharmony_ciProvides API for stream operations. Before calling any API of **Stream**, you need to create a **Stream** instance by using [fs.createStream](#fscreatestream) or [fs.fdopenStream](#fsfdopenstream). 4429e41f4b71Sopenharmony_ci 4430e41f4b71Sopenharmony_ci### close 4431e41f4b71Sopenharmony_ci 4432e41f4b71Sopenharmony_ciclose(): Promise<void> 4433e41f4b71Sopenharmony_ci 4434e41f4b71Sopenharmony_ciCloses this stream. This API uses a promise to return the result. 4435e41f4b71Sopenharmony_ci 4436e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4437e41f4b71Sopenharmony_ci 4438e41f4b71Sopenharmony_ci**Return value** 4439e41f4b71Sopenharmony_ci 4440e41f4b71Sopenharmony_ci| Type | Description | 4441e41f4b71Sopenharmony_ci| ------------------- | ------------- | 4442e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4443e41f4b71Sopenharmony_ci 4444e41f4b71Sopenharmony_ci**Error codes** 4445e41f4b71Sopenharmony_ci 4446e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4447e41f4b71Sopenharmony_ci 4448e41f4b71Sopenharmony_ci**Example** 4449e41f4b71Sopenharmony_ci 4450e41f4b71Sopenharmony_ci ```ts 4451e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4452e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4453e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4454e41f4b71Sopenharmony_ci stream.close().then(() => { 4455e41f4b71Sopenharmony_ci console.info("File stream closed"); 4456e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4457e41f4b71Sopenharmony_ci console.error("close fileStream failed with error message: " + err.message + ", error code: " + err.code); 4458e41f4b71Sopenharmony_ci }); 4459e41f4b71Sopenharmony_ci ``` 4460e41f4b71Sopenharmony_ci 4461e41f4b71Sopenharmony_ci### close 4462e41f4b71Sopenharmony_ci 4463e41f4b71Sopenharmony_ciclose(callback: AsyncCallback<void>): void 4464e41f4b71Sopenharmony_ci 4465e41f4b71Sopenharmony_ciCloses this stream. This API uses an asynchronous callback to return the result. 4466e41f4b71Sopenharmony_ci 4467e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4468e41f4b71Sopenharmony_ci 4469e41f4b71Sopenharmony_ci**Parameters** 4470e41f4b71Sopenharmony_ci 4471e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4472e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------- | 4473e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback invoked immediately after the stream is closed.| 4474e41f4b71Sopenharmony_ci 4475e41f4b71Sopenharmony_ci**Error codes** 4476e41f4b71Sopenharmony_ci 4477e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4478e41f4b71Sopenharmony_ci 4479e41f4b71Sopenharmony_ci**Example** 4480e41f4b71Sopenharmony_ci 4481e41f4b71Sopenharmony_ci ```ts 4482e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4483e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4484e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4485e41f4b71Sopenharmony_ci stream.close((err: BusinessError) => { 4486e41f4b71Sopenharmony_ci if (err) { 4487e41f4b71Sopenharmony_ci console.error("close stream failed with error message: " + err.message + ", error code: " + err.code); 4488e41f4b71Sopenharmony_ci } else { 4489e41f4b71Sopenharmony_ci console.info("close stream succeed"); 4490e41f4b71Sopenharmony_ci } 4491e41f4b71Sopenharmony_ci }); 4492e41f4b71Sopenharmony_ci ``` 4493e41f4b71Sopenharmony_ci 4494e41f4b71Sopenharmony_ci### closeSync 4495e41f4b71Sopenharmony_ci 4496e41f4b71Sopenharmony_cicloseSync(): void 4497e41f4b71Sopenharmony_ci 4498e41f4b71Sopenharmony_ciCloses this stream. This API returns the result synchronously. 4499e41f4b71Sopenharmony_ci 4500e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4501e41f4b71Sopenharmony_ci 4502e41f4b71Sopenharmony_ci**Error codes** 4503e41f4b71Sopenharmony_ci 4504e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4505e41f4b71Sopenharmony_ci 4506e41f4b71Sopenharmony_ci**Example** 4507e41f4b71Sopenharmony_ci 4508e41f4b71Sopenharmony_ci ```ts 4509e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4510e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4511e41f4b71Sopenharmony_ci stream.closeSync(); 4512e41f4b71Sopenharmony_ci ``` 4513e41f4b71Sopenharmony_ci 4514e41f4b71Sopenharmony_ci### flush 4515e41f4b71Sopenharmony_ci 4516e41f4b71Sopenharmony_ciflush(): Promise<void> 4517e41f4b71Sopenharmony_ci 4518e41f4b71Sopenharmony_ciFlushes this stream. This API uses a promise to return the result. 4519e41f4b71Sopenharmony_ci 4520e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4521e41f4b71Sopenharmony_ci 4522e41f4b71Sopenharmony_ci**Return value** 4523e41f4b71Sopenharmony_ci 4524e41f4b71Sopenharmony_ci| Type | Description | 4525e41f4b71Sopenharmony_ci| ------------------- | ------------- | 4526e41f4b71Sopenharmony_ci| Promise<void> | Promise used to return the result.| 4527e41f4b71Sopenharmony_ci 4528e41f4b71Sopenharmony_ci**Error codes** 4529e41f4b71Sopenharmony_ci 4530e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4531e41f4b71Sopenharmony_ci 4532e41f4b71Sopenharmony_ci**Example** 4533e41f4b71Sopenharmony_ci 4534e41f4b71Sopenharmony_ci ```ts 4535e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4536e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4537e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4538e41f4b71Sopenharmony_ci stream.flush().then(() => { 4539e41f4b71Sopenharmony_ci console.info("Stream flushed"); 4540e41f4b71Sopenharmony_ci stream.close(); 4541e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4542e41f4b71Sopenharmony_ci console.error("flush failed with error message: " + err.message + ", error code: " + err.code); 4543e41f4b71Sopenharmony_ci }); 4544e41f4b71Sopenharmony_ci ``` 4545e41f4b71Sopenharmony_ci 4546e41f4b71Sopenharmony_ci### flush 4547e41f4b71Sopenharmony_ci 4548e41f4b71Sopenharmony_ciflush(callback: AsyncCallback<void>): void 4549e41f4b71Sopenharmony_ci 4550e41f4b71Sopenharmony_ciFlushes this stream. This API uses an asynchronous callback to return the result. 4551e41f4b71Sopenharmony_ci 4552e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4553e41f4b71Sopenharmony_ci 4554e41f4b71Sopenharmony_ci**Parameters** 4555e41f4b71Sopenharmony_ci 4556e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4557e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | -------------- | 4558e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 4559e41f4b71Sopenharmony_ci 4560e41f4b71Sopenharmony_ci**Error codes** 4561e41f4b71Sopenharmony_ci 4562e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4563e41f4b71Sopenharmony_ci 4564e41f4b71Sopenharmony_ci**Example** 4565e41f4b71Sopenharmony_ci 4566e41f4b71Sopenharmony_ci ```ts 4567e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4568e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4569e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4570e41f4b71Sopenharmony_ci stream.flush((err: BusinessError) => { 4571e41f4b71Sopenharmony_ci if (err) { 4572e41f4b71Sopenharmony_ci console.error("flush stream failed with error message: " + err.message + ", error code: " + err.code); 4573e41f4b71Sopenharmony_ci } else { 4574e41f4b71Sopenharmony_ci console.info("Stream flushed"); 4575e41f4b71Sopenharmony_ci stream.close(); 4576e41f4b71Sopenharmony_ci } 4577e41f4b71Sopenharmony_ci }); 4578e41f4b71Sopenharmony_ci ``` 4579e41f4b71Sopenharmony_ci 4580e41f4b71Sopenharmony_ci### flushSync 4581e41f4b71Sopenharmony_ci 4582e41f4b71Sopenharmony_ciflushSync(): void 4583e41f4b71Sopenharmony_ci 4584e41f4b71Sopenharmony_ciFlushes this stream. This API returns the result synchronously. 4585e41f4b71Sopenharmony_ci 4586e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4587e41f4b71Sopenharmony_ci 4588e41f4b71Sopenharmony_ci**Error codes** 4589e41f4b71Sopenharmony_ci 4590e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4591e41f4b71Sopenharmony_ci 4592e41f4b71Sopenharmony_ci**Example** 4593e41f4b71Sopenharmony_ci 4594e41f4b71Sopenharmony_ci ```ts 4595e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4596e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4597e41f4b71Sopenharmony_ci stream.flushSync(); 4598e41f4b71Sopenharmony_ci stream.close(); 4599e41f4b71Sopenharmony_ci ``` 4600e41f4b71Sopenharmony_ci 4601e41f4b71Sopenharmony_ci### write 4602e41f4b71Sopenharmony_ci 4603e41f4b71Sopenharmony_ciwrite(buffer: ArrayBuffer | string, options?: WriteOptions): Promise<number> 4604e41f4b71Sopenharmony_ci 4605e41f4b71Sopenharmony_ciWrites data to this stream. This API uses a promise to return the result. 4606e41f4b71Sopenharmony_ci 4607e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4608e41f4b71Sopenharmony_ci 4609e41f4b71Sopenharmony_ci**Parameters** 4610e41f4b71Sopenharmony_ci 4611e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4612e41f4b71Sopenharmony_ci| ------- | ------------------------------- | ---- | ---------------------------------------- | 4613e41f4b71Sopenharmony_ci| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | 4614e41f4b71Sopenharmony_ci| options | [WriteOptions](#writeoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to write. The default value is the buffer length.<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 4615e41f4b71Sopenharmony_ci 4616e41f4b71Sopenharmony_ci**Return value** 4617e41f4b71Sopenharmony_ci 4618e41f4b71Sopenharmony_ci| Type | Description | 4619e41f4b71Sopenharmony_ci| --------------------- | -------- | 4620e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the length of the data written.| 4621e41f4b71Sopenharmony_ci 4622e41f4b71Sopenharmony_ci**Error codes** 4623e41f4b71Sopenharmony_ci 4624e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4625e41f4b71Sopenharmony_ci 4626e41f4b71Sopenharmony_ci**Example** 4627e41f4b71Sopenharmony_ci 4628e41f4b71Sopenharmony_ci ```ts 4629e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4630e41f4b71Sopenharmony_ci import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit'; 4631e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4632e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4633e41f4b71Sopenharmony_ci let writeOption: WriteOptions = { 4634e41f4b71Sopenharmony_ci offset: 5, 4635e41f4b71Sopenharmony_ci length: 5, 4636e41f4b71Sopenharmony_ci encoding: 'utf-8' 4637e41f4b71Sopenharmony_ci }; 4638e41f4b71Sopenharmony_ci stream.write("hello, world", writeOption).then((number: number) => { 4639e41f4b71Sopenharmony_ci console.info("write succeed and size is:" + number); 4640e41f4b71Sopenharmony_ci stream.close(); 4641e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4642e41f4b71Sopenharmony_ci console.error("write failed with error message: " + err.message + ", error code: " + err.code); 4643e41f4b71Sopenharmony_ci }); 4644e41f4b71Sopenharmony_ci ``` 4645e41f4b71Sopenharmony_ci 4646e41f4b71Sopenharmony_ci### write 4647e41f4b71Sopenharmony_ci 4648e41f4b71Sopenharmony_ciwrite(buffer: ArrayBuffer | string, options?: WriteOptions, callback: AsyncCallback<number>): void 4649e41f4b71Sopenharmony_ci 4650e41f4b71Sopenharmony_ciWrites data to this stream. This API uses an asynchronous callback to return the result. 4651e41f4b71Sopenharmony_ci 4652e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4653e41f4b71Sopenharmony_ci 4654e41f4b71Sopenharmony_ci**Parameters** 4655e41f4b71Sopenharmony_ci 4656e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4657e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 4658e41f4b71Sopenharmony_ci| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | 4659e41f4b71Sopenharmony_ci| options | [WriteOptions](#writeoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 4660e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the result. | 4661e41f4b71Sopenharmony_ci 4662e41f4b71Sopenharmony_ci**Error codes** 4663e41f4b71Sopenharmony_ci 4664e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4665e41f4b71Sopenharmony_ci 4666e41f4b71Sopenharmony_ci**Example** 4667e41f4b71Sopenharmony_ci 4668e41f4b71Sopenharmony_ci ```ts 4669e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4670e41f4b71Sopenharmony_ci import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit'; 4671e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4672e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4673e41f4b71Sopenharmony_ci let writeOption: WriteOptions = { 4674e41f4b71Sopenharmony_ci offset: 5, 4675e41f4b71Sopenharmony_ci length: 5, 4676e41f4b71Sopenharmony_ci encoding: 'utf-8' 4677e41f4b71Sopenharmony_ci }; 4678e41f4b71Sopenharmony_ci stream.write("hello, world", writeOption, (err: BusinessError, bytesWritten: number) => { 4679e41f4b71Sopenharmony_ci if (err) { 4680e41f4b71Sopenharmony_ci console.error("write stream failed with error message: " + err.message + ", error code: " + err.code); 4681e41f4b71Sopenharmony_ci } else { 4682e41f4b71Sopenharmony_ci if (bytesWritten) { 4683e41f4b71Sopenharmony_ci console.info("write succeed and size is:" + bytesWritten); 4684e41f4b71Sopenharmony_ci stream.close(); 4685e41f4b71Sopenharmony_ci } 4686e41f4b71Sopenharmony_ci } 4687e41f4b71Sopenharmony_ci }); 4688e41f4b71Sopenharmony_ci ``` 4689e41f4b71Sopenharmony_ci 4690e41f4b71Sopenharmony_ci### writeSync 4691e41f4b71Sopenharmony_ci 4692e41f4b71Sopenharmony_ciwriteSync(buffer: ArrayBuffer | string, options?: WriteOptions): number 4693e41f4b71Sopenharmony_ci 4694e41f4b71Sopenharmony_ciWrites data to this stream. This API returns the result synchronously. 4695e41f4b71Sopenharmony_ci 4696e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4697e41f4b71Sopenharmony_ci 4698e41f4b71Sopenharmony_ci**Parameters** 4699e41f4b71Sopenharmony_ci 4700e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4701e41f4b71Sopenharmony_ci| ------- | ------------------------------- | ---- | ---------------------------------------- | 4702e41f4b71Sopenharmony_ci| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | 4703e41f4b71Sopenharmony_ci| options | [WriteOptions](#writeoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 4704e41f4b71Sopenharmony_ci 4705e41f4b71Sopenharmony_ci**Return value** 4706e41f4b71Sopenharmony_ci 4707e41f4b71Sopenharmony_ci| Type | Description | 4708e41f4b71Sopenharmony_ci| ------ | -------- | 4709e41f4b71Sopenharmony_ci| number | Length of the data written in the file.| 4710e41f4b71Sopenharmony_ci 4711e41f4b71Sopenharmony_ci**Error codes** 4712e41f4b71Sopenharmony_ci 4713e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4714e41f4b71Sopenharmony_ci 4715e41f4b71Sopenharmony_ci**Example** 4716e41f4b71Sopenharmony_ci 4717e41f4b71Sopenharmony_ci ```ts 4718e41f4b71Sopenharmony_ci import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit'; 4719e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4720e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath,"r+"); 4721e41f4b71Sopenharmony_ci let writeOption: WriteOptions = { 4722e41f4b71Sopenharmony_ci offset: 5, 4723e41f4b71Sopenharmony_ci length: 5, 4724e41f4b71Sopenharmony_ci encoding: 'utf-8' 4725e41f4b71Sopenharmony_ci }; 4726e41f4b71Sopenharmony_ci let num = stream.writeSync("hello, world", writeOption); 4727e41f4b71Sopenharmony_ci stream.close(); 4728e41f4b71Sopenharmony_ci ``` 4729e41f4b71Sopenharmony_ci 4730e41f4b71Sopenharmony_ci### read 4731e41f4b71Sopenharmony_ci 4732e41f4b71Sopenharmony_ciread(buffer: ArrayBuffer, options?: ReadOptions): Promise<number> 4733e41f4b71Sopenharmony_ci 4734e41f4b71Sopenharmony_ciReads data from this stream. This API uses a promise to return the result. 4735e41f4b71Sopenharmony_ci 4736e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4737e41f4b71Sopenharmony_ci 4738e41f4b71Sopenharmony_ci**Parameters** 4739e41f4b71Sopenharmony_ci 4740e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4741e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ---------------------------------------- | 4742e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 4743e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.| 4744e41f4b71Sopenharmony_ci 4745e41f4b71Sopenharmony_ci**Return value** 4746e41f4b71Sopenharmony_ci 4747e41f4b71Sopenharmony_ci| Type | Description | 4748e41f4b71Sopenharmony_ci| ---------------------------------- | ------ | 4749e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the data read.| 4750e41f4b71Sopenharmony_ci 4751e41f4b71Sopenharmony_ci**Error codes** 4752e41f4b71Sopenharmony_ci 4753e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4754e41f4b71Sopenharmony_ci 4755e41f4b71Sopenharmony_ci**Example** 4756e41f4b71Sopenharmony_ci 4757e41f4b71Sopenharmony_ci ```ts 4758e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4759e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 4760e41f4b71Sopenharmony_ci import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit'; 4761e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4762e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4763e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(4096); 4764e41f4b71Sopenharmony_ci let readOption: ReadOptions = { 4765e41f4b71Sopenharmony_ci offset: 5, 4766e41f4b71Sopenharmony_ci length: 5 4767e41f4b71Sopenharmony_ci }; 4768e41f4b71Sopenharmony_ci stream.read(arrayBuffer, readOption).then((readLen: number) => { 4769e41f4b71Sopenharmony_ci console.info("Read data successfully"); 4770e41f4b71Sopenharmony_ci let buf = buffer.from(arrayBuffer, 0, readLen); 4771e41f4b71Sopenharmony_ci console.log(`The content of file: ${buf.toString()}`); 4772e41f4b71Sopenharmony_ci stream.close(); 4773e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4774e41f4b71Sopenharmony_ci console.error("read data failed with error message: " + err.message + ", error code: " + err.code); 4775e41f4b71Sopenharmony_ci }); 4776e41f4b71Sopenharmony_ci ``` 4777e41f4b71Sopenharmony_ci 4778e41f4b71Sopenharmony_ci### read 4779e41f4b71Sopenharmony_ci 4780e41f4b71Sopenharmony_ciread(buffer: ArrayBuffer, options?: ReadOptions, callback: AsyncCallback<number>): void 4781e41f4b71Sopenharmony_ci 4782e41f4b71Sopenharmony_ciReads data from this stream. This API uses an asynchronous callback to return the result. 4783e41f4b71Sopenharmony_ci 4784e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4785e41f4b71Sopenharmony_ci 4786e41f4b71Sopenharmony_ci**Parameters** 4787e41f4b71Sopenharmony_ci 4788e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4789e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 4790e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 4791e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.| 4792e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the result. | 4793e41f4b71Sopenharmony_ci 4794e41f4b71Sopenharmony_ci**Error codes** 4795e41f4b71Sopenharmony_ci 4796e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4797e41f4b71Sopenharmony_ci 4798e41f4b71Sopenharmony_ci**Example** 4799e41f4b71Sopenharmony_ci 4800e41f4b71Sopenharmony_ci ```ts 4801e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4802e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 4803e41f4b71Sopenharmony_ci import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit'; 4804e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4805e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4806e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(4096); 4807e41f4b71Sopenharmony_ci let readOption: ReadOptions = { 4808e41f4b71Sopenharmony_ci offset: 5, 4809e41f4b71Sopenharmony_ci length: 5 4810e41f4b71Sopenharmony_ci }; 4811e41f4b71Sopenharmony_ci stream.read(arrayBuffer, readOption, (err: BusinessError, readLen: number) => { 4812e41f4b71Sopenharmony_ci if (err) { 4813e41f4b71Sopenharmony_ci console.error("read stream failed with error message: " + err.message + ", error code: " + err.code); 4814e41f4b71Sopenharmony_ci } else { 4815e41f4b71Sopenharmony_ci console.info("Read data successfully"); 4816e41f4b71Sopenharmony_ci let buf = buffer.from(arrayBuffer, 0, readLen); 4817e41f4b71Sopenharmony_ci console.log(`The content of file: ${buf.toString()}`); 4818e41f4b71Sopenharmony_ci stream.close(); 4819e41f4b71Sopenharmony_ci } 4820e41f4b71Sopenharmony_ci }); 4821e41f4b71Sopenharmony_ci ``` 4822e41f4b71Sopenharmony_ci 4823e41f4b71Sopenharmony_ci### readSync 4824e41f4b71Sopenharmony_ci 4825e41f4b71Sopenharmony_cireadSync(buffer: ArrayBuffer, options?: ReadOptions): number 4826e41f4b71Sopenharmony_ci 4827e41f4b71Sopenharmony_ciReads data from this stream. This API returns the result synchronously. 4828e41f4b71Sopenharmony_ci 4829e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4830e41f4b71Sopenharmony_ci 4831e41f4b71Sopenharmony_ci**Parameters** 4832e41f4b71Sopenharmony_ci 4833e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4834e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ---------------------------------------- | 4835e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 4836e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.<br> | 4837e41f4b71Sopenharmony_ci 4838e41f4b71Sopenharmony_ci**Return value** 4839e41f4b71Sopenharmony_ci 4840e41f4b71Sopenharmony_ci| Type | Description | 4841e41f4b71Sopenharmony_ci| ------ | -------- | 4842e41f4b71Sopenharmony_ci| number | Length of the data read.| 4843e41f4b71Sopenharmony_ci 4844e41f4b71Sopenharmony_ci**Error codes** 4845e41f4b71Sopenharmony_ci 4846e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4847e41f4b71Sopenharmony_ci 4848e41f4b71Sopenharmony_ci**Example** 4849e41f4b71Sopenharmony_ci 4850e41f4b71Sopenharmony_ci ```ts 4851e41f4b71Sopenharmony_ci import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit'; 4852e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4853e41f4b71Sopenharmony_ci let stream = fs.createStreamSync(filePath, "r+"); 4854e41f4b71Sopenharmony_ci let readOption: ReadOptions = { 4855e41f4b71Sopenharmony_ci offset: 5, 4856e41f4b71Sopenharmony_ci length: 5 4857e41f4b71Sopenharmony_ci }; 4858e41f4b71Sopenharmony_ci let buf = new ArrayBuffer(4096); 4859e41f4b71Sopenharmony_ci let num = stream.readSync(buf, readOption); 4860e41f4b71Sopenharmony_ci stream.close(); 4861e41f4b71Sopenharmony_ci ``` 4862e41f4b71Sopenharmony_ci 4863e41f4b71Sopenharmony_ci## File 4864e41f4b71Sopenharmony_ci 4865e41f4b71Sopenharmony_ciRepresents a **File** object opened by **open()**. 4866e41f4b71Sopenharmony_ci 4867e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4868e41f4b71Sopenharmony_ci 4869e41f4b71Sopenharmony_ci### Properties 4870e41f4b71Sopenharmony_ci 4871e41f4b71Sopenharmony_ci| Name | Type | Read-Only | Writable | Description | 4872e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------- | 4873e41f4b71Sopenharmony_ci| fd | number | Yes | No | FD of the file.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 4874e41f4b71Sopenharmony_ci| path<sup>10+</sup> | string | Yes | No | Path of the file.| 4875e41f4b71Sopenharmony_ci| name<sup>10+</sup> | string | Yes | No | Name of the file.| 4876e41f4b71Sopenharmony_ci 4877e41f4b71Sopenharmony_ci### getParent<sup>11+</sup> 4878e41f4b71Sopenharmony_ci 4879e41f4b71Sopenharmony_cigetParent(): string 4880e41f4b71Sopenharmony_ci 4881e41f4b71Sopenharmony_ciObtains the parent directory of this file object. 4882e41f4b71Sopenharmony_ci 4883e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4884e41f4b71Sopenharmony_ci 4885e41f4b71Sopenharmony_ci**Return value** 4886e41f4b71Sopenharmony_ci 4887e41f4b71Sopenharmony_ci| Type | Description | 4888e41f4b71Sopenharmony_ci| ---------------------------------- | ------ | 4889e41f4b71Sopenharmony_ci| string | Parent directory obtained.| 4890e41f4b71Sopenharmony_ci 4891e41f4b71Sopenharmony_ci**Error codes** 4892e41f4b71Sopenharmony_ci 4893e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4894e41f4b71Sopenharmony_ci 4895e41f4b71Sopenharmony_ci**Example** 4896e41f4b71Sopenharmony_ci 4897e41f4b71Sopenharmony_ci ```ts 4898e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4899e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4900e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 4901e41f4b71Sopenharmony_ci console.info('The parent path is: ' + file.getParent()); 4902e41f4b71Sopenharmony_ci fs.closeSync(file); 4903e41f4b71Sopenharmony_ci ``` 4904e41f4b71Sopenharmony_ci 4905e41f4b71Sopenharmony_ci### lock 4906e41f4b71Sopenharmony_ci 4907e41f4b71Sopenharmony_cilock(exclusive?: boolean): Promise\<void> 4908e41f4b71Sopenharmony_ci 4909e41f4b71Sopenharmony_ciApplies an exclusive lock or a shared lock on this file in blocking mode. This API uses a promise to return the result. 4910e41f4b71Sopenharmony_ci 4911e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4912e41f4b71Sopenharmony_ci 4913e41f4b71Sopenharmony_ci**Parameters** 4914e41f4b71Sopenharmony_ci 4915e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4916e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ---------------------------------------- | 4917e41f4b71Sopenharmony_ci| exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | 4918e41f4b71Sopenharmony_ci 4919e41f4b71Sopenharmony_ci**Return value** 4920e41f4b71Sopenharmony_ci 4921e41f4b71Sopenharmony_ci| Type | Description | 4922e41f4b71Sopenharmony_ci| ---------------------------------- | ------ | 4923e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4924e41f4b71Sopenharmony_ci 4925e41f4b71Sopenharmony_ci**Error codes** 4926e41f4b71Sopenharmony_ci 4927e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4928e41f4b71Sopenharmony_ci 4929e41f4b71Sopenharmony_ci**Example** 4930e41f4b71Sopenharmony_ci 4931e41f4b71Sopenharmony_ci ```ts 4932e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4933e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4934e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 4935e41f4b71Sopenharmony_ci file.lock(true).then(() => { 4936e41f4b71Sopenharmony_ci console.log("lock file succeed"); 4937e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4938e41f4b71Sopenharmony_ci console.error("lock file failed with error message: " + err.message + ", error code: " + err.code); 4939e41f4b71Sopenharmony_ci }).finally(() => { 4940e41f4b71Sopenharmony_ci fs.closeSync(file); 4941e41f4b71Sopenharmony_ci }); 4942e41f4b71Sopenharmony_ci ``` 4943e41f4b71Sopenharmony_ci 4944e41f4b71Sopenharmony_ci### lock 4945e41f4b71Sopenharmony_ci 4946e41f4b71Sopenharmony_cilock(exclusive?: boolean, callback: AsyncCallback\<void>): void 4947e41f4b71Sopenharmony_ci 4948e41f4b71Sopenharmony_ciApplies an exclusive lock or a shared lock on this file in blocking mode. This API uses an asynchronous callback to return the result. 4949e41f4b71Sopenharmony_ci 4950e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4951e41f4b71Sopenharmony_ci 4952e41f4b71Sopenharmony_ci**Parameters** 4953e41f4b71Sopenharmony_ci 4954e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4955e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ---------------------------------------- | 4956e41f4b71Sopenharmony_ci| exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | 4957e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 4958e41f4b71Sopenharmony_ci 4959e41f4b71Sopenharmony_ci**Error codes** 4960e41f4b71Sopenharmony_ci 4961e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4962e41f4b71Sopenharmony_ci 4963e41f4b71Sopenharmony_ci**Example** 4964e41f4b71Sopenharmony_ci 4965e41f4b71Sopenharmony_ci ```ts 4966e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 4967e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 4968e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 4969e41f4b71Sopenharmony_ci file.lock(true, (err: BusinessError) => { 4970e41f4b71Sopenharmony_ci if (err) { 4971e41f4b71Sopenharmony_ci console.error("lock file failed with error message: " + err.message + ", error code: " + err.code); 4972e41f4b71Sopenharmony_ci } else { 4973e41f4b71Sopenharmony_ci console.log("lock file succeed"); 4974e41f4b71Sopenharmony_ci } 4975e41f4b71Sopenharmony_ci fs.closeSync(file); 4976e41f4b71Sopenharmony_ci }); 4977e41f4b71Sopenharmony_ci ``` 4978e41f4b71Sopenharmony_ci 4979e41f4b71Sopenharmony_ci### tryLock 4980e41f4b71Sopenharmony_ci 4981e41f4b71Sopenharmony_citryLock(exclusive?: boolean): void 4982e41f4b71Sopenharmony_ci 4983e41f4b71Sopenharmony_ciApplies an exclusive lock or a shared lock on this file in non-blocking mode. 4984e41f4b71Sopenharmony_ci 4985e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 4986e41f4b71Sopenharmony_ci 4987e41f4b71Sopenharmony_ci**Parameters** 4988e41f4b71Sopenharmony_ci 4989e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 4990e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ---------------------------------------- | 4991e41f4b71Sopenharmony_ci| exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | 4992e41f4b71Sopenharmony_ci 4993e41f4b71Sopenharmony_ci**Error codes** 4994e41f4b71Sopenharmony_ci 4995e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 4996e41f4b71Sopenharmony_ci 4997e41f4b71Sopenharmony_ci**Example** 4998e41f4b71Sopenharmony_ci 4999e41f4b71Sopenharmony_ci ```ts 5000e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5001e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5002e41f4b71Sopenharmony_ci file.tryLock(true); 5003e41f4b71Sopenharmony_ci console.log("lock file succeed"); 5004e41f4b71Sopenharmony_ci fs.closeSync(file); 5005e41f4b71Sopenharmony_ci ``` 5006e41f4b71Sopenharmony_ci 5007e41f4b71Sopenharmony_ci### unlock 5008e41f4b71Sopenharmony_ci 5009e41f4b71Sopenharmony_ciunlock(): void 5010e41f4b71Sopenharmony_ci 5011e41f4b71Sopenharmony_ciUnlocks this file. This API returns the result synchronously. 5012e41f4b71Sopenharmony_ci 5013e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5014e41f4b71Sopenharmony_ci 5015e41f4b71Sopenharmony_ci**Error codes** 5016e41f4b71Sopenharmony_ci 5017e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5018e41f4b71Sopenharmony_ci 5019e41f4b71Sopenharmony_ci**Example** 5020e41f4b71Sopenharmony_ci 5021e41f4b71Sopenharmony_ci ```ts 5022e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5023e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5024e41f4b71Sopenharmony_ci file.tryLock(true); 5025e41f4b71Sopenharmony_ci file.unlock(); 5026e41f4b71Sopenharmony_ci console.log("unlock file succeed"); 5027e41f4b71Sopenharmony_ci fs.closeSync(file); 5028e41f4b71Sopenharmony_ci ``` 5029e41f4b71Sopenharmony_ci 5030e41f4b71Sopenharmony_ci ## fs.DfsListeners<sup>12+</sup> 5031e41f4b71Sopenharmony_ci 5032e41f4b71Sopenharmony_ciinterface DfsListeners { 5033e41f4b71Sopenharmony_ci onStatus(networkId: string, status: number): void; 5034e41f4b71Sopenharmony_ci} 5035e41f4b71Sopenharmony_ci 5036e41f4b71Sopenharmony_ciProvides APIs for listening for the distributed file system status. 5037e41f4b71Sopenharmony_ci 5038e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5039e41f4b71Sopenharmony_ci 5040e41f4b71Sopenharmony_ci### onStatus<sup>12+</sup> 5041e41f4b71Sopenharmony_ci 5042e41f4b71Sopenharmony_cionStatus(networkId: string, status: number): void; 5043e41f4b71Sopenharmony_ci 5044e41f4b71Sopenharmony_ciCalled to return the specified status. Its parameters are passed in by [connectDfs](#fsconnectdfs12). 5045e41f4b71Sopenharmony_ci 5046e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5047e41f4b71Sopenharmony_ci 5048e41f4b71Sopenharmony_ci**Parameters** 5049e41f4b71Sopenharmony_ci 5050e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5051e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 5052e41f4b71Sopenharmony_ci| networkId | string | Yes | Network ID of the device. | 5053e41f4b71Sopenharmony_ci | status | number | Yes | Status code of the distributed file system. The status code is the error code returned by **onStatus** invoked by **connectDfs**. If the device is abnormal when **connectDfs()** is called, **onStatus** will be called to return the error code:<br>- [13900046](errorcode-filemanagement.md#13900046): disconnection caused by software. 5054e41f4b71Sopenharmony_ci 5055e41f4b71Sopenharmony_ci## RandomAccessFile 5056e41f4b71Sopenharmony_ci 5057e41f4b71Sopenharmony_ciProvides APIs for randomly reading and writing a stream. Before invoking any API of **RandomAccessFile**, you need to use **createRandomAccess()** to create a **RandomAccessFile** instance synchronously or asynchronously. 5058e41f4b71Sopenharmony_ci 5059e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5060e41f4b71Sopenharmony_ci 5061e41f4b71Sopenharmony_ci### Properties 5062e41f4b71Sopenharmony_ci 5063e41f4b71Sopenharmony_ci| Name | Type | Read-Only | Writable | Description | 5064e41f4b71Sopenharmony_ci| ----------- | ------ | ---- | ----- | ---------------- | 5065e41f4b71Sopenharmony_ci| fd | number | Yes | No | FD of the file.| 5066e41f4b71Sopenharmony_ci| filePointer | number | Yes | Yes | Offset pointer to the **RandomAccessFile** instance.| 5067e41f4b71Sopenharmony_ci 5068e41f4b71Sopenharmony_ci### setFilePointer<sup>10+</sup> 5069e41f4b71Sopenharmony_ci 5070e41f4b71Sopenharmony_cisetFilePointer(): void 5071e41f4b71Sopenharmony_ci 5072e41f4b71Sopenharmony_ciSets the file offset pointer. 5073e41f4b71Sopenharmony_ci 5074e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5075e41f4b71Sopenharmony_ci 5076e41f4b71Sopenharmony_ci**Error codes** 5077e41f4b71Sopenharmony_ci 5078e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5079e41f4b71Sopenharmony_ci 5080e41f4b71Sopenharmony_ci**Example** 5081e41f4b71Sopenharmony_ci 5082e41f4b71Sopenharmony_ci ```ts 5083e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5084e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5085e41f4b71Sopenharmony_ci randomAccessFile.setFilePointer(1); 5086e41f4b71Sopenharmony_ci randomAccessFile.close(); 5087e41f4b71Sopenharmony_ci ``` 5088e41f4b71Sopenharmony_ci 5089e41f4b71Sopenharmony_ci 5090e41f4b71Sopenharmony_ci### close<sup>10+</sup> 5091e41f4b71Sopenharmony_ci 5092e41f4b71Sopenharmony_ciclose(): void 5093e41f4b71Sopenharmony_ci 5094e41f4b71Sopenharmony_ciCloses this **RandomAccessFile** instance. This API returns the result synchronously. 5095e41f4b71Sopenharmony_ci 5096e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5097e41f4b71Sopenharmony_ci 5098e41f4b71Sopenharmony_ci**Error codes** 5099e41f4b71Sopenharmony_ci 5100e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5101e41f4b71Sopenharmony_ci 5102e41f4b71Sopenharmony_ci**Example** 5103e41f4b71Sopenharmony_ci 5104e41f4b71Sopenharmony_ci ```ts 5105e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5106e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 5107e41f4b71Sopenharmony_ci randomAccessFile.close(); 5108e41f4b71Sopenharmony_ci ``` 5109e41f4b71Sopenharmony_ci 5110e41f4b71Sopenharmony_ci### write<sup>10+</sup> 5111e41f4b71Sopenharmony_ci 5112e41f4b71Sopenharmony_ciwrite(buffer: ArrayBuffer | string, options?: WriteOptions): Promise<number> 5113e41f4b71Sopenharmony_ci 5114e41f4b71Sopenharmony_ciWrites data to a file. This API uses a promise to return the result. 5115e41f4b71Sopenharmony_ci 5116e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5117e41f4b71Sopenharmony_ci 5118e41f4b71Sopenharmony_ci**Parameters** 5119e41f4b71Sopenharmony_ci 5120e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5121e41f4b71Sopenharmony_ci| ------- | ------------------------------- | ---- | ---------------------------------------- | 5122e41f4b71Sopenharmony_ci| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | 5123e41f4b71Sopenharmony_ci| options | [WriteOptions](#writeoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to write. The default value is the buffer length.<br>- **offset** (number): start position to write the data (it is determined by **filePointer** plus **offset**). This parameter is optional. By default, data is written from the **filePointer**.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 5124e41f4b71Sopenharmony_ci 5125e41f4b71Sopenharmony_ci**Return value** 5126e41f4b71Sopenharmony_ci 5127e41f4b71Sopenharmony_ci| Type | Description | 5128e41f4b71Sopenharmony_ci| --------------------- | -------- | 5129e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the length of the data written.| 5130e41f4b71Sopenharmony_ci 5131e41f4b71Sopenharmony_ci**Error codes** 5132e41f4b71Sopenharmony_ci 5133e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5134e41f4b71Sopenharmony_ci 5135e41f4b71Sopenharmony_ci**Example** 5136e41f4b71Sopenharmony_ci 5137e41f4b71Sopenharmony_ci ```ts 5138e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 5139e41f4b71Sopenharmony_ci import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit'; 5140e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5141e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fsfileIo.OpenMode.READ_WRITE); 5142e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(file); 5143e41f4b71Sopenharmony_ci let bufferLength: number = 4096; 5144e41f4b71Sopenharmony_ci let writeOption: WriteOptions = { 5145e41f4b71Sopenharmony_ci offset: 1, 5146e41f4b71Sopenharmony_ci length: 5, 5147e41f4b71Sopenharmony_ci encoding: 'utf-8' 5148e41f4b71Sopenharmony_ci }; 5149e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(bufferLength); 5150e41f4b71Sopenharmony_ci randomAccessFile.write(arrayBuffer, writeOption).then((bytesWritten: number) => { 5151e41f4b71Sopenharmony_ci console.info("randomAccessFile bytesWritten: " + bytesWritten); 5152e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5153e41f4b71Sopenharmony_ci console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code); 5154e41f4b71Sopenharmony_ci }).finally(() => { 5155e41f4b71Sopenharmony_ci randomAccessFile.close(); 5156e41f4b71Sopenharmony_ci fs.closeSync(file); 5157e41f4b71Sopenharmony_ci }); 5158e41f4b71Sopenharmony_ci 5159e41f4b71Sopenharmony_ci ``` 5160e41f4b71Sopenharmony_ci 5161e41f4b71Sopenharmony_ci### write<sup>10+</sup> 5162e41f4b71Sopenharmony_ci 5163e41f4b71Sopenharmony_ciwrite(buffer: ArrayBuffer | string, options?: WriteOptions, callback: AsyncCallback<number>): void 5164e41f4b71Sopenharmony_ci 5165e41f4b71Sopenharmony_ciWrites data to a file. This API uses an asynchronous callback to return the result. 5166e41f4b71Sopenharmony_ci 5167e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5168e41f4b71Sopenharmony_ci 5169e41f4b71Sopenharmony_ci**Parameters** 5170e41f4b71Sopenharmony_ci 5171e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5172e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 5173e41f4b71Sopenharmony_ci| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | 5174e41f4b71Sopenharmony_ci| options | [WriteOptions](#writeoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to write the data (it is determined by **filePointer** plus **offset**). This parameter is optional. By default, data is written from the **filePointer**.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 5175e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the result. | 5176e41f4b71Sopenharmony_ci 5177e41f4b71Sopenharmony_ci**Error codes** 5178e41f4b71Sopenharmony_ci 5179e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5180e41f4b71Sopenharmony_ci 5181e41f4b71Sopenharmony_ci**Example** 5182e41f4b71Sopenharmony_ci 5183e41f4b71Sopenharmony_ci ```ts 5184e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 5185e41f4b71Sopenharmony_ci import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit'; 5186e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5187e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 5188e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(file); 5189e41f4b71Sopenharmony_ci let bufferLength: number = 4096; 5190e41f4b71Sopenharmony_ci let writeOption: WriteOptions = { 5191e41f4b71Sopenharmony_ci offset: 1, 5192e41f4b71Sopenharmony_ci length: bufferLength, 5193e41f4b71Sopenharmony_ci encoding: 'utf-8' 5194e41f4b71Sopenharmony_ci }; 5195e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(bufferLength); 5196e41f4b71Sopenharmony_ci randomAccessFile.write(arrayBuffer, writeOption, (err: BusinessError, bytesWritten: number) => { 5197e41f4b71Sopenharmony_ci if (err) { 5198e41f4b71Sopenharmony_ci console.error("write failed with error message: " + err.message + ", error code: " + err.code); 5199e41f4b71Sopenharmony_ci } else { 5200e41f4b71Sopenharmony_ci if (bytesWritten) { 5201e41f4b71Sopenharmony_ci console.info("write succeed and size is:" + bytesWritten); 5202e41f4b71Sopenharmony_ci } 5203e41f4b71Sopenharmony_ci } 5204e41f4b71Sopenharmony_ci randomAccessFile.close(); 5205e41f4b71Sopenharmony_ci fs.closeSync(file); 5206e41f4b71Sopenharmony_ci }); 5207e41f4b71Sopenharmony_ci ``` 5208e41f4b71Sopenharmony_ci 5209e41f4b71Sopenharmony_ci### writeSync<sup>10+</sup> 5210e41f4b71Sopenharmony_ci 5211e41f4b71Sopenharmony_ciwriteSync(buffer: ArrayBuffer | string, options?: WriteOptions): number 5212e41f4b71Sopenharmony_ci 5213e41f4b71Sopenharmony_ciWrites data to a file. This API returns the result synchronously. 5214e41f4b71Sopenharmony_ci 5215e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5216e41f4b71Sopenharmony_ci 5217e41f4b71Sopenharmony_ci**Parameters** 5218e41f4b71Sopenharmony_ci 5219e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5220e41f4b71Sopenharmony_ci| ------- | ------------------------------- | ---- | ---------------------------------------- | 5221e41f4b71Sopenharmony_ci| buffer | ArrayBuffer \| string | Yes | Data to write. It can be a string or data from a buffer. | 5222e41f4b71Sopenharmony_ci| options | [WriteOptions](#writeoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to write the data (it is determined by **filePointer** plus **offset**). This parameter is optional. By default, data is written from the **filePointer**.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 5223e41f4b71Sopenharmony_ci 5224e41f4b71Sopenharmony_ci**Return value** 5225e41f4b71Sopenharmony_ci 5226e41f4b71Sopenharmony_ci| Type | Description | 5227e41f4b71Sopenharmony_ci| ------ | -------- | 5228e41f4b71Sopenharmony_ci| number | Length of the data written in the file.| 5229e41f4b71Sopenharmony_ci 5230e41f4b71Sopenharmony_ci**Error codes** 5231e41f4b71Sopenharmony_ci 5232e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5233e41f4b71Sopenharmony_ci 5234e41f4b71Sopenharmony_ci**Example** 5235e41f4b71Sopenharmony_ci 5236e41f4b71Sopenharmony_ci ```ts 5237e41f4b71Sopenharmony_ci import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit'; 5238e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5239e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 5240e41f4b71Sopenharmony_ci let writeOption: WriteOptions = { 5241e41f4b71Sopenharmony_ci offset: 5, 5242e41f4b71Sopenharmony_ci length: 5, 5243e41f4b71Sopenharmony_ci encoding: 'utf-8' 5244e41f4b71Sopenharmony_ci }; 5245e41f4b71Sopenharmony_ci let bytesWritten = randomAccessFile.writeSync("hello, world", writeOption); 5246e41f4b71Sopenharmony_ci randomAccessFile.close(); 5247e41f4b71Sopenharmony_ci ``` 5248e41f4b71Sopenharmony_ci 5249e41f4b71Sopenharmony_ci### read<sup>10+</sup> 5250e41f4b71Sopenharmony_ci 5251e41f4b71Sopenharmony_ciread(buffer: ArrayBuffer, options?: ReadOptions): Promise<number> 5252e41f4b71Sopenharmony_ci 5253e41f4b71Sopenharmony_ciReads data from a file. This API uses a promise to return the result. 5254e41f4b71Sopenharmony_ci 5255e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5256e41f4b71Sopenharmony_ci 5257e41f4b71Sopenharmony_ci**Parameters** 5258e41f4b71Sopenharmony_ci 5259e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5260e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ---------------------------------------- | 5261e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 5262e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data (it is determined by **filePointer** plus **offset**). This parameter is optional. By default, data is read from the **filePointer**.| 5263e41f4b71Sopenharmony_ci 5264e41f4b71Sopenharmony_ci**Return value** 5265e41f4b71Sopenharmony_ci 5266e41f4b71Sopenharmony_ci| Type | Description | 5267e41f4b71Sopenharmony_ci| ---------------------------------- | ------ | 5268e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the data read.| 5269e41f4b71Sopenharmony_ci 5270e41f4b71Sopenharmony_ci**Error codes** 5271e41f4b71Sopenharmony_ci 5272e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5273e41f4b71Sopenharmony_ci 5274e41f4b71Sopenharmony_ci**Example** 5275e41f4b71Sopenharmony_ci 5276e41f4b71Sopenharmony_ci ```ts 5277e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 5278e41f4b71Sopenharmony_ci import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit'; 5279e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5280e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 5281e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(file); 5282e41f4b71Sopenharmony_ci let bufferLength: number = 4096; 5283e41f4b71Sopenharmony_ci let readOption: ReadOptions = { 5284e41f4b71Sopenharmony_ci offset: 1, 5285e41f4b71Sopenharmony_ci length: 5 5286e41f4b71Sopenharmony_ci }; 5287e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(bufferLength); 5288e41f4b71Sopenharmony_ci randomAccessFile.read(arrayBuffer, readOption).then((readLength: number) => { 5289e41f4b71Sopenharmony_ci console.info("randomAccessFile readLength: " + readLength); 5290e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5291e41f4b71Sopenharmony_ci console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code); 5292e41f4b71Sopenharmony_ci }).finally(() => { 5293e41f4b71Sopenharmony_ci randomAccessFile.close(); 5294e41f4b71Sopenharmony_ci fs.closeSync(file); 5295e41f4b71Sopenharmony_ci }); 5296e41f4b71Sopenharmony_ci ``` 5297e41f4b71Sopenharmony_ci 5298e41f4b71Sopenharmony_ci### read<sup>10+</sup> 5299e41f4b71Sopenharmony_ci 5300e41f4b71Sopenharmony_ciread(buffer: ArrayBuffer, options?: ReadOptions, callback: AsyncCallback<number>): void 5301e41f4b71Sopenharmony_ci 5302e41f4b71Sopenharmony_ciReads data from a file. This API uses an asynchronous callback to return the result. 5303e41f4b71Sopenharmony_ci 5304e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5305e41f4b71Sopenharmony_ci 5306e41f4b71Sopenharmony_ci**Parameters** 5307e41f4b71Sopenharmony_ci 5308e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5309e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 5310e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 5311e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data (it is determined by **filePointer** plus **offset**). This parameter is optional. By default, data is read from the **filePointer**.| 5312e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the result. | 5313e41f4b71Sopenharmony_ci 5314e41f4b71Sopenharmony_ci**Error codes** 5315e41f4b71Sopenharmony_ci 5316e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5317e41f4b71Sopenharmony_ci 5318e41f4b71Sopenharmony_ci**Example** 5319e41f4b71Sopenharmony_ci 5320e41f4b71Sopenharmony_ci ```ts 5321e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 5322e41f4b71Sopenharmony_ci import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit'; 5323e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5324e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 5325e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(file); 5326e41f4b71Sopenharmony_ci let length: number = 20; 5327e41f4b71Sopenharmony_ci let readOption: ReadOptions = { 5328e41f4b71Sopenharmony_ci offset: 1, 5329e41f4b71Sopenharmony_ci length: 5 5330e41f4b71Sopenharmony_ci }; 5331e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(length); 5332e41f4b71Sopenharmony_ci randomAccessFile.read(arrayBuffer, readOption, (err: BusinessError, readLength: number) => { 5333e41f4b71Sopenharmony_ci if (err) { 5334e41f4b71Sopenharmony_ci console.error("read failed with error message: " + err.message + ", error code: " + err.code); 5335e41f4b71Sopenharmony_ci } else { 5336e41f4b71Sopenharmony_ci if (readLength) { 5337e41f4b71Sopenharmony_ci console.info("read succeed and size is:" + readLength); 5338e41f4b71Sopenharmony_ci } 5339e41f4b71Sopenharmony_ci } 5340e41f4b71Sopenharmony_ci randomAccessFile.close(); 5341e41f4b71Sopenharmony_ci fs.closeSync(file); 5342e41f4b71Sopenharmony_ci }); 5343e41f4b71Sopenharmony_ci ``` 5344e41f4b71Sopenharmony_ci 5345e41f4b71Sopenharmony_ci### readSync<sup>10+</sup> 5346e41f4b71Sopenharmony_ci 5347e41f4b71Sopenharmony_cireadSync(buffer: ArrayBuffer, options?: ReadOptions): number 5348e41f4b71Sopenharmony_ci 5349e41f4b71Sopenharmony_ciReads data from a file. This API returns the result synchronously. 5350e41f4b71Sopenharmony_ci 5351e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5352e41f4b71Sopenharmony_ci 5353e41f4b71Sopenharmony_ci**Parameters** 5354e41f4b71Sopenharmony_ci 5355e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5356e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ---------------------------------------- | 5357e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 5358e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11) | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data (it is determined by **filePointer** plus **offset**). This parameter is optional. By default, data is read from the **filePointer**.<br> | 5359e41f4b71Sopenharmony_ci 5360e41f4b71Sopenharmony_ci**Return value** 5361e41f4b71Sopenharmony_ci 5362e41f4b71Sopenharmony_ci| Type | Description | 5363e41f4b71Sopenharmony_ci| ------ | -------- | 5364e41f4b71Sopenharmony_ci| number | Length of the data read.| 5365e41f4b71Sopenharmony_ci 5366e41f4b71Sopenharmony_ci**Error codes** 5367e41f4b71Sopenharmony_ci 5368e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5369e41f4b71Sopenharmony_ci 5370e41f4b71Sopenharmony_ci**Example** 5371e41f4b71Sopenharmony_ci 5372e41f4b71Sopenharmony_ci ```ts 5373e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5374e41f4b71Sopenharmony_ci let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 5375e41f4b71Sopenharmony_ci let randomAccessFile = fs.createRandomAccessFileSync(file); 5376e41f4b71Sopenharmony_ci let length: number = 4096; 5377e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(length); 5378e41f4b71Sopenharmony_ci let readLength = randomAccessFile.readSync(arrayBuffer); 5379e41f4b71Sopenharmony_ci randomAccessFile.close(); 5380e41f4b71Sopenharmony_ci fs.closeSync(file); 5381e41f4b71Sopenharmony_ci ``` 5382e41f4b71Sopenharmony_ci 5383e41f4b71Sopenharmony_ci### getReadStream<sup>12+</sup> 5384e41f4b71Sopenharmony_ci 5385e41f4b71Sopenharmony_cigetReadStream(): ReadStream; 5386e41f4b71Sopenharmony_ci 5387e41f4b71Sopenharmony_ciObtains a **ReadStream** instance of this **RandomAccessFile**. 5388e41f4b71Sopenharmony_ci 5389e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5390e41f4b71Sopenharmony_ci 5391e41f4b71Sopenharmony_ci**Return value** 5392e41f4b71Sopenharmony_ci 5393e41f4b71Sopenharmony_ci| Type | Description | 5394e41f4b71Sopenharmony_ci| ------------------ | --------- | 5395e41f4b71Sopenharmony_ci| [ReadStream](#readstream12) | **ReadStream** instance obtained.| 5396e41f4b71Sopenharmony_ci 5397e41f4b71Sopenharmony_ci**Example** 5398e41f4b71Sopenharmony_ci 5399e41f4b71Sopenharmony_ci ```ts 5400e41f4b71Sopenharmony_ci const filePath = pathDir + "/test.txt"; 5401e41f4b71Sopenharmony_ci const randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 5402e41f4b71Sopenharmony_ci const rs = randomAccessFile.getReadStream(); 5403e41f4b71Sopenharmony_ci rs.close(); 5404e41f4b71Sopenharmony_ci randomAccessFile.close(); 5405e41f4b71Sopenharmony_ci ``` 5406e41f4b71Sopenharmony_ci 5407e41f4b71Sopenharmony_ci### getWriteStream<sup>12+</sup> 5408e41f4b71Sopenharmony_ci 5409e41f4b71Sopenharmony_cigetWriteStream(): WriteStream; 5410e41f4b71Sopenharmony_ci 5411e41f4b71Sopenharmony_ciObtains a **WriteStream** instance of this **RandomAccessFile**. 5412e41f4b71Sopenharmony_ci 5413e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5414e41f4b71Sopenharmony_ci 5415e41f4b71Sopenharmony_ci**Return value** 5416e41f4b71Sopenharmony_ci 5417e41f4b71Sopenharmony_ci| Type | Description | 5418e41f4b71Sopenharmony_ci| ------------------ | --------- | 5419e41f4b71Sopenharmony_ci| [WriteStream](#writestream12) | **WriteStream** instance obtained.| 5420e41f4b71Sopenharmony_ci 5421e41f4b71Sopenharmony_ci**Example** 5422e41f4b71Sopenharmony_ci 5423e41f4b71Sopenharmony_ci ```ts 5424e41f4b71Sopenharmony_ci const filePath = pathDir + "/test.txt"; 5425e41f4b71Sopenharmony_ci const randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 5426e41f4b71Sopenharmony_ci const ws = randomAccessFile.getWriteStream(); 5427e41f4b71Sopenharmony_ci ws.close(); 5428e41f4b71Sopenharmony_ci randomAccessFile.close(); 5429e41f4b71Sopenharmony_ci ``` 5430e41f4b71Sopenharmony_ci 5431e41f4b71Sopenharmony_ci 5432e41f4b71Sopenharmony_ci## Watcher<sup>10+</sup> 5433e41f4b71Sopenharmony_ci 5434e41f4b71Sopenharmony_ciProvides APIs for observing the changes of files or folders. Before using the APIs of **Watcher** , call **createWatcher()** to create a **Watcher** object. 5435e41f4b71Sopenharmony_ci 5436e41f4b71Sopenharmony_ci### start<sup>10+</sup> 5437e41f4b71Sopenharmony_ci 5438e41f4b71Sopenharmony_cistart(): void 5439e41f4b71Sopenharmony_ci 5440e41f4b71Sopenharmony_ciStarts listening. 5441e41f4b71Sopenharmony_ci 5442e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5443e41f4b71Sopenharmony_ci 5444e41f4b71Sopenharmony_ci**Error codes** 5445e41f4b71Sopenharmony_ci 5446e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5447e41f4b71Sopenharmony_ci 5448e41f4b71Sopenharmony_ci**Example** 5449e41f4b71Sopenharmony_ci 5450e41f4b71Sopenharmony_ci ```ts 5451e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5452e41f4b71Sopenharmony_ci let watcher = fs.createWatcher(filePath, 0xfff, () => {}); 5453e41f4b71Sopenharmony_ci watcher.start(); 5454e41f4b71Sopenharmony_ci watcher.stop(); 5455e41f4b71Sopenharmony_ci ``` 5456e41f4b71Sopenharmony_ci 5457e41f4b71Sopenharmony_ci### stop<sup>10+</sup> 5458e41f4b71Sopenharmony_ci 5459e41f4b71Sopenharmony_cistop(): void 5460e41f4b71Sopenharmony_ci 5461e41f4b71Sopenharmony_ciStops listening. 5462e41f4b71Sopenharmony_ci 5463e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5464e41f4b71Sopenharmony_ci 5465e41f4b71Sopenharmony_ci**Error codes** 5466e41f4b71Sopenharmony_ci 5467e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5468e41f4b71Sopenharmony_ci 5469e41f4b71Sopenharmony_ci**Example** 5470e41f4b71Sopenharmony_ci 5471e41f4b71Sopenharmony_ci ```ts 5472e41f4b71Sopenharmony_ci let filePath = pathDir + "/test.txt"; 5473e41f4b71Sopenharmony_ci let watcher = fs.createWatcher(filePath, 0xfff, () => {}); 5474e41f4b71Sopenharmony_ci watcher.start(); 5475e41f4b71Sopenharmony_ci watcher.stop(); 5476e41f4b71Sopenharmony_ci ``` 5477e41f4b71Sopenharmony_ci 5478e41f4b71Sopenharmony_ci## OpenMode 5479e41f4b71Sopenharmony_ci 5480e41f4b71Sopenharmony_ciDefines the constants of the **mode** parameter used in **open()**. It specifies the mode for opening a file. 5481e41f4b71Sopenharmony_ci 5482e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5483e41f4b71Sopenharmony_ci 5484e41f4b71Sopenharmony_ci| Name | Type | Value | Description | 5485e41f4b71Sopenharmony_ci| ---- | ------ |---- | ------- | 5486e41f4b71Sopenharmony_ci| READ_ONLY | number | 0o0 | Open the file in read-only mode.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 5487e41f4b71Sopenharmony_ci| WRITE_ONLY | number | 0o1 | Open the file in write-only mode.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 5488e41f4b71Sopenharmony_ci| READ_WRITE | number | 0o2 | Open the file in read/write mode.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 5489e41f4b71Sopenharmony_ci| CREATE | number | 0o100 | Create a file if the specified file does not exist.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 5490e41f4b71Sopenharmony_ci| TRUNC | number | 0o1000 | If the file exists and is opened in write-only or read/write mode, truncate the file length to 0.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 5491e41f4b71Sopenharmony_ci| APPEND | number | 0o2000 | Open the file in append mode. New data will be written to the end of the file.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 5492e41f4b71Sopenharmony_ci| NONBLOCK | number | 0o4000 | If **path** points to a named pipe (FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.| 5493e41f4b71Sopenharmony_ci| DIR | number | 0o200000 | If **path** does not point to a directory, throw an exception.| 5494e41f4b71Sopenharmony_ci| NOFOLLOW | number | 0o400000 | If **path** points to a symbolic link, throw an exception.| 5495e41f4b71Sopenharmony_ci| SYNC | number | 0o4010000 | Open the file in synchronous I/O mode.| 5496e41f4b71Sopenharmony_ci 5497e41f4b71Sopenharmony_ci## Filter<sup>10+</sup> 5498e41f4b71Sopenharmony_ci 5499e41f4b71Sopenharmony_ciDefines the file filtering configuration used by **listFile()**. 5500e41f4b71Sopenharmony_ci 5501e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 5502e41f4b71Sopenharmony_ci 5503e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5504e41f4b71Sopenharmony_ci 5505e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5506e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ | 5507e41f4b71Sopenharmony_ci| suffix | Array<string> | No| Locate files that fully match the specified file name extensions, which are of the OR relationship. | 5508e41f4b71Sopenharmony_ci| displayName | Array<string> | No| Locate files that fuzzy match the specified file names, which are of the OR relationship. Currently, only the wildcard * is supported.| 5509e41f4b71Sopenharmony_ci| mimeType | Array<string> | No| Locate files that fully match the specified MIME types, which are of the OR relationship. | 5510e41f4b71Sopenharmony_ci| fileSizeOver | number | No| Locate files that are greater than or equal to the specified size. | 5511e41f4b71Sopenharmony_ci| lastModifiedAfter | number | No| Locate files whose last modification time is the same or later than the specified time. | 5512e41f4b71Sopenharmony_ci| excludeMedia | boolean | No| Whether to exclude the files already in **Media**. | 5513e41f4b71Sopenharmony_ci 5514e41f4b71Sopenharmony_ci## ConflictFiles<sup>10+</sup> 5515e41f4b71Sopenharmony_ci 5516e41f4b71Sopenharmony_ciDefines conflicting file information used in **copyDir()** or **moveDir()**. 5517e41f4b71Sopenharmony_ci 5518e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5519e41f4b71Sopenharmony_ci 5520e41f4b71Sopenharmony_ci| Name | Type | Description | 5521e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | 5522e41f4b71Sopenharmony_ci| srcFile | string | Path of the source file. | 5523e41f4b71Sopenharmony_ci| destFile | string | Path of the destination file.| 5524e41f4b71Sopenharmony_ci 5525e41f4b71Sopenharmony_ci## Options<sup>11+</sup> 5526e41f4b71Sopenharmony_ci 5527e41f4b71Sopenharmony_ciDefines the options used in **readLines()**. 5528e41f4b71Sopenharmony_ci 5529e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5530e41f4b71Sopenharmony_ci 5531e41f4b71Sopenharmony_ci| Name | Type | Description | 5532e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | 5533e41f4b71Sopenharmony_ci| encoding | string | File encoding format. It is optional. | 5534e41f4b71Sopenharmony_ci 5535e41f4b71Sopenharmony_ci## WhenceType<sup>11+</sup> 5536e41f4b71Sopenharmony_ci 5537e41f4b71Sopenharmony_ciEnumerates the types of the relative offset position used in **lseek()**. 5538e41f4b71Sopenharmony_ci 5539e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5540e41f4b71Sopenharmony_ci 5541e41f4b71Sopenharmony_ci| Name | Value | Description | 5542e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | 5543e41f4b71Sopenharmony_ci| SEEK_SET | 0 | Beginning of the file. | 5544e41f4b71Sopenharmony_ci| SEEK_CUR | 1 | Current offset position.| 5545e41f4b71Sopenharmony_ci| SEEK_END | 2 | End of the file.| 5546e41f4b71Sopenharmony_ci 5547e41f4b71Sopenharmony_ci## LocationType<sup>11+</sup> 5548e41f4b71Sopenharmony_ci 5549e41f4b71Sopenharmony_ciEnumerates the file locations. 5550e41f4b71Sopenharmony_ci 5551e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5552e41f4b71Sopenharmony_ci 5553e41f4b71Sopenharmony_ci| Name | Value | Description | 5554e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | 5555e41f4b71Sopenharmony_ci| LOCAl | 1 | The file is stored in a local device. | 5556e41f4b71Sopenharmony_ci| CLOUD | 2 | The file is stored in the cloud.| 5557e41f4b71Sopenharmony_ci 5558e41f4b71Sopenharmony_ci## AccessModeType<sup>12+</sup> 5559e41f4b71Sopenharmony_ci 5560e41f4b71Sopenharmony_ciEnumerates the access modes to verify. If this parameter is left blank, the system checks whether the file exists. 5561e41f4b71Sopenharmony_ci 5562e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 5563e41f4b71Sopenharmony_ci 5564e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5565e41f4b71Sopenharmony_ci 5566e41f4b71Sopenharmony_ci| Name | Value | Description | 5567e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | 5568e41f4b71Sopenharmony_ci| EXIST | 0 | Verify whether the file exists. | 5569e41f4b71Sopenharmony_ci| WRITE | 2 | Verify the write permission on the file.| 5570e41f4b71Sopenharmony_ci| READ | 4 | Verify the read permission on the file.| 5571e41f4b71Sopenharmony_ci| READ_WRITE | 6 | Verify the read/write permission on the file.| 5572e41f4b71Sopenharmony_ci 5573e41f4b71Sopenharmony_ci## ReadOptions<sup>11+</sup> 5574e41f4b71Sopenharmony_ci 5575e41f4b71Sopenharmony_ciDefines the options used in **read()**. 5576e41f4b71Sopenharmony_ci 5577e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 5578e41f4b71Sopenharmony_ci 5579e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5580e41f4b71Sopenharmony_ci 5581e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5582e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ |------------------ | 5583e41f4b71Sopenharmony_ci| length | number | No| Length of the data to read, in bytes. This parameter is optional. The default value is the buffer length. | 5584e41f4b71Sopenharmony_ci| offset | number | No| Start position of the file to read, which is determined by **filePointer** plus **offset**. This parameter is optional. By default, data is read from the **filePointer**.| 5585e41f4b71Sopenharmony_ci 5586e41f4b71Sopenharmony_ci## ReadTextOptions<sup>11+</sup> 5587e41f4b71Sopenharmony_ci 5588e41f4b71Sopenharmony_ciDefines the options used in **readText()**. It inherits from [ReadOptions](#readoptions11). 5589e41f4b71Sopenharmony_ci 5590e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5591e41f4b71Sopenharmony_ci 5592e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5593e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ | 5594e41f4b71Sopenharmony_ci| length | number | No| Length of the data to read, in bytes. This parameter is optional. The default value is the file length. | 5595e41f4b71Sopenharmony_ci| offset | number | No| Start position of the file to read, in bytes. This parameter is optional. By default, data is read from the current position.| 5596e41f4b71Sopenharmony_ci| encoding | string | No| Format of the data to be encoded. This parameter is valid only when the data type is string. The default value is **'utf-8'**, which is the only value supported.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 5597e41f4b71Sopenharmony_ci 5598e41f4b71Sopenharmony_ci## WriteOptions<sup>11+</sup> 5599e41f4b71Sopenharmony_ci 5600e41f4b71Sopenharmony_ciDefines the options use din **write()**. It inherits from [Options](#options11). 5601e41f4b71Sopenharmony_ci 5602e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5603e41f4b71Sopenharmony_ci 5604e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5605e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ | 5606e41f4b71Sopenharmony_ci| length | number | No| Length of the data to write, in bytes. This parameter is optional. The default value is the buffer length.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 5607e41f4b71Sopenharmony_ci| offset | number | No| Start position of the file to write, in bytes (which is determined by **filePointer** plus **offset**). This parameter is optional. By default, data is written from the **filePointer**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 5608e41f4b71Sopenharmony_ci| encoding | string | No| Format of the data to be encoded. This parameter is valid only when the data type is string. The default value is **'utf-8'**, which is the only value supported. | 5609e41f4b71Sopenharmony_ci 5610e41f4b71Sopenharmony_ci## ListFileOptions<sup>11+</sup> 5611e41f4b71Sopenharmony_ci 5612e41f4b71Sopenharmony_ciDefines the options used in **listFile()**. 5613e41f4b71Sopenharmony_ci 5614e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 5615e41f4b71Sopenharmony_ci 5616e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5617e41f4b71Sopenharmony_ci 5618e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5619e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ | 5620e41f4b71Sopenharmony_ci| recursion | boolean | No| Whether to list all files in the subfolders recursively. This parameter is optional. The default value is **false**. If **recursion** is **false**, the names of the files and folders that meet the specified conditions in the current directory are returned. If **recursion** is **true**, relative paths (starting with /) of all files that meet the specified conditions in the current directory are returned. | 5621e41f4b71Sopenharmony_ci| listNum | number | No| Number of file names to list. This parameter is optional. The default value is **0**, which means to list all files.| 5622e41f4b71Sopenharmony_ci| filter | [Filter](#filter10) | No| File filtering configuration. It specifies the file filtering conditions.| 5623e41f4b71Sopenharmony_ci 5624e41f4b71Sopenharmony_ci## ReadStream<sup>12+</sup> 5625e41f4b71Sopenharmony_ci 5626e41f4b71Sopenharmony_ciDefines a readable stream. You need to use [fs.createReadStream](#fscreatereadstream12) to create a **ReadStream** instance. 5627e41f4b71Sopenharmony_ci 5628e41f4b71Sopenharmony_ci### Properties 5629e41f4b71Sopenharmony_ci 5630e41f4b71Sopenharmony_ci| Name | Type | Read-Only | Writable | Description | 5631e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ---------------------------------------- | 5632e41f4b71Sopenharmony_ci| bytesRead | number | Yes | No | Number of bytes read by the readable stream.| 5633e41f4b71Sopenharmony_ci| path | string | Yes | No | Path of the file corresponding to the readable stream.| 5634e41f4b71Sopenharmony_ci 5635e41f4b71Sopenharmony_ci### Seek 5636e41f4b71Sopenharmony_ci 5637e41f4b71Sopenharmony_ciseek(offset: number, whence?: WhenceType): number; 5638e41f4b71Sopenharmony_ci 5639e41f4b71Sopenharmony_ci 5640e41f4b71Sopenharmony_ciAdjusts the position of the readable stream offset pointer. 5641e41f4b71Sopenharmony_ci 5642e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5643e41f4b71Sopenharmony_ci 5644e41f4b71Sopenharmony_ci**Parameters** 5645e41f4b71Sopenharmony_ci 5646e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5647e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 5648e41f4b71Sopenharmony_ci| offset | number | Yes | Number of bytes to move the offset, in bytes.| 5649e41f4b71Sopenharmony_ci| whence | [WhenceType](#whencetype11) | No | Where to start the offset. The default value is **SEEK_SET**, which indicates the beginning of the file.| 5650e41f4b71Sopenharmony_ci 5651e41f4b71Sopenharmony_ci**Return value** 5652e41f4b71Sopenharmony_ci 5653e41f4b71Sopenharmony_ci| Type | Description | 5654e41f4b71Sopenharmony_ci| --------------------- | ---------- | 5655e41f4b71Sopenharmony_ci| number | Position of the current offset as measured from the beginning of the file, in bytes).| 5656e41f4b71Sopenharmony_ci 5657e41f4b71Sopenharmony_ci**Error codes** 5658e41f4b71Sopenharmony_ci 5659e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5660e41f4b71Sopenharmony_ci 5661e41f4b71Sopenharmony_ci**Example** 5662e41f4b71Sopenharmony_ci 5663e41f4b71Sopenharmony_ci ```ts 5664e41f4b71Sopenharmony_ci const filePath = pathDir + "/test.txt"; 5665e41f4b71Sopenharmony_ci const rs = fs.createReadStream(filePath); 5666e41f4b71Sopenharmony_ci const curOff = rs.seek(5, fs.WhenceType.SEEK_SET); 5667e41f4b71Sopenharmony_ci console.info(`current offset is ${curOff}`); 5668e41f4b71Sopenharmony_ci rs.close(); 5669e41f4b71Sopenharmony_ci ``` 5670e41f4b71Sopenharmony_ci 5671e41f4b71Sopenharmony_ci### close 5672e41f4b71Sopenharmony_ci 5673e41f4b71Sopenharmony_ciclose(): void 5674e41f4b71Sopenharmony_ci 5675e41f4b71Sopenharmony_ciCloses this readable stream. 5676e41f4b71Sopenharmony_ci 5677e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5678e41f4b71Sopenharmony_ci 5679e41f4b71Sopenharmony_ci**Error codes** 5680e41f4b71Sopenharmony_ci 5681e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5682e41f4b71Sopenharmony_ci 5683e41f4b71Sopenharmony_ci**Example** 5684e41f4b71Sopenharmony_ci 5685e41f4b71Sopenharmony_ci ```ts 5686e41f4b71Sopenharmony_ci const filePath = pathDir + "/test.txt"; 5687e41f4b71Sopenharmony_ci const rs = fs.createReadStream(filePath); 5688e41f4b71Sopenharmony_ci rs.close(); 5689e41f4b71Sopenharmony_ci ``` 5690e41f4b71Sopenharmony_ci 5691e41f4b71Sopenharmony_ci## WriteStream<sup>12+</sup> 5692e41f4b71Sopenharmony_ci 5693e41f4b71Sopenharmony_ciDefines a writeable stream. You need to use [fs.createWriteStream](#fscreatewritestream12) to create a **WriteStream** instance. 5694e41f4b71Sopenharmony_ci 5695e41f4b71Sopenharmony_ci### Properties 5696e41f4b71Sopenharmony_ci 5697e41f4b71Sopenharmony_ci| Name | Type | Read-Only | Writable | Description | 5698e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ---------------------------------------- | 5699e41f4b71Sopenharmony_ci| bytesWritten | number | Yes | No | Number of bytes written to the writeable stream.| 5700e41f4b71Sopenharmony_ci| path | string | Yes | No | Path of the file corresponding to the writeable stream.| 5701e41f4b71Sopenharmony_ci 5702e41f4b71Sopenharmony_ci### Seek 5703e41f4b71Sopenharmony_ci 5704e41f4b71Sopenharmony_ciseek(offset: number, whence?: WhenceType): number; 5705e41f4b71Sopenharmony_ci 5706e41f4b71Sopenharmony_ciAdjusts the position of the writeable stream offset pointer. 5707e41f4b71Sopenharmony_ci 5708e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5709e41f4b71Sopenharmony_ci 5710e41f4b71Sopenharmony_ci**Parameters** 5711e41f4b71Sopenharmony_ci 5712e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5713e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------- | 5714e41f4b71Sopenharmony_ci| offset | number | Yes | Offset to set, in bytes.| 5715e41f4b71Sopenharmony_ci| whence | [WhenceType](#whencetype11) | No | Where to start the offset. The default value is **SEEK_SET**, which indicates the beginning of the file.| 5716e41f4b71Sopenharmony_ci 5717e41f4b71Sopenharmony_ci**Return value** 5718e41f4b71Sopenharmony_ci 5719e41f4b71Sopenharmony_ci| Type | Description | 5720e41f4b71Sopenharmony_ci| --------------------- | ---------- | 5721e41f4b71Sopenharmony_ci| number | Position of the current offset as measured from the beginning of the file.| 5722e41f4b71Sopenharmony_ci 5723e41f4b71Sopenharmony_ci**Error codes** 5724e41f4b71Sopenharmony_ci 5725e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5726e41f4b71Sopenharmony_ci 5727e41f4b71Sopenharmony_ci**Example** 5728e41f4b71Sopenharmony_ci 5729e41f4b71Sopenharmony_ci ```ts 5730e41f4b71Sopenharmony_ci const filePath = pathDir + "/test.txt"; 5731e41f4b71Sopenharmony_ci const ws = fs.createWriteStream(filePath); 5732e41f4b71Sopenharmony_ci const curOff = ws.seek(5, fs.WhenceType.SEEK_SET); 5733e41f4b71Sopenharmony_ci console.info(`current offset is ${curOff}`); 5734e41f4b71Sopenharmony_ci ws.close(); 5735e41f4b71Sopenharmony_ci ``` 5736e41f4b71Sopenharmony_ci 5737e41f4b71Sopenharmony_ci### close 5738e41f4b71Sopenharmony_ci 5739e41f4b71Sopenharmony_ciclose(): void 5740e41f4b71Sopenharmony_ci 5741e41f4b71Sopenharmony_ciCloses this writeable stream. 5742e41f4b71Sopenharmony_ci 5743e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5744e41f4b71Sopenharmony_ci 5745e41f4b71Sopenharmony_ci**Error codes** 5746e41f4b71Sopenharmony_ci 5747e41f4b71Sopenharmony_ciFor details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes). 5748e41f4b71Sopenharmony_ci 5749e41f4b71Sopenharmony_ci**Example** 5750e41f4b71Sopenharmony_ci 5751e41f4b71Sopenharmony_ci ```ts 5752e41f4b71Sopenharmony_ci const filePath = pathDir + "/test.txt"; 5753e41f4b71Sopenharmony_ci const ws = fs.createWriteStream(filePath); 5754e41f4b71Sopenharmony_ci ws.close(); 5755e41f4b71Sopenharmony_ci ``` 5756e41f4b71Sopenharmony_ci 5757e41f4b71Sopenharmony_ci## RandomAccessFileOptions<sup>12+</sup> 5758e41f4b71Sopenharmony_ci 5759e41f4b71Sopenharmony_ciDefines the options used in **createRandomAccessFile()**. 5760e41f4b71Sopenharmony_ci 5761e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5762e41f4b71Sopenharmony_ci 5763e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5764e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ | 5765e41f4b71Sopenharmony_ci| start | number | No| Start position to read the data, in bytes. This parameter is optional. By default, data is read from the current position. | 5766e41f4b71Sopenharmony_ci| end | number | No| End position to read the data, in bytes. This parameter is optional. The default value is the end of the file.| 5767e41f4b71Sopenharmony_ci 5768e41f4b71Sopenharmony_ci## ReadStreamOptions<sup>12+</sup> 5769e41f4b71Sopenharmony_ci 5770e41f4b71Sopenharmony_ciDefines the options used in **createReadStream()**. 5771e41f4b71Sopenharmony_ci 5772e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5773e41f4b71Sopenharmony_ci 5774e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5775e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ | 5776e41f4b71Sopenharmony_ci| start | number | No| Start position to read the data, in bytes. This parameter is optional. By default, data is read from the current position. | 5777e41f4b71Sopenharmony_ci| end | number | No| End position to read the data, in bytes. This parameter is optional. The default value is the end of the file.| 5778e41f4b71Sopenharmony_ci 5779e41f4b71Sopenharmony_ci## WriteStreamOptions<sup>12+</sup> 5780e41f4b71Sopenharmony_ci 5781e41f4b71Sopenharmony_ciDefines the options used in **createWriteStream()**. 5782e41f4b71Sopenharmony_ci 5783e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.File.FileIO 5784e41f4b71Sopenharmony_ci 5785e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 5786e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ | 5787e41f4b71Sopenharmony_ci| start | number | No| Start position to write the data, in bytes. This parameter is optional. By default, data is written from the beginning of the file. | 5788e41f4b71Sopenharmony_ci| mode | number | No| [Option](#openmode) for creating the writeable stream. You must specify one of the following options.<br>- **OpenMode.READ_ONLY(0o0)**: read-only, which is the default value.<br>- **OpenMode.WRITE_ONLY(0o1)**: write-only.<br>- **OpenMode.READ_WRITE(0o2)**: read/write.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is opened in write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the opened file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception. The write permission is not allowed.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.| 5789