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