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