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