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