1# @ohos.fileio (File Management) 2 3The **fileio** module provides APIs for file storage and management, including basic file management, directory management, file information statistics, and stream read and write. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - The APIs provided by this module are deprecated since API version 9. You are advised to use [@ohos.file.fs](js-apis-file-fs.md). 9 10## Modules to Import 11 12```js 13import fileio from '@ohos.fileio'; 14``` 15 16 17## Guidelines 18 19Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows: 20 21**Stage Model** 22 23 ```js 24import Ability from '@ohos.application.Ability'; 25class MainAbility extends Ability { 26 onWindowStageCreate(windowStage) { 27 let context = this.context; 28 let pathDir = context.filesDir; 29 } 30} 31 ``` 32 33 For details about how to obtain the stage model context, see [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md). 34 35**FA Model** 36 37 ```js 38 import featureAbility from '@ohos.ability.featureAbility'; 39 40 let context = featureAbility.getContext(); 41 context.getFilesDir().then((data) => { 42 let pathDir = data; 43 }) 44 ``` 45 46 For details about how to obtain the FA model context, see [Contex](js-apis-inner-app-context.md#context). 47 48## fileio.stat 49 50stat(path: string): Promise<Stat> 51 52Obtains file information. This API uses a promise to return the result. 53 54**System capability**: SystemCapability.FileManagement.File.FileIO 55 56**Parameters** 57 58| Name| Type | Mandatory| Description | 59| ------ | ------ | ---- | -------------------------- | 60| path | string | Yes | Application sandbox path of the file.| 61 62**Return value** 63 64 | Type | Description | 65 | ---------------------------- | ---------- | 66 | Promise<[Stat](#stat)> | Promise used to return the file information obtained.| 67 68**Example** 69 70 ```js 71 let filePath = pathDir + "test.txt"; 72 fileio.stat(filePath).then(function (stat) { 73 console.info("getFileInfo succeed, the size of file is " + stat.size); 74 }).catch(function (err) { 75 console.info("getFileInfo failed with error:" + err); 76 }); 77 ``` 78 79 80## fileio.stat 81 82stat(path: string, callback: AsyncCallback<Stat>): void 83 84Obtains file information. This API uses an asynchronous callback to return the result. 85 86**System capability**: SystemCapability.FileManagement.File.FileIO 87 88**Parameters** 89 90| Name | Type | Mandatory| Description | 91| -------- | ---------------------------------- | ---- | ------------------------------ | 92| path | string | Yes | Application sandbox path of the file. | 93| callback | AsyncCallback<[Stat](#stat)> | Yes | Callback invoked to return the file information obtained.| 94 95**Example** 96 97 ```js 98 fileio.stat(pathDir, function (err, stat) { 99 // Example code in Stat 100 }); 101 ``` 102 103 104## fileio.statSync 105 106statSync(path: string): Stat 107 108Synchronously obtains file information. 109 110**System capability**: SystemCapability.FileManagement.File.FileIO 111 112**Parameters** 113 114| Name| Type | Mandatory| Description | 115| ------ | ------ | ---- | -------------------------- | 116| path | string | Yes | Application sandbox path of the file.| 117 118 119**Return value** 120 121 | Type | Description | 122 | ------------- | ---------- | 123 | [Stat](#stat) | File information obtained.| 124 125**Example** 126 127 ```js 128 let stat = fileio.statSync(pathDir); 129 // Example code in Stat 130 ``` 131 132 133## fileio.opendir 134 135opendir(path: string): Promise<Dir> 136 137Opens a file directory. This API uses a promise to return the result. 138 139**System capability**: SystemCapability.FileManagement.File.FileIO 140 141**Parameters** 142 143| Name| Type | Mandatory| Description | 144| ------ | ------ | ---- | ------------------------------ | 145| path | string | Yes | Application sandbox path of the directory to open.| 146 147**Return value** 148 149 | Type | Description | 150 | -------------------------- | -------- | 151 | Promise<[Dir](#dir)> | Promise used to return the **Dir** object.| 152 153**Example** 154 155 ```js 156 let dirPath = pathDir + "/testDir"; 157 fileio.opendir(dirPath).then(function (dir) { 158 console.info("opendir succeed"); 159 }).catch(function (err) { 160 console.info("opendir failed with error:" + err); 161 }); 162 ``` 163 164 165## fileio.opendir 166 167opendir(path: string, callback: AsyncCallback<Dir>): void 168 169Opens a file directory. This API uses an asynchronous callback to return the result. 170 171**System capability**: SystemCapability.FileManagement.File.FileIO 172 173**Parameters** 174 175| Name | Type | Mandatory| Description | 176| -------- | -------------------------------- | ---- | ------------------------------ | 177| path | string | Yes | Application sandbox path of the directory to open.| 178| callback | AsyncCallback<[Dir](#dir)> | Yes | Callback invoked when the directory is open asynchronously. | 179 180**Example** 181 182 ```js 183 fileio.opendir(pathDir, function (err, dir) { 184 // Example code in Dir struct 185 // Use read/readSync/close. 186 }); 187 ``` 188 189 190## fileio.opendirSync 191 192opendirSync(path: string): Dir 193 194Synchronously opens a directory. 195 196**System capability**: SystemCapability.FileManagement.File.FileIO 197 198 199**Parameters** 200 201| Name| Type | Mandatory| Description | 202| ------ | ------ | ---- | ------------------------------ | 203| path | string | Yes | Application sandbox path of the directory to open.| 204 205**Return value** 206 207 | Type | Description | 208 | ----------- | -------- | 209 | [Dir](#dir) | A **Dir** instance corresponding to the directory.| 210 211**Example** 212 213 ```js 214 let dir = fileio.opendirSync(pathDir); 215 // Example code in Dir struct 216 // Use read/readSync/close. 217 ``` 218 219 220## fileio.access 221 222access(path: string, mode?: number): Promise<void> 223 224Checks whether the current process can access a file. This API uses a promise to return the result. 225 226**System capability**: SystemCapability.FileManagement.File.FileIO 227 228**Parameters** 229 230| Name| Type | Mandatory| Description | 231| ------ | ------ | ---- | ------------------------------------------------------------ | 232| path | string | Yes | Application sandbox path of the file. | 233| mode | number | No | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (|). The default value is **0**.<br>The options are as follows:<br>- **0**: Check whether the file exists.<br>- **1**: Check whether the process has the execute permission on the file.<br>- **2**: Check whether the process has the write permission on the file.<br>- **4**: Check whether the process has the read permission on the file. | 234 235**Return value** 236 237 | Type | Description | 238 | ------------------- | ---------------------------- | 239 | Promise<void> | Promise that returns no value.| 240 241**Example** 242 243 ```js 244 let filePath = pathDir + "/test.txt"; 245 fileio.access(filePath).then(function () { 246 console.info("Access successful"); 247 }).catch(function (err) { 248 console.info("access failed with error:" + err); 249 }); 250 ``` 251 252 253## fileio.access 254 255access(path: string, mode?: number, callback: AsyncCallback<void>): void 256 257Checks whether the current process can access a file. This API uses an asynchronous callback to return the result. 258 259**System capability**: SystemCapability.FileManagement.File.FileIO 260 261**Parameters** 262 263| Name | Type | Mandatory| Description | 264| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 265| path | string | Yes | Application sandbox path of the file. | 266| mode | number | No | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (|). The default value is **0**.<br>The options are as follows:<br>- **0**: Check whether the file exists.<br>- **1**: Check whether the process has the execute permission on the file.<br>- **2**: Check whether the process has the write permission on the file.<br>- **4**: Check whether the process has the read permission on the file. | 267| callback | AsyncCallback<void> | Yes | Callback invoked when the file is asynchronously checked. | 268 269**Example** 270 271 ```js 272 let filePath = pathDir + "/test.txt"; 273 fileio.access(filePath, function (err) { 274 // Do something. 275 }); 276 ``` 277 278 279## fileio.accessSync 280 281accessSync(path: string, mode?: number): void 282 283Synchronously checks whether the current process can access the specified file. 284 285**System capability**: SystemCapability.FileManagement.File.FileIO 286 287**Parameters** 288 289| Name| Type | Mandatory| Description | 290| ------ | ------ | ---- | ------------------------------------------------------------ | 291| path | string | Yes | Application sandbox path of the file. | 292| mode | number | No | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (|). The default value is **0**.<br>The options are as follows:<br>- **0**: Check whether the file exists.<br>- **1**: Check whether the process has the execute permission on the file.<br>- **2**: Check whether the process has the write permission on the file.<br>- **4**: Check whether the process has the read permission on the file. | 293 294**Example** 295 296 ```js 297 let filePath = pathDir + "/test.txt"; 298 try { 299 fileio.accessSync(filePath); 300 } catch(err) { 301 console.info("accessSync failed with error:" + err); 302 } 303 ``` 304 305 306## fileio.close<sup>7+</sup> 307 308close(fd: number): Promise<void> 309 310Closes a file. This API uses a promise to return the result. 311 312**System capability**: SystemCapability.FileManagement.File.FileIO 313 314**Parameters** 315 316 | Name | Type | Mandatory | Description | 317 | ---- | ------ | ---- | ------------ | 318 | fd | number | Yes | File descriptor of the file to close.| 319 320**Return value** 321 322 | Type | Description | 323 | ------------------- | ---------------------------- | 324 | Promise<void> | Promise that returns no value.| 325 326**Example** 327 328 ```js 329 let filePath = pathDir + "/test.txt"; 330 let fd = fileio.openSync(filePath); 331 fileio.close(fd).then(function () { 332 console.info("File closed"); 333 }).catch(function (err) { 334 console.info("close file failed with error:" + err); 335 }); 336 ``` 337 338 339## fileio.close<sup>7+</sup> 340 341close(fd: number, callback: AsyncCallback<void>): void 342 343Closes a file. This API uses an asynchronous callback to return the result. 344 345**System capability**: SystemCapability.FileManagement.File.FileIO 346 347**Parameters** 348 349 | Name | Type | Mandatory | Description | 350 | -------- | ------------------------- | ---- | ------------ | 351 | fd | number | Yes | File descriptor of the file to close.| 352 | callback | AsyncCallback<void> | Yes | Callback invoked when the file is closed asynchronously.| 353 354**Example** 355 356 ```js 357 let filePath = pathDir + "/test.txt"; 358 let fd = fileio.openSync(filePath); 359 fileio.close(fd, function (err) { 360 // Do something. 361 }); 362 ``` 363 364 365## fileio.closeSync 366 367closeSync(fd: number): void 368 369Synchronously closes a file. 370 371**System capability**: SystemCapability.FileManagement.File.FileIO 372 373**Parameters** 374 375 | Name | Type | Mandatory | Description | 376 | ---- | ------ | ---- | ------------ | 377 | fd | number | Yes | File descriptor of the file to close.| 378 379**Example** 380 381 ```js 382 let filePath = pathDir + "/test.txt"; 383 let fd = fileio.openSync(filePath); 384 fileio.closeSync(fd); 385 ``` 386 387 388## fileio.copyFile 389 390copyFile(src: string|number, dest: string|number, mode?: number): Promise<void> 391 392Copies a file. This API uses a promise to return the result. 393 394**System capability**: SystemCapability.FileManagement.File.FileIO 395 396**Parameters** 397 398 | Name | Type | Mandatory | Description | 399 | ---- | -------------------------- | ---- | ---------------------------------------- | 400 | src | string\|number | Yes | Path or file descriptor of the file to copy. | 401 | dest | string\|number | Yes | Path or file descriptor of the new file. | 402 | mode | number | No | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.| 403 404**Return value** 405 406 | Type | Description | 407 | ------------------- | ---------------------------- | 408 | Promise<void> | Promise that returns no value.| 409 410**Example** 411 412 ```js 413 let srcPath = pathDir + "srcDir/test.txt"; 414 let dstPath = pathDir + "dstDir/test.txt"; 415 fileio.copyFile(srcPath, dstPath).then(function () { 416 console.info("File copied"); 417 }).catch(function (err) { 418 console.info("copyFile failed with error:" + err); 419 }); 420 ``` 421 422 423## fileio.copyFile 424 425copyFile(src: string|number, dest: string|number, mode: number, callback: AsyncCallback<void>): void 426 427Copies a file. This API uses an asynchronous callback to return the result. 428 429**System capability**: SystemCapability.FileManagement.File.FileIO 430 431**Parameters** 432 433 | Name | Type | Mandatory | Description | 434 | -------- | -------------------------- | ---- | ---------------------------------------- | 435 | src | string\|number | Yes | Path or file descriptor of the file to copy. | 436 | dest | string\|number | Yes | Path or file descriptor of the new file. | 437 | mode | number | No | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.| 438 | callback | AsyncCallback<void> | Yes | Callback invoked when the file is copied asynchronously. | 439 440**Example** 441 442 ```js 443 let srcPath = pathDir + "srcDir/test.txt"; 444 let dstPath = pathDir + "dstDir/test.txt"; 445 fileio.copyFile(srcPath, dstPath, function (err) { 446 // Do something. 447 }); 448 ``` 449 450 451## fileio.copyFileSync 452 453copyFileSync(src: string|number, dest: string|number, mode?: number): void 454 455Synchronously copies a file. 456 457**System capability**: SystemCapability.FileManagement.File.FileIO 458 459**Parameters** 460 461 | Name | Type | Mandatory | Description | 462 | ---- | -------------------------- | ---- | ---------------------------------------- | 463 | src | string\|number | Yes | Path or file descriptor of the file to copy. | 464 | dest | string\|number | Yes | Path or file descriptor of the new file. | 465 | mode | number | No | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.| 466 467**Example** 468 469 ```js 470 let srcPath = pathDir + "srcDir/test.txt"; 471 let dstPath = pathDir + "dstDir/test.txt"; 472 fileio.copyFileSync(srcPath, dstPath); 473 ``` 474 475 476## fileio.mkdir 477 478mkdir(path: string, mode?: number): Promise<void> 479 480Creates a directory. This API uses a promise to return the result. 481 482**System capability**: SystemCapability.FileManagement.File.FileIO 483 484**Parameters** 485 486| Name| Type | Mandatory| Description | 487| ------ | ------ | ---- | ------------------------------------------------------------ | 488| path | string | Yes | Application sandbox path of the directory. | 489| mode | number | No | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o775**.<br>- **0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 490 491**Return value** 492 493 | Type | Description | 494 | ------------------- | ---------------------------- | 495 | Promise<void> | Promise that returns no value.| 496 497**Example** 498 499 ```js 500 let dirPath = pathDir + '/testDir'; 501 fileio.mkdir(dirPath).then(function () { 502 console.info("Directory created"); 503 }).catch(function (error) { 504 console.info("mkdir failed with error:" + error); 505 }); 506 ``` 507 508 509## fileio.mkdir 510 511mkdir(path: string, mode: number, callback: AsyncCallback<void>): void 512 513Creates a directory. This API uses an asynchronous callback to return the result. 514 515**System capability**: SystemCapability.FileManagement.File.FileIO 516 517**Parameters** 518 519| Name | Type | Mandatory| Description | 520| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 521| path | string | Yes | Application sandbox path of the directory. | 522| mode | number | No | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o775**.<br>- **0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 523| callback | AsyncCallback<void> | Yes | Callback invoked when the directory is created asynchronously. | 524 525**Example** 526 527 ```js 528 let dirPath = pathDir + '/testDir'; 529 fileio.mkdir(dirPath, function (err) { 530 console.info("Directory created"); 531 }); 532 ``` 533 534 535## fileio.mkdirSync 536 537mkdirSync(path: string, mode?: number): void 538 539Synchronously creates a directory. 540 541**System capability**: SystemCapability.FileManagement.File.FileIO 542 543**Parameters** 544 545| Name| Type | Mandatory| Description | 546| ------ | ------ | ---- | ------------------------------------------------------------ | 547| path | string | Yes | Application sandbox path of the directory. | 548| mode | number | No | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o775**.<br>- **0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 549 550**Example** 551 552 ```js 553 let dirPath = path + '/testDir'; 554 fileio.mkdirSync(dirPath); 555 ``` 556 557 558## fileio.open<sup>7+</sup> 559 560open(path: string, flags?: number, mode?: number): Promise<number> 561 562Opens a file. This API uses a promise to return the result. 563 564**System capability**: SystemCapability.FileManagement.File.FileIO 565 566**Parameters** 567 568| Name| Type | Mandatory| Description | 569| ------ | ------ | ---- | ------------------------------------------------------------ | 570| path | string | Yes | Application sandbox path of the file. | 571| flags | number | No | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **0o0**: Open the file in read-only mode.<br>- **0o1**: Open the file in write-only mode.<br>- **0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (|). By default, no additional option is specified.<br>- **0o100**: If the file does not exist, create a file. The third parameter **mode** must also be specified.<br>- **0o200**: If **0o100** is added and the file already exists, throw an exception.<br>- **0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **0o2000**: Open the file in append mode. New data will be appended to the file (written to the end of the file).<br>- **0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>- **0o200000**: If **path** does not point to a directory, throw an exception.<br>- **0o400000**: If **path** points to a symbolic link, throw an exception.<br>- **0o4010000**: Open the file in synchronous I/O mode.| 572| mode | number | No | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o666**.<br>- **0o666**: The owner, user group, and other users have the read and write permissions.<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 573 574**Return value** 575 576 | Type | Description | 577 | --------------------- | ----------- | 578 | Promise<number> | Promise used to return the file descriptor of the file opened.| 579 580**Example** 581 582 ```js 583 let filePath = pathDir + "/test.txt"; 584 fileio.open(filePath, 0o1, 0o0200).then(function (number) { 585 console.info("File opened"); 586 }).catch(function (err) { 587 console.info("open file failed with error:" + err); 588 }); 589 ``` 590 591 592## fileio.open<sup>7+</sup> 593 594open(path: string, flags: number, mode: number, callback: AsyncCallback<number>): void 595 596Opens a file. This API uses an asynchronous callback to return the result. 597 598**System capability**: SystemCapability.FileManagement.File.FileIO 599 600**Parameters** 601 602| Name | Type | Mandatory| Description | 603| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 604| path | string | Yes | Application sandbox path of the file. | 605| flags | number | No | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **0o0**: Open the file in read-only mode.<br>- **0o1**: Open the file in write-only mode.<br>- **0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (|). By default, no additional option is specified.<br>- **0o100**: If the file does not exist, create a file. The third parameter **mode** must also be specified.<br>- **0o200**: If **0o100** is added and the file already exists, throw an exception.<br>- **0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **0o2000**: Open the file in append mode. New data will be appended to the file (written to the end of the file).<br>- **0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>- **0o200000**: If **path** does not point to a directory, throw an exception.<br>- **0o400000**: If **path** points to a symbolic link, throw an exception.<br>- **0o4010000**: Open the file in synchronous I/O mode.| 606| mode | number | No | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o666**.<br>- **0o666**: The owner, user group, and other users have the read and write permissions.<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 607| callback | AsyncCallback<number> | Yes | Callback invoked when the file is open asynchronously. | 608 609**Example** 610 611 ```js 612 let filePath = pathDir + "/test.txt"; 613 fileio.open(filePath, 0, function (err, fd) { 614 // Do something. 615 }); 616 ``` 617 618 619## fileio.openSync 620 621openSync(path: string, flags?: number, mode?: number): number 622 623Synchronously opens a file. 624 625**System capability**: SystemCapability.FileManagement.File.FileIO 626 627**Parameters** 628 629| Name| Type | Mandatory| Description | 630| ------ | ------ | ---- | ------------------------------------------------------------ | 631| path | string | Yes | Application sandbox path of the file. | 632| flags | number | No | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **0o0**: Open the file in read-only mode.<br>- **0o1**: Open the file in write-only mode.<br>- **0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (|). By default, no additional option is specified.<br>- **0o100**: If the file does not exist, create a file. The third parameter **mode** must also be specified.<br>- **0o200**: If **0o100** is added and the file already exists, throw an exception.<br>- **0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **0o2000**: Open the file in append mode. New data will be appended to the file (written to the end of the file).<br>- **0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>- **0o200000**: If **path** does not point to a directory, throw an exception.<br>- **0o400000**: If **path** points to a symbolic link, throw an exception.<br>- **0o4010000**: Open the file in synchronous I/O mode.| 633| mode | number | No | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o666**.<br>- **0o666**: The owner, user group, and other users have the read and write permissions.<br>- **0o640**: The owner has the read and write permissions, and the user group has the read permission.<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission.<br>The file permissions on newly created files are affected by umask, which is set as the process starts. Currently, the modification of umask is not open. | 634 635**Return value** 636 637 | Type | Description | 638 | ------ | ----------- | 639 | number | File descriptor of the file opened.| 640 641**Example** 642 643 ```js 644 let filePath = pathDir + "/test.txt"; 645 let fd = fileio.openSync(filePath, 0o102, 0o640); 646 ``` 647 ```js 648 let filePath = pathDir + "/test.txt"; 649 let fd = fileio.openSync(filePath, 0o102, 0o666); 650 fileio.writeSync(fd, 'hello world'); 651 let fd1 = fileio.openSync(filePath, 0o2002); 652 fileio.writeSync(fd1, 'hello world'); 653 let num = fileio.readSync(fd1, new ArrayBuffer(4096), {position: 0}); 654 console.info("num == " + num); 655 ``` 656 657 658## fileio.read 659 660read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): Promise<ReadOut> 661 662Reads data from a file. This API uses a promise to return the result. 663 664**System capability**: SystemCapability.FileManagement.File.FileIO 665 666**Parameters** 667 668| Name | Type | Mandatory| Description | 669| ------- | ----------- | ---- | ------------------------------------------------------------ | 670| fd | number | Yes | File descriptor of the file to read. | 671| buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | 672| options | Object | No | The options are as follows:<br>- **offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size| 673 674**Return value** 675 676 | Type | Description | 677 | ---------------------------------- | ------ | 678 | Promise<[ReadOut](#readout)> | Promise used to return the data read.| 679 680**Example** 681 682 ```js 683 let filePath = pathDir + "/test.txt"; 684 let fd = fileio.openSync(filePath, 0o2); 685 let buf = new ArrayBuffer(4096); 686 fileio.read(fd, buf).then(function (readOut) { 687 console.info("Read file data successfully"); 688 console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); 689 }).catch(function (err) { 690 console.info("read file data failed with error:" + err); 691 }); 692 ``` 693 694 695## fileio.read 696 697read(fd: number, buffer: ArrayBuffer, options: { offset?: number; length?: number; position?: number; }, callback: AsyncCallback<ReadOut>): void 698 699Reads data from a file. This API uses an asynchronous callback to return the result. 700 701**System capability**: SystemCapability.FileManagement.File.FileIO 702 703**Parameters** 704 705 | Name | Type | Mandatory | Description | 706 | -------- | ---------------------------------------- | ---- | ---------------------------------------- | 707 | fd | number | Yes | File descriptor of the file to read. | 708 | buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | 709 | options | Object | No | The options are as follows:<br>- **offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size | 710 | callback | AsyncCallback<[ReadOut](#readout)> | Yes | Callback invoked when the data is read asynchronously. | 711 712**Example** 713 714 ```js 715 let filePath = pathDir + "/test.txt"; 716 let fd = fileio.openSync(filePath, 0o2); 717 let buf = new ArrayBuffer(4096); 718 fileio.read(fd, buf, function (err, readOut) { 719 if (readOut) { 720 console.info("Read file data successfully"); 721 console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); 722 } 723 }); 724 ``` 725 726 727## fileio.readSync 728 729readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): number 730 731Synchronously reads data from a file. 732 733**System capability**: SystemCapability.FileManagement.File.FileIO 734 735**Parameters** 736 737 | Name | Type | Mandatory | Description | 738 | ------- | ----------- | ---- | ---------------------------------------- | 739 | fd | number | Yes | File descriptor of the file to read. | 740 | buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | 741 | options | Object | No | The options are as follows:<br>- **offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size | 742 743**Return value** 744 745 | Type | Description | 746 | ------ | -------- | 747 | number | Length of the data read.| 748 749**Example** 750 751 ```js 752 let filePath = pathDir + "/test.txt"; 753 let fd = fileio.openSync(filePath, 0o2); 754 let buf = new ArrayBuffer(4096); 755 let num = fileio.readSync(fd, buf); 756 ``` 757 758 759## fileio.rmdir<sup>7+</sup> 760 761rmdir(path: string): Promise<void> 762 763Deletes a directory. This API uses a promise to return the result. 764 765**System capability**: SystemCapability.FileManagement.File.FileIO 766 767**Parameters** 768 769| Name| Type | Mandatory| Description | 770| ------ | ------ | ---- | -------------------------- | 771| path | string | Yes | Application sandbox path of the directory.| 772 773**Return value** 774 775 | Type | Description | 776 | ------------------- | ---------------------------- | 777 | Promise<void> | Promise that returns no value.| 778 779**Example** 780 781 ```js 782 let dirPath = pathDir + '/testDir'; 783 fileio.rmdir(dirPath).then(function () { 784 console.info("Directory deleted"); 785 }).catch(function (err) { 786 console.info("rmdir failed with error:" + err); 787 }); 788 ``` 789 790 791## fileio.rmdir<sup>7+</sup> 792 793rmdir(path: string, callback: AsyncCallback<void>): void 794 795Deletes a directory. This API uses an asynchronous callback to return the result. 796 797**System capability**: SystemCapability.FileManagement.File.FileIO 798 799**Parameters** 800 801| Name | Type | Mandatory| Description | 802| -------- | ------------------------- | ---- | -------------------------- | 803| path | string | Yes | Application sandbox path of the directory.| 804| callback | AsyncCallback<void> | Yes | Callback invoked when the directory is deleted asynchronously. | 805 806**Example** 807 808 ```js 809 let dirPath = pathDir + '/testDir'; 810 fileio.rmdir(dirPath, function (err) { 811 // Do something. 812 console.info("Directory deleted"); 813 }); 814 ``` 815 816 817## fileio.rmdirSync<sup>7+</sup> 818 819rmdirSync(path: string): void 820 821Synchronously deletes a directory. 822 823**System capability**: SystemCapability.FileManagement.File.FileIO 824 825**Parameters** 826 827| Name| Type | Mandatory| Description | 828| ------ | ------ | ---- | -------------------------- | 829| path | string | Yes | Application sandbox path of the directory.| 830 831**Example** 832 833 ```js 834 let dirPath = pathDir + '/testDir'; 835 fileio.rmdirSync(dirPath); 836 ``` 837 838 839## fileio.unlink 840 841unlink(path: string): Promise<void> 842 843Deletes a file. This API uses a promise to return the result. 844 845**System capability**: SystemCapability.FileManagement.File.FileIO 846 847**Parameters** 848 849| Name| Type | Mandatory| Description | 850| ------ | ------ | ---- | -------------------------- | 851| path | string | Yes | Application sandbox path of the file.| 852 853**Return value** 854 855 | Type | Description | 856 | ------------------- | ---------------------------- | 857 | Promise<void> | Promise that returns no value.| 858 859**Example** 860 861 ```js 862 let filePath = pathDir + "/test.txt"; 863 fileio.unlink(filePath).then(function () { 864 console.info("File deleted"); 865 }).catch(function (error) { 866 console.info("remove file failed with error:" + error); 867 }); 868 ``` 869 870 871## fileio.unlink 872 873unlink(path: string, callback: AsyncCallback<void>): void 874 875Deletes a file. This API uses an asynchronous callback to return the result. 876 877**System capability**: SystemCapability.FileManagement.File.FileIO 878 879**Parameters** 880 881| Name | Type | Mandatory| Description | 882| -------- | ------------------------- | ---- | -------------------------- | 883| path | string | Yes | Application sandbox path of the file.| 884| callback | AsyncCallback<void> | Yes | Callback invoked when the file is deleted asynchronously. | 885 886**Example** 887 888 ```js 889 let filePath = pathDir + "/test.txt"; 890 fileio.unlink(filePath, function (err) { 891 console.info("File deleted"); 892 }); 893 ``` 894 895 896## fileio.unlinkSync 897 898unlinkSync(path: string): void 899 900Synchronously deletes a file. 901 902**System capability**: SystemCapability.FileManagement.File.FileIO 903 904**Parameters** 905 906| Name| Type | Mandatory| Description | 907| ------ | ------ | ---- | -------------------------- | 908| path | string | Yes | Application sandbox path of the file.| 909 910**Example** 911 912 ```js 913 let filePath = pathDir + "/test.txt"; 914 fileio.unlinkSync(filePath); 915 ``` 916 917 918## fileio.write 919 920write(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise<number> 921 922Writes data into a file. This API uses a promise to return the result. 923 924**System capability**: SystemCapability.FileManagement.File.FileIO 925 926**Parameters** 927 928 | Name | Type | Mandatory | Description | 929 | ------- | ------------------------------- | ---- | ---------------------------------------- | 930 | fd | number | Yes | File descriptor of the file to write. | 931 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 932 | options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.<br>Constraints: offset + length <= Buffer size| 933 934**Return value** 935 936 | Type | Description | 937 | --------------------- | -------- | 938 | Promise<number> | Promise used to return the length of the data written.| 939 940**Example** 941 942 ```js 943 let filePath = pathDir + "/test.txt"; 944 let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666); 945 fileio.write(fd, "hello, world").then(function (number) { 946 console.info("write data to file succeed and size is:" + number); 947 }).catch(function (err) { 948 console.info("write data to file failed with error:" + err); 949 }); 950 ``` 951 952 953## fileio.write 954 955write(fd: number, buffer: ArrayBuffer|string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback<number>): void 956 957Writes data into a file. This API uses an asynchronous callback to return the result. 958 959**System capability**: SystemCapability.FileManagement.File.FileIO 960 961**Parameters** 962 963 | Name | Type | Mandatory | Description | 964 | -------- | ------------------------------- | ---- | ---------------------------------------- | 965 | fd | number | Yes | File descriptor of the file to write. | 966 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 967 | options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.<br>Constraints: offset + length <= Buffer size| 968 | callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | 969 970**Example** 971 972 ```js 973 let filePath = pathDir + "/test.txt"; 974 let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666); 975 fileio.write(fd, "hello, world", function (err, bytesWritten) { 976 if (bytesWritten) { 977 console.info("write data to file succeed and size is:" + bytesWritten); 978 } 979 }); 980 ``` 981 982 983## fileio.writeSync 984 985writeSync(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number 986 987Synchronously writes data into a file. 988 989**System capability**: SystemCapability.FileManagement.File.FileIO 990 991**Parameters** 992 993 | Name | Type | Mandatory | Description | 994 | ------- | ------------------------------- | ---- | ---------------------------------------- | 995 | fd | number | Yes | File descriptor of the file to write. | 996 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 997 | options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.<br>Constraints: offset + length <= Buffer size| 998 999**Return value** 1000 1001 | Type | Description | 1002 | ------ | -------- | 1003 | number | Length of the data written in the file.| 1004 1005**Example** 1006 1007 ```js 1008 let filePath = pathDir + "/test.txt"; 1009 let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666); 1010 let num = fileio.writeSync(fd, "hello, world"); 1011 ``` 1012 1013 1014## fileio.hash 1015 1016hash(path: string, algorithm: string): Promise<string> 1017 1018Calculates the hash value of a file. This API uses a promise to return the result. 1019 1020**System capability**: SystemCapability.FileManagement.File.FileIO 1021 1022**Parameters** 1023 1024| Name | Type | Mandatory| Description | 1025| --------- | ------ | ---- | ------------------------------------------------------------ | 1026| path | string | Yes | Application sandbox path of the file. | 1027| algorithm | string | Yes | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.| 1028 1029**Return value** 1030 1031 | Type | Description | 1032 | --------------------- | -------------------------- | 1033 | Promise<string> | Promise used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.| 1034 1035**Example** 1036 1037 ```js 1038 let filePath = pathDir + "/test.txt"; 1039 fileio.hash(filePath, "sha256").then(function (str) { 1040 console.info("calculate file hash succeed:" + str); 1041 }).catch(function (err) { 1042 console.info("calculate file hash failed with error:" + err); 1043 }); 1044 ``` 1045 1046 1047## fileio.hash 1048 1049hash(path: string, algorithm: string, callback: AsyncCallback<string>): void 1050 1051Calculates the hash value of a file. This API uses an asynchronous callback to return the result. 1052 1053**System capability**: SystemCapability.FileManagement.File.FileIO 1054 1055**Parameters** 1056 1057| Name | Type | Mandatory| Description | 1058| --------- | --------------------------- | ---- | ------------------------------------------------------------ | 1059| path | string | Yes | Application sandbox path of the file. | 1060| algorithm | string | Yes | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.| 1061| callback | AsyncCallback<string> | Yes | Callback used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.| 1062 1063**Example** 1064 1065 ```js 1066 let filePath = pathDir + "/test.txt"; 1067 fileio.hash(filePath, "sha256", function (err, hashStr) { 1068 if (hashStr) { 1069 console.info("calculate file hash succeed:" + hashStr); 1070 } 1071 }); 1072 ``` 1073 1074 1075## fileio.chmod<sup>7+</sup> 1076 1077chmod(path: string, mode: number): Promise<void> 1078 1079Changes file permissions. This API uses a promise to return the result. 1080 1081**System capability**: SystemCapability.FileManagement.File.FileIO 1082 1083**Parameters** 1084 1085| Name| Type | Mandatory| Description | 1086| ------ | ------ | ---- | ------------------------------------------------------------ | 1087| path | string | Yes | Application sandbox path of the file. | 1088| mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|).<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 1089 1090**Return value** 1091 1092 | Type | Description | 1093 | ------------------- | ---------------------------- | 1094 | Promise<void> | Promise that returns no value.| 1095 1096**Example** 1097 1098 ```js 1099 let filePath = pathDir + "/test.txt"; 1100 fileio.chmod(filePath, 0o700).then(function () { 1101 console.info("File permissions changed"); 1102 }).catch(function (err) { 1103 console.info("chmod failed with error:" + err); 1104 }); 1105 ``` 1106 1107 1108## fileio.chmod<sup>7+</sup> 1109 1110chmod(path: string, mode: number, callback: AsyncCallback<void>): void 1111 1112Changes file permissions. This API uses an asynchronous callback to return the result. 1113 1114**System capability**: SystemCapability.FileManagement.File.FileIO 1115 1116**Parameters** 1117 1118| Name | Type | Mandatory| Description | 1119| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 1120| path | string | Yes | Application sandbox path of the file. | 1121| mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|).<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 1122| callback | AsyncCallback<void> | Yes | Callback invoked when the file permissions are changed asynchronously. | 1123 1124**Example** 1125 1126 ```js 1127 let filePath = pathDir + "/test.txt"; 1128 fileio.chmod(filePath, 0o700, function (err) { 1129 // Do something. 1130 }); 1131 ``` 1132 1133 1134## fileio.chmodSync<sup>7+</sup> 1135 1136chmodSync(path: string, mode: number): void 1137 1138Synchronously changes file permissions. 1139 1140**System capability**: SystemCapability.FileManagement.File.FileIO 1141 1142**Parameters** 1143 1144| Name| Type | Mandatory| Description | 1145| ------ | ------ | ---- | ------------------------------------------------------------ | 1146| path | string | Yes | Application sandbox path of the file. | 1147| mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|).<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 1148 1149**Example** 1150 1151 ```js 1152 let filePath = pathDir + "/test.txt"; 1153 fileio.chmodSync(filePath, 0o700); 1154 ``` 1155 1156 1157## fileio.fstat<sup>7+</sup> 1158 1159fstat(fd: number): Promise<Stat> 1160 1161Obtains file information based on the file descriptor. This API uses a promise to return the result. 1162 1163**System capability**: SystemCapability.FileManagement.File.FileIO 1164 1165**Parameters** 1166 1167 | Name | Type | Mandatory | Description | 1168 | ---- | ------ | ---- | ------------ | 1169 | fd | number | Yes | Descriptor of the target file.| 1170 1171**Return value** 1172 1173 | Type | Description | 1174 | ---------------------------- | ---------- | 1175 | Promise<[Stat](#stat)> | Promise used to return the file information obtained.| 1176 1177**Example** 1178 1179 ```js 1180 let filePath = pathDir + "/test.txt"; 1181 let fd = fileio.openSync(filePath); 1182 fileio.fstat(fd).then(function (stat) { 1183 console.info("fstat succeed, the size of file is " + stat.size); 1184 }).catch(function (err) { 1185 console.info("fstat failed with error:" + err); 1186 }); 1187 ``` 1188 1189 1190## fileio.fstat<sup>7+</sup> 1191 1192fstat(fd: number, callback: AsyncCallback<Stat>): void 1193 1194Obtains file information based on the file descriptor. This API uses an asynchronous callback to return the result. 1195 1196**System capability**: SystemCapability.FileManagement.File.FileIO 1197 1198**Parameters** 1199 1200 | Name | Type | Mandatory | Description | 1201 | -------- | ---------------------------------- | ---- | ---------------- | 1202 | fd | number | Yes | File descriptor of the target file. | 1203 | callback | AsyncCallback<[Stat](#stat)> | Yes | Callback invoked to return the file information obtained.| 1204 1205**Example** 1206 1207 ```js 1208 let filePath = pathDir + "/test.txt"; 1209 let fd = fileio.openSync(filePath); 1210 fileio.fstat(fd, function (err) { 1211 // Do something. 1212 }); 1213 ``` 1214 1215 1216## fileio.fstatSync<sup>7+</sup> 1217 1218fstatSync(fd: number): Stat 1219 1220Synchronously obtains file information based on the file descriptor. 1221 1222**System capability**: SystemCapability.FileManagement.File.FileIO 1223 1224**Parameters** 1225 1226 | Name | Type | Mandatory | Description | 1227 | ---- | ------ | ---- | ------------ | 1228 | fd | number | Yes | File descriptor of the target file.| 1229 1230**Return value** 1231 1232 | Type | Description | 1233 | ------------- | ---------- | 1234 | [Stat](#stat) | File information obtained.| 1235 1236**Example** 1237 1238 ```js 1239 let filePath = pathDir + "/test.txt"; 1240 let fd = fileio.openSync(filePath); 1241 let stat = fileio.fstatSync(fd); 1242 ``` 1243 1244 1245## fileio.ftruncate<sup>7+</sup> 1246 1247ftruncate(fd: number, len?: number): Promise<void> 1248 1249Truncates a file based on the file descriptor. This API uses a promise to return the result. 1250 1251**System capability**: SystemCapability.FileManagement.File.FileIO 1252 1253**Parameters** 1254 1255 | Name | Type | Mandatory | Description | 1256 | ---- | ------ | ---- | ---------------- | 1257 | fd | number | Yes | File descriptor of the file to truncate. | 1258 | len | number | No | File length, in bytes, after truncation.| 1259 1260**Return value** 1261 1262 | Type | Description | 1263 | ------------------- | ---------------------------- | 1264 | Promise<void> | Promise that returns no value.| 1265 1266**Example** 1267 1268 ```js 1269 let filePath = pathDir + "/test.txt"; 1270 let fd = fileio.openSync(filePath); 1271 fileio.ftruncate(fd, 5).then(function (err) { 1272 console.info("File truncated"); 1273 }).catch(function (err) { 1274 console.info("truncate file failed with error:" + err); 1275 }); 1276 ``` 1277 1278 1279## fileio.ftruncate<sup>7+</sup> 1280 1281ftruncate(fd: number, len?: number, callback: AsyncCallback<void>): void 1282 1283Truncates a file based on the file descriptor. This API uses an asynchronous callback to return the result. 1284 1285**System capability**: SystemCapability.FileManagement.File.FileIO 1286 1287**Parameters** 1288 1289 | Name | Type | Mandatory | Description | 1290 | -------- | ------------------------- | ---- | ---------------- | 1291 | fd | number | Yes | File descriptor of the file to truncate. | 1292 | len | number | No | File length, in bytes, after truncation.| 1293 | callback | AsyncCallback<void> | Yes | Callback that returns no value. | 1294 1295**Example** 1296 1297 ```js 1298 let filePath = pathDir + "/test.txt"; 1299 let fd = fileio.openSync(filePath); 1300 let len = 5; 1301 fileio.ftruncate(fd, 5, function (err) { 1302 // Do something. 1303 }); 1304 ``` 1305 1306 1307## fileio.ftruncateSync<sup>7+</sup> 1308 1309ftruncateSync(fd: number, len?: number): void 1310 1311Synchronously truncates a file based on the file descriptor. 1312 1313**System capability**: SystemCapability.FileManagement.File.FileIO 1314 1315**Parameters** 1316 1317 | Name | Type | Mandatory | Description | 1318 | ---- | ------ | ---- | ---------------- | 1319 | fd | number | Yes | File descriptor of the file to truncate. | 1320 | len | number | No | File length, in bytes, after truncation.| 1321 1322**Example** 1323 1324 ```js 1325 let filePath = pathDir + "/test.txt"; 1326 let fd = fileio.openSync(filePath); 1327 let len = 5; 1328 fileio.ftruncateSync(fd, len); 1329 ``` 1330 1331 1332## fileio.truncate<sup>7+</sup> 1333 1334truncate(path: string, len?: number): Promise<void> 1335 1336Truncates a file based on the file path. This API uses a promise to return the result. 1337 1338**System capability**: SystemCapability.FileManagement.File.FileIO 1339 1340**Parameters** 1341 1342| Name| Type | Mandatory| Description | 1343| ------ | ------ | ---- | -------------------------------- | 1344| path | string | Yes | Application sandbox path of the file to truncate. | 1345| len | number | No | File length, in bytes, after truncation.| 1346 1347**Return value** 1348 1349 | Type | Description | 1350 | ------------------- | ---------------------------- | 1351 | Promise<void> | Promise that returns no value.| 1352 1353**Example** 1354 1355 ```js 1356 let filePath = pathDir + "/test.txt"; 1357 let len = 5; 1358 fileio.truncate(filePath, len).then(function () { 1359 console.info("File truncated"); 1360 }).catch(function (err) { 1361 console.info("truncate file failed with error:" + err); 1362 }); 1363 ``` 1364 1365 1366## fileio.truncate<sup>7+</sup> 1367 1368truncate(path: string, len?: number, callback: AsyncCallback<void>): void 1369 1370Truncates a file based on the file path. This API uses an asynchronous callback to return the result. 1371 1372**System capability**: SystemCapability.FileManagement.File.FileIO 1373 1374**Parameters** 1375 1376| Name | Type | Mandatory| Description | 1377| -------- | ------------------------- | ---- | -------------------------------- | 1378| path | string | Yes | Application sandbox path of the file to truncate. | 1379| len | number | No | File length, in bytes, after truncation.| 1380| callback | AsyncCallback<void> | Yes | Callback that returns no value. | 1381 1382**Example** 1383 1384 ```js 1385 let filePath = pathDir + "/test.txt"; 1386 let len = 5; 1387 fileio.truncate(filePath, len, function (err) { 1388 // Do something. 1389 }); 1390 ``` 1391 1392 1393## fileio.truncateSync<sup>7+</sup> 1394 1395truncateSync(path: string, len?: number): void 1396 1397Synchronously truncates a file based on the file path. 1398 1399**System capability**: SystemCapability.FileManagement.File.FileIO 1400 1401**Parameters** 1402 1403| Name| Type | Mandatory| Description | 1404| ------ | ------ | ---- | -------------------------------- | 1405| path | string | Yes | Application sandbox path of the file to truncate. | 1406| len | number | No | File length, in bytes, after truncation.| 1407 1408**Example** 1409 1410 ```js 1411 let filePath = pathDir + "/test.txt"; 1412 let len = 5; 1413 fileio.truncateSync(filePath, len); 1414 ``` 1415 1416 1417## fileio.readText<sup>7+</sup> 1418 1419readText(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): Promise<string> 1420 1421Reads the text content of a file. This API uses a promise to return the result. 1422 1423**System capability**: SystemCapability.FileManagement.File.FileIO 1424 1425**Parameters** 1426 1427| Name | Type | Mandatory| Description | 1428| -------- | ------ | ---- | ------------------------------------------------------------ | 1429| filePath | string | Yes | Application sandbox path of the file to read. | 1430| options | Object | No | The options are as follows:<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.| 1431 1432**Return value** 1433 1434 | Type | Description | 1435 | --------------------- | ---------- | 1436 | Promise<string> | Promise used to return the content read.| 1437 1438**Example** 1439 1440 ```js 1441 let filePath = pathDir + "/test.txt"; 1442 fileio.readText(filePath).then(function (str) { 1443 console.info("readText succeed:" + str); 1444 }).catch(function (err) { 1445 console.info("readText failed with error:" + err); 1446 }); 1447 ``` 1448 1449 1450## fileio.readText<sup>7+</sup> 1451 1452readText(filePath: string, options: { position?: number; length?: number; encoding?: string; }, callback: AsyncCallback<string>): void 1453 1454Reads the text content of a file. This API uses an asynchronous callback to return the result. 1455 1456**System capability**: SystemCapability.FileManagement.File.FileIO 1457 1458**Parameters** 1459 1460| Name | Type | Mandatory| Description | 1461| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 1462| filePath | string | Yes | Application sandbox path of the file to read. | 1463| options | Object | No | The options are as follows:<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.| 1464| callback | AsyncCallback<string> | Yes | Callback used to return the content read. | 1465 1466**Example** 1467 1468 ```js 1469 let filePath = pathDir + "/test.txt"; 1470 fileio.readText(filePath, { position: 1, encoding: 'UTF-8' }, function (err, str) { 1471 // Do something. 1472 }); 1473 ``` 1474 1475 1476## fileio.readTextSync<sup>7+</sup> 1477 1478readTextSync(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): string 1479 1480Synchronously reads the text of a file. 1481 1482**System capability**: SystemCapability.FileManagement.File.FileIO 1483 1484**Parameters** 1485 1486| Name | Type | Mandatory| Description | 1487| -------- | ------ | ---- | ------------------------------------------------------------ | 1488| filePath | string | Yes | Application sandbox path of the file to read. | 1489| options | Object | No | The options are as follows:<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.| 1490 1491**Return value** 1492 1493 | Type | Description | 1494 | ------ | -------------------- | 1495 | string | Promise used to return the content of the file read.| 1496 1497**Example** 1498 1499 ```js 1500 let filePath = pathDir + "/test.txt"; 1501 let str = fileio.readTextSync(filePath, {position: 1, length: 3}); 1502 ``` 1503 1504 1505## fileio.lstat<sup>7+</sup> 1506 1507lstat(path: string): Promise<Stat> 1508 1509Obtains link information. This API uses a promise to return the result. 1510 1511**System capability**: SystemCapability.FileManagement.File.FileIO 1512 1513**Parameters** 1514 1515| Name| Type | Mandatory| Description | 1516| ------ | ------ | ---- | -------------------------------------- | 1517| path | string | Yes | Application sandbox path of the target file.| 1518 1519**Return value** 1520 1521 | Type | Description | 1522 | ---------------------------- | ---------- | 1523 | Promise<[Stat](#stat)> | Promise used to return the link information obtained. For details, see [Stat](#stat).| 1524 1525**Example** 1526 1527 ```js 1528 let filePath = pathDir + "/test.txt"; 1529 fileio.lstat(filePath).then(function (stat) { 1530 console.info("get link status succeed, the size of file is" + stat.size); 1531 }).catch(function (err) { 1532 console.info("get link status failed with error:" + err); 1533 }); 1534 ``` 1535 1536 1537## fileio.lstat<sup>7+</sup> 1538 1539lstat(path: string, callback: AsyncCallback<Stat>): void 1540 1541Obtains link information. This API uses an asynchronous callback to return the result. 1542 1543**System capability**: SystemCapability.FileManagement.File.FileIO 1544 1545**Parameters** 1546 1547| Name | Type | Mandatory| Description | 1548| -------- | ---------------------------------- | ---- | -------------------------------------- | 1549| path | string | Yes | Application sandbox path of the target file.| 1550| callback | AsyncCallback<[Stat](#stat)> | Yes | Callback used to return the link information obtained. | 1551 1552**Example** 1553 1554 ```js 1555 let filePath = pathDir + "/test.txt"; 1556 fileio.lstat(filePath, function (err, stat) { 1557 // Do something. 1558 }); 1559 ``` 1560 1561 1562## fileio.lstatSync<sup>7+</sup> 1563 1564lstatSync(path: string): Stat 1565 1566Synchronously obtains the link information. 1567 1568**System capability**: SystemCapability.FileManagement.File.FileIO 1569 1570**Parameters** 1571 1572| Name| Type | Mandatory| Description | 1573| ------ | ------ | ---- | -------------------------------------- | 1574| path | string | Yes | Application sandbox path of the target file.| 1575 1576**Return value** 1577 1578 | Type | Description | 1579 | ------------- | ---------- | 1580 | [Stat](#stat) | Link information obtained.| 1581 1582**Example** 1583 1584 ```js 1585 let filePath = pathDir + "/test.txt"; 1586 let stat = fileio.lstatSync(filePath); 1587 ``` 1588 1589 1590## fileio.rename<sup>7+</sup> 1591 1592rename(oldPath: string, newPath: string): Promise<void> 1593 1594Renames a file. This API uses a promise to return the result. 1595 1596**System capability**: SystemCapability.FileManagement.File.FileIO 1597 1598**Parameters** 1599 1600| Name | Type | Mandatory| Description | 1601| ------- | ------ | ---- | ---------------------------- | 1602| oldPath | string | Yes | Application sandbox path of the file to rename.| 1603| newPath | string | Yes | Application sandbox path of the file renamed. | 1604 1605**Return value** 1606 1607 | Type | Description | 1608 | ------------------- | ---------------------------- | 1609 | Promise<void> | Promise that returns no value.| 1610 1611**Example** 1612 1613 ```js 1614 let srcFile = pathDir + "/test.txt"; 1615 let dstFile = pathDir + '/new.txt'; 1616 fileio.rename(srcFile, dstFile).then(function () { 1617 console.info("File renamed"); 1618 }).catch(function (err) { 1619 console.info("rename failed with error:" + err); 1620 }); 1621 ``` 1622 1623 1624## fileio.rename<sup>7+</sup> 1625 1626rename(oldPath: string, newPath: string, callback: AsyncCallback<void>): void 1627 1628Renames a file. This API uses an asynchronous callback to return the result. 1629 1630**System capability**: SystemCapability.FileManagement.File.FileIO 1631 1632**Parameters** 1633 1634| Name | Type | Mandatory| Description | 1635| -------- | ------------------------- | ---- | ---------------------------- | 1636| oldPath | string | Yes | Application sandbox path of the file to rename.| 1637| newPath | string | Yes | Application sandbox path of the file renamed. | 1638| callback | AsyncCallback<void> | Yes | Callback invoked when the file is asynchronously renamed. | 1639 1640**Example** 1641 1642 ```js 1643 let srcFile = pathDir + "/test.txt"; 1644 let dstFile = pathDir + '/new.txt'; 1645 fileio.rename(srcFile, dstFile, function (err) { 1646 }); 1647 ``` 1648 1649 1650## fileio.renameSync<sup>7+</sup> 1651 1652renameSync(oldPath: string, newPath: string): void 1653 1654Synchronously renames a file. 1655 1656**System capability**: SystemCapability.FileManagement.File.FileIO 1657 1658**Parameters** 1659 1660| Name | Type | Mandatory| Description | 1661| ------- | ------ | ---- | ---------------------------- | 1662| oldPath | string | Yes | Application sandbox path of the file to rename.| 1663| newPath | string | Yes | Application sandbox path of the file renamed. | 1664 1665**Example** 1666 1667 ```js 1668 let srcFile = pathDir + "/test.txt"; 1669 let dstFile = pathDir + '/new.txt'; 1670 fileio.renameSync(srcFile, dstFile); 1671 ``` 1672 1673 1674## fileio.fsync<sup>7+</sup> 1675 1676fsync(fd: number): Promise<void> 1677 1678Flushes data of a file to disk. This API uses a promise to return the result. 1679 1680**System capability**: SystemCapability.FileManagement.File.FileIO 1681 1682**Parameters** 1683 1684 | Name | Type | Mandatory | Description | 1685 | ---- | ------ | ---- | ------------ | 1686 | fd | number | Yes | File descriptor of the file to flush.| 1687 1688**Return value** 1689 1690 | Type | Description | 1691 | ------------------- | ---------------------------- | 1692 | Promise<void> | Promise that returns no value.| 1693 1694**Example** 1695 1696 ```js 1697 let filePath = pathDir + "/test.txt"; 1698 let fd = fileio.openSync(filePath); 1699 fileio.fsync(fd).then(function () { 1700 console.info("Data flushed"); 1701 }).catch(function (err) { 1702 console.info("sync data failed with error:" + err); 1703 }); 1704 ``` 1705 1706 1707## fileio.fsync<sup>7+</sup> 1708 1709fsync(fd: number, callback: AsyncCallback<void>): void 1710 1711Flushes data of a file to disk. This API uses an asynchronous callback to return the result. 1712 1713**System capability**: SystemCapability.FileManagement.File.FileIO 1714 1715**Parameters** 1716 1717 | Name | Type | Mandatory | Description | 1718 | -------- | ------------------------- | ---- | --------------- | 1719 | fd | number | Yes | File descriptor of the file to flush. | 1720 | Callback | AsyncCallback<void> | Yes | Callback invoked when the file is synchronized in asynchronous mode.| 1721 1722**Example** 1723 1724 ```js 1725 let filePath = pathDir + "/test.txt"; 1726 let fd = fileio.openSync(filePath); 1727 fileio.fsync(fd, function (err) { 1728 // Do something. 1729 }); 1730 ``` 1731 1732 1733## fileio.fsyncSync<sup>7+</sup> 1734 1735fsyncSync(fd: number): void 1736 1737Flushes data of a file to disk in synchronous mode. 1738 1739**System capability**: SystemCapability.FileManagement.File.FileIO 1740 1741**Parameters** 1742 1743 | Name | Type | Mandatory | Description | 1744 | ---- | ------ | ---- | ------------ | 1745 | fd | number | Yes | File descriptor of the file to flush.| 1746 1747**Example** 1748 1749 ```js 1750 let filePath = pathDir + "/test.txt"; 1751 let fd = fileio.openSync(filePath); 1752 fileio.fsyncSync(fd); 1753 ``` 1754 1755 1756## fileio.fdatasync<sup>7+</sup> 1757 1758fdatasync(fd: number): Promise<void> 1759 1760Flushes data of a file to disk. This API uses a promise to return the result. **fdatasync()** is similar to **fsync()**, but does not flush modified metadata unless that metadata is needed. 1761 1762**System capability**: SystemCapability.FileManagement.File.FileIO 1763 1764**Parameters** 1765 1766 | Name | Type | Mandatory | Description | 1767 | ---- | ------ | ---- | ------------ | 1768 | fd | number | Yes | File descriptor of the file to flush.| 1769 1770**Return value** 1771 1772 | Type | Description | 1773 | ------------------- | ---------------------------- | 1774 | Promise<void> | Promise that returns no value.| 1775 1776**Example** 1777 1778 ```js 1779 let filePath = pathDir + "/test.txt"; 1780 let fd = fileio.openSync(filePath); 1781 fileio.fdatasync(fd).then(function (err) { 1782 console.info("Data flushed"); 1783 }).catch(function (err) { 1784 console.info("sync data failed with error:" + err); 1785 }); 1786 ``` 1787 1788 1789## fileio.fdatasync<sup>7+</sup> 1790 1791fdatasync(fd: number, callback: AsyncCallback<void>): void 1792 1793Flushes data of a file to disk. This API uses an asynchronous callback to return the result. 1794 1795**System capability**: SystemCapability.FileManagement.File.FileIO 1796 1797**Parameters** 1798 1799 | Name | Type | Mandatory | Description | 1800 | -------- | ------------------------------- | ---- | ----------------- | 1801 | fd | number | Yes | File descriptor of the file to synchronize. | 1802 | callback | AsyncCallback<void> | Yes | Callback invoked when the file data is synchronized in asynchronous mode.| 1803 1804**Example** 1805 1806 ```js 1807 let filePath = pathDir + "/test.txt"; 1808 let fd = fileio.openSync(filePath); 1809 fileio.fdatasync (fd, function (err) { 1810 // Do something. 1811 }); 1812 ``` 1813 1814 1815## fileio.fdatasyncSync<sup>7+</sup> 1816 1817fdatasyncSync(fd: number): void 1818 1819Synchronizes data in a file in synchronous mode. 1820 1821**System capability**: SystemCapability.FileManagement.File.FileIO 1822 1823**Parameters** 1824 1825 | Name | Type | Mandatory | Description | 1826 | ---- | ------ | ---- | ------------ | 1827 | fd | number | Yes | File descriptor of the file to flush.| 1828 1829**Example** 1830 1831 ```js 1832 let filePath = pathDir + "/test.txt"; 1833 let fd = fileio.openSync(filePath); 1834 let stat = fileio.fdatasyncSync(fd); 1835 ``` 1836 1837 1838## fileio.symlink<sup>7+</sup> 1839 1840symlink(target: string, srcPath: string): Promise<void> 1841 1842Creates a symbolic link based on the file path. This API uses a promise to return the result. 1843 1844**System capability**: SystemCapability.FileManagement.File.FileIO 1845 1846**Parameters** 1847 1848| Name | Type | Mandatory| Description | 1849| ------- | ------ | ---- | ---------------------------- | 1850| target | string | Yes | Application sandbox path of the target file. | 1851| srcPath | string | Yes | Application sandbox path of the symbolic link file.| 1852 1853**Return value** 1854 1855 | Type | Description | 1856 | ------------------- | ---------------------------- | 1857 | Promise<void> | Promise that returns no value.| 1858 1859**Example** 1860 1861 ```js 1862 let srcFile = pathDir + "/test.txt"; 1863 let dstFile = pathDir + '/test'; 1864 fileio.symlink(srcFile, dstFile).then(function () { 1865 console.info("Symbolic link created"); 1866 }).catch(function (err) { 1867 console.info("symlink failed with error:" + err); 1868 }); 1869 ``` 1870 1871 1872## fileio.symlink<sup>7+</sup> 1873 1874symlink(target: string, srcPath: string, callback: AsyncCallback<void>): void 1875 1876Creates a symbolic link based on the file path. This API uses an asynchronous callback to return the result. 1877 1878**System capability**: SystemCapability.FileManagement.File.FileIO 1879 1880**Parameters** 1881 1882| Name | Type | Mandatory| Description | 1883| -------- | ------------------------- | ---- | -------------------------------- | 1884| target | string | Yes | Application sandbox path of the target file. | 1885| srcPath | string | Yes | Application sandbox path of the symbolic link file. | 1886| callback | AsyncCallback<void> | Yes | Callback invoked when the symbolic link is created asynchronously.| 1887 1888**Example** 1889 1890 ```js 1891 let srcFile = pathDir + "/test.txt"; 1892 let dstFile = pathDir + '/test'; 1893 fileio.symlink(srcFile, dstFile, function (err) { 1894 // Do something. 1895 }); 1896 ``` 1897 1898 1899## fileio.symlinkSync<sup>7+</sup> 1900 1901symlinkSync(target: string, srcPath: string): void 1902 1903Synchronously creates a symbolic link based on a specified path. 1904 1905**System capability**: SystemCapability.FileManagement.File.FileIO 1906 1907**Parameters** 1908 1909| Name | Type | Mandatory| Description | 1910| ------- | ------ | ---- | ---------------------------- | 1911| target | string | Yes | Application sandbox path of the target file. | 1912| srcPath | string | Yes | Application sandbox path of the symbolic link file.| 1913 1914**Example** 1915 1916 ```js 1917 let srcFile = pathDir + "/test.txt"; 1918 let dstFile = pathDir + '/test'; 1919 fileio.symlinkSync(srcFile, dstFile); 1920 ``` 1921 1922 1923## fileio.chown<sup>7+</sup> 1924 1925chown(path: string, uid: number, gid: number): Promise<void> 1926 1927Changes the file owner based on the file path. This API uses a promise to return the result. 1928 1929**System capability**: SystemCapability.FileManagement.File.FileIO 1930 1931**Parameters** 1932 1933| Name| Type | Mandatory| Description | 1934| ------ | ------ | ---- | -------------------------- | 1935| path | string | Yes | Application sandbox path of the file.| 1936| uid | number | Yes | New user ID (UID). | 1937| gid | number | Yes | New group ID (GID). | 1938 1939**Return value** 1940 1941 | Type | Description | 1942 | ------------------- | ---------------------------- | 1943 | Promise<void> | Promise that returns no value.| 1944 1945**Example** 1946 1947 ```js 1948 let filePath = pathDir + "/test.txt"; 1949 let stat = fileio.statSync(filePath); 1950 fileio.chown(filePath, stat.uid, stat.gid).then(function () { 1951 console.info("File owner changed"); 1952 }).catch(function (err) { 1953 console.info("chown failed with error:" + err); 1954 }); 1955 ``` 1956 1957 1958## fileio.chown<sup>7+</sup> 1959 1960chown(path: string, uid: number, gid: number, callback: AsyncCallback<void>): void 1961 1962Changes the file owner based on the file path. This API uses an asynchronous callback to return the result. 1963 1964**System capability**: SystemCapability.FileManagement.File.FileIO 1965 1966**Parameters** 1967 1968| Name | Type | Mandatory| Description | 1969| -------- | ------------------------- | ---- | ------------------------------ | 1970| path | string | Yes | Application sandbox path of the file. | 1971| uid | number | Yes | New UID. | 1972| gid | number | Yes | New GID. | 1973| callback | AsyncCallback<void> | Yes | Callback invoked when the file owner is changed asynchronously.| 1974 1975**Example** 1976 1977 ```js 1978 let filePath = pathDir + "/test.txt"; 1979 let stat = fileio.statSync(filePath) 1980 fileio.chown(filePath, stat.uid, stat.gid, function (err) { 1981 // Do something. 1982 }); 1983 ``` 1984 1985 1986## fileio.chownSync<sup>7+</sup> 1987 1988chownSync(path: string, uid: number, gid: number): void 1989 1990Synchronously changes the file owner based on its path. 1991 1992**System capability**: SystemCapability.FileManagement.File.FileIO 1993 1994**Parameters** 1995 1996| Name| Type | Mandatory| Description | 1997| ------ | ------ | ---- | -------------------------- | 1998| path | string | Yes | Application sandbox path of the file.| 1999| uid | number | Yes | New UID. | 2000| gid | number | Yes | New GID. | 2001 2002**Example** 2003 2004 ```js 2005 let filePath = pathDir + "/test.txt"; 2006 let stat = fileio.statSync(filePath) 2007 fileio.chownSync(filePath, stat.uid, stat.gid); 2008 ``` 2009 2010 2011## fileio.mkdtemp<sup>7+</sup> 2012 2013mkdtemp(prefix: string): Promise<string> 2014 2015Creates a temporary directory. This API uses a promise to return the result. 2016 2017**System capability**: SystemCapability.FileManagement.File.FileIO 2018 2019**Parameters** 2020 2021 | Name | Type | Mandatory | Description | 2022 | ------ | ------ | ---- | --------------------------- | 2023 | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| 2024 2025**Return value** 2026 2027 | Type | Description | 2028 | --------------------- | ---------- | 2029 | Promise<string> | Promise used to return the unique directory generated.| 2030 2031**Example** 2032 2033 ```js 2034 fileio.mkdtemp(pathDir + "/XXXXXX").then(function (pathDir) { 2035 console.info("mkdtemp succeed:" + pathDir); 2036 }).catch(function (err) { 2037 console.info("mkdtemp failed with error:" + err); 2038 }); 2039 ``` 2040 2041 2042## fileio.mkdtemp<sup>7+</sup> 2043 2044mkdtemp(prefix: string, callback: AsyncCallback<string>): void 2045 2046Creates a temporary directory. This API uses an asynchronous callback to return the result. 2047 2048**System capability**: SystemCapability.FileManagement.File.FileIO 2049 2050**Parameters** 2051 2052 | Name | Type | Mandatory | Description | 2053 | -------- | --------------------------- | ---- | --------------------------- | 2054 | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| 2055 | callback | AsyncCallback<string> | Yes | Callback invoked when a temporary directory is created asynchronously. | 2056 2057**Example** 2058 2059 ```js 2060 fileio.mkdtemp(pathDir + "/XXXXXX", function (err, res) { 2061 // Do something. 2062 }); 2063 ``` 2064 2065 2066## fileio.mkdtempSync<sup>7+</sup> 2067 2068mkdtempSync(prefix: string): string 2069 2070Synchronously creates a temporary directory. 2071 2072**System capability**: SystemCapability.FileManagement.File.FileIO 2073 2074**Parameters** 2075 2076 | Name | Type | Mandatory | Description | 2077 | ------ | ------ | ---- | --------------------------- | 2078 | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| 2079 2080**Return value** 2081 2082 | Type | Description | 2083 | ------ | ---------- | 2084 | string | Unique path generated.| 2085 2086**Example** 2087 2088 ```js 2089 let res = fileio.mkdtempSync(pathDir + "/XXXXXX"); 2090 ``` 2091 2092 2093## fileio.fchmod<sup>7+</sup> 2094 2095fchmod(fd: number, mode: number): Promise<void> 2096 2097Changes file permissions based on the file descriptor. This API uses a promise to return the result. 2098 2099**System capability**: SystemCapability.FileManagement.File.FileIO 2100 2101**Parameters** 2102 2103 | Name | Type | Mandatory | Description | 2104 | ---- | ------ | ---- | ---------------------------------------- | 2105 | fd | number | Yes | File descriptor of the target file. | 2106| mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|).<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 2107 2108**Return value** 2109 2110 | Type | Description | 2111 | ------------------- | ---------------------------- | 2112 | Promise<void> | Promise that returns no value.| 2113 2114**Example** 2115 2116 ```js 2117 let filePath = pathDir + "/test.txt"; 2118 let fd = fileio.openSync(filePath); 2119 let mode = 0o700; 2120 fileio.fchmod(fd, mode).then(function () { 2121 console.info("File permissions changed"); 2122 }).catch(function (err) { 2123 console.info("chmod failed with error:" + err); 2124 }); 2125 ``` 2126 2127 2128## fileio.fchmod<sup>7+</sup> 2129 2130fchmod(fd: number, mode: number, callback: AsyncCallback<void>): void 2131 2132Changes file permissions based on the file descriptor. This API uses an asynchronous callback to return the result. 2133 2134**System capability**: SystemCapability.FileManagement.File.FileIO 2135 2136**Parameters** 2137 2138 | Name | Type | Mandatory | Description | 2139 | -------- | ------------------------------- | ---- | ---------------------------------------- | 2140 | fd | number | Yes | File descriptor of the target file. | 2141| mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|).<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 2142 | callback | AsyncCallback<void> | Yes | Callback invoked when the file permissions are changed asynchronously. | 2143 2144**Example** 2145 2146 ```js 2147 let filePath = pathDir + "/test.txt"; 2148 let fd = fileio.openSync(filePath); 2149 let mode = 0o700; 2150 fileio.fchmod(fd, mode, function (err) { 2151 // Do something. 2152 }); 2153 ``` 2154 2155 2156## fileio.fchmodSync<sup>7+</sup> 2157 2158fchmodSync(fd: number, mode: number): void 2159 2160Synchronously changes the file permissions based on the file descriptor. 2161 2162**System capability**: SystemCapability.FileManagement.File.FileIO 2163 2164**Parameters** 2165 2166 | Name | Type | Mandatory | Description | 2167 | ---- | ------ | ---- | ---------------------------------------- | 2168 | fd | number | Yes | File descriptor of the target file. | 2169| mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|).<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission. | 2170 2171**Example** 2172 2173 ```js 2174 let filePath = pathDir + "/test.txt"; 2175 let fd = fileio.openSync(filePath); 2176 let mode = 0o700; 2177 fileio.fchmodSync(fd, mode); 2178 ``` 2179 2180 2181## fileio.createStream<sup>7+</sup> 2182 2183createStream(path: string, mode: string): Promise<Stream> 2184 2185Opens a file stream based on the file path. This API uses a promise to return the result. 2186 2187**System capability**: SystemCapability.FileManagement.File.FileIO 2188 2189**Parameters** 2190 2191| Name| Type | Mandatory| Description | 2192| ------ | ------ | ---- | ------------------------------------------------------------ | 2193| path | string | Yes | Application sandbox path of the file. | 2194| 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).| 2195 2196**Return value** 2197 2198 | Type | Description | 2199 | --------------------------------- | --------- | 2200 | Promise<[Stream](#stream)> | Promise used to return the result.| 2201 2202**Example** 2203 2204 ```js 2205 let filePath = pathDir + "/test.txt"; 2206 fileio.createStream(filePath, "r+").then(function (stream) { 2207 console.info("Stream created"); 2208 }).catch(function (err) { 2209 console.info("createStream failed with error:" + err); 2210 }); 2211 ``` 2212 2213 2214## fileio.createStream<sup>7+</sup> 2215 2216createStream(path: string, mode: string, callback: AsyncCallback<Stream>): void 2217 2218Opens a file stream based on the file path. This API uses an asynchronous callback to return the result. 2219 2220**System capability**: SystemCapability.FileManagement.File.FileIO 2221 2222**Parameters** 2223 2224| Name | Type | Mandatory| Description | 2225| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 2226| path | string | Yes | Application sandbox path of the file. | 2227| 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).| 2228| callback | AsyncCallback<[Stream](#stream)> | Yes | Callback invoked when the stream is open asynchronously. | 2229 2230**Example** 2231 2232 ```js 2233 let filePath = pathDir + "/test.txt"; 2234 fileio.createStream(filePath, "r+", function (err, stream) { 2235 // Do something. 2236 }); 2237 ``` 2238 2239 2240## fileio.createStreamSync<sup>7+</sup> 2241 2242createStreamSync(path: string, mode: string): Stream 2243 2244Synchronously opens a stream based on the file path. 2245 2246**System capability**: SystemCapability.FileManagement.File.FileIO 2247 2248**Parameters** 2249 2250| Name| Type | Mandatory| Description | 2251| ------ | ------ | ---- | ------------------------------------------------------------ | 2252| path | string | Yes | Application sandbox path of the file. | 2253| 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).| 2254 2255**Return value** 2256 2257 | Type | Description | 2258 | ------------------ | --------- | 2259 | [Stream](#stream) | Stream opened.| 2260 2261**Example** 2262 2263 ```js 2264 let filePath = pathDir + "/test.txt"; 2265 let ss = fileio.createStreamSync(filePath, "r+"); 2266 ``` 2267 2268 2269## fileio.fdopenStream<sup>7+</sup> 2270 2271fdopenStream(fd: number, mode: string): Promise<Stream> 2272 2273Opens a file stream based on the file descriptor. This API uses a promise to return the result. 2274 2275**System capability**: SystemCapability.FileManagement.File.FileIO 2276 2277**Parameters** 2278 2279 | Name | Type | Mandatory | Description | 2280 | ---- | ------ | ---- | ---------------------------------------- | 2281 | fd | number | Yes | File descriptor of the target file. | 2282 | 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).| 2283 2284**Return value** 2285 2286 | Type | Description | 2287 | --------------------------------- | --------- | 2288 | Promise<[Stream](#stream)> | Promise used to return the result.| 2289 2290**Example** 2291 2292 ```js 2293 let filePath = pathDir + "/test.txt"; 2294 let fd = fileio.openSync(filePath); 2295 fileio.fdopenStream(fd, "r+").then(function (stream) { 2296 console.info("Stream opened"); 2297 }).catch(function (err) { 2298 console.info("openStream failed with error:" + err); 2299 }); 2300 ``` 2301 2302 2303## fileio.fdopenStream<sup>7+</sup> 2304 2305fdopenStream(fd: number, mode: string, callback: AsyncCallback<Stream>): void 2306 2307Opens a file stream based on the file descriptor. This API uses an asynchronous callback to return the result. 2308 2309**System capability**: SystemCapability.FileManagement.File.FileIO 2310 2311**Parameters** 2312 2313 | Name | Type | Mandatory | Description | 2314 | -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2315 | fd | number | Yes | File descriptor of the target file. | 2316 | 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).| 2317 | callback | AsyncCallback<[Stream](#stream)> | Yes | Callback invoked when the stream is open asynchronously. | 2318 2319**Example** 2320 2321 ```js 2322 let filePath = pathDir + "/test.txt"; 2323 let fd = fileio.openSync(filePath); 2324 fileio.fdopenStream(fd, "r+", function (err, stream) { 2325 // Do something. 2326 }); 2327 ``` 2328 2329 2330## fileio.fdopenStreamSync<sup>7+</sup> 2331 2332fdopenStreamSync(fd: number, mode: string): Stream 2333 2334Synchronously opens a stream based on the file descriptor. 2335 2336**System capability**: SystemCapability.FileManagement.File.FileIO 2337 2338**Parameters** 2339 2340 | Name | Type | Mandatory | Description | 2341 | ---- | ------ | ---- | ---------------------------------------- | 2342 | fd | number | Yes | File descriptor of the target file. | 2343 | 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).| 2344 2345**Return value** 2346 2347 | Type | Description | 2348 | ------------------ | --------- | 2349 | [Stream](#stream) | Stream opened.| 2350 2351**Example** 2352 2353 ```js 2354 let filePath = pathDir + "/test.txt"; 2355 let fd = fileio.openSync(filePath); 2356 let ss = fileio.fdopenStreamSync(fd, "r+"); 2357 ``` 2358 2359 2360## fileio.fchown<sup>7+</sup> 2361 2362fchown(fd: number, uid: number, gid: number): Promise<void> 2363 2364Changes the file owner based on the file descriptor. This API uses a promise to return the result. 2365 2366**System capability**: SystemCapability.FileManagement.File.FileIO 2367 2368**Parameters** 2369 2370 | Name | Type | Mandatory | Description | 2371 | ---- | ------ | ---- | ------------ | 2372 | fd | number | Yes | File descriptor of the target file.| 2373 | uid | number | Yes | New UID. | 2374 | gid | number | Yes | New GID. | 2375 2376**Return value** 2377 2378 | Type | Description | 2379 | ------------------- | ---------------------------- | 2380 | Promise<void> | Promise that returns no value.| 2381 2382**Example** 2383 2384 ```js 2385 let filePath = pathDir + "/test.txt"; 2386 let fd = fileio.openSync(filePath); 2387 let stat = fileio.statSync(filePath); 2388 fileio.fchown(fd, stat.uid, stat.gid).then(function () { 2389 console.info("File owner changed"); 2390 }).catch(function (err) { 2391 console.info("chown failed with error:" + err); 2392 }); 2393 ``` 2394 2395 2396## fileio.fchown<sup>7+</sup> 2397 2398fchown(fd: number, uid: number, gid: number, callback: AsyncCallback<void>): void 2399 2400Changes the file owner based on the file descriptor. This API uses an asynchronous callback to return the result. 2401 2402**System capability**: SystemCapability.FileManagement.File.FileIO 2403 2404**Parameters** 2405 2406 | Name | Type | Mandatory | Description | 2407 | -------- | ------------------------- | ---- | --------------- | 2408 | fd | number | Yes | File descriptor of the target file. | 2409 | uid | number | Yes | New UID. | 2410 | gid | number | Yes | New GID. | 2411 | callback | AsyncCallback<void> | Yes | Callback invoked when the file owner is changed asynchronously.| 2412 2413**Example** 2414 2415 ```js 2416 let filePath = pathDir + "/test.txt"; 2417 let fd = fileio.openSync(filePath); 2418 let stat = fileio.statSync(filePath); 2419 fileio.fchown(fd, stat.uid, stat.gid, function (err) { 2420 // Do something. 2421 }); 2422 ``` 2423 2424 2425## fileio.fchownSync<sup>7+</sup> 2426 2427fchownSync(fd: number, uid: number, gid: number): void 2428 2429Synchronously changes the file owner based on the file descriptor. 2430 2431**System capability**: SystemCapability.FileManagement.File.FileIO 2432 2433**Parameters** 2434 2435 | Name | Type | Mandatory | Description | 2436 | ---- | ------ | ---- | ------------ | 2437 | fd | number | Yes | File descriptor of the target file.| 2438 | uid | number | Yes | New UID. | 2439 | gid | number | Yes | New GID. | 2440 2441**Example** 2442 2443 ```js 2444 let filePath = pathDir + "/test.txt"; 2445 let fd = fileio.openSync(filePath); 2446 let stat = fileio.statSync(filePath); 2447 fileio.fchownSync(fd, stat.uid, stat.gid); 2448 ``` 2449 2450 2451## fileio.lchown<sup>7+</sup> 2452 2453lchown(path: string, uid: number, gid: number): Promise<void> 2454 2455Changes the file owner (owner of the symbolic link, not the file referred to by the symbolic link) based on the file path. This API uses a promise to return the result. 2456 2457**System capability**: SystemCapability.FileManagement.File.FileIO 2458 2459**Parameters** 2460 2461| Name| Type | Mandatory| Description | 2462| ------ | ------ | ---- | -------------------------- | 2463| path | string | Yes | Application sandbox path of the file.| 2464| uid | number | Yes | New UID. | 2465| gid | number | Yes | New GID. | 2466 2467**Return value** 2468 2469 | Type | Description | 2470 | ------------------- | ---------------------------- | 2471 | Promise<void> | Promise that returns no value.| 2472 2473**Example** 2474 2475 ```js 2476 let filePath = pathDir + "/test.txt"; 2477 let stat = fileio.statSync(filePath); 2478 fileio.lchown(filePath, stat.uid, stat.gid).then(function () { 2479 console.info("File owner changed"); 2480 }).catch(function (err) { 2481 console.info("chown failed with error:" + err); 2482 }); 2483 ``` 2484 2485 2486## fileio.lchown<sup>7+</sup> 2487 2488lchown(path: string, uid: number, gid: number, callback: AsyncCallback<void>): void 2489 2490Changes the file owner (owner of the symbolic link, not the file referred to by the symbolic link) based on the file path. This API uses an asynchronous callback to return the result. 2491 2492**System capability**: SystemCapability.FileManagement.File.FileIO 2493 2494**Parameters** 2495 2496| Name | Type | Mandatory| Description | 2497| -------- | ------------------------- | ---- | ------------------------------ | 2498| path | string | Yes | Application sandbox path of the file. | 2499| uid | number | Yes | New UID. | 2500| gid | number | Yes | New GID. | 2501| callback | AsyncCallback<void> | Yes | Callback invoked when the file owner is changed asynchronously.| 2502 2503**Example** 2504 2505 ```js 2506 let filePath = pathDir + "/test.txt"; 2507 let stat = fileio.statSync(filePath); 2508 fileio.lchown(filePath, stat.uid, stat.gid, function (err) { 2509 // Do something. 2510 }); 2511 ``` 2512 2513 2514## fileio.lchownSync<sup>7+</sup> 2515 2516lchownSync(path: string, uid: number, gid: number): void 2517 2518Synchronously changes the file owner based on the file path and changes the owner of the symbolic link (not the referenced file). 2519 2520**System capability**: SystemCapability.FileManagement.File.FileIO 2521 2522**Parameters** 2523 2524| Name| Type | Mandatory| Description | 2525| ------ | ------ | ---- | -------------------------- | 2526| path | string | Yes | Application sandbox path of the file.| 2527| uid | number | Yes | New UID. | 2528| gid | number | Yes | New GID. | 2529 2530**Example** 2531 2532 ```js 2533 let filePath = pathDir + "/test.txt"; 2534 let stat = fileio.statSync(filePath); 2535 fileio.lchownSync(filePath, stat.uid, stat.gid); 2536 ``` 2537 2538 2539## fileio.createWatcher<sup>7+</sup> 2540 2541createWatcher(filename: string, events: number, callback: AsyncCallback<number>): Watcher 2542 2543Listens for file or directory changes. This API uses an asynchronous callback to return the result. 2544 2545**System capability**: SystemCapability.FileManagement.File.FileIO 2546 2547**Parameters** 2548 2549| Name | Type | Mandatory| Description | 2550| -------- | --------------------------------- | ---- | ------------------------------------------------------------ | 2551| filePath | string | Yes | Application sandbox path of the file. | 2552| events | number | Yes | -**1**: The file or directory is renamed.<br>- **2**: The file or directory is modified.<br>- **3**: The file or directory is modified and renamed.| 2553| callback | AsyncCallback<number> | Yes | Called each time a change is detected. | 2554 2555**Return value** 2556 2557 | Type | Description | 2558 | -------------------- | ---------- | 2559 | [Watcher](#watcher7) | Promise used to return the **Watcher** instance.| 2560 2561**Example** 2562 2563 ```js 2564 let filePath = pathDir +"/test.txt"; 2565 fileio.createWatcher(filePath, 1, function (number) { 2566 console.info("Monitoring times: " +number); 2567 }); 2568 2569 ``` 2570 2571 2572## Readout 2573 2574Obtains the file read result. This class applies only to the **read()** method. 2575 2576**System capability**: SystemCapability.FileManagement.File.FileIO 2577 2578| Name | Type | Readable | Writable | Description | 2579| --------- | ---------- | ---- | ---- | ----------------- | 2580| bytesRead | number | Yes | Yes | Length of the data read. | 2581| offset | number | Yes | Yes | Position of the buffer to which the data will be read in reference to the start address of the buffer.| 2582| buffer | ArrayBuffer | Yes | Yes | Buffer for storing the data read. | 2583 2584 2585## Stat 2586 2587Provides detailed file information. Before calling a method of the **Stat** class, use the [stat()](#fileiostat) method synchronously or asynchronously to create a **Stat** instance. 2588 2589**System capability**: SystemCapability.FileManagement.File.FileIO 2590 2591### Attributes 2592 2593| Name | Type | Readable | Writable | Description | 2594| ------ | ------ | ---- | ---- | ---------------------------------------- | 2595| dev | number | Yes | No | Major device number. | 2596| ino | number | Yes | No | File ID. Different files on the same device have different **ino**s. | 2597| mode | number | Yes | No | File type and permissions. The first four bits indicate the file type, and the last 12 bits indicate the permissions. The bit fields are described as follows:<br>- **0o170000**: mask used to obtain the file type.<br>- **0o140000**: The file is a socket.<br>- **0o120000**: The file is a symbolic link.<br>- **0o100000**: The file is a regular file.<br>- **0o060000**: The file is a block device.<br>- **0o040000**: The file is a directory.<br>- **0o020000**: The file is a character device.<br>- **0o010000**: The file is a named pipe, that is, FIFO.<br>- **0o0700**: mask used to obtain owner permissions.<br>- **0o0400**: The owner has the read permission on a regular file or a directory entry.<br>- **0o0200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>- **0o0100**: The owner has the permission to execute a regular file or has the permission to search for the specified path in a directory.<br>- **0o0070**: mask used to obtain user group permissions.<br>- **0o0040**: The user group has the read permission on a regular file or a directory entry.<br>- **0o0020**: The user group has the permission to write a regular file or has the permission to create and delete a directory entry.<br>- **0o0010**: The user group has the permission to execute a regular file or has the permission to search for the specified path in a directory.<br>- **0o0007**: mask used to obtain permissions of other users.<br>- **0o0004**: Other user groups have the read permission on a regular file or a directory entry.<br>- **0o0002**: Other user groups have the permission to write a regular file or have the permission to create and delete a directory entry.<br>- **0o0001**: Other users have the permission to execute a regular file or search for the specified path in a directory.| 2598| nlink | number | Yes | No | Number of hard links in the file. | 2599| uid | number | Yes | No | User ID, that is ID of the file owner. | 2600| gid | number | Yes | No | Group ID, that is, ID of the user group of the file. | 2601| rdev | number | Yes | No | Minor device number. | 2602| size | number | Yes | No | File size, in bytes. This parameter is valid only for regular files. | 2603| blocks | number | Yes | No | Number of blocks occupied by a file. Each block is 512 bytes. | 2604| atime | number | Yes | No | Time of the last access to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970. | 2605| mtime | number | Yes | No | Time of the last modification to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970. | 2606| ctime | number | Yes | No | Time of the last status change of the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970. | 2607 2608 2609### isBlockDevice 2610 2611isBlockDevice(): boolean 2612 2613Checks whether this file is a block special file. A block special file supports access by block only, and it is cached when accessed. 2614 2615**System capability**: SystemCapability.FileManagement.File.FileIO 2616 2617**Return value** 2618 2619 | Type | Description | 2620 | ------- | ---------------- | 2621 | boolean | Whether the file is a block special file.| 2622 2623**Example** 2624 2625 ```js 2626 let filePath = pathDir + "/test.txt"; 2627 let isBLockDevice = fileio.statSync(filePath).isBlockDevice(); 2628 ``` 2629 2630 2631### isCharacterDevice 2632 2633isCharacterDevice(): boolean 2634 2635Checks whether this file is a character special file. A character special file supports random access, and it is not cached when accessed. 2636 2637**System capability**: SystemCapability.FileManagement.File.FileIO 2638 2639**Return value** 2640 2641 | Type | Description | 2642 | ------- | ----------------- | 2643 | boolean | Whether the file is a character special file.| 2644 2645**Example** 2646 2647 ```js 2648 let filePath = pathDir + "/test.txt"; 2649 let isCharacterDevice = fileio.statSync(filePath).isCharacterDevice(); 2650 ``` 2651 2652 2653### isDirectory 2654 2655isDirectory(): boolean 2656 2657Checks whether this file is a directory. 2658 2659**System capability**: SystemCapability.FileManagement.File.FileIO 2660 2661**Return value** 2662 2663 | Type | Description | 2664 | ------- | ------------- | 2665 | boolean | Whether the file is a directory.| 2666 2667**Example** 2668 2669 ```js 2670 let dirPath = pathDir + "/test"; 2671 let isDirectory = fileio.statSync(dirPath).isDirectory(); 2672 ``` 2673 2674 2675### isFIFO 2676 2677isFIFO(): boolean 2678 2679Checks whether this file is a named pipe (or FIFO). Named pipes are used for inter-process communication. 2680 2681**System capability**: SystemCapability.FileManagement.File.FileIO 2682 2683**Return value** 2684 2685 | Type | Description | 2686 | ------- | --------------------- | 2687 | boolean | Whether the file is an FIFO.| 2688 2689**Example** 2690 2691 ```js 2692 let filePath = pathDir + "/test.txt"; 2693 let isFIFO = fileio.statSync(filePath).isFIFO(); 2694 ``` 2695 2696 2697### isFile 2698 2699isFile(): boolean 2700 2701Checks whether this file is a regular file. 2702 2703**System capability**: SystemCapability.FileManagement.File.FileIO 2704 2705**Return value** 2706 2707 | Type | Description | 2708 | ------- | --------------- | 2709 | boolean | Whether the file is a regular file.| 2710 2711**Example** 2712 2713 ```js 2714 let filePath = pathDir + "/test.txt"; 2715 let isFile = fileio.statSync(filePath).isFile(); 2716 ``` 2717 2718 2719### isSocket 2720 2721isSocket(): boolean 2722 2723Checks whether this file is a socket. 2724 2725**System capability**: SystemCapability.FileManagement.File.FileIO 2726 2727**Return value** 2728 2729 | Type | Description | 2730 | ------- | -------------- | 2731 | boolean | Whether the file is a socket.| 2732 2733**Example** 2734 2735 ```js 2736 let filePath = pathDir + "/test.txt"; 2737 let isSocket = fileio.statSync(filePath).isSocket(); 2738 ``` 2739 2740 2741### isSymbolicLink 2742 2743isSymbolicLink(): boolean 2744 2745Checks whether this file is a symbolic link. 2746 2747**System capability**: SystemCapability.FileManagement.File.FileIO 2748 2749**Return value** 2750 2751 | Type | Description | 2752 | ------- | --------------- | 2753 | boolean | Whether the file is a symbolic link.| 2754 2755**Example** 2756 2757 ```js 2758 let filePath = pathDir + "/test"; 2759 let isSymbolicLink = fileio.statSync(filePath).isSymbolicLink(); 2760 ``` 2761 2762 2763## Watcher<sup>7+</sup> 2764 2765Listens for the changes of a file. You can call the **Watcher.stop()** method synchronously or asynchronously to stop the listening. 2766 2767 2768### stop<sup>7+</sup> 2769 2770stop(): Promise<void> 2771 2772Stops the **watcher** instance. This API uses a promise to return the result. 2773 2774**System capability**: SystemCapability.FileManagement.File.FileIO 2775 2776**Example** 2777 2778 ```js 2779 let filePath = path + "/test.txt"; 2780 let watcher = fileio.createWatcher(filePath, 1, function (number) { 2781 console.info("Monitoring times: " +number); 2782 }); 2783 watcher.stop().then(function () { 2784 console.info("Watcher stopped"); 2785 }); 2786 ``` 2787 2788 2789### stop<sup>7+</sup> 2790 2791stop(callback: AsyncCallback<void>): void 2792 2793Stops the **watcher** instance. This API uses an asynchronous callback to return the result. 2794 2795**System capability**: SystemCapability.FileManagement.File.FileIO 2796 2797**Parameters** 2798 2799 | Name | Type | Mandatory | Description | 2800 | -------- | ------------------------- | ---- | ---------------------- | 2801 | callback | AsyncCallback<void> | Yes | Callback invoked when **watcher** is stopped asynchronously.| 2802 2803**Example** 2804 2805 ```js 2806 let filePath = path +"/test.txt"; 2807 let watcher = fileio.createWatcher(filePath, 1, function (number) { 2808 console.info("Monitoring times: " +number); 2809 }); 2810 watcher.stop(function () { 2811 console.info("Watcher stopped"); 2812 }) 2813 ``` 2814 2815 2816## Stream 2817 2818Provides file stream management. Before calling a method of the **Stream** class, use the **createStream()** method synchronously or asynchronously to create a **Stream** instance. 2819 2820 2821### close<sup>7+</sup> 2822 2823close(): Promise<void> 2824 2825Closes the stream. This API uses a promise to return the result. 2826 2827**System capability**: SystemCapability.FileManagement.File.FileIO 2828 2829**Return value** 2830 2831 | Type | Description | 2832 | ------------------- | ------------- | 2833 | Promise<void> | Promise used to return the stream close result.| 2834 2835**Example** 2836 2837 ```js 2838 let filePath = pathDir + "/test.txt"; 2839 let ss= fileio.createStreamSync(filePath, "r+"); 2840 ss.close().then(function () { 2841 console.info("File stream closed"); 2842 }).catch(function (err) { 2843 console.info("close fileStream failed with error:" + err); 2844 }); 2845 ``` 2846 2847 2848### close<sup>7+</sup> 2849 2850close(callback: AsyncCallback<void>): void 2851 2852Closes the stream. This API uses an asynchronous callback to return the result. 2853 2854**System capability**: SystemCapability.FileManagement.File.FileIO 2855 2856**Parameters** 2857 2858 | Name | Type | Mandatory | Description | 2859 | -------- | ------------------------- | ---- | ------------- | 2860 | callback | AsyncCallback<void> | Yes | Callback invoked when the stream is closed asynchronously.| 2861 2862**Example** 2863 2864 ```js 2865 let filePath = pathDir + "/test.txt"; 2866 let ss= fileio.createStreamSync(filePath, "r+"); 2867 ss.close(function (err) { 2868 // Do something. 2869 }); 2870 ``` 2871 2872 2873### closeSync 2874 2875closeSync(): void 2876 2877Synchronously closes the stream. 2878 2879**System capability**: SystemCapability.FileManagement.File.FileIO 2880 2881**Example** 2882 2883 ```js 2884 let filePath = pathDir + "/test.txt"; 2885 let ss= fileio.createStreamSync(filePath, "r+"); 2886 ss.closeSync(); 2887 ``` 2888 2889 2890### flush<sup>7+</sup> 2891 2892flush(): Promise<void> 2893 2894Flushes the stream. This API uses a promise to return the result. 2895 2896**System capability**: SystemCapability.FileManagement.File.FileIO 2897 2898**Return value** 2899 2900 | Type | Description | 2901 | ------------------- | ------------- | 2902 | Promise<void> | Promise used to return the stream flushing result.| 2903 2904**Example** 2905 2906 ```js 2907 let filePath = pathDir + "/test.txt"; 2908 let ss= fileio.createStreamSync(filePath, "r+"); 2909 ss.flush().then(function () { 2910 console.info("Stream flushed"); 2911 }).catch(function (err) { 2912 console.info("flush failed with error:" + err); 2913 }); 2914 ``` 2915 2916 2917### flush<sup>7+</sup> 2918 2919flush(callback: AsyncCallback<void>): void 2920 2921Flushes the stream. This API uses an asynchronous callback to return the result. 2922 2923**System capability**: SystemCapability.FileManagement.File.FileIO 2924 2925**Parameters** 2926 2927 | Name | Type | Mandatory | Description | 2928 | -------- | ------------------------- | ---- | -------------- | 2929 | callback | AsyncCallback<void> | Yes | Callback invoked when the stream is asynchronously flushed.| 2930 2931**Example** 2932 2933 ```js 2934 let filePath = pathDir + "/test.txt"; 2935 let ss= fileio.createStreamSync(filePath, "r+"); 2936 ss.flush(function (err) { 2937 // Do something. 2938 }); 2939 ``` 2940 2941 2942### flushSync<sup>7+</sup> 2943 2944flushSync(): void 2945 2946Synchronously flushes the stream. 2947 2948**System capability**: SystemCapability.FileManagement.File.FileIO 2949 2950**Example** 2951 2952 ```js 2953 let filePath = pathDir + "/test.txt"; 2954 let ss= fileio.createStreamSync(filePath, "r+"); 2955 ss.flushSync(); 2956 ``` 2957 2958 2959### write<sup>7+</sup> 2960 2961write(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise<number> 2962 2963Writes data into the stream. This API uses a promise to return the result. 2964 2965**System capability**: SystemCapability.FileManagement.File.FileIO 2966 2967**Parameters** 2968 2969 | Name | Type | Mandatory | Description | 2970 | ------- | ------------------------------- | ---- | ---------------------------------------- | 2971 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 2972 | options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.<br>Constraints: offset + length <= Buffer size | 2973 2974**Return value** 2975 2976 | Type | Description | 2977 | --------------------- | -------- | 2978 | Promise<number> | Promise used to return the length of the data written.| 2979 2980**Example** 2981 2982 ```js 2983 let filePath = pathDir + "/test.txt"; 2984 let ss= fileio.createStreamSync(filePath, "r+"); 2985 ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number) { 2986 console.info("write succeed and size is:" + number); 2987 }).catch(function (err) { 2988 console.info("write failed with error:" + err); 2989 }); 2990 ``` 2991 2992 2993### write<sup>7+</sup> 2994 2995write(buffer: ArrayBuffer|string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback<number>): void 2996 2997Writes data into the stream. This API uses an asynchronous callback to return the result. 2998 2999**System capability**: SystemCapability.FileManagement.File.FileIO 3000 3001**Parameters** 3002 3003 | Name | Type | Mandatory| Description | 3004 | -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 3005 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 3006 | options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.<br>Constraints: offset + length <= Buffer size| 3007 | callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | 3008 3009**Example** 3010 3011 ```js 3012 let filePath = pathDir + "/test.txt"; 3013 let ss= fileio.createStreamSync(filePath, "r+"); 3014 ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) { 3015 if (bytesWritten) { 3016 // Do something. 3017 console.info("write succeed and size is:" + bytesWritten); 3018 } 3019 }); 3020 ``` 3021 3022 3023### writeSync<sup>7+</sup> 3024 3025writeSync(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number 3026 3027Synchronously writes data into the stream. 3028 3029**System capability**: SystemCapability.FileManagement.File.FileIO 3030 3031**Parameters** 3032 3033 | Name | Type | Mandatory | Description | 3034 | ------- | ------------------------------- | ---- | ---------------------------------------- | 3035 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 3036 | options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>- **length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>- **position** (number): start position to write the data in the file. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.<br>Constraints: offset + length <= Buffer size | 3037 3038**Return value** 3039 3040 | Type | Description | 3041 | ------ | -------- | 3042 | number | Length of the data written in the file.| 3043 3044**Example** 3045 3046 ```js 3047 let filePath = pathDir + "/test.txt"; 3048 let ss= fileio.createStreamSync(filePath,"r+"); 3049 let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}); 3050 ``` 3051 3052 3053### read<sup>7+</sup> 3054 3055read(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): Promise<ReadOut> 3056 3057Reads data from the stream. This API uses a promise to return the result. 3058 3059**System capability**: SystemCapability.FileManagement.File.FileIO 3060 3061**Parameters** 3062 3063 | Name | Type | Mandatory | Description | 3064 | ------- | ----------- | ---- | ---------------------------------------- | 3065 | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 3066 | options | Object | No | The options are as follows:<br>- **offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size | 3067 3068**Return value** 3069 3070 | Type | Description | 3071 | ---------------------------------- | ------ | 3072 | Promise<[ReadOut](#readout)> | Promise used to return the data read.| 3073 3074**Example** 3075 3076 ```js 3077 let filePath = pathDir + "/test.txt"; 3078 let ss = fileio.createStreamSync(filePath, "r+"); 3079 ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut) { 3080 console.info("Read data successfully"); 3081 console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); 3082 }).catch(function (err) { 3083 console.info("read data failed with error:" + err); 3084 }); 3085 ``` 3086 3087 3088### read<sup>7+</sup> 3089 3090read(buffer: ArrayBuffer, options: { position?: number; offset?: number; length?: number; }, callback: AsyncCallback<ReadOut>): void 3091 3092Reads data from the stream. This API uses an asynchronous callback to return the result. 3093 3094**System capability**: SystemCapability.FileManagement.File.FileIO 3095 3096**Parameters** 3097 3098 | Name | Type | Mandatory | Description | 3099 | -------- | ---------------------------------------- | ---- | ---------------------------------------- | 3100 | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 3101 | options | Object | No | The options are as follows:<br>- **offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size | 3102 | callback | AsyncCallback<[ReadOut](#readout)> | Yes | Callback invoked when data is read asynchronously from the stream. | 3103 3104**Example** 3105 3106 ```js 3107 let filePath = pathDir + "/test.txt"; 3108 let ss = fileio.createStreamSync(filePath, "r+"); 3109 ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) { 3110 if (readOut) { 3111 console.info("Read data successfully"); 3112 console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); 3113 } 3114 }); 3115 ``` 3116 3117 3118### readSync<sup>7+</sup> 3119 3120readSync(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): number 3121 3122Synchronously reads data from the stream. 3123 3124**System capability**: SystemCapability.FileManagement.File.FileIO 3125 3126**Parameters** 3127 3128 | Name | Type | Mandatory | Description | 3129 | ------- | ----------- | ---- | ---------------------------------------- | 3130 | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 3131 | options | Object | No | The options are as follows:<br>- **offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size | 3132 3133**Return value** 3134 3135 | Type | Description | 3136 | ------ | -------- | 3137 | number | Length of the data read.| 3138 3139**Example** 3140 3141 ```js 3142 let filePath = pathDir + "/test.txt"; 3143 let ss = fileio.createStreamSync(filePath, "r+"); 3144 let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}); 3145 ``` 3146 3147 3148## Dir 3149 3150Manages directories. Before calling a method of the **Dir** class, use the **opendir()** method synchronously or asynchronously to create a **Dir** instance. 3151 3152 3153### read 3154 3155read(): Promise<Dirent> 3156 3157Reads the next directory entry. This API uses a promise to return the result. 3158 3159**System capability**: SystemCapability.FileManagement.File.FileIO 3160 3161**Return value** 3162 3163 | Type | Description | 3164 | -------------------------------- | ------------- | 3165 | Promise<[Dirent](#dirent)> | Promise used to return the directory entry read.| 3166 3167**Example** 3168 3169 ```js 3170 dir.read().then(function (dirent) { 3171 console.log("read succeed, the name of dirent is " + dirent.name); 3172 }).catch(function (err) { 3173 console.info("read failed with error:" + err); 3174 }); 3175 ``` 3176 3177 3178### read 3179 3180read(callback: AsyncCallback<Dirent>): void 3181 3182Reads the next directory entry. This API uses an asynchronous callback to return the result. 3183 3184**System capability**: SystemCapability.FileManagement.File.FileIO 3185 3186**Parameters** 3187 3188 | Name | Type | Mandatory | Description | 3189 | -------- | -------------------------------------- | ---- | ---------------- | 3190 | callback | AsyncCallback<[Dirent](#dirent)> | Yes | Callback invoked when the next directory entry is asynchronously read.| 3191 3192**Example** 3193 3194 ```js 3195 dir.read(function (err, dirent) { 3196 if (dirent) { 3197 // Do something. 3198 console.log("read succeed, the name of file is " + dirent.name); 3199 } 3200 }); 3201 ``` 3202 3203 3204### readSync 3205 3206readSync(): Dirent 3207 3208Synchronously reads the next directory entry. 3209 3210**System capability**: SystemCapability.FileManagement.File.FileIO 3211 3212**Return value** 3213 3214 | Type | Description | 3215 | ----------------- | -------- | 3216 | [Dirent](#dirent) | Directory entry read.| 3217 3218**Example** 3219 3220 ```js 3221 let dirent = dir.readSync(); 3222 ``` 3223 3224 3225### close<sup>7+</sup> 3226 3227close(): Promise<void> 3228 3229Closes a directory. This API uses a promise to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir. 3230 3231**System capability**: SystemCapability.FileManagement.File.FileIO 3232 3233**Example** 3234 3235 ```js 3236 dir.close().then(function (err) { 3237 console.info("close dir successfully"); 3238 }); 3239 ``` 3240 3241 3242 ### close<sup>7+</sup> 3243 3244close(callback: AsyncCallback<void>): void 3245 3246Closes a directory. This API uses an asynchronous callback to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir. 3247 3248**System capability**: SystemCapability.FileManagement.File.FileIO 3249 3250**Example** 3251 3252 ```js 3253 dir.close(function (err) { 3254 console.info("close dir successfully"); 3255 }); 3256 ``` 3257 3258 3259### closeSync 3260 3261closeSync(): void 3262 3263Closes a directory. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir. 3264 3265**System capability**: SystemCapability.FileManagement.File.FileIO 3266 3267**Example** 3268 3269 ```js 3270 dir.closeSync(); 3271 ``` 3272 3273 3274## Dirent 3275 3276Provides information about files and directories. Before calling a method of the **Dirent** class, use the [dir.read()](#read) method synchronously or asynchronously to create a **Dirent** instance. 3277 3278**System capability**: SystemCapability.FileManagement.File.FileIO 3279 3280### Attributes 3281 3282| Name | Type | Readable | Writable | Description | 3283| ---- | ------ | ---- | ---- | ------- | 3284| name | string | Yes | No | Directory entry name.| 3285 3286 3287### isBlockDevice 3288 3289isBlockDevice(): boolean 3290 3291Checks whether this directory entry is a block special file. A block special file supports access by block only, and it is cached when accessed. 3292 3293**System capability**: SystemCapability.FileManagement.File.FileIO 3294 3295**Return value** 3296 3297 | Type | Description | 3298 | ------- | ---------------- | 3299 | boolean | Whether the directory entry is a block special file.| 3300 3301**Example** 3302 3303 ```js 3304 let dir = fileio.opendirSync(pathDir); 3305 let isBLockDevice = dir.readSync().isBlockDevice(); 3306 ``` 3307 3308 3309### isCharacterDevice 3310 3311isCharacterDevice(): boolean 3312 3313Checks whether a directory entry is a character special file. A character special file supports random access, and it is not cached when accessed. 3314 3315**System capability**: SystemCapability.FileManagement.File.FileIO 3316 3317**Return value** 3318 3319 | Type | Description | 3320 | ------- | ----------------- | 3321 | boolean | Whether the directory entry is a character special file.| 3322 3323**Example** 3324 3325 ```js 3326 let dir = fileio.opendirSync(pathDir); 3327 let isCharacterDevice = dir.readSync().isCharacterDevice(); 3328 ``` 3329 3330 3331### isDirectory 3332 3333isDirectory(): boolean 3334 3335Checks whether a directory entry is a directory. 3336 3337**System capability**: SystemCapability.FileManagement.File.FileIO 3338 3339**Return value** 3340 3341 | Type | Description | 3342 | ------- | ------------- | 3343 | boolean | Whether the directory entry is a directory.| 3344 3345**Example** 3346 3347 ```js 3348 let dir = fileio.opendirSync(pathDir); 3349 let isDirectory = dir.readSync().isDirectory(); 3350 ``` 3351 3352 3353### isFIFO 3354 3355isFIFO(): boolean 3356 3357Checks whether this directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication. 3358 3359**System capability**: SystemCapability.FileManagement.File.FileIO 3360 3361**Return value** 3362 3363 | Type | Description | 3364 | ------- | --------------- | 3365 | boolean | Whether the directory entry is a FIFO.| 3366 3367**Example** 3368 3369 ```js 3370 let dir = fileio.opendirSync(pathDir); 3371 let isFIFO = dir.readSync().isFIFO(); 3372 ``` 3373 3374 3375### isFile 3376 3377isFile(): boolean 3378 3379Checks whether a directory entry is a regular file. 3380 3381**System capability**: SystemCapability.FileManagement.File.FileIO 3382 3383**Return value** 3384 3385 | Type | Description | 3386 | ------- | --------------- | 3387 | boolean | Whether the directory entry is a regular file.| 3388 3389**Example** 3390 3391 ```js 3392 let dir = fileio.opendirSync(pathDir); 3393 let isFile = dir.readSync().isFile(); 3394 ``` 3395 3396 3397### isSocket 3398 3399isSocket(): boolean 3400 3401Checks whether a directory entry is a socket. 3402 3403**System capability**: SystemCapability.FileManagement.File.FileIO 3404 3405**Return value** 3406 3407 | Type | Description | 3408 | ------- | -------------- | 3409 | boolean | Whether the directory entry is a socket.| 3410 3411**Example** 3412 3413 ```js 3414 let dir = fileio.opendirSync(pathDir); 3415 let isSocket = dir.readSync().isSocket(); 3416 ``` 3417 3418 3419### isSymbolicLink 3420 3421isSymbolicLink(): boolean 3422 3423Checks whether a directory entry is a symbolic link. 3424 3425**System capability**: SystemCapability.FileManagement.File.FileIO 3426 3427**Return value** 3428 3429 | Type | Description | 3430 | ------- | --------------- | 3431 | boolean | Whether the directory entry is a symbolic link.| 3432 3433**Example** 3434 3435 ```js 3436 let dir = fileio.opendirSync(pathDir); 3437 let isSymbolicLink = dir.readSync().isSymbolicLink(); 3438 ``` 3439