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