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