1# @ohos.file.fs (File Management) 2 3The **fs** module provides APIs for file operations, 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 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - The APIs of this module support processing of error codes. For details, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 9 10## Modules to Import 11 12```js 13import fs from '@ohos.file.fs'; 14``` 15 16## Guidelines 17 18Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the file or directory in the application sandbox as follows: 19 20**Stage Model** 21 22 ```js 23import UIAbility from '@ohos.app.ability.UIAbility'; 24 25export default class EntryAbility extends UIAbility { 26 onWindowStageCreate(windowStage) { 27 let context = this.context; 28 let pathDir = context.filesDir; 29 } 30} 31 ``` 32 33**FA Model** 34 35 ```js 36 import featureAbility from '@ohos.ability.featureAbility'; 37 38 let context = featureAbility.getContext(); 39 context.getFilesDir().then((data) => { 40 let pathDir = data; 41 }) 42 ``` 43 44For details about how to obtain the FA model context, see [Context](js-apis-inner-app-context.md#context). 45 46## fs.stat 47 48stat(file: string|number): Promise<Stat> 49 50Obtains detailed file information. This API uses a promise to return the result. 51 52**System capability**: SystemCapability.FileManagement.File.FileIO 53 54**Parameters** 55 56| Name| Type | Mandatory| Description | 57| ------ | ------ | ---- | -------------------------- | 58| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file.| 59 60**Return value** 61 62 | Type | Description | 63 | ---------------------------- | ---------- | 64 | Promise<[Stat](#stat)> | Promise used to return the file information obtained.| 65 66**Example** 67 68 ```js 69 let filePath = pathDir + "/test.txt"; 70 fs.stat(filePath).then((stat) => { 71 console.info("get file info succeed, the size of file is " + stat.size); 72 }).catch((err) => { 73 console.info("get file info failed with error message: " + err.message + ", error code: " + err.code); 74 }); 75 ``` 76 77## fs.stat 78 79stat(file: string|number, callback: AsyncCallback<Stat>): void 80 81Obtains detailed file information. This API uses an asynchronous callback to return the result. 82 83**System capability**: SystemCapability.FileManagement.File.FileIO 84 85**Parameters** 86 87| Name | Type | Mandatory| Description | 88| -------- | ---------------------------------- | ---- | ------------------------------ | 89| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file. | 90| callback | AsyncCallback<[Stat](#stat)> | Yes | Callback invoked to return the file information obtained.| 91 92**Example** 93 94 ```js 95 fs.stat(pathDir, (err, stat) => { 96 if (err) { 97 console.info("get file info failed with error message: " + err.message + ", error code: " + err.code); 98 } else { 99 console.info("get file info succeed, the size of file is " + stat.size); 100 } 101 }); 102 ``` 103 104## fs.statSync 105 106statSync(file: string|number): Stat 107 108Obtains detailed file information synchronously. 109 110**System capability**: SystemCapability.FileManagement.File.FileIO 111 112**Parameters** 113 114| Name| Type | Mandatory| Description | 115| ------ | ------ | ---- | -------------------------- | 116| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) 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 = fs.statSync(pathDir); 129 console.info("get file info succeed, the size of file is " + stat.size); 130 ``` 131 132## fs.access 133 134access(path: string): Promise<boolean> 135 136Checks whether a file exists. This API uses a promise to return the result. 137 138**System capability**: SystemCapability.FileManagement.File.FileIO 139 140**Parameters** 141 142| Name| Type | Mandatory| Description | 143| ------ | ------ | ---- | ------------------------------------------------------------ | 144| path | string | Yes | Path of the file in the application sandbox. | 145 146**Return value** 147 148 | Type | Description | 149 | ------------------- | ---------------------------- | 150 | Promise<boolean> | Promise used to return a Boolean value.| 151 152**Example** 153 154 ```js 155 let filePath = pathDir + "/test.txt"; 156 fs.access(filePath).then((res) => { 157 if (res) { 158 console.info("file exists"); 159 } 160 }).catch((err) => { 161 console.info("access failed with error message: " + err.message + ", error code: " + err.code); 162 }); 163 ``` 164 165 166## fs.access 167 168access(path: string, callback: AsyncCallback<boolean>): void 169 170Checks whether a file exists. This API uses an asynchronous callback to return the result. 171 172**System capability**: SystemCapability.FileManagement.File.FileIO 173 174**Parameters** 175 176| Name | Type | Mandatory| Description | 177| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 178| path | string | Yes | Path of the file in the application sandbox. | 179| callback | AsyncCallback<boolean> | Yes | Callback invoked to return the result. | 180 181**Example** 182 183 ```js 184 let filePath = pathDir + "/test.txt"; 185 fs.access(filePath, (err, res) => { 186 if (err) { 187 console.info("access failed with error message: " + err.message + ", error code: " + err.code); 188 } else { 189 if (res) { 190 console.info("file exists"); 191 } 192 } 193 }); 194 ``` 195 196## fs.accessSync 197 198accessSync(path: string): boolean 199 200Synchronously checks whether a file exists. 201 202**System capability**: SystemCapability.FileManagement.File.FileIO 203 204**Parameters** 205 206| Name| Type | Mandatory| Description | 207| ------ | ------ | ---- | ------------------------------------------------------------ | 208| path | string | Yes | Path of the file in the application sandbox. | 209 210**Example** 211 212 ```js 213 let filePath = pathDir + "/test.txt"; 214 try { 215 let res = fs.accessSync(filePath); 216 if (res) { 217 console.info("file exists"); 218 } 219 } catch(err) { 220 console.info("accessSync failed with error message: " + err.message + ", error code: " + err.code); 221 } 222 ``` 223 224 225## fs.close 226 227close(file: File|number): Promise<void> 228 229Closes a file. This API uses a promise to return the result. 230 231**System capability**: SystemCapability.FileManagement.File.FileIO 232 233**Parameters** 234 235 | Name | Type | Mandatory | Description | 236 | ---- | ------ | ---- | ------------ | 237 | file | [File](#file)\|number | Yes | File object or FD of the file to close.| 238 239**Return value** 240 241 | Type | Description | 242 | ------------------- | ---------------------------- | 243 | Promise<void> | Promise that returns no value.| 244 245**Example** 246 247 ```js 248 let filePath = pathDir + "/test.txt"; 249 let file = fs.openSync(filePath); 250 fs.close(file).then(() => { 251 console.info("File closed"); 252 fs.closeSync(file); 253 }).catch((err) => { 254 console.info("close file failed with error message: " + err.message + ", error code: " + err.code); 255 }); 256 ``` 257 258## fs.close 259 260close(file: File|number, callback: AsyncCallback<void>): void 261 262Closes a file. This API uses an asynchronous callback to return the result. 263 264**System capability**: SystemCapability.FileManagement.File.FileIO 265 266**Parameters** 267 268 | Name | Type | Mandatory | Description | 269 | -------- | ------------------------- | ---- | ------------ | 270 | file | [File](#file)\|number | Yes | File object or FD of the file to close.| 271 | callback | AsyncCallback<void> | Yes | Callback invoked when the file is closed asynchronously.| 272 273**Example** 274 275 ```js 276 let filePath = pathDir + "/test.txt"; 277 let file = fs.openSync(filePath); 278 fs.close(file, (err) => { 279 if (err) { 280 console.info("close file failed with error message: " + err.message + ", error code: " + err.code); 281 } else { 282 console.info("close file success"); 283 } 284 }); 285 ``` 286 287## fs.closeSync 288 289closeSync(file: File|number): void 290 291Synchronously closes a file. 292 293**System capability**: SystemCapability.FileManagement.File.FileIO 294 295**Parameters** 296 297 | Name | Type | Mandatory | Description | 298 | ---- | ------ | ---- | ------------ | 299 | file | [File](#file)\|number | Yes | File object or FD of the file to close.| 300 301**Example** 302 303 ```js 304 let filePath = pathDir + "/test.txt"; 305 let file = fs.openSync(filePath); 306 fs.closeSync(file); 307 ``` 308 309## fs.copyFile 310 311copyFile(src: string|number, dest: string|number, mode?: number): Promise<void> 312 313Copies a file. This API uses a promise to return the result. 314 315**System capability**: SystemCapability.FileManagement.File.FileIO 316 317**Parameters** 318 319 | Name | Type | Mandatory | Description | 320 | ---- | -------------------------- | ---- | ---------------------------------------- | 321 | src | string\|number | Yes | Path or FD of the file to copy. | 322 | dest | string\|number | Yes | Destination path of the file or FD of the file created. | 323 | mode | number | No | Whether to overwrite the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: overwrite the file of the same name.| 324 325**Return value** 326 327 | Type | Description | 328 | ------------------- | ---------------------------- | 329 | Promise<void> | Promise that returns no value.| 330 331**Example** 332 333 ```js 334 let srcPath = pathDir + "/srcDir/test.txt"; 335 let dstPath = pathDir + "/dstDir/test.txt"; 336 fs.copyFile(srcPath, dstPath).then(() => { 337 console.info("copy file succeed"); 338 }).catch((err) => { 339 console.info("copy file failed with error message: " + err.message + ", error code: " + err.code); 340 }); 341 ``` 342 343## fs.copyFile 344 345copyFile(src: string|number, dest: string|number, mode?: number, callback: AsyncCallback<void>): void 346 347Copies a file. This API uses an asynchronous callback to return the result. 348 349**System capability**: SystemCapability.FileManagement.File.FileIO 350 351**Parameters** 352 353 | Name | Type | Mandatory | Description | 354 | -------- | -------------------------- | ---- | ---------------------------------------- | 355 | src | string\|number | Yes | Path or FD of the file to copy. | 356 | dest | string\|number | Yes | Destination path of the file or FD of the file created. | 357 | mode | number | No | Whether to overwrite the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: overwrite the file with the same name and truncate the part that is not overwritten.| 358 | callback | AsyncCallback<void> | Yes | Callback invoked when the file is copied asynchronously. | 359 360**Example** 361 362 ```js 363 let srcPath = pathDir + "/srcDir/test.txt"; 364 let dstPath = pathDir + "/dstDir/test.txt"; 365 fs.copyFile(srcPath, dstPath, (err) => { 366 if (err) { 367 console.info("copy file failed with error message: " + err.message + ", error code: " + err.code); 368 } else { 369 console.info("copy file success"); 370 } 371 }); 372 ``` 373 374 375## fs.copyFileSync 376 377copyFileSync(src: string|number, dest: string|number, mode?: number): void 378 379Synchronously copies a file. 380 381**System capability**: SystemCapability.FileManagement.File.FileIO 382 383**Parameters** 384 385 | Name | Type | Mandatory | Description | 386 | ---- | -------------------------- | ---- | ---------------------------------------- | 387 | src | string\|number | Yes | Path or FD of the file to copy. | 388 | dest | string\|number | Yes | Destination path of the file or FD of the file created. | 389 | mode | number | No | Whether to overwrite the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: overwrite the file with the same name and truncate the part that is not overwritten.| 390 391**Example** 392 393 ```js 394 let srcPath = pathDir + "/srcDir/test.txt"; 395 let dstPath = pathDir + "/dstDir/test.txt"; 396 fs.copyFileSync(srcPath, dstPath); 397 ``` 398 399 400## fs.mkdir 401 402mkdir(path: string): Promise<void> 403 404Creates a directory. This API uses a promise to return the result. 405 406**System capability**: SystemCapability.FileManagement.File.FileIO 407 408**Parameters** 409 410| Name| Type | Mandatory| Description | 411| ------ | ------ | ---- | ------------------------------------------------------------ | 412| path | string | Yes | Path of the directory in the application sandbox. | 413 414**Return value** 415 416 | Type | Description | 417 | ------------------- | ---------------------------- | 418 | Promise<void> | Promise that returns no value.| 419 420**Example** 421 422 ```js 423 let dirPath = pathDir + "/testDir"; 424 fs.mkdir(dirPath).then(() => { 425 console.info("Directory created"); 426 }).catch((err) => { 427 console.info("mkdir failed with error message: " + err.message + ", error code: " + err.code); 428 }); 429 ``` 430 431 432## fs.mkdir 433 434mkdir(path: string, callback: AsyncCallback<void>): void 435 436Creates a directory. This API uses an asynchronous callback to return the result. 437 438**System capability**: SystemCapability.FileManagement.File.FileIO 439 440**Parameters** 441 442| Name | Type | Mandatory| Description | 443| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 444| path | string | Yes | Path of the directory in the application sandbox. | 445| callback | AsyncCallback<void> | Yes | Callback invoked when the directory is created asynchronously. | 446 447**Example** 448 449 ```js 450 let dirPath = pathDir + "/testDir"; 451 fs.mkdir(dirPath, (err) => { 452 if (err) { 453 console.info("mkdir failed with error message: " + err.message + ", error code: " + err.code); 454 } else { 455 console.info("mkdir success"); 456 } 457 }); 458 ``` 459 460 461## fs.mkdirSync 462 463mkdirSync(path: string): void 464 465Synchronously creates a directory. 466 467**System capability**: SystemCapability.FileManagement.File.FileIO 468 469**Parameters** 470 471| Name| Type | Mandatory| Description | 472| ------ | ------ | ---- | ------------------------------------------------------------ | 473| path | string | Yes | Path of the directory in the application sandbox. | 474 475**Example** 476 477 ```js 478 let dirPath = pathDir + "/testDir"; 479 fs.mkdirSync(dirPath); 480 ``` 481 482 483## fs.open 484 485open(path: string, mode?: number): Promise<File> 486 487Opens a file. This API uses a promise to return the result. File uniform resource identifiers (URIs) are supported. 488 489**System capability**: SystemCapability.FileManagement.File.FileIO 490 491**Parameters** 492 493| Name| Type | Mandatory| Description | 494| ------ | ------ | ---- | ------------------------------------------------------------ | 495| path | string | Yes | Path of the file in the application sandbox or URI of the file. | 496| mode | number | No | [Mode](#openmode) for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.<br>- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.| 497 498**Return value** 499 500 | Type | Description | 501 | --------------------- | ----------- | 502 | Promise<[File](#file)> | Promise used to return the file object.| 503 504**Example** 505 506 ```js 507 let filePath = pathDir + "/test.txt"; 508 fs.open(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).then((file) => { 509 console.info("file fd: " + file.fd); 510 }).catch((err) => { 511 console.info("open file failed with error message: " + err.message + ", error code: " + err.code); 512 }); 513 ``` 514 515 516## fs.open 517 518open(path: string, mode?: number, callback: AsyncCallback<File>): void 519 520Opens a file. This API uses an asynchronous callback to return the result. File URIs are supported. 521 522**System capability**: SystemCapability.FileManagement.File.FileIO 523 524**Parameters** 525 526| Name | Type | Mandatory| Description | 527| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 528| path | string | Yes | Path of the file in the application sandbox or URI of the file. | 529| mode | number | No | [Mode](#openmode) for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.<br>- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.| 530 531**Example** 532 533 ```js 534 let filePath = pathDir + "/test.txt"; 535 fs.open(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE, (err, file) => { 536 if (err) { 537 console.info("mkdir failed with error message: " + err.message + ", error code: " + err.code); 538 } else { 539 console.info("file fd: " + file.fd); 540 } 541 }); 542 ``` 543 544## fs.openSync 545 546openSync(path: string, mode?: number): File 547 548Synchronously opens a file. File URIs are supported. 549 550**System capability**: SystemCapability.FileManagement.File.FileIO 551 552**Parameters** 553 554| Name| Type | Mandatory| Description | 555| ------ | ------ | ---- | ------------------------------------------------------------ | 556| path | string | Yes | Path of the file in the application sandbox or URI of the file. | 557| mode | number | No | [Mode](#openmode) for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **OpenMode.READ_ONLY(0o0)**: Open the file in read-only mode.<br>- **OpenMode.WRITE_ONLY(0o1)**: Open the file in write-only mode.<br>- **OpenMode.READ_WRITE(0o2)**: Open the file in read/write mode.<br>You can also specify the following options, separated by a bitwise OR operator (|). By default, no additional options are given.<br>- **OpenMode.CREATE(0o100)**: If the file does not exist, create it.<br>- **OpenMode.TRUNC(0o1000)**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **OpenMode.APPEND(0o2000)**: Open the file in append mode. New data will be added to the end of the file.<br>- **OpenMode.NONBLOCK(0o4000)**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>- **OpenMode.DIR(0o200000)**: If **path** does not point to a directory, throw an exception.<br>- **OpenMode.NOFOLLOW(0o400000)**: If **path** points to a symbolic link, throw an exception.<br>- **OpenMode.SYNC(0o4010000)**: Open the file in synchronous I/O mode.| 558 559**Return value** 560 561 | Type | Description | 562 | ------ | ----------- | 563 | [File](#file) | File object opened.| 564 565**Example** 566 567 ```js 568 let filePath = pathDir + "/test.txt"; 569 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 570 console.info("file fd: " + file.fd); 571 fs.closeSync(file); 572 ``` 573 574## fs.read 575 576read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): Promise<number> 577 578Reads data from a file. This API uses a promise to return the result. 579 580**System capability**: SystemCapability.FileManagement.File.FileIO 581 582**Parameters** 583 584| Name | Type | Mandatory| Description | 585| ------- | ----------- | ---- | ------------------------------------------------------------ | 586| fd | number | Yes | FD of the file. | 587| buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | 588| options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| 589 590**Return value** 591 592 | Type | Description | 593 | ---------------------------------- | ------ | 594 | Promise<number> | Promise used to return the data read.| 595 596**Example** 597 598 ```js 599 let filePath = pathDir + "/test.txt"; 600 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 601 let buf = new ArrayBuffer(4096); 602 fs.read(file.fd, buf).then((readLen) => { 603 console.info("Read file data successfully"); 604 console.info(String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen)))); 605 fs.closeSync(file); 606 }).catch((err) => { 607 console.info("read file data failed with error message: " + err.message + ", error code: " + err.code); 608 }); 609 ``` 610 611## fs.read 612 613read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }, callback: AsyncCallback<number>): void 614 615Reads data from a file. This API uses an asynchronous callback to return the result. 616 617**System capability**: SystemCapability.FileManagement.File.FileIO 618 619**Parameters** 620 621 | Name | Type | Mandatory | Description | 622 | -------- | ---------------------------------------- | ---- | ---------------------------------------- | 623 | fd | number | Yes | FD of the file. | 624 | buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | 625 | options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| 626 | callback | AsyncCallback<number> | Yes | Callback invoked when the data is read asynchronously. | 627 628**Example** 629 630 ```js 631 let filePath = pathDir + "/test.txt"; 632 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 633 let buf = new ArrayBuffer(4096); 634 fs.read(file.fd, buf, (err, readLen) => { 635 if (err) { 636 console.info("mkdir failed with error message: " + err.message + ", error code: " + err.code); 637 } else { 638 console.info("Read file data successfully"); 639 console.info(String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen)))); 640 fs.closeSync(file); 641 } 642 }); 643 ``` 644 645 646## fs.readSync 647 648readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number 649 650Synchronously reads data from a file. 651 652**System capability**: SystemCapability.FileManagement.File.FileIO 653 654**Parameters** 655 656 | Name | Type | Mandatory | Description | 657 | ------- | ----------- | ---- | ---------------------------------------- | 658 | fd | number | Yes | FD of the file. | 659 | buffer | ArrayBuffer | Yes | Buffer used to store the file data read. | 660 | options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.| 661 662**Return value** 663 664 | Type | Description | 665 | ------ | -------- | 666 | number | Length of the data read.| 667 668**Example** 669 670 ```js 671 let filePath = pathDir + "/test.txt"; 672 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE); 673 let buf = new ArrayBuffer(4096); 674 let num = fs.readSync(file.fd, buf); 675 fs.closeSync(file); 676 ``` 677 678 679## fs.rmdir 680 681rmdir(path: string): Promise<void> 682 683Deletes a directory. This API uses a promise to return the result. 684 685**System capability**: SystemCapability.FileManagement.File.FileIO 686 687**Parameters** 688 689| Name| Type | Mandatory| Description | 690| ------ | ------ | ---- | -------------------------- | 691| path | string | Yes | Path of the directory in the application sandbox.| 692 693**Return value** 694 695 | Type | Description | 696 | ------------------- | ---------------------------- | 697 | Promise<void> | Promise that returns no value.| 698 699**Example** 700 701 ```js 702 let dirPath = pathDir + "/testDir"; 703 fs.rmdir(dirPath).then(() => { 704 console.info("Directory deleted"); 705 }).catch((err) => { 706 console.info("rmdir failed with error message: " + err.message + ", error code: " + err.code); 707 }); 708 ``` 709 710 711## fs.rmdir 712 713rmdir(path: string, callback: AsyncCallback<void>): void 714 715Deletes a directory. This API uses an asynchronous callback to return the result. 716 717**System capability**: SystemCapability.FileManagement.File.FileIO 718 719**Parameters** 720 721| Name | Type | Mandatory| Description | 722| -------- | ------------------------- | ---- | -------------------------- | 723| path | string | Yes | Path of the directory in the application sandbox.| 724| callback | AsyncCallback<void> | Yes | Callback invoked when the directory is deleted asynchronously. | 725 726**Example** 727 728 ```js 729 let dirPath = pathDir + "/testDir"; 730 fs.rmdir(dirPath, (err) => { 731 if (err) { 732 console.info("rmdir failed with error message: " + err.message + ", error code: " + err.code); 733 } else { 734 console.info("Directory deleted"); 735 } 736 }); 737 ``` 738 739 740## fs.rmdirSync 741 742rmdirSync(path: string): void 743 744Synchronously deletes a directory. 745 746**System capability**: SystemCapability.FileManagement.File.FileIO 747 748**Parameters** 749 750| Name| Type | Mandatory| Description | 751| ------ | ------ | ---- | -------------------------- | 752| path | string | Yes | Path of the directory in the application sandbox.| 753 754**Example** 755 756 ```js 757 let dirPath = pathDir + "/testDir"; 758 fs.rmdirSync(dirPath); 759 ``` 760 761 762## fs.unlink 763 764unlink(path: string): Promise<void> 765 766Deletes a file. This API uses a promise to return the result. 767 768**System capability**: SystemCapability.FileManagement.File.FileIO 769 770**Parameters** 771 772| Name| Type | Mandatory| Description | 773| ------ | ------ | ---- | -------------------------- | 774| path | string | Yes | Path of the file in the application sandbox.| 775 776**Return value** 777 778 | Type | Description | 779 | ------------------- | ---------------------------- | 780 | Promise<void> | Promise that returns no value.| 781 782**Example** 783 784 ```js 785 let filePath = pathDir + "/test.txt"; 786 fs.unlink(filePath).then(() => { 787 console.info("File deleted"); 788 }).catch((err) => { 789 console.info("remove file failed with error message: " + err.message + ", error code: " + err.codeor); 790 }); 791 ``` 792 793 794## fs.unlink 795 796unlink(path: string, callback: AsyncCallback<void>): void 797 798Deletes a file. This API uses an asynchronous callback to return the result. 799 800**System capability**: SystemCapability.FileManagement.File.FileIO 801 802**Parameters** 803 804| Name | Type | Mandatory| Description | 805| -------- | ------------------------- | ---- | -------------------------- | 806| path | string | Yes | Path of the file in the application sandbox.| 807| callback | AsyncCallback<void> | Yes | Callback invoked when the file is deleted asynchronously. | 808 809**Example** 810 811 ```js 812 let filePath = pathDir + "/test.txt"; 813 fs.unlink(filePath, (err) => { 814 if (err) { 815 console.info("remove file failed with error message: " + err.message + ", error code: " + err.code); 816 } else { 817 console.info("File deleted"); 818 } 819 }); 820 ``` 821 822 823## fs.unlinkSync 824 825unlinkSync(path: string): void 826 827Synchronously deletes a file. 828 829**System capability**: SystemCapability.FileManagement.File.FileIO 830 831**Parameters** 832 833| Name| Type | Mandatory| Description | 834| ------ | ------ | ---- | -------------------------- | 835| path | string | Yes | Path of the file in the application sandbox.| 836 837**Example** 838 839 ```js 840 let filePath = pathDir + "/test.txt"; 841 fs.unlinkSync(filePath); 842 ``` 843 844 845## fs.write 846 847write(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }): Promise<number> 848 849Writes data into a file. This API uses a promise to return the result. 850 851**System capability**: SystemCapability.FileManagement.File.FileIO 852 853**Parameters** 854 855 | Name | Type | Mandatory | Description | 856 | ------- | ------------------------------- | ---- | ---------------------------------------- | 857 | fd | number | Yes | FD of the file. | 858 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 859 | options | Object | No | The options are as follows:<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 860 861**Return value** 862 863 | Type | Description | 864 | --------------------- | -------- | 865 | Promise<number> | Promise used to return the length of the data written.| 866 867**Example** 868 869 ```js 870 let filePath = pathDir + "/test.txt"; 871 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 872 fs.write(file.fd, "hello, world").then((writeLen) => { 873 console.info("write data to file succeed and size is:" + writeLen); 874 fs.closeSync(file); 875 }).catch((err) => { 876 console.info("write data to file failed with error message: " + err.message + ", error code: " + err.code); 877 }); 878 ``` 879 880 881## fs.write 882 883write(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }, callback: AsyncCallback<number>): void 884 885Writes data into a file. This API uses an asynchronous callback to return the result. 886 887**System capability**: SystemCapability.FileManagement.File.FileIO 888 889**Parameters** 890 891 | Name | Type | Mandatory | Description | 892 | -------- | ------------------------------- | ---- | ---------------------------------------- | 893 | fd | number | Yes | FD of the file. | 894 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 895 | options | Object | No | The options are as follows:<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 896 | callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | 897 898**Example** 899 900 ```js 901 let filePath = pathDir + "/test.txt"; 902 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 903 fs.write(file.fd, "hello, world", (err, writeLen) => { 904 if (err) { 905 console.info("write failed with error message: " + err.message + ", error code: " + err.code); 906 } else { 907 console.info("write data to file succeed and size is:" + writeLen); 908 fs.closeSync(file); 909 } 910 }); 911 ``` 912 913 914## fs.writeSync 915 916writeSync(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }): number 917 918Synchronously writes data into a file. 919 920**System capability**: SystemCapability.FileManagement.File.FileIO 921 922**Parameters** 923 924 | Name | Type | Mandatory | Description | 925 | ------- | ------------------------------- | ---- | ---------------------------------------- | 926 | fd | number | Yes | FD of the file. | 927 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 928 | options | Object | No | The options are as follows:<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 929 930**Return value** 931 932 | Type | Description | 933 | ------ | -------- | 934 | number | Length of the data written in the file.| 935 936**Example** 937 938 ```js 939 let filePath = pathDir + "/test.txt"; 940 let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 941 let writeLen = fs.writeSync(file.fd, "hello, world"); 942 console.info("write data to file succeed and size is:" + writeLen); 943 fs.closeSync(file); 944 ``` 945 946## fs.truncate 947 948truncate(file: string|number, len?: number): Promise<void> 949 950Truncates a file. This API uses a promise to return the result. 951 952**System capability**: SystemCapability.FileManagement.File.FileIO 953 954**Parameters** 955 956| Name| Type | Mandatory| Description | 957| ------ | ------ | ---- | -------------------------------- | 958| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file. | 959| len | number | No | File length, in bytes, after truncation.| 960 961**Return value** 962 963 | Type | Description | 964 | ------------------- | ---------------------------- | 965 | Promise<void> | Promise that returns no value.| 966 967**Example** 968 969 ```js 970 let filePath = pathDir + "/test.txt"; 971 let len = 5; 972 fs.truncate(filePath, len).then(() => { 973 console.info("File truncated"); 974 }).catch((err) => { 975 console.info("truncate file failed with error message: " + err.message + ", error code: " + err.code); 976 }); 977 ``` 978 979 980## fs.truncate 981 982truncate(file: string|number, len?: number, callback: AsyncCallback<void>): void 983 984Truncates a file. This API uses an asynchronous callback to return the result. 985 986**System capability**: SystemCapability.FileManagement.File.FileIO 987 988**Parameters** 989 990| Name | Type | Mandatory| Description | 991| -------- | ------------------------- | ---- | -------------------------------- | 992| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file. | 993| len | number | No | File length, in bytes, after truncation.| 994| callback | AsyncCallback<void> | Yes | Callback that returns no value. | 995 996**Example** 997 998 ```js 999 let filePath = pathDir + "/test.txt"; 1000 let len = 5; 1001 fs.truncate(filePath, len, (err) => { 1002 if (err) { 1003 console.info("truncate failed with error message: " + err.message + ", error code: " + err.code); 1004 } else { 1005 console.info("truncate success"); 1006 } 1007 }); 1008 ``` 1009 1010 1011## fs.truncateSync 1012 1013truncateSync(file: string|number, len?: number): void 1014 1015Synchronously truncates a file. 1016 1017**System capability**: SystemCapability.FileManagement.File.FileIO 1018 1019**Parameters** 1020 1021| Name| Type | Mandatory| Description | 1022| ------ | ------ | ---- | -------------------------------- | 1023| file | string\|number | Yes | Path of the file in the application sandbox or file descriptor (FD) of the file. | 1024| len | number | No | File length, in bytes, after truncation.| 1025 1026**Example** 1027 1028 ```js 1029 let filePath = pathDir + "/test.txt"; 1030 let len = 5; 1031 fs.truncateSync(filePath, len); 1032 ``` 1033 1034 1035## fs.readText 1036 1037readText(filePath: string, options?: { offset?: number; length?: number; encoding?: string; }): Promise<string> 1038 1039Reads the text content of a file. This API uses a promise to return the result. 1040 1041**System capability**: SystemCapability.FileManagement.File.FileIO 1042 1043**Parameters** 1044 1045| Name | Type | Mandatory| Description | 1046| -------- | ------ | ---- | ------------------------------------------------------------ | 1047| filePath | string | Yes | Path of the file in the application sandbox. | 1048| options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.<br>- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.| 1049 1050**Return value** 1051 1052 | Type | Description | 1053 | --------------------- | ---------- | 1054 | Promise<string> | Promise used to return the content read.| 1055 1056**Example** 1057 1058 ```js 1059 let filePath = pathDir + "/test.txt"; 1060 fs.readText(filePath).then((str) => { 1061 console.info("readText succeed:" + str); 1062 }).catch((err) => { 1063 console.info("readText failed with error message: " + err.message + ", error code: " + err.code); 1064 }); 1065 ``` 1066 1067 1068## fs.readText 1069 1070readText(filePath: string, options?: { offset?: number; length?: number; encoding?: string; }, callback: AsyncCallback<string>): void 1071 1072Reads the text content of a file. This API uses an asynchronous callback to return the result. 1073 1074**System capability**: SystemCapability.FileManagement.File.FileIO 1075 1076**Parameters** 1077 1078| Name | Type | Mandatory| Description | 1079| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 1080| filePath | string | Yes | Path of the file in the application sandbox. | 1081| options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.<br>- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.| 1082| callback | AsyncCallback<string> | Yes | Callback invoked to return the content read. | 1083 1084**Example** 1085 1086 ```js 1087 let filePath = pathDir + "/test.txt"; 1088 fs.readText(filePath, { offset: 1, encoding: 'UTF-8' }, (err, str) => { 1089 if (err) { 1090 console.info("read text failed with error message: " + err.message + ", error code: " + err.code); 1091 } else { 1092 console.info("readText succeed:" + str); 1093 } 1094 }); 1095 ``` 1096 1097 1098## fs.readTextSync 1099 1100readTextSync(filePath: string, options?: { offset?: number; length?: number; encoding?: string; }): string 1101 1102Synchronously reads the text of a file. 1103 1104**System capability**: SystemCapability.FileManagement.File.FileIO 1105 1106**Parameters** 1107 1108| Name | Type | Mandatory| Description | 1109| -------- | ------ | ---- | ------------------------------------------------------------ | 1110| filePath | string | Yes | Path of the file in the application sandbox. | 1111| options | Object | No | The options are as follows:<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the file length.<br>- **encoding** (string): format of the string to be encoded. The default value is **'utf-8'**, which is the only value supported.| 1112 1113**Return value** 1114 1115 | Type | Description | 1116 | ------ | -------------------- | 1117 | string | Promise used to return the content of the file read.| 1118 1119**Example** 1120 1121 ```js 1122 let filePath = pathDir + "/test.txt"; 1123 let str = fs.readTextSync(filePath, {offset: 1, length: 3}); 1124 console.info("readText succeed:" + str); 1125 ``` 1126 1127## fs.lstat 1128 1129lstat(path: string): Promise<Stat> 1130 1131Obtains information about a symbolic link. This API uses a promise to return the result. 1132 1133**System capability**: SystemCapability.FileManagement.File.FileIO 1134 1135**Parameters** 1136 1137| Name| Type | Mandatory| Description | 1138| ------ | ------ | ---- | -------------------------------------- | 1139| path | string | Yes | Path of the symbolic link in the application sandbox.| 1140 1141**Return value** 1142 1143 | Type | Description | 1144 | ---------------------------- | ---------- | 1145 | Promise<[Stat](#stat)> | Promise used to return the symbolic link information obtained. For details, see **stat**.| 1146 1147**Example** 1148 1149 ```js 1150 let filePath = pathDir + "/test.txt"; 1151 fs.lstat(filePath).then((stat) => { 1152 console.info("get link status succeed, the size of file is" + stat.size); 1153 }).catch((err) => { 1154 console.info("get link status failed with error message: " + err.message + ", error code: " + err.code); 1155 }); 1156 ``` 1157 1158 1159## fs.lstat 1160 1161lstat(path: string, callback: AsyncCallback<Stat>): void 1162 1163Obtains information about a symbolic link. This API uses an asynchronous callback to return the result. 1164 1165**System capability**: SystemCapability.FileManagement.File.FileIO 1166 1167**Parameters** 1168 1169| Name | Type | Mandatory| Description | 1170| -------- | ---------------------------------- | ---- | -------------------------------------- | 1171| path | string | Yes | Path of the symbolic link in the application sandbox.| 1172| callback | AsyncCallback<[Stat](#stat)> | Yes | Callback invoked to return the symbolic link information obtained. | 1173 1174**Example** 1175 1176 ```js 1177 let filePath = pathDir + "/test.txt"; 1178 fs.lstat(filePath, (err, stat) => { 1179 if (err) { 1180 console.info("lstat failed with error message: " + err.message + ", error code: " + err.code); 1181 } else { 1182 console.info("get link status succeed, the size of file is" + stat.size); 1183 } 1184 }); 1185 ``` 1186 1187## fs.lstatSync 1188 1189lstatSync(path: string): Stat 1190 1191Obtains information about a symbolic link synchronously. 1192 1193**System capability**: SystemCapability.FileManagement.File.FileIO 1194 1195**Parameters** 1196 1197| Name| Type | Mandatory| Description | 1198| ------ | ------ | ---- | -------------------------------------- | 1199| path | string | Yes | Path of the file in the application sandbox.| 1200 1201**Return value** 1202 1203 | Type | Description | 1204 | ------------- | ---------- | 1205 | [Stat](#stat) | File information obtained.| 1206 1207**Example** 1208 1209 ```js 1210 let filePath = pathDir + "/test.txt"; 1211 let stat = fs.lstatSync(filePath); 1212 ``` 1213 1214## fs.rename 1215 1216rename(oldPath: string, newPath: string): Promise<void> 1217 1218Renames a file or directory. This API uses a promise to return the result. 1219 1220**System capability**: SystemCapability.FileManagement.File.FileIO 1221 1222**Parameters** 1223 1224| Name | Type | Mandatory| Description | 1225| ------- | ------ | ---- | ---------------------------- | 1226| oldPath | string | Yes | Path of the file to rename in the application sandbox.| 1227| newPath | string | Yes | Path of the renamed file in the application sandbox. | 1228 1229**Return value** 1230 1231 | Type | Description | 1232 | ------------------- | ---------------------------- | 1233 | Promise<void> | Promise that returns no value.| 1234 1235**Example** 1236 1237 ```js 1238 let srcFile = pathDir + "/test.txt"; 1239 let dstFile = pathDir + "/new.txt"; 1240 fs.rename(srcFile, dstFile).then(() => { 1241 console.info("File renamed"); 1242 }).catch((err) => { 1243 console.info("rename failed with error message: " + err.message + ", error code: " + err.code); 1244 }); 1245 ``` 1246 1247## fs.rename 1248 1249rename(oldPath: string, newPath: string, callback: AsyncCallback<void>): void 1250 1251Renames a file or directory. This API uses an asynchronous callback to return the result. 1252 1253**System capability**: SystemCapability.FileManagement.File.FileIO 1254 1255**Parameters** 1256 1257| Name | Type | Mandatory| Description | 1258| -------- | ------------------------- | ---- | ---------------------------- | 1259| oldPath | string | Yes | Path of the file to rename in the application sandbox.| 1260| newPath | string | Yes | Path of the renamed file in the application sandbox. | 1261| callback | AsyncCallback<void> | Yes | Callback invoked when the file is asynchronously renamed. | 1262 1263**Example** 1264 1265 ```js 1266 let srcFile = pathDir + "/test.txt"; 1267 let dstFile = pathDir + "/new.txt"; 1268 fs.rename(srcFile, dstFile, (err) => { 1269 if (err) { 1270 console.info("rename failed with error message: " + err.message + ", error code: " + err.code); 1271 } else { 1272 console.info("rename success"); 1273 } 1274 }); 1275 ``` 1276 1277## fs.renameSync 1278 1279renameSync(oldPath: string, newPath: string): void 1280 1281Renames a file or directory synchronously. 1282 1283**System capability**: SystemCapability.FileManagement.File.FileIO 1284 1285**Parameters** 1286 1287| Name | Type | Mandatory| Description | 1288| ------- | ------ | ---- | ---------------------------- | 1289| oldPath | string | Yes | Path of the file to rename in the application sandbox.| 1290| newPath | string | Yes | Path of the renamed file in the application sandbox. | 1291 1292**Example** 1293 1294 ```js 1295 let srcFile = pathDir + "/test.txt"; 1296 let dstFile = pathDir + "/new.txt"; 1297 fs.renameSync(srcFile, dstFile); 1298 ``` 1299 1300 1301## fs.fsync 1302 1303fsync(fd: number): Promise<void> 1304 1305Flushes data of a file to disk. This API uses a promise to return the result. 1306 1307**System capability**: SystemCapability.FileManagement.File.FileIO 1308 1309**Parameters** 1310 1311 | Name | Type | Mandatory | Description | 1312 | ---- | ------ | ---- | ------------ | 1313 | fd | number | Yes | FD of the file.| 1314 1315**Return value** 1316 1317 | Type | Description | 1318 | ------------------- | ---------------------------- | 1319 | Promise<void> | Promise that returns no value.| 1320 1321**Example** 1322 1323 ```js 1324 let filePath = pathDir + "/test.txt"; 1325 let file = fs.openSync(filePath); 1326 fs.fsync(file.fd).then(() => { 1327 console.info("Data flushed"); 1328 }).catch((err) => { 1329 console.info("sync data failed with error message: " + err.message + ", error code: " + err.code); 1330 }); 1331 ``` 1332 1333 1334## fs.fsync 1335 1336fsync(fd: number, callback: AsyncCallback<void>): void 1337 1338Flushes data of a file to disk. This API uses an asynchronous callback to return the result. 1339 1340**System capability**: SystemCapability.FileManagement.File.FileIO 1341 1342**Parameters** 1343 1344 | Name | Type | Mandatory | Description | 1345 | -------- | ------------------------- | ---- | --------------- | 1346 | fd | number | Yes | FD of the file. | 1347 | Callback | AsyncCallback<void> | Yes | Callback invoked when the file data is synchronized in asynchronous mode.| 1348 1349**Example** 1350 1351 ```js 1352 let filePath = pathDir + "/test.txt"; 1353 let file = fs.openSync(filePath); 1354 fs.fsync(file.fd, (err) => { 1355 if (err) { 1356 console.info("fsync failed with error message: " + err.message + ", error code: " + err.code); 1357 } else { 1358 console.info("fsync success"); 1359 fs.closeSync(file); 1360 } 1361 }); 1362 ``` 1363 1364 1365## fs.fsyncSync 1366 1367fsyncSync(fd: number): void 1368 1369Flushes data of a file to disk synchronously. 1370 1371**System capability**: SystemCapability.FileManagement.File.FileIO 1372 1373**Parameters** 1374 1375 | Name | Type | Mandatory | Description | 1376 | ---- | ------ | ---- | ------------ | 1377 | fd | number | Yes | FD of the file.| 1378 1379**Example** 1380 1381 ```js 1382 let filePath = pathDir + "/test.txt"; 1383 let file = fs.openSync(filePath); 1384 fs.fsyncSync(file.fd); 1385 fs.closeSync(file); 1386 ``` 1387 1388 1389## fs.fdatasync 1390 1391fdatasync(fd: number): Promise<void> 1392 1393Flushes 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. 1394 1395**System capability**: SystemCapability.FileManagement.File.FileIO 1396 1397**Parameters** 1398 1399 | Name | Type | Mandatory | Description | 1400 | ---- | ------ | ---- | ------------ | 1401 | fd | number | Yes | FD of the file.| 1402 1403**Return value** 1404 1405 | Type | Description | 1406 | ------------------- | ---------------------------- | 1407 | Promise<void> | Promise that returns no value.| 1408 1409**Example** 1410 1411 ```js 1412 let filePath = pathDir + "/test.txt"; 1413 let file = fs.openSync(filePath); 1414 fs.fdatasync(file.fd).then((err) => { 1415 console.info("Data flushed"); 1416 fs.closeSync(file); 1417 }).catch((err) => { 1418 console.info("sync data failed with error message: " + err.message + ", error code: " + err.code); 1419 }); 1420 ``` 1421 1422 1423## fs.fdatasync 1424 1425fdatasync(fd: number, callback: AsyncCallback<void>): void 1426 1427Flushes data of a file to disk. This API uses an asynchronous callback to return the result. 1428 1429**System capability**: SystemCapability.FileManagement.File.FileIO 1430 1431**Parameters** 1432 1433 | Name | Type | Mandatory | Description | 1434 | -------- | ------------------------------- | ---- | ----------------- | 1435 | fd | number | Yes | FD of the file. | 1436 | callback | AsyncCallback<void> | Yes | Callback invoked when the file data is synchronized in asynchronous mode.| 1437 1438**Example** 1439 1440 ```js 1441 let filePath = pathDir + "/test.txt"; 1442 let file = fs.openSync(filePath); 1443 fs.fdatasync (file.fd, (err) => { 1444 if (err) { 1445 console.info("fdatasync failed with error message: " + err.message + ", error code: " + err.code); 1446 } else { 1447 console.info("fdatasync success"); 1448 fs.closeSync(file); 1449 } 1450 }); 1451 ``` 1452 1453## fs.fdatasyncSync 1454 1455fdatasyncSync(fd: number): void 1456 1457Synchronizes data in a file synchronously. 1458 1459**System capability**: SystemCapability.FileManagement.File.FileIO 1460 1461**Parameters** 1462 1463 | Name | Type | Mandatory | Description | 1464 | ---- | ------ | ---- | ------------ | 1465 | fd | number | Yes | FD of the file.| 1466 1467**Example** 1468 1469 ```js 1470 let filePath = pathDir + "/test.txt"; 1471 let file = fs.openSync(filePath); 1472 let stat = fs.fdatasyncSync(file.fd); 1473 fs.closeSync(file); 1474 ``` 1475 1476 1477## fs.symlink 1478 1479symlink(target: string, srcPath: string): Promise<void> 1480 1481Creates a symbolic link based on a file path. This API uses a promise to return the result. 1482 1483**System capability**: SystemCapability.FileManagement.File.FileIO 1484 1485**Parameters** 1486 1487| Name | Type | Mandatory| Description | 1488| ------- | ------ | ---- | ---------------------------- | 1489| target | string | Yes | Path of the source file in the application sandbox. | 1490| srcPath | string | Yes | Path of the symbolic link in the application sandbox.| 1491 1492**Return value** 1493 1494 | Type | Description | 1495 | ------------------- | ---------------------------- | 1496 | Promise<void> | Promise that returns no value.| 1497 1498**Example** 1499 1500 ```js 1501 let srcFile = pathDir + "/test.txt"; 1502 let dstFile = pathDir + "/test"; 1503 fs.symlink(srcFile, dstFile).then(() => { 1504 console.info("Symbolic link created"); 1505 }).catch((err) => { 1506 console.info("symlink failed with error message: " + err.message + ", error code: " + err.code); 1507 }); 1508 ``` 1509 1510 1511## fs.symlink 1512symlink(target: string, srcPath: string, callback: AsyncCallback<void>): void 1513 1514Creates a symbolic link based on a file path. This API uses an asynchronous callback to return the result. 1515 1516**System capability**: SystemCapability.FileManagement.File.FileIO 1517 1518**Parameters** 1519 1520| Name | Type | Mandatory| Description | 1521| -------- | ------------------------- | ---- | -------------------------------- | 1522| target | string | Yes | Path of the source file in the application sandbox. | 1523| srcPath | string | Yes | Path of the symbolic link in the application sandbox. | 1524| callback | AsyncCallback<void> | Yes | Callback invoked when the symbolic link is created asynchronously.| 1525 1526**Example** 1527 1528 ```js 1529 let srcFile = pathDir + "/test.txt"; 1530 let dstFile = pathDir + "/test"; 1531 fs.symlink(srcFile, dstFile, (err) => { 1532 if (err) { 1533 console.info("symlink failed with error message: " + err.message + ", error code: " + err.code); 1534 } else { 1535 console.info("symlink success"); 1536 } 1537 }); 1538 ``` 1539 1540## fs.symlinkSync 1541 1542symlinkSync(target: string, srcPath: string): void 1543 1544Synchronously creates a symbolic link based on a file path. 1545 1546**System capability**: SystemCapability.FileManagement.File.FileIO 1547 1548**Parameters** 1549 1550| Name | Type | Mandatory| Description | 1551| ------- | ------ | ---- | ---------------------------- | 1552| target | string | Yes | Path of the source file in the application sandbox. | 1553| srcPath | string | Yes | Path of the symbolic link in the application sandbox.| 1554 1555**Example** 1556 1557 ```js 1558 let srcFile = pathDir + "/test.txt"; 1559 let dstFile = pathDir + "/test"; 1560 fs.symlinkSync(srcFile, dstFile); 1561 ``` 1562 1563## fs.listFile 1564listFile(path: string, options?: { 1565 recursion?: boolean; 1566 listNum?: number; 1567 filter?: Filter; 1568}): Promise<string[]>; 1569 1570Lists all files in a directory. This API uses a promise to return the result.<br>This API supports recursive listing of all files (including files in subdirectories) and file filtering. 1571 1572**System capability**: SystemCapability.FileManagement.File.FileIO 1573 1574**Parameters** 1575 1576 | Name | Type | Mandatory | Description | 1577 | ------ | ------ | ---- | --------------------------- | 1578 | path | string | Yes | Path of the directory in the application sandbox.| 1579 | options | Object | No | File filtering options.| 1580 1581**options parameters** 1582 1583 | Name | Type | Mandatory | Description | 1584 | ------ | ------ | ---- | --------------------------- | 1585 | recursion | boolean | No | Whether to list all files in subdirectories recursively. The default value is **false**.| 1586 | listNum | number | No | Number of file names to list. The default value **0** means to list all files.| 1587 | filter | [Filter](#filter) | No | File filtering options. Currently, only the match by file name extension, fuzzy search by file name, and filter by file size or latest modification time are supported.| 1588 1589**Return value** 1590 1591 | Type | Description | 1592 | --------------------- | ---------- | 1593 | Promise<string[]> | Promise used to return the files names listed.| 1594 1595**Example** 1596 1597 ```js 1598 let options = { 1599 "recursion": false, 1600 "listNum": 0, 1601 "filter": { 1602 "suffix": [".png", ".jpg", ".jpeg"], 1603 "displayName": ["%abc", "efg%"], 1604 "fileSizeOver": 1024, 1605 "lastModifiedAfter": new Date().getTime(), 1606 } 1607 }; 1608 fs.listFile(pathDir, options).then((filenames) => { 1609 console.info("listFile succeed"); 1610 for (let i = 0; i < filenames.length; i++) { 1611 console.info("fileName: %s", filenames[i]); 1612 } 1613 }).catch((err) => { 1614 console.info("list file failed with error message: " + err.message + ", error code: " + err.code); 1615 }); 1616 ``` 1617 1618## fs.listFile 1619listFile(path: string, options?: { 1620 recursion?: boolean; 1621 listNum?: number; 1622 filter?: Filter; 1623}, callback: AsyncCallback<string[]>): void; 1624 1625Lists all files in a directory. This API uses an asynchronous callback to return the result.<br>This API supports recursive listing of all files (including files in subdirectories) and file filtering. 1626 1627**Parameters** 1628 1629 | Name | Type | Mandatory | Description | 1630 | ------ | ------ | ---- | --------------------------- | 1631 | path | string | Yes | Path of the directory in the application sandbox.| 1632 | options | Object | No | File filtering options.| 1633 | callback | AsyncCallback<string[]> | Yes | Callback invoked to return the file names listed. | 1634 1635**options parameters** 1636 1637 | Name | Type | Mandatory | Description | 1638 | ------ | ------ | ---- | --------------------------- | 1639 | recursion | boolean | No | Whether to list all files in subdirectories recursively. The default value is **false**.| 1640 | listNum | number | No | Number of file names to list. The default value **0** means to list all files.| 1641 | filter | [Filter](#filter) | No | File filtering options. Currently, only the match by file name extension, fuzzy search by file name, and filter by file size or latest modification time are supported.| 1642 1643**Example** 1644 1645 ```js 1646 let options = { 1647 "recursion": false, 1648 "listNum": 0, 1649 "filter": { 1650 "suffix": [".png", ".jpg", ".jpeg"], 1651 "displayName": ["%abc", "efg%"], 1652 "fileSizeOver": 1024, 1653 "lastModifiedAfter": new Date().getTime(), 1654 } 1655 }; 1656 fs.listFile(pathDir, options, (err, filenames) => { 1657 if (err) { 1658 console.info("list file failed with error message: " + err.message + ", error code: " + err.code); 1659 } else { 1660 console.info("listFile succeed"); 1661 for (let i = 0; i < filenames.length; i++) { 1662 console.info("filename: %s", filenames[i]); 1663 } 1664 } 1665 }); 1666 ``` 1667 1668## fs.listFileSync 1669 1670listFileSync(path: string, options?: { 1671 recursion?: boolean; 1672 listNum?: number; 1673 filter?: Filter; 1674}): string[]; 1675 1676Lists all files in a directory synchronously. This API supports recursive listing of all files (including files in subdirectories) and file filtering. 1677 1678**Parameters** 1679 1680 | Name | Type | Mandatory | Description | 1681 | ------ | ------ | ---- | --------------------------- | 1682 | path | string | Yes | Path of the directory in the application sandbox.| 1683 | options | Object | No | File filtering options.| 1684 1685**options parameters** 1686 1687 | Name | Type | Mandatory | Description | 1688 | ------ | ------ | ---- | --------------------------- | 1689 | recursion | boolean | No | Whether to list all files in subdirectories recursively. The default value is **false**.| 1690 | listNum | number | No | Number of file names to list. The default value **0** means to list all files.| 1691 | filter | [Filter](#filter) | No | File filtering options. Currently, only the match by file name extension, fuzzy search by file name, and filter by file size or latest modification time are supported.| 1692 1693**Return value** 1694 1695 | Type | Description | 1696 | --------------------- | ---------- | 1697 | string[] | File names listed.| 1698 1699**Example** 1700 1701 ```js 1702 let options = { 1703 "recursion": false, 1704 "listNum": 0, 1705 "filter": { 1706 "suffix": [".png", ".jpg", ".jpeg"], 1707 "displayName": ["%abc", "efg%"], 1708 "fileSizeOver": 1024, 1709 "lastModifiedAfter": new Date().getTime(), 1710 } 1711 }; 1712 let filenames = fs.listFileSync(pathDir, options); 1713 console.info("listFile succeed"); 1714 for (let i = 0; i < filenames.length; i++) { 1715 console.info("filename: %s", filenames[i]); 1716 } 1717 ``` 1718## fs.moveFile 1719 1720moveFile(src: string, dest: string, mode?: number): Promise\<void>; 1721 1722Moves a file. This API uses a promise to return the result. 1723 1724**System capability**: SystemCapability.FileManagement.File.FileIO 1725 1726**Parameters** 1727 1728 | Name | Type | Mandatory | Description | 1729 | ------ | ------ | ---- | --------------------------- | 1730 | src | string | Yes | Path of the file to move in the application sandbox.| 1731 | dest | string | Yes | Destination path of the file in the application sandbox.| 1732 | mode | number | No | Whether to overwrite the file of the same name in the destination directory. The value **0** means to overwrite the file of the same name in the destination directory. The value **1** means to throw an exception if a file of the same name exists in the destination directory. The default value is **0**.| 1733 1734**Example** 1735 1736 ```js 1737 let srcPath = pathDir + "/source.txt"; 1738 let destPath = pathDir + "/dest.txt"; 1739 fs.moveFile(srcPath, destPath, 0).then(() => { 1740 console.info("move file succeed"); 1741 }).catch((err) => { 1742 console.info("move file failed with error message: " + err.message + ", error code: " + err.code); 1743 }); 1744 ``` 1745 1746## fs.moveFile 1747 1748moveFile(src: string, dest: string, mode?: number, callback: AsyncCallback\<void>): void; 1749 1750Moves a file. This API uses an asynchronous callback to return the result. 1751 1752**System capability**: SystemCapability.FileManagement.File.FileIO 1753 1754**Parameters** 1755 1756 | Name | Type | Mandatory | Description | 1757 | ------ | ------ | ---- | --------------------------- | 1758 | src | string | Yes | Path of the file to move in the application sandbox.| 1759 | dest | string | Yes | Destination path of the file in the application sandbox.| 1760 | mode | number | No | Whether to overwrite the file of the same name in the destination directory. The value **0** means to overwrite the file of the same name in the destination directory. The value **1** means to throw an exception if a file of the same name exists in the destination directory. The default value is **0**.| 1761 | callback | AsyncCallback<void> | Yes | Callback invoked when the file is moved. | 1762 1763**Example** 1764 1765 ```js 1766 let srcPath = pathDir + "/source.txt"; 1767 let destPath = pathDir + "/dest.txt"; 1768 fs.moveFile(srcPath, destPath, 0, (err) => { 1769 if (err) { 1770 console.info("move file failed with error message: " + err.message + ", error code: " + err.code); 1771 } else { 1772 console.info("move file succeed"); 1773 } 1774 }); 1775 ``` 1776 1777## fs.moveFileSync 1778 1779moveFile(src: string, dest: string, mode?: number): void; 1780 1781Moves a file synchronously. 1782 1783**System capability**: SystemCapability.FileManagement.File.FileIO 1784 1785**Parameters** 1786 1787 | Name | Type | Mandatory | Description | 1788 | ------ | ------ | ---- | --------------------------- | 1789 | src | string | Yes | Path of the source file in the application sandbox.| 1790 | dest | string | Yes | Destination path of the file in the application sandbox.| 1791 | mode | number | No | Whether to overwrite the file of the same name in the destination directory. The value **0** means to overwrite the file of the same name in the destination directory. The value **1** means to throw an exception if a file of the same name exists in the destination directory. The default value is **0**.| 1792 1793**Example** 1794 1795 ```js 1796 let srcPath = pathDir + "/source.txt"; 1797 let destPath = pathDir + "/dest.txt"; 1798 fs.moveFileSync(srcPath, destPath, 0); 1799 console.info("move file succeed"); 1800 ``` 1801 1802## fs.mkdtemp 1803 1804mkdtemp(prefix: string): Promise<string> 1805 1806Creates a temporary directory. This API uses a promise to return the result. 1807 1808**System capability**: SystemCapability.FileManagement.File.FileIO 1809 1810**Parameters** 1811 1812 | Name | Type | Mandatory | Description | 1813 | ------ | ------ | ---- | --------------------------- | 1814 | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| 1815 1816**Return value** 1817 1818 | Type | Description | 1819 | --------------------- | ---------- | 1820 | Promise<string> | Promise used to return the unique directory generated.| 1821 1822**Example** 1823 1824 ```js 1825 fs.mkdtemp(pathDir + "/XXXXXX").then((pathDir) => { 1826 console.info("mkdtemp succeed:" + pathDir); 1827 }).catch((err) => { 1828 console.info("mkdtemp failed with error message: " + err.message + ", error code: " + err.code); 1829 }); 1830 ``` 1831 1832 1833## fs.mkdtemp 1834 1835mkdtemp(prefix: string, callback: AsyncCallback<string>): void 1836 1837Creates a temporary directory. This API uses an asynchronous callback to return the result. 1838 1839**System capability**: SystemCapability.FileManagement.File.FileIO 1840 1841**Parameters** 1842 1843 | Name | Type | Mandatory | Description | 1844 | -------- | --------------------------- | ---- | --------------------------- | 1845 | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| 1846 | callback | AsyncCallback<string> | Yes | Callback invoked when a temporary directory is created asynchronously. | 1847 1848**Example** 1849 1850 ```js 1851 fs.mkdtemp(pathDir + "/XXXXXX", (err, res) => { 1852 if (err) { 1853 console.info("mkdtemp failed with error message: " + err.message + ", error code: " + err.code); 1854 } else { 1855 console.info("mkdtemp success"); 1856 } 1857 }); 1858 ``` 1859 1860## fs.mkdtempSync 1861 1862mkdtempSync(prefix: string): string 1863 1864Synchronously creates a temporary directory. 1865 1866**System capability**: SystemCapability.FileManagement.File.FileIO 1867 1868**Parameters** 1869 1870 | Name | Type | Mandatory | Description | 1871 | ------ | ------ | ---- | --------------------------- | 1872 | prefix | string | Yes | A randomly generated string used to replace "XXXXXX" in a directory.| 1873 1874**Return value** 1875 1876 | Type | Description | 1877 | ------ | ---------- | 1878 | string | Unique path generated.| 1879 1880**Example** 1881 1882 ```js 1883 let res = fs.mkdtempSync(pathDir + "/XXXXXX"); 1884 ``` 1885 1886## fs.createStream 1887 1888createStream(path: string, mode: string): Promise<Stream> 1889 1890Opens a file stream based on the file path. This API uses a promise to return the result. 1891 1892**System capability**: SystemCapability.FileManagement.File.FileIO 1893 1894**Parameters** 1895 1896| Name| Type | Mandatory| Description | 1897| ------ | ------ | ---- | ------------------------------------------------------------ | 1898| path | string | Yes | Path of the file in the application sandbox. | 1899| 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).| 1900 1901**Return value** 1902 1903 | Type | Description | 1904 | --------------------------------- | --------- | 1905 | Promise<[Stream](#stream)> | Promise used to return the result.| 1906 1907**Example** 1908 1909 ```js 1910 let filePath = pathDir + "/test.txt"; 1911 fs.createStream(filePath, "r+").then((stream) => { 1912 console.info("Stream created"); 1913 }).catch((err) => { 1914 console.info("createStream failed with error message: " + err.message + ", error code: " + err.code); 1915 }); 1916 ``` 1917 1918 1919## fs.createStream 1920 1921createStream(path: string, mode: string, callback: AsyncCallback<Stream>): void 1922 1923Opens a file stream based on the file path. This API uses an asynchronous callback to return the result. 1924 1925**System capability**: SystemCapability.FileManagement.File.FileIO 1926 1927**Parameters** 1928 1929| Name | Type | Mandatory| Description | 1930| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 1931| path | string | Yes | Path of the file in the application sandbox. | 1932| 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).| 1933| callback | AsyncCallback<[Stream](#stream)> | Yes | Callback invoked when the stream is open asynchronously. | 1934 1935**Example** 1936 1937 ```js 1938 let filePath = pathDir + "/test.txt"; 1939 fs.createStream(filePath, "r+", (err, stream) => { 1940 if (err) { 1941 console.info("create stream failed with error message: " + err.message + ", error code: " + err.code); 1942 } else { 1943 console.info("create stream success"); 1944 } 1945 }); 1946 ``` 1947 1948## fs.createStreamSync 1949 1950createStreamSync(path: string, mode: string): Stream 1951 1952Synchronously opens a stream based on the file path. 1953 1954**System capability**: SystemCapability.FileManagement.File.FileIO 1955 1956**Parameters** 1957 1958| Name| Type | Mandatory| Description | 1959| ------ | ------ | ---- | ------------------------------------------------------------ | 1960| path | string | Yes | Path of the file in the application sandbox. | 1961| 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).| 1962 1963**Return value** 1964 1965 | Type | Description | 1966 | ------------------ | --------- | 1967 | [Stream](#stream) | Stream opened.| 1968 1969**Example** 1970 1971 ```js 1972 let filePath = pathDir + "/test.txt"; 1973 let ss = fs.createStreamSync(filePath, "r+"); 1974 ``` 1975 1976 1977## fs.fdopenStream 1978 1979fdopenStream(fd: number, mode: string): Promise<Stream> 1980 1981Opens a file stream based on the file descriptor. This API uses a promise to return the result. 1982 1983**System capability**: SystemCapability.FileManagement.File.FileIO 1984 1985**Parameters** 1986 1987 | Name | Type | Mandatory | Description | 1988 | ---- | ------ | ---- | ---------------------------------------- | 1989 | fd | number | Yes | FD of the file. | 1990 | 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).| 1991 1992**Return value** 1993 1994 | Type | Description | 1995 | --------------------------------- | --------- | 1996 | Promise<[Stream](#stream)> | Promise used to return the result.| 1997 1998**Example** 1999 2000 ```js 2001 let filePath = pathDir + "/test.txt"; 2002 let file = fs.openSync(filePath); 2003 fs.fdopenStream(file.fd, "r+").then((stream) => { 2004 console.info("Stream opened"); 2005 fs.closeSync(file); 2006 }).catch((err) => { 2007 console.info("openStream failed with error message: " + err.message + ", error code: " + err.code); 2008 }); 2009 ``` 2010 2011 2012## fs.fdopenStream 2013 2014fdopenStream(fd: number, mode: string, callback: AsyncCallback<Stream>): void 2015 2016Opens a file stream based on the file descriptor. This API uses an asynchronous callback to return the result. 2017 2018**System capability**: SystemCapability.FileManagement.File.FileIO 2019 2020**Parameters** 2021 2022 | Name | Type | Mandatory | Description | 2023 | -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2024 | fd | number | Yes | FD of the file. | 2025 | 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).| 2026 | callback | AsyncCallback<[Stream](#stream)> | Yes | Callback invoked when the stream is open asynchronously. | 2027 2028**Example** 2029 2030 ```js 2031 let filePath = pathDir + "/test.txt"; 2032 let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY); 2033 fs.fdopenStream(file.fd, "r+", (err, stream) => { 2034 if (err) { 2035 console.info("fdopen stream failed with error message: " + err.message + ", error code: " + err.code); 2036 } else { 2037 console.info("fdopen stream success"); 2038 fs.closeSync(file); 2039 } 2040 }); 2041 ``` 2042 2043## fs.fdopenStreamSync 2044 2045fdopenStreamSync(fd: number, mode: string): Stream 2046 2047Synchronously opens a stream based on the file descriptor. 2048 2049**System capability**: SystemCapability.FileManagement.File.FileIO 2050 2051**Parameters** 2052 2053 | Name | Type | Mandatory | Description | 2054 | ---- | ------ | ---- | ---------------------------------------- | 2055 | fd | number | Yes | FD of the file. | 2056 | 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).| 2057 2058**Return value** 2059 2060 | Type | Description | 2061 | ------------------ | --------- | 2062 | [Stream](#stream) | Stream opened.| 2063 2064**Example** 2065 2066 ```js 2067 let filePath = pathDir + "/test.txt"; 2068 let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY | fs.OpenMode.CREATE); 2069 let ss = fs.fdopenStreamSync(file.fd, "r+"); 2070 fs.closeSync(file); 2071 ``` 2072 2073## Stat 2074 2075Represents detailed file information. Before calling any API of the **Stat()** class, use [stat()](#fsstat) to create a **Stat** instance synchronously or asynchronously. 2076 2077**System capability**: SystemCapability.FileManagement.File.FileIO 2078 2079### Attributes 2080 2081| Name | Type | Readable | Writable | Description | 2082| ------ | ------ | ---- | ---- | ---------------------------------------- | 2083| ino | number | Yes | No | File ID. Different files on the same device have different **ino**s.| | 2084| mode | number | Yes | No | File permissions. The meaning of each bit is as follows:<br>- **0o400**: The owner has the read permission on a regular file or a directory entry.<br>- **0o200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>- **0o100**: The owner has the permission to execute a regular file or search for the specified path in a directory.<br>- **0o040**: The user group has the read permission on a regular file or a directory entry.<br>- **0o020**: The user group has the permission to write a regular file or create and delete a directory entry.<br>- **0o010**: The user group has the permission to execute a regular file or search for the specified path in a directory.<br>- **0o004**: Other users have the permission to read a regular file or read a directory entry.<br>- **0o002**: Other users have the permission to write a regular file or create and delete a directory entry.<br>- **0o001**: Other users have the permission to execute a regular file or search for the specified path in a directory.| 2085| uid | number | Yes | No | ID of the file owner.| 2086| gid | number | Yes | No | ID of the user group of the file.| 2087| size | number | Yes | No | File size, in bytes. This parameter is valid only for regular files. | 2088| 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. | 2089| 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. | 2090| 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. | 2091 2092 2093### isBlockDevice 2094 2095isBlockDevice(): boolean 2096 2097Checks whether this file is a block special file. A block special file supports access by block only, and it is cached when accessed. 2098 2099**System capability**: SystemCapability.FileManagement.File.FileIO 2100 2101**Return value** 2102 2103 | Type | Description | 2104 | ------- | ---------------- | 2105 | boolean | Whether the file is a block special file.| 2106 2107**Example** 2108 2109 ```js 2110 let filePath = pathDir + "/test.txt"; 2111 let isBLockDevice = fs.statSync(filePath).isBlockDevice(); 2112 ``` 2113 2114### isCharacterDevice 2115 2116isCharacterDevice(): boolean 2117 2118Checks whether this file is a character special file. A character special file supports random access, and it is not cached when accessed. 2119 2120**System capability**: SystemCapability.FileManagement.File.FileIO 2121 2122**Return value** 2123 2124 | Type | Description | 2125 | ------- | ----------------- | 2126 | boolean | Whether the file is a character special file.| 2127 2128**Example** 2129 2130 ```js 2131 let filePath = pathDir + "/test.txt"; 2132 let isCharacterDevice = fs.statSync(filePath).isCharacterDevice(); 2133 ``` 2134 2135 2136### isDirectory 2137 2138isDirectory(): boolean 2139 2140Checks whether this file is a directory. 2141 2142**System capability**: SystemCapability.FileManagement.File.FileIO 2143 2144**Return value** 2145 2146 | Type | Description | 2147 | ------- | ------------- | 2148 | boolean | Whether the file is a directory.| 2149 2150**Example** 2151 2152 ```js 2153 let dirPath = pathDir + "/test"; 2154 let isDirectory = fs.statSync(dirPath).isDirectory(); 2155 ``` 2156 2157 2158### isFIFO 2159 2160isFIFO(): boolean 2161 2162Checks whether this file is a named pipe (or FIFO). Named pipes are used for inter-process communication. 2163 2164**System capability**: SystemCapability.FileManagement.File.FileIO 2165 2166**Return value** 2167 2168 | Type | Description | 2169 | ------- | --------------------- | 2170 | boolean | Whether the file is a FIFO.| 2171 2172**Example** 2173 2174 ```js 2175 let filePath = pathDir + "/test.txt"; 2176 let isFIFO = fs.statSync(filePath).isFIFO(); 2177 ``` 2178 2179 2180### isFile 2181 2182isFile(): boolean 2183 2184Checks whether this file is a regular file. 2185 2186**System capability**: SystemCapability.FileManagement.File.FileIO 2187 2188**Return value** 2189 2190 | Type | Description | 2191 | ------- | --------------- | 2192 | boolean | Whether the file is a regular file.| 2193 2194**Example** 2195 2196 ```js 2197 let filePath = pathDir + "/test.txt"; 2198 let isFile = fs.statSync(filePath).isFile(); 2199 ``` 2200 2201 2202### isSocket 2203 2204isSocket(): boolean 2205 2206Checks whether this file is a socket. 2207 2208**System capability**: SystemCapability.FileManagement.File.FileIO 2209 2210**Return value** 2211 2212 | Type | Description | 2213 | ------- | -------------- | 2214 | boolean | Whether the file is a socket.| 2215 2216**Example** 2217 2218 ```js 2219 let filePath = pathDir + "/test.txt"; 2220 let isSocket = fs.statSync(filePath).isSocket(); 2221 ``` 2222 2223 2224### isSymbolicLink 2225 2226isSymbolicLink(): boolean 2227 2228Checks whether this file is a symbolic link. 2229 2230**System capability**: SystemCapability.FileManagement.File.FileIO 2231 2232**Return value** 2233 2234 | Type | Description | 2235 | ------- | --------------- | 2236 | boolean | Whether the file is a symbolic link.| 2237 2238**Example** 2239 2240 ```js 2241 let filePath = pathDir + "/test"; 2242 let isSymbolicLink = fs.statSync(filePath).isSymbolicLink(); 2243 ``` 2244 2245## Stream 2246 2247Provides file stream management. Before calling any API of the **Stream** class, use **createStream()** to create a **Stream** instance synchronously or asynchronously. 2248 2249 2250### close 2251 2252close(): Promise<void> 2253 2254Closes the stream. This API uses a promise to return the result. 2255 2256**System capability**: SystemCapability.FileManagement.File.FileIO 2257 2258**Return value** 2259 2260 | Type | Description | 2261 | ------------------- | ------------- | 2262 | Promise<void> | Promise used to return the stream close result.| 2263 2264**Example** 2265 2266 ```js 2267 let filePath = pathDir + "/test.txt"; 2268 let ss= fs.createStreamSync(filePath, "r+"); 2269 ss.close().then(() => { 2270 console.info("File stream closed"); 2271 }).catch((err) => { 2272 console.info("close fileStream failed with error message: " + err.message + ", error code: " + err.code); 2273 }); 2274 ``` 2275 2276 2277### close 2278 2279close(callback: AsyncCallback<void>): void 2280 2281Closes the stream. This API uses an asynchronous callback to return the result. 2282 2283**System capability**: SystemCapability.FileManagement.File.FileIO 2284 2285**Parameters** 2286 2287 | Name | Type | Mandatory | Description | 2288 | -------- | ------------------------- | ---- | ------------- | 2289 | callback | AsyncCallback<void> | Yes | Callback invoked when the stream is closed asynchronously.| 2290 2291**Example** 2292 2293 ```js 2294 let filePath = pathDir + "/test.txt"; 2295 let ss= fs.createStreamSync(filePath, "r+"); 2296 ss.close((err) => { 2297 if (err) { 2298 console.info("close stream failed with error message: " + err.message + ", error code: " + err.code); 2299 } else { 2300 console.info("close stream success"): 2301 } 2302 }); 2303 ``` 2304 2305### closeSync 2306 2307closeSync(): void 2308 2309Synchronously closes the stream. 2310 2311**System capability**: SystemCapability.FileManagement.File.FileIO 2312 2313**Example** 2314 2315 ```js 2316 let filePath = pathDir + "/test.txt"; 2317 let ss= fs.createStreamSync(filePath, "r+"); 2318 ss.closeSync(); 2319 ``` 2320 2321### flush 2322 2323flush(): Promise<void> 2324 2325Flushes the stream. This API uses a promise to return the result. 2326 2327**System capability**: SystemCapability.FileManagement.File.FileIO 2328 2329**Return value** 2330 2331 | Type | Description | 2332 | ------------------- | ------------- | 2333 | Promise<void> | Promise used to return the stream flushing result.| 2334 2335**Example** 2336 2337 ```js 2338 let filePath = pathDir + "/test.txt"; 2339 let ss= fs.createStreamSync(filePath, "r+"); 2340 ss.flush().then(() => { 2341 console.info("Stream flushed"); 2342 }).catch((err) => { 2343 console.info("flush failed with error message: " + err.message + ", error code: " + err.code); 2344 }); 2345 ``` 2346 2347 2348### flush 2349 2350flush(callback: AsyncCallback<void>): void 2351 2352Flushes the stream. This API uses an asynchronous callback to return the result. 2353 2354**System capability**: SystemCapability.FileManagement.File.FileIO 2355 2356**Parameters** 2357 2358 | Name | Type | Mandatory | Description | 2359 | -------- | ------------------------- | ---- | -------------- | 2360 | callback | AsyncCallback<void> | Yes | Callback invoked when the stream is asynchronously flushed.| 2361 2362**Example** 2363 2364 ```js 2365 let filePath = pathDir + "/test.txt"; 2366 let ss= fs.createStreamSync(filePath, "r+"); 2367 ss.flush((err) => { 2368 if (err) { 2369 console.info("flush stream failed with error message: " + err.message + ", error code: " + err.code); 2370 } else { 2371 console.info("flush success"); 2372 } 2373 }); 2374 ``` 2375 2376### flushSync 2377 2378flushSync(): void 2379 2380Synchronously flushes the stream. 2381 2382**System capability**: SystemCapability.FileManagement.File.FileIO 2383 2384**Example** 2385 2386 ```js 2387 let filePath = pathDir + "/test.txt"; 2388 let ss= fs.createStreamSync(filePath, "r+"); 2389 ss.flushSync(); 2390 ``` 2391 2392### write 2393 2394write(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }): Promise<number> 2395 2396Writes data into the stream. This API uses a promise to return the result. 2397 2398**System capability**: SystemCapability.FileManagement.File.FileIO 2399 2400**Parameters** 2401 2402 | Name | Type | Mandatory | Description | 2403 | ------- | ------------------------------- | ---- | ---------------------------------------- | 2404 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 2405 | options | Object | No | The options are as follows:<br>- **length** (number): length of the data to write. The default value is the buffer length.<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 2406 2407**Return value** 2408 2409 | Type | Description | 2410 | --------------------- | -------- | 2411 | Promise<number> | Promise used to return the length of the data written.| 2412 2413**Example** 2414 2415 ```js 2416 let filePath = pathDir + "/test.txt"; 2417 let ss= fs.createStreamSync(filePath, "r+"); 2418 ss.write("hello, world",{ offset: 5, length: 5, encoding: 'utf-8' }).then((number) => { 2419 console.info("write succeed and size is:" + number); 2420 }).catch((err) => { 2421 console.info("write failed with error message: " + err.message + ", error code: " + err.code); 2422 }); 2423 ``` 2424 2425 2426### write 2427 2428write(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }, callback: AsyncCallback<number>): void 2429 2430Writes data into the stream. This API uses an asynchronous callback to return the result. 2431 2432**System capability**: SystemCapability.FileManagement.File.FileIO 2433 2434**Parameters** 2435 2436 | Name | Type | Mandatory| Description | 2437 | -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 2438 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 2439 | options | Object | No | The options are as follows:<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 2440 | callback | AsyncCallback<number> | Yes | Callback invoked when the data is written asynchronously. | 2441 2442**Example** 2443 2444 ```js 2445 let filePath = pathDir + "/test.txt"; 2446 let ss= fs.createStreamSync(filePath, "r+"); 2447 ss.write("hello, world", { offset: 5, length: 5, encoding :'utf-8'}, (err, bytesWritten) => { 2448 if (err) { 2449 console.info("write stream failed with error message: " + err.message + ", error code: " + err.code); 2450 } else { 2451 if (bytesWritten) { 2452 console.info("write succeed and size is:" + bytesWritten); 2453 } 2454 } 2455 }); 2456 ``` 2457 2458### writeSync 2459 2460writeSync(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }): number 2461 2462Synchronously writes data into the stream. 2463 2464**System capability**: SystemCapability.FileManagement.File.FileIO 2465 2466**Parameters** 2467 2468 | Name | Type | Mandatory | Description | 2469 | ------- | ------------------------------- | ---- | ---------------------------------------- | 2470 | buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. | 2471 | options | Object | No | The options are as follows:<br>- **length** (number): length of the data to write. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to write the data in the file. This parameter is optional. By default, data is written from the current position.<br>- **encoding** (string): format of the data to be encoded when the data is a string. The default value is **'utf-8'**, which is the only value supported.| 2472 2473**Return value** 2474 2475 | Type | Description | 2476 | ------ | -------- | 2477 | number | Length of the data written in the file.| 2478 2479**Example** 2480 2481 ```js 2482 let filePath = pathDir + "/test.txt"; 2483 let ss= fs.createStreamSync(filePath,"r+"); 2484 let num = ss.writeSync("hello, world", {offset: 5, length: 5, encoding :'utf-8'}); 2485 ``` 2486 2487### read 2488 2489read(buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): Promise<number> 2490 2491Reads data from the stream. This API uses a promise to return the result. 2492 2493**System capability**: SystemCapability.FileManagement.File.FileIO 2494 2495**Parameters** 2496 2497 | Name | Type | Mandatory | Description | 2498 | ------- | ----------- | ---- | ---------------------------------------- | 2499 | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 2500 | options | Object | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): position of the data to read in the file. By default, data is read from the current position.| 2501 2502**Return value** 2503 2504 | Type | Description | 2505 | ---------------------------------- | ------ | 2506 | Promise<number> | Promise used to return the data read.| 2507 2508**Example** 2509 2510 ```js 2511 let filePath = pathDir + "/test.txt"; 2512 let ss = fs.createStreamSync(filePath, "r+"); 2513 let buf = new ArrayBuffer(4096); 2514 ss.read(buf, {offset: 5, length: 5}).then((readLen) => { 2515 console.info("Read data successfully"); 2516 console.log(String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen)))); 2517 }).catch((err) => { 2518 console.info("read data failed with error message: " + err.message + ", error code: " + err.code); 2519 }); 2520 ``` 2521 2522 2523### read 2524 2525read(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }, callback: AsyncCallback<number>): void 2526 2527Reads data from the stream. This API uses an asynchronous callback to return the result. 2528 2529**System capability**: SystemCapability.FileManagement.File.FileIO 2530 2531**Parameters** 2532 2533 | Name | Type | Mandatory | Description | 2534 | -------- | ---------------------------------------- | ---- | ---------------------------------------- | 2535 | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 2536 | options | Object | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): position of the data to read in the file. This parameter is optional. By default, data is read from the current position.| 2537 | callback | AsyncCallback<number> | Yes | Callback invoked when data is read asynchronously from the stream. | 2538 2539**Example** 2540 2541 ```js 2542 let filePath = pathDir + "/test.txt"; 2543 let ss = fs.createStreamSync(filePath, "r+"); 2544 let buf = new ArrayBuffer(4096) 2545 ss.read(buf, {offset: 5, length: 5}, (err, readLen) => { 2546 if (err) { 2547 console.info("read stream failed with error message: " + err.message + ", error code: " + err.code); 2548 } else { 2549 console.info("Read data successfully"); 2550 console.log(String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen)))); 2551 } 2552 }); 2553 ``` 2554 2555### readSync 2556 2557readSync(buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number 2558 2559Synchronously reads data from the stream. 2560 2561**System capability**: SystemCapability.FileManagement.File.FileIO 2562 2563**Parameters** 2564 2565 | Name | Type | Mandatory | Description | 2566 | ------- | ----------- | ---- | ---------------------------------------- | 2567 | buffer | ArrayBuffer | Yes | Buffer used to store the file read. | 2568 | options | Object | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): position of the data to read in the file. By default, data is read from the current position.<br> | 2569 2570**Return value** 2571 2572 | Type | Description | 2573 | ------ | -------- | 2574 | number | Length of the data read.| 2575 2576**Example** 2577 2578 ```js 2579 let filePath = pathDir + "/test.txt"; 2580 let ss = fs.createStreamSync(filePath, "r+"); 2581 let num = ss.readSync(new ArrayBuffer(4096), {offset: 5, length: 5}); 2582 ``` 2583 2584## File 2585 2586Represents a **File** object opened by **open()**. 2587 2588**System capability**: SystemCapability.FileManagement.File.FileIO 2589 2590### Attributes 2591 2592| Name | Type | Readable | Writable | Description | 2593| ---- | ------ | ---- | ---- | ------- | 2594| fd | number | Yes | No | FD of the file.| 2595 2596### lock 2597 2598lock(exclusive?: boolean): Promise\<void>; 2599 2600Applies an exclusive lock or a shared lock on this file in blocking mode. This API uses a promise to return the result. 2601 2602**System capability**: SystemCapability.FileManagement.File.FileIO 2603 2604**Parameters** 2605 2606 | Name | Type | Mandatory | Description | 2607 | ------- | ----------- | ---- | ---------------------------------------- | 2608 | exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | 2609 2610**Return value** 2611 2612 | Type | Description | 2613 | ---------------------------------- | ------ | 2614 | Promise<void> | Promise that returns no value.| 2615 2616**Example** 2617 2618 ```js 2619 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2620 file.lock(true).then(() => { 2621 console.log("lock file successful"); 2622 }).catch((err) => { 2623 console.info("lock file failed with error message: " + err.message + ", error code: " + err.code); 2624 }); 2625 ``` 2626 2627### lock 2628 2629lock(exclusive?: boolean, callback: AsyncCallback\<void>): void; 2630 2631Applies an exclusive lock or a shared lock on this file in blocking mode. This API uses a promise to return the result. 2632 2633**System capability**: SystemCapability.FileManagement.File.FileIO 2634 2635**Parameters** 2636 2637 | Name | Type | Mandatory | Description | 2638 | ------- | ----------- | ---- | ---------------------------------------- | 2639 | exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | 2640 | callback | AsyncCallback<void> | Yes | Callback invoked when the file is locked. | 2641 2642**Example** 2643 2644 ```js 2645 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2646 file.lock(true, (err) => { 2647 if (err) { 2648 console.info("lock file failed with error message: " + err.message + ", error code: " + err.code); 2649 } else { 2650 console.log("lock file successful"); 2651 } 2652 }); 2653 ``` 2654 2655### tryLock 2656 2657tryLock(exclusive?: boolean): void; 2658 2659Applies an exclusive lock or a shared lock on this file in non-blocking mode. 2660 2661**System capability**: SystemCapability.FileManagement.File.FileIO 2662 2663**Parameters** 2664 2665 | Name | Type | Mandatory | Description | 2666 | ------- | ----------- | ---- | ---------------------------------------- | 2667 | exclusive | boolean | No | Lock to apply. The value **true** means an exclusive lock, and the value **false** (default) means a shared lock. | 2668 2669**Example** 2670 2671 ```js 2672 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2673 file.tryLock(true); 2674 console.log("lock file successful"); 2675 ``` 2676 2677### unlock 2678 2679unlock(): void; 2680 2681Unlocks this file synchronously. 2682 2683**System capability**: SystemCapability.FileManagement.File.FileIO 2684 2685**Example** 2686 2687 ```js 2688 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 2689 file.tryLock(true); 2690 file.unlock(); 2691 console.log("unlock file successful"); 2692 ``` 2693 2694## OpenMode 2695 2696Defines the constants of the **mode** parameter used in **open()**. It species the mode for opening a file. 2697 2698**System capability**: SystemCapability.FileManagement.File.FileIO 2699 2700| Name | Type | Value | Description | 2701| ---- | ------ |---- | ------- | 2702| READ_ONLY | number | 0o0 | Open the file in read-only mode.| 2703| WRITE_ONLY | number | 0o1 | Open the file in write-only mode.| 2704| READ_WRITE | number | 0o2 | Open the file in read/write mode.| 2705| CREATE | number | 0o100 | Create a file if the specified file does not exist.| 2706| TRUNC | number | 0o1000 | If the file exists and is open in write-only or read/write mode, truncate the file length to 0.| 2707| APPEND | number | 0o2000 | Open the file in append mode. New data will be written to the end of the file.| 2708| NONBLOCK | number | 0o4000 | If **path** points to a named pipe (FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.| 2709| DIR | number | 0o200000 | If **path** does not point to a directory, throw an exception.| 2710| NOFOLLOW | number | 0o400000 | If **path** points to a symbolic link, throw an exception.| 2711| SYNC | number | 0o4010000 | Open the file in synchronous I/O mode.| 2712 2713## Filter 2714 2715**System capability**: SystemCapability.FileManagement.File.FileIO 2716 2717Defines the file filtering configuration, which can be used by **listFile()**. 2718 2719| Name | Type | Description | 2720| ----------- | --------------- | ------------------ | 2721| suffix | Array<string> | Locate files that fully match the specified file name extensions, which are of the OR relationship. | 2722| displayName | Array<string> | Locate files that fuzzy match the specified file names, which are of the OR relationship.| 2723| mimeType | Array<string> | Locate files that fully match the specified MIME types, which are of the OR relationship. | 2724| fileSizeOver | number | Locate files that are greater than or equal to the specified size. | 2725| lastModifiedAfter | number | Locate files whose last modification time is the same or later than the specified time. | 2726| excludeMedia | boolean | Whether to exclude the files already in **Media**. | 2727