1# @ohos.file.fileAccess (User File Access and Management) (System API) 2 3The **fileAccess** module provides a framework for accessing and operating user files based on [extension](../../application-models/extensionability-overview.md). This module interacts with a variety of file management services, such as the storage management service, and provides a set of unified file access and management interfaces for system applications. The storage management service manages both the directories of the built-in storage and resources on external devices, such as shared disks, USB flash drives, and SD cards. 4 5>**NOTE** 6> 7> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - The APIs provided by this module are system APIs. 9> - Currently, the APIs of this module can be called only by **FilePicker** and **FileManager**. 10 11## Modules to Import 12 13```ts 14import fileAccess from '@ohos.file.fileAccess'; 15``` 16 17## Constant 18 19Represents a URI used for observing the device online/offline status. 20 21**Model restriction**: This constant can be used only in the stage model. 22 23**System capability**: SystemCapability.FileManagement.UserFileService 24 25**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 26 27| Name| Type | Read-Only| Writable| Description | 28| ---- | --------------------------- | ---- | ---- | --------------------------------------------------------- | 29| DEVICES_URI<sup>11+</sup> | string | Yes | No | URI used for observing the device online/offline status. | 30 31## fileAccess.getFileAccessAbilityInfo 32 33getFileAccessAbilityInfo() : Promise<Array<Want>> 34 35Obtains information about all Wants with **extension** set to **fileAccess** in the system. A Want contains information for starting an ability. This API uses a promise to return the result. 36 37**Model restriction**: This API can be used only in the stage model. 38 39**System capability**: SystemCapability.FileManagement.UserFileService 40 41**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 42 43**Return value** 44 45| Type| Description| 46| --- | -- | 47| Promise<Array<[Want](../apis-ability-kit/js-apis-app-ability-want.md)>> | Promise used to return the want information obtained.| 48 49**Error codes** 50 51For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 52 53**Example** 54 55 ```ts 56 import { BusinessError } from '@ohos.base'; 57 import Want from '@ohos.app.ability.Want'; 58 async function getFileAccessAbilityInfo() { 59 let wantInfos: Array<Want> = []; 60 try { 61 wantInfos = await fileAccess.getFileAccessAbilityInfo(); 62 console.log("getFileAccessAbilityInfo data " + JSON.stringify(wantInfos)); 63 } catch (err) { 64 let error: BusinessError = err as BusinessError; 65 console.error("getFileAccessAbilityInfo failed, errCode:" + error.code + ", errMessage:" + error.message); 66 } 67 } 68 ``` 69 70## fileAccess.getFileAccessAbilityInfo 71 72getFileAccessAbilityInfo(callback: AsyncCallback<Array<Want>>): void 73 74Obtains information about all Wants with **extension** set to **fileAccess** in the system. A Want contains information for starting an ability. This API uses an asynchronous callback to return the result. 75 76**Model restriction**: This API can be used only in the stage model. 77 78**System capability**: SystemCapability.FileManagement.UserFileService 79 80**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 81 82**Parameters** 83 84| Name| Type| Mandatory| Description| 85| --- | --- | --- | -- | 86| callback | AsyncCallback<Array<[Want](../apis-ability-kit/js-apis-app-ability-want.md)>> | Yes| Callback used to return the want information obtained.| 87 88**Error codes** 89 90For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 91 92**Example** 93 94 ```ts 95 import { BusinessError } from '@ohos.base'; 96 import Want from '@ohos.app.ability.Want'; 97 async function getFileAccessAbilityInfo() { 98 try { 99 fileAccess.getFileAccessAbilityInfo((err: BusinessError, wantInfos: Array<Want>) => { 100 if (err) { 101 console.error("Failed to getFileAccessAbilityInfo in async, errCode:" + err.code + ", errMessage:" + err.message); 102 return; 103 } 104 console.log("getFileAccessAbilityInfo data " + JSON.stringify(wantInfos)); 105 }); 106 } catch (err) { 107 let error: BusinessError = err as BusinessError; 108 console.error("getFileAccessAbilityInfo failed, errCode:" + error.code + ", errMessage:" + error.message); 109 } 110 } 111 ``` 112 113## fileAccess.createFileAccessHelper 114 115createFileAccessHelper(context: Context, wants: Array<Want>) : FileAccessHelper 116 117Creates a **Helper** object to bind with the specified Wants. This API returns the result synchronously. The **Helper** object provides file access and management capabilities. 118 119**Model restriction**: This API can be used only in the stage model. 120 121**System capability**: SystemCapability.FileManagement.UserFileService 122 123**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 124 125**Parameters** 126 127| Name| Type| Mandatory| Description| 128| --- | --- | --- | -- | 129| context | [Context](../apis-ability-kit/js-apis-inner-application-context.md) | Yes| Context of the ability.| 130| wants | Array<[Want](../apis-ability-kit/js-apis-app-ability-want.md)> | Yes| Wants to start the abilities.| 131 132**Return value** 133 134| Type| Description| 135| --- | -- | 136| [FileAccessHelper](#fileaccesshelper) | **Helper** object created.| 137 138**Error codes** 139 140For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 141 142**Example** 143 144 ```ts 145 import { BusinessError } from '@ohos.base'; 146 import Want from '@ohos.app.ability.Want'; 147 import common from '@ohos.app.ability.common'; 148 let context = getContext(this) as common.UIAbilityContext; 149 function createFileAccessHelper01() { 150 let fileAccessHelper: fileAccess.FileAccessHelper; 151 // Obtain wantInfos by using getFileAccessAbilityInfo(). 152 let wantInfos: Array<Want> = [ 153 { 154 bundleName: "com.ohos.UserFile.ExternalFileManager", 155 abilityName: "FileExtensionAbility", 156 }, 157 ] 158 try { 159 // context is passed by EntryAbility. 160 fileAccessHelper = fileAccess.createFileAccessHelper(context, wantInfos); 161 if (!fileAccessHelper) { 162 console.error("createFileAccessHelper interface returns an undefined object"); 163 } 164 } catch (err) { 165 let error: BusinessError = err as BusinessError; 166 console.error("createFileAccessHelper failed, errCode:" + error.code + ", errMessage:" + error.message); 167 } 168 } 169 ``` 170 171## fileAccess.createFileAccessHelper 172 173createFileAccessHelper(context: Context) : FileAccessHelper 174 175Creates a **Helper** object to bind with all file management services in the system. This API returns the result synchronously. 176 177**Model restriction**: This API can be used only in the stage model. 178 179**System capability**: SystemCapability.FileManagement.UserFileService 180 181**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 182 183**Parameters** 184 185| Name| Type| Mandatory| Description| 186| --- | --- | --- | -- | 187| context | [Context](../apis-ability-kit/js-apis-inner-application-context.md) | Yes| Context of the ability.| 188 189**Return value** 190 191| Type| Description| 192| --- | -- | 193| [FileAccessHelper](#fileaccesshelper) | **Helper** object created.| 194 195**Error codes** 196 197For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 198 199**Example** 200 201 ```ts 202 import { BusinessError } from '@ohos.base'; 203 import common from '@ohos.app.ability.common'; 204 let context = getContext(this) as common.UIAbilityContext; 205 function createFileAccessHelper02() { 206 let fileAccessHelperAllServer: fileAccess.FileAccessHelper; 207 // Create a Helper object to interact with all file management services configured with fileAccess in the system. 208 try { 209 // context is passed by EntryAbility. 210 fileAccessHelperAllServer = fileAccess.createFileAccessHelper(context); 211 if (!fileAccessHelperAllServer) { 212 console.error("createFileAccessHelper interface returns an undefined object"); 213 } 214 } catch (err) { 215 let error: BusinessError = err as BusinessError; 216 console.error("createFileAccessHelper failed, errCode:" + error.code + ", errMessage:" + error.message); 217 } 218 } 219 ``` 220 221## FileInfo 222 223Provides APIs for managing file or folder attribute information. 224 225**Model restriction**: This API can be used only in the stage model. 226 227**System capability**: SystemCapability.FileManagement.UserFileService 228 229**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 230 231### Properties 232 233| Name| Type | Read-Only| Writable| Description | 234| ------ | ------ | -------- | ------ | -------- | 235| uri | string | Yes| No| URI of the file or folder.| 236| relativePath<sup>10+</sup> | string | Yes| No| Relative path of the file or folder.| 237| fileName | string | Yes| No| Name of the file or folder.| 238| mode | number | Yes| No| Permissions on the file or folder.| 239| size | number | Yes| No| Size of the file or folder.| 240| mtime | number | Yes| No| Time when the file or folder was last modified.| 241| mimeType | string | Yes| No| Multipurpose Internet Mail Extensions (MIME) type of the file or folder.| 242 243### listFile 244 245listFile(filter?: Filter) : FileIterator 246 247Obtains a **FileIterator** object that lists the next-level files (folders) matching the specified conditions of this directory. This API returns the result synchronously. [FileInfo](#fileinfo) is returned by [next()](#next). Currently, only built-in storage devices support the file filter. 248 249**Model restriction**: This API can be used only in the stage model. 250 251**System capability**: SystemCapability.FileManagement.UserFileService 252 253**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 254 255**Parameters** 256 257| Name| Type| Mandatory| Description| 258| --- | --- | -- | -- | 259| filter | [Filter](js-apis-file-fs.md#filter) | No| **Filter** object that specifies the conditions for listing files. | 260 261**Return value** 262 263| Type| Description| 264| --- | -- | 265| [FileIterator](#fileiterator) | **FileIterator** object obtained.| 266 267**Error codes** 268 269For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 270 271**Example** 272 273 ```ts 274 import { BusinessError } from '@ohos.base'; 275 // fileInfoDir indicates information about a directory. 276 // let filter = { suffix : [".txt", ".jpg", ".xlsx"] }; 277 let fileInfoDir: fileAccess.FileInfo; // = fileInfos[0]; 278 let subfileInfos: Array<fileAccess.FileInfo> = []; 279 let isDone: boolean = false; 280 try { 281 let fileIterator = fileInfoDir.listFile(); 282 // listFile() with the filter implementation. 283 // let fileIterator = fileInfoDir.listFile(filter); 284 if (!fileIterator) { 285 console.error("listFile interface returns an undefined object"); 286 } 287 while (!isDone) { 288 let result = fileIterator.next(); 289 console.log("next result = " + JSON.stringify(result)); 290 isDone = result.done; 291 if (!isDone) { 292 subfileInfos.push(result.value); 293 } 294 } 295 } catch (err) { 296 let error: BusinessError = err as BusinessError; 297 console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message); 298 } 299 ``` 300 301### scanFile 302 303scanFile(filter?: Filter) : FileIterator; 304 305Obtains a **FileIterator** object that recursively retrieves the files matching the specified conditions of this directory. This API returns the result synchronously. [FileInfo](#fileinfo) is returned by [next()](#next). Currently, this API supports only built-in storage devices. 306 307**Model restriction**: This API can be used only in the stage model. 308 309**System capability**: SystemCapability.FileManagement.UserFileService 310 311**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 312 313**Parameters** 314 315| Name| Type| Mandatory| Description| 316| --- | --- | -- | -- | 317| filter | [Filter](js-apis-file-fs.md#filter) | No| **Filter** object. | 318 319**Return value** 320 321| Type| Description| 322| --- | -- | 323| [FileIterator](#fileiterator) | **FileIterator** object obtained.| 324 325**Error codes** 326 327For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 328 329**Example** 330 331 ```ts 332 import { BusinessError } from '@ohos.base'; 333 // fileInfoDir indicates information about a directory. 334 // let filter = {suffix : [".txt", ".jpg", ".xlsx"]}; 335 let fileInfoDir: fileAccess.FileInfo; // = fileInfos[0]; 336 let subfileInfos: Array<fileAccess.FileInfo> = []; 337 let isDone: boolean = false; 338 try { 339 let fileIterator = fileInfoDir.scanFile(); 340 // scanFile() with the filter implementation. 341 // let fileIterator = fileInfoDir.scanFile(filter); 342 if (!fileIterator) { 343 console.error("scanFile interface returns an undefined object"); 344 } 345 while (!isDone) { 346 let result = fileIterator.next(); 347 console.log("next result = " + JSON.stringify(result)); 348 isDone = result.done; 349 if (!isDone) { 350 subfileInfos.push(result.value); 351 } 352 } 353 } catch (err) { 354 let error: BusinessError = err as BusinessError; 355 console.error("scanFile failed, errCode:" + error.code + ", errMessage:" + error.message); 356 } 357 ``` 358 359## FileIterator 360 361Provides the **FileIterator** object. 362 363**Model restriction**: This API can be used only in the stage model. 364 365**System capability**: SystemCapability.FileManagement.UserFileService 366 367**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 368 369### next 370 371next() : { value: FileInfo, done: boolean } 372 373Obtains information about the next-level files or folders. 374 375**Model restriction**: This API can be used only in the stage model. 376 377**System capability**: SystemCapability.FileManagement.UserFileService 378 379**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 380 381**Return value** 382 383| Type| Description| 384| --- | -- | 385| {value: [FileInfo](#fileinfo), done: boolean} | File or folder information obtained. This API traverses the directory until **true** is returned. The **value** field contains the file or folder information obtained.| 386 387**Error codes** 388 389For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 390 391## RootInfo 392 393Provides APIs for managing the device's root attribute information. 394 395**Model restriction**: This API can be used only in the stage model. 396 397**System capability**: SystemCapability.FileManagement.UserFileService 398 399**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 400 401### Properties 402 403| Name| Type | Read-Only| Writable| Description | 404| ------ | ------ | -------- | ------ | -------- | 405| deviceType | number | Yes| No|Type of the device.| 406| uri | string | Yes| No| Root directory URI of the device.| 407| relativePath<sup>10+</sup> | string | Yes| No| Relative path of the root directory.| 408| displayName | string | Yes| No| Device name.| 409| deviceFlags | number | Yes| No| Capabilities supported by the device.| 410 411### listFile 412 413listFile(filter?: Filter) : FileIterator 414 415Obtains a **FileIterator** object that lists the first-level files (directories) matching the specified conditions from the device root directory. This API returns the result synchronously. [FileInfo](#fileinfo) is return by [next()](#next-1). Currently, only built-in storage devices support the file filter. 416 417**Model restriction**: This API can be used only in the stage model. 418 419**System capability**: SystemCapability.FileManagement.UserFileService 420 421**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 422 423**Parameters** 424 425| Name| Type| Mandatory| Description| 426| --- | --- | -- | -- | 427| filter | [Filter](js-apis-file-fs.md#filter) | No| **Filter** object. | 428 429**Return value** 430 431| Type| Description| 432| --- | -- | 433| [FileIterator](#fileiterator) | **FileIterator** object obtained.| 434 435**Error codes** 436 437For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 438 439**Example** 440 441 ```ts 442 import { BusinessError } from '@ohos.base'; 443 // Obtain rootInfos by using getRoots(). 444 // let filter = {suffix : [".txt", ".jpg", ".xlsx"]}; 445 let rootInfo: fileAccess.RootInfo; // = rootinfos[0]; 446 let fileInfos: Array<fileAccess.FileInfo> = []; 447 let isDone: boolean = false; 448 try { 449 let fileIterator = rootInfo.listFile(); 450 // listFile() with the filter implementation. 451 // let fileIterator = rootInfo.listFile(filter); 452 if (!fileIterator) { 453 console.error("listFile interface returns an undefined object"); 454 } 455 while (!isDone) { 456 let result = fileIterator.next(); 457 console.log("next result = " + JSON.stringify(result)); 458 isDone = result.done; 459 if (!isDone) { 460 fileInfos.push(result.value); 461 } 462 } 463 } catch (err) { 464 let error: BusinessError = err as BusinessError; 465 console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message); 466 } 467 ``` 468 469### scanFile 470 471scanFile(filter?: Filter) : FileIterator 472 473Obtains a **FileIterator** object that recursively retrieves the files matching the specified conditions from the device root directory. This API returns the result synchronously. [FileInfo](#fileinfo) is returned by [next()](#next-1). Currently, this API supports only built-in storage devices. 474 475**Model restriction**: This API can be used only in the stage model. 476 477**System capability**: SystemCapability.FileManagement.UserFileService 478 479**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 480 481**Parameters** 482 483| Name| Type| Mandatory| Description| 484| --- | --- | -- | -- | 485| filter | [Filter](js-apis-file-fs.md#filter) | No| **Filter** object. | 486 487**Return value** 488 489| Type| Description| 490| --- | -- | 491| [FileIterator](#fileiterator) | **FileIterator** object obtained.| 492 493**Error codes** 494 495For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 496 497**Example** 498 499 ```ts 500 import { BusinessError } from '@ohos.base'; 501 // Obtain rootInfos by using getRoots(). 502 // let filter = {suffix : [".txt", ".jpg", ".xlsx"]}; 503 let rootInfo: fileAccess.RootInfo; // = rootinfos[0]; 504 let fileInfos: Array<fileAccess.FileInfo> = []; 505 let isDone: boolean = false; 506 try { 507 let fileIterator = rootInfo.scanFile(); 508 // scanFile with the filter implementation. 509 // let fileIterator = rootInfo.scanFile(filter); 510 if (!fileIterator) { 511 console.error("scanFile interface returns undefined object"); 512 } 513 while (!isDone) { 514 let result = fileIterator.next(); 515 console.log("next result = " + JSON.stringify(result)); 516 isDone = result.done; 517 if (!isDone) { 518 fileInfos.push(result.value); 519 } 520 } 521 } catch (err) { 522 let error: BusinessError = err as BusinessError; 523 console.error("scanFile failed, errCode:" + error.code + ", errMessage:" + error.message); 524 } 525 ``` 526 527## RootIterator 528 529Provides an iterator object of the device root directory. 530 531**Model restriction**: This API can be used only in the stage model. 532 533**System capability**: SystemCapability.FileManagement.UserFileService 534 535**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 536 537### next 538 539next() : { value: RootInfo, done: boolean } 540 541Obtains the next-level root directory. 542 543**Model restriction**: This API can be used only in the stage model. 544 545**System capability**: SystemCapability.FileManagement.UserFileService 546 547**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 548 549**Return value** 550 551| Type| Description| 552| --- | -- | 553| {value: [RootInfo](#rootinfo), done: boolean} | Root directory information obtained. This API traverses the directory until **true** is returned. The **value** field contains the root directory information obtained.| 554 555**Error codes** 556 557For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 558 559## FileAccessHelper 560 561Provides a **FileAccessHelper** object. 562 563**System capability**: SystemCapability.FileManagement.UserFileService 564 565**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 566 567### getRoots 568 569getRoots() : Promise<RootIterator> 570 571Obtains information about the device root nodes of the file management services associated with the **Helper** object. This API uses a promise to return 572a **RootIterator** object. You can use [next](#next-1) to return [RootInfo](#rootinfo). 573 574**System capability**: SystemCapability.FileManagement.UserFileService 575 576**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 577 578**Return value** 579 580| Type| Description| 581| --- | -- | 582| Promise<[RootIterator](#rootiterator)> | Promise used to return a **RootIterator** object.| 583 584**Error codes** 585 586For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 587 588**Example** 589 590 ```ts 591async function getRoots() { 592 let rootIterator: fileAccess.RootIterator; 593 let rootinfos: Array<fileAccess.RootInfo> = []; 594 let isDone: boolean = false; 595 try { 596 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 597 rootIterator = await fileAccessHelper.getRoots(); 598 if (!rootIterator) { 599 console.error("getRoots interface returns an undefined object"); 600 } 601 while (!isDone) { 602 let result = rootIterator.next(); 603 console.log("next result = " + JSON.stringify(result)); 604 isDone = result.done; 605 if (!isDone) { 606 rootinfos.push(result.value); 607 } 608 } 609 } catch (err) { 610 let error: BusinessError = err as BusinessError; 611 console.error("getRoots failed, errCode:" + error.code + ", errMessage:" + error.message); 612 } 613} 614 ``` 615 616### getRoots 617 618getRoots(callback:AsyncCallback<RootIterator>) : void 619 620Obtains information about the device root nodes of the file management services associated with the **Helper** object. This API uses an asynchronous callback to return 621a **RootIterator** object. You can use [next](#next-1) to return [RootInfo](#rootinfo). 622 623**System capability**: SystemCapability.FileManagement.UserFileService 624 625**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 626 627**Parameters** 628 629| Name| Type| Mandatory| Description| 630| --- | --- | --- | -- | 631| callback | AsyncCallback<[RootIterator](#rootiterator)> | Yes| Callback used to return a **RootIterator** object.| 632 633**Error codes** 634 635For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 636 637**Example** 638 639 ```ts 640 import { BusinessError } from '@ohos.base'; 641 async function getRoots() { 642 let rootinfos: Array<fileAccess.RootInfo> = []; 643 let isDone: boolean = false; 644 try { 645 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 646 fileAccessHelper.getRoots((err: BusinessError, rootIterator: fileAccess.RootIterator) => { 647 if (err) { 648 console.error("Failed to getRoots in async, errCode:" + err.code + ", errMessage:" + err.message); 649 } 650 while (!isDone) { 651 let result = rootIterator.next(); 652 console.log("next result = " + JSON.stringify(result)); 653 isDone = result.done; 654 if (!isDone) { 655 rootinfos.push(result.value); 656 } 657 } 658 }); 659 } catch (err) { 660 let error: BusinessError = err as BusinessError; 661 console.error("getRoots failed, errCode:" + error.code + ", errMessage:" + error.message); 662 } 663 } 664 ``` 665 666### createFile 667 668createFile(uri: string, displayName: string) : Promise<string> 669 670Creates a file in a directory. This API uses a promise to return the result. 671 672**System capability**: SystemCapability.FileManagement.UserFileService 673 674**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 675 676**Parameters** 677 678| Name| Type| Mandatory| Description| 679| --- | --- | --- | -- | 680| uri | string | Yes| URI of the destination directory for the file to create.| 681| displayName | string | Yes| Name of the file to create. By default, the name of a local file must contain the file name extension.| 682 683**Return value** 684 685| Type| Description| 686| --- | -- | 687| Promise<string> | Promise used to return the URI of the file created.| 688 689**Error codes** 690 691For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 692 693**Example** 694 695 ```ts 696 import { BusinessError } from '@ohos.base'; 697 async function createFile() { 698 // A built-in storage directory is used as an example. 699 // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo. 700 // You can use the URI obtained. 701 let sourceUri: string = "file://docs/storage/Users/currentUser/Download"; 702 let displayName: string = "file1"; 703 let fileUri: string; 704 try { 705 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 706 fileUri = await fileAccessHelper.createFile(sourceUri, displayName); 707 if (!fileUri) { 708 console.error("createFile return undefined object"); 709 return; 710 } 711 console.log("createFile sucess, fileUri: " + JSON.stringify(fileUri)); 712 } catch (err) { 713 let error: BusinessError = err as BusinessError; 714 console.error("createFile failed, errCode:" + error.code + ", errMessage:" + error.message); 715 } 716 } 717 ``` 718 719### createFile 720 721createFile(uri: string, displayName: string, callback: AsyncCallback<string>) : void 722 723Creates a file in a directory. This API uses an asynchronous callback to return the result. 724 725**System capability**: SystemCapability.FileManagement.UserFileService 726 727**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 728 729**Parameters** 730 731| Name| Type| Mandatory| Description| 732| --- | --- | --- | -- | 733| uri | string | Yes| URI of the destination directory for the file to create.| 734| displayName | string | Yes| Name of the file to create. By default, the name of a local file must contain the file name extension.| 735| callback | AsyncCallback<string> | Yes| Callback used to return the URI of the file created.| 736 737**Error codes** 738 739For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 740 741**Example** 742 743 ```ts 744 import { BusinessError } from '@ohos.base'; 745 // A built-in storage directory is used as an example. 746 // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo. 747 // You can use the URI obtained. 748 let sourceUri: string = "file://docs/storage/Users/currentUser/Download"; 749 let displayName: string = "file1"; 750 try { 751 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 752 fileAccessHelper.createFile(sourceUri, displayName, (err: BusinessError, fileUri: string) => { 753 if (err) { 754 console.error("Failed to createFile in async, errCode:" + err.code + ", errMessage:" + err.message); 755 } 756 console.log("createFile sucess, fileUri: " + JSON.stringify(fileUri)); 757 }); 758 } catch (err) { 759 let error: BusinessError = err as BusinessError; 760 console.error("createFile failed, errCode:" + error.code + ", errMessage:" + error.message); 761 } 762 ``` 763 764### mkDir 765 766mkDir(parentUri: string, displayName: string) : Promise<string> 767 768Creates a folder in a directory. This API uses a promise to return the result. 769 770**System capability**: SystemCapability.FileManagement.UserFileService 771 772**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 773 774**Parameters** 775 776| Name| Type| Mandatory| Description| 777| --- | --- | --- | -- | 778| parentUri | string | Yes| URI of the destination directory for the folder to create.| 779| displayName | string | Yes| Name of the folder to create.| 780 781**Return value** 782 783| Type| Description| 784| --- | -- | 785| Promise<string> | Promise used to return the URI of the folder created.| 786 787**Error codes** 788 789For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 790 791**Example** 792 793 ```ts 794 import { BusinessError } from '@ohos.base'; 795 // A built-in storage directory is used as an example. 796 // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo. 797 // You can use the URI obtained. 798 async function createDirectory() { 799 let sourceUri: string = "file://docs/storage/Users/currentUser/Download"; 800 let dirName: string = "dirTest"; 801 let dirUri: string; 802 803 try { 804 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 805 dirUri = await fileAccessHelper.mkDir(sourceUri, dirName); 806 if (!dirUri) { 807 console.error("mkDir return undefined object"); 808 } else { 809 console.log("mkDir success, dirUri: " + JSON.stringify(dirUri)); 810 } 811 } catch (err) { 812 let error: BusinessError = err as BusinessError; 813 console.error("mkDir failed, errCode:" + error.code + ", errMessage:" + error.message); 814 } 815 } 816 ``` 817 818### mkDir 819 820mkDir(parentUri: string, displayName: string, callback: AsyncCallback<string>) : void 821 822Creates a folder. This API uses an asynchronous callback to return the result. 823 824**System capability**: SystemCapability.FileManagement.UserFileService 825 826**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 827 828**Parameters** 829 830| Name| Type| Mandatory| Description| 831| --- | --- | --- | -- | 832| parentUri | string | Yes| URI of the destination directory for the folder to create.| 833| displayName | string | Yes| Name of the folder to create.| 834| callback | AsyncCallback<string> | Yes| Callback used to return the URI of the folder created.| 835 836**Error codes** 837 838For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 839 840**Example** 841 842 ```ts 843 import { BusinessError } from '@ohos.base'; 844 // A built-in storage directory is used as an example. 845 // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo. 846 // You can use the URI obtained. 847 let sourceUri: string = "file://docs/storage/Users/currentUser/Download"; 848 let dirName: string = "dirTest"; 849 try { 850 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 851 fileAccessHelper.mkDir(sourceUri, dirName, (err: BusinessError, dirUri: string) => { 852 if (err) { 853 console.error("Failed to mkDir in async, errCode:" + err.code + ", errMessage:" + err.message); 854 } 855 console.log("mkDir sucess, dirUri: " + JSON.stringify(dirUri)); 856 }); 857 } catch (err) { 858 let error: BusinessError = err as BusinessError; 859 console.error("mkDir failed, errCode:" + error.code + ", errMessage:" + error.message); 860 } 861 ``` 862 863### openFile 864 865openFile(uri: string, flags: OPENFLAGS) : Promise<number> 866 867Opens a file. This API uses a promise to return the result. 868 869**System capability**: SystemCapability.FileManagement.UserFileService 870 871**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 872 873**Parameters** 874 875| Name| Type| Mandatory| Description| 876| --- | --- | --- | -- | 877| uri | string | Yes| URI of the file to open.| 878| flags | [OPENFLAGS](#openflags) | Yes| File open mode.| 879 880**Return value** 881 882| Type| Description| 883| --- | -- | 884| Promise<number> | Promise used to return the file descriptor (FD) of the file opened.| 885 886**Error codes** 887 888For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 889 890**Example** 891 892 ```ts 893 import { BusinessError } from '@ohos.base'; 894 async function openFile01() { 895 // A built-in storage directory is used as an example. 896 // In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo. 897 // You can use the URI obtained. 898 let targetUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 899 try { 900 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 901 let fd = await fileAccessHelper.openFile(targetUri, fileAccess.OPENFLAGS.READ); 902 } catch (err) { 903 let error: BusinessError = err as BusinessError; 904 console.error("openFile failed, errCode:" + error.code + ", errMessage:" + error.message); 905 } 906 } 907 ``` 908 909### openFile 910 911openFile(uri: string, flags: OPENFLAGS, callback: AsyncCallback<number>) : void 912 913Opens a file. This API uses an asynchronous callback to return the result. 914 915**System capability**: SystemCapability.FileManagement.UserFileService 916 917**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 918 919**Parameters** 920 921| Name| Type| Mandatory| Description| 922| --- | --- | --- | -- | 923| uri | string | Yes| URI of the file to open.| 924| flags | [OPENFLAGS](#openflags) | Yes| File open mode.| 925| callback | AsyncCallback<number> | Yes| Callback used to return the FD of the file opened.| 926 927**Error codes** 928 929For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 930 931**Example** 932 933 ```ts 934 import { BusinessError } from '@ohos.base'; 935 // A built-in storage directory is used as an example. 936 // In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo. 937 // You can use the URI obtained. 938 let targetUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 939 try { 940 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 941 fileAccessHelper.openFile(targetUri, fileAccess.OPENFLAGS.READ, (err: BusinessError, fd: number) => { 942 if (err) { 943 console.error("Failed to openFile in async, errCode:" + err.code + ", errMessage:" + err.message); 944 } 945 console.log("openFile sucess, fd: " + fd); 946 }); 947 } catch (err) { 948 let error: BusinessError = err as BusinessError; 949 console.error("openFile failed, errCode:" + error.code + ", errMessage:" + error.message); 950 } 951 ``` 952 953### delete 954 955delete(uri: string) : Promise<number> 956 957Deletes a file or folder. This API uses a promise to return the result. 958 959**System capability**: SystemCapability.FileManagement.UserFileService 960 961**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 962 963**Parameters** 964 965| Name| Type| Mandatory| Description| 966| --- | --- | --- | -- | 967| uri | string | Yes| URI of the file or folder to delete.| 968 969**Return value** 970 971| Type| Description| 972| --- | -- | 973| Promise<number> | Promise used to return the result.| 974 975**Error codes** 976 977For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 978 979**Example** 980 981 ```ts 982 import { BusinessError } from '@ohos.base'; 983 async function deleteFile01() { 984 // A built-in storage directory is used as an example. 985 // In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo. 986 // You can use the URI obtained. 987 let targetUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 988 try { 989 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 990 let code = await fileAccessHelper.delete(targetUri); 991 if (code != 0) 992 console.error("delete failed, code " + code); 993 } catch (err) { 994 let error: BusinessError = err as BusinessError; 995 console.error("delete failed, errCode:" + error.code + ", errMessage:" + error.message); 996 } 997 } 998 ``` 999 1000### delete 1001 1002delete(uri: string, callback: AsyncCallback<number>) : void 1003 1004Deletes a file or folder. This API uses an asynchronous callback to return the result. 1005 1006**System capability**: SystemCapability.FileManagement.UserFileService 1007 1008**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1009 1010**Parameters** 1011 1012| Name| Type| Mandatory| Description| 1013| --- | --- | --- | -- | 1014| uri | string | Yes| URI of the file or folder to delete.| 1015| callback | AsyncCallback<number> | Yes| Callback used to return the result.| 1016 1017**Error codes** 1018 1019For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 1020 1021**Example** 1022 1023 ```ts 1024 import { BusinessError } from '@ohos.base'; 1025 // A built-in storage directory is used as an example. 1026 // In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo. 1027 // You can use the URI obtained. 1028 let targetUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1029 try { 1030 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1031 fileAccessHelper.delete(targetUri, (err: BusinessError, code: number) => { 1032 if (err) { 1033 console.error("Failed to delete in async, errCode:" + err.code + ", errMessage:" + err.message); 1034 } 1035 console.log("delete sucess, code: " + code); 1036 }); 1037 } catch (err) { 1038 let error: BusinessError = err as BusinessError; 1039 console.error("delete failed, errCode:" + error.code + ", errMessage:" + error.message); 1040 } 1041 ``` 1042 1043### move 1044 1045move(sourceFile: string, destFile: string) : Promise<string> 1046 1047Moves a file or folder. This API uses a promise to return the result. Currently, this API does not support move of files or folders across devices. 1048 1049**System capability**: SystemCapability.FileManagement.UserFileService 1050 1051**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1052 1053**Parameters** 1054 1055| Name| Type| Mandatory| Description| 1056| --- | --- | --- | -- | 1057| sourceFile | string | Yes| URI of the file or folder to move.| 1058| destFile | string | Yes| URI of the destination directory, to which the file or folder is moved.| 1059 1060**Return value** 1061 1062| Type| Description| 1063| ----- | ------ | 1064| Promise<string> | Promise used to return the URI of the file or folder in the destination directory.| 1065 1066**Error codes** 1067 1068For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 1069 1070**Example** 1071 1072 ```ts 1073 import { BusinessError } from '@ohos.base'; 1074 async function moveFile01() { 1075 // A built-in storage directory is used as an example. 1076 // In the sample code, sourceFile and destFile indicate the files and directories in the Download directory. The URI is the URI in fileInfo. 1077 // You can use the URI obtained. 1078 let sourceFile: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1079 let destFile: string = "file://docs/storage/Users/currentUser/Download/test"; 1080 try { 1081 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1082 let fileUri = await fileAccessHelper.move(sourceFile, destFile); 1083 console.log("move sucess, fileUri: " + JSON.stringify(fileUri)); 1084 } catch (err) { 1085 let error: BusinessError = err as BusinessError; 1086 console.error("move failed, errCode:" + error.code + ", errMessage:" + error.message); 1087 } 1088 } 1089 ``` 1090 1091### move 1092 1093move(sourceFile: string, destFile: string, callback: AsyncCallback<string>) : void 1094 1095Moves a file or folder. This API uses an asynchronous callback to return the result. Currently, this API does not support move of files or folders across devices. 1096 1097**System capability**: SystemCapability.FileManagement.UserFileService 1098 1099**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1100 1101**Parameters** 1102 1103| Name| Type| Mandatory| Description| 1104| --- | --- | --- | -- | 1105| sourceFile | string | Yes| URI of the file or folder to move.| 1106| destFile | string | Yes| URI of the destination directory, to which the file or folder is moved.| 1107| callback | AsyncCallback<string> | Yes| Callback used to return the URI of the file or folder in the destination directory.| 1108 1109**Error codes** 1110 1111For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 1112 1113**Example** 1114 1115 ```ts 1116 import { BusinessError } from '@ohos.base'; 1117 // A built-in storage directory is used as an example. 1118 // In the sample code, sourceFile and destFile indicate the files and directories in the Download directory. The URI is the URI in fileInfo. 1119 // You can use the URI obtained. 1120 let sourceFile: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1121 let destFile: string = "file://docs/storage/Users/currentUser/Download/test"; 1122 try { 1123 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1124 fileAccessHelper.move(sourceFile, destFile, (err: BusinessError, fileUri: string) => { 1125 if (err) { 1126 console.error("Failed to move in async, errCode:" + err.code + ", errMessage:" + err.message); 1127 } 1128 console.log("move sucess, fileUri: " + JSON.stringify(fileUri)); 1129 }); 1130 } catch (err) { 1131 let error: BusinessError = err as BusinessError; 1132 console.error("move failed, errCode:" + error.code + ", errMessage:" + error.message); 1133 } 1134 ``` 1135 1136### rename 1137 1138rename(uri: string, displayName: string) : Promise<string> 1139 1140Renames a file or folder. This API uses a promise to return the result. 1141 1142**System capability**: SystemCapability.FileManagement.UserFileService 1143 1144**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1145 1146**Parameters** 1147 1148| Name| Type| Mandatory| Description| 1149| --- | --- | --- | -- | 1150| uri | string | Yes| URI of the file or folder to rename.| 1151| displayName | string | Yes| New name of the file or folder, which can contain the file name extension.| 1152 1153**Return value** 1154 1155| Type| Description| 1156| --- | -- | 1157| Promise<string> | Promise used to return the URI of the renamed file or folder.| 1158 1159**Error codes** 1160 1161For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 1162 1163**Example** 1164 1165 ```ts 1166 import { BusinessError } from '@ohos.base'; 1167 async function renameFile01() { 1168 // A built-in storage directory is used as an example. 1169 // In the sample code, sourceDir indicates a file in the Download directory. The URI is the URI in fileInfo. 1170 // You can use the URI obtained. 1171 let sourceDir: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1172 try { 1173 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1174 let DestDir = await fileAccessHelper.rename(sourceDir, "testDir"); 1175 console.log("rename sucess, DestDir: " + JSON.stringify(DestDir)); 1176 } catch (err) { 1177 let error: BusinessError = err as BusinessError; 1178 console.error("rename failed, errCode:" + error.code + ", errMessage:" + error.message); 1179 } 1180 } 1181 ``` 1182 1183### rename 1184 1185rename(uri: string, displayName: string, callback: AsyncCallback<string>) : void 1186 1187Renames a file or folder. This API uses an asynchronous callback to return the result. 1188 1189**System capability**: SystemCapability.FileManagement.UserFileService 1190 1191**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1192 1193**Parameters** 1194 1195| Name| Type| Mandatory| Description| 1196| --- | --- | --- | -- | 1197| uri | string | Yes| URI of the file or folder to rename.| 1198| displayName | string | Yes| New name of the file or folder, which can contain the file name extension.| 1199| callback | AsyncCallback<string> | Yes| Callback used to return the URI of the renamed file or folder.| 1200 1201**Error codes** 1202 1203For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 1204 1205**Example** 1206 1207 ```ts 1208 import { BusinessError } from '@ohos.base'; 1209 // A built-in storage directory is used as an example. 1210 // In the sample code, sourceDir indicates a file in the Download directory. The URI is the URI in fileInfo. 1211 // You can use the URI obtained. 1212 let sourceDir: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1213 try { 1214 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1215 fileAccessHelper.rename(sourceDir, "testDir", (err: BusinessError, DestDir: string) => { 1216 if (err) { 1217 console.error("Failed to rename in async, errCode:" + err.code + ", errMessage:" + err.message); 1218 } 1219 console.log("rename sucess, DestDir: " + JSON.stringify(DestDir)); 1220 }); 1221 } catch (err) { 1222 let error: BusinessError = err as BusinessError; 1223 console.error("rename failed, errCode:" + error.code + ", errMessage:" + error.message); 1224 } 1225 ``` 1226 1227### access 1228 1229access(sourceFileUri: string) : Promise<boolean> 1230 1231Checks whether a file or folder exists. This API uses a promise to return the result. 1232 1233**System capability**: SystemCapability.FileManagement.UserFileService 1234 1235**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1236 1237**Parameters** 1238 1239| Name| Type| Mandatory| Description| 1240| --- | --- | --- | -- | 1241| sourceFileUri | string | Yes| URI of the file or folder to check.| 1242 1243**Return value** 1244 1245| Type| Description| 1246| --- | -- | 1247| Promise<boolean> | Promise used to return whether the file or folder exists. The value **true** means the file or folder exists, and the value **false** means the opposite.| 1248 1249**Error codes** 1250 1251For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 1252 1253**Example** 1254 1255 ```ts 1256 import { BusinessError } from '@ohos.base'; 1257 // A built-in storage directory is used as an example. 1258 // In the sample code, sourceDir indicates a file in the Download directory. The URI is the URI in fileInfo. 1259 // You can use the URI obtained. 1260 async function accessFunc() { 1261 let sourceDir: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1262 try { 1263 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1264 let existJudgment = await fileAccessHelper.access(sourceDir); 1265 if (existJudgment) { 1266 console.log("sourceDir exists"); 1267 } else { 1268 console.log("sourceDir does not exist"); 1269 } 1270 } catch (err) { 1271 let error: BusinessError = err as BusinessError; 1272 console.error("access failed, errCode:" + error.code + ", errMessage:" + error.message); 1273 } 1274 } 1275 ``` 1276 1277### access 1278 1279access(sourceFileUri: string, callback: AsyncCallback<boolean>) : void 1280 1281Checks whether a file or folder exists. This API uses an asynchronous callback to return the result. 1282 1283**System capability**: SystemCapability.FileManagement.UserFileService 1284 1285**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1286 1287**Parameters** 1288 1289| Name| Type| Mandatory| Description| 1290| --- | --- | --- | -- | 1291| sourceFileUri | string | Yes| URI of the file or folder to check.| 1292| callback | AsyncCallback<boolean> | Yes| Callback used to return whether the file or folder exists. The value **true** means the file or folder exists, and the value **false** means the opposite.| 1293 1294**Error codes** 1295 1296For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 1297 1298**Example** 1299 1300 ```ts 1301 import { BusinessError } from '@ohos.base'; 1302 // A built-in storage directory is used as an example. 1303 // In the sample code, sourceDir indicates a folder in the Download directory. The URI is the URI in fileInfo. 1304 // You can use the URI obtained. 1305 let sourceDir: string = "file://docs/storage/Users/currentUser/Download/test"; 1306 try { 1307 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1308 fileAccessHelper.access(sourceDir, (err: BusinessError, existJudgment: boolean) => { 1309 if (err) { 1310 console.error("Failed to access in async, errCode:" + err.code + ", errMessage:" + err.message); 1311 return; 1312 } 1313 if (existJudgment) 1314 console.log("sourceDir exists"); 1315 else 1316 console.log("sourceDir does not exist"); 1317 }); 1318 } catch (err) { 1319 let error: BusinessError = err as BusinessError; 1320 console.error("access failed, errCode:" + error.code + ", errMessage:" + error.message); 1321 } 1322 ``` 1323 1324### getFileInfoFromUri<sup>10+</sup> 1325 1326getFileInfoFromUri(uri: string) : Promise\<FileInfo> 1327 1328Obtains a **FileInfo** object based on a URI. This API uses a promise to return the result. 1329 1330**System capability**: SystemCapability.FileManagement.UserFileService 1331 1332**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1333 1334**Parameters** 1335 1336| Name| Type| Mandatory| Description| 1337| --- | --- | --- | -- | 1338| uri | string | Yes| URI of the file or folder.| 1339 1340**Return value** 1341 1342| Type| Description| 1343| --- | -- | 1344| Promise\<[FileInfo](#fileinfo)> | Promise used to return the **FileInfo** object obtained.| 1345 1346**Example** 1347 1348 ```ts 1349 import { BusinessError } from '@ohos.base'; 1350 // A built-in storage directory is used as an example. 1351 // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo. 1352 // You can use the URI obtained. 1353 async function getUri() { 1354 let sourceUri: string = "file://docs/storage/Users/currentUser/Download"; 1355 try { 1356 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1357 let fileInfo = await fileAccessHelper.getFileInfoFromUri(sourceUri); 1358 } catch (err) { 1359 let error: BusinessError = err as BusinessError; 1360 console.error("getFileInfoFromUri failed, errCode:" + error.code + ", errMessage:" + error.message); 1361 } 1362 } 1363 ``` 1364 1365### getFileInfoFromUri<sup>10+</sup> 1366 1367getFileInfoFromUri(uri: string, callback: AsyncCallback\<FileInfo>) : void 1368 1369Obtains a **FileInfo** object based on a URI. This API uses an asynchronous callback to return the result. 1370 1371**System capability**: SystemCapability.FileManagement.UserFileService 1372 1373**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1374 1375**Parameters** 1376 1377| Name| Type| Mandatory| Description| 1378| --- | --- | --- | -- | 1379| uri | string | Yes| URI of the file or folder.| 1380| callback | AsyncCallback<[FileInfo](#fileinfo)> | Yes| Callback used to return the **FileInfo** object obtained.| 1381 1382**Example** 1383 1384 ```ts 1385 import { BusinessError } from '@ohos.base'; 1386 // A built-in storage directory is used as an example. 1387 // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo. 1388 // You can use the URI obtained. 1389 let sourceUri: string = "file://docs/storage/Users/currentUser/Download"; 1390 try { 1391 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1392 fileAccessHelper.getFileInfoFromUri(sourceUri, (err: BusinessError, fileInfo: fileAccess.FileInfo) => { 1393 if (err) { 1394 console.error("Failed to getFileInfoFromUri in async, errCode:" + err.code + ", errMessage:" + err.message); 1395 return; 1396 } 1397 console.log("getFileInfoFromUri success, fileInfo: " + JSON.stringify(fileInfo)); 1398 }); 1399 } catch (err) { 1400 let error: BusinessError = err as BusinessError; 1401 console.error("getFileInfoFromUri failed, errCode:" + error.code + ", errMessage:" + error.message); 1402 } 1403 ``` 1404 1405 1406### getFileInfoFromRelativePath<sup>10+</sup> 1407 1408getFileInfoFromRelativePath(relativePath: string) : Promise\<FileInfo> 1409 1410Obtains a **FileInfo** object based on a relative path. This API uses a promise to return the result. 1411 1412**System capability**: SystemCapability.FileManagement.UserFileService 1413 1414**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1415 1416**Parameters** 1417 1418| Name| Type| Mandatory| Description| 1419| --- | --- | --- | -- | 1420| relativePath | string | Yes| Relative path of the file or folder.| 1421 1422**Return value** 1423 1424| Type| Description| 1425| --- | -- | 1426| Promise\<[FileInfo](#fileinfo)> | Promise used to return the **FileInfo** object obtained.| 1427 1428**Example** 1429 1430 ```ts 1431 import { BusinessError } from '@ohos.base'; 1432 // In the sample code, relativePath indicates the Download directory, which is the relativePath in fileInfo. 1433 // You can use the relativePath obtained. 1434 async function getRelativePath() { 1435 let relativePath: string = "Download/"; 1436 try { 1437 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1438 let fileInfo = await fileAccessHelper.getFileInfoFromRelativePath(relativePath); 1439 } catch (err) { 1440 let error: BusinessError = err as BusinessError; 1441 console.error("getFileInfoFromRelativePath failed, errCode:" + error.code + ", errMessage:" + error.message); 1442 } 1443 } 1444 ``` 1445 1446### getFileInfoFromRelativePath<sup>10+</sup> 1447 1448getFileInfoFromRelativePath(relativePath: string, callback: AsyncCallback\<FileInfo>) : void 1449 1450Obtains a **FileInfo** object based on a relative path. This API uses an asynchronous callback to return the result. 1451 1452**System capability**: SystemCapability.FileManagement.UserFileService 1453 1454**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1455 1456**Parameters** 1457 1458| Name| Type| Mandatory| Description| 1459| --- | --- | --- | -- | 1460| relativePath | string | Yes| Relative path of the file or folder.| 1461| callback | AsyncCallback<[FileInfo](#fileinfo)> | Yes| Callback used to return the **FileInfo** object obtained.| 1462 1463**Example** 1464 1465 ```ts 1466 import { BusinessError } from '@ohos.base'; 1467 // In the sample code, relativePath indicates the Download directory, which is the relativePath in fileInfo. 1468 // You can use the relativePath obtained. 1469 let relativePath: string = "Download/"; 1470 try { 1471 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1472 fileAccessHelper.getFileInfoFromRelativePath(relativePath, (err: BusinessError, fileInfo: fileAccess.FileInfo) => { 1473 if (err) { 1474 console.error("Failed to getFileInfoFromRelativePath in async, errCode:" + err.code + ", errMessage:" + err.message); 1475 return; 1476 } 1477 console.log("getFileInfoFromRelativePath success, fileInfo: " + JSON.stringify(fileInfo)); 1478 }); 1479 } catch (err) { 1480 let error: BusinessError = err as BusinessError; 1481 console.error("getFileInfoFromRelativePath failed, errCode:" + error.code + ", errMessage:" + error.message); 1482 } 1483 ``` 1484 1485### query<sup>10+</sup> 1486 1487query(uri:string, metaJson: string) : Promise<string> 1488 1489Queries the attribute information about a file or folder based on a URI. This API uses a promise to return the result. 1490 1491**System capability**: SystemCapability.FileManagement.UserFileService 1492 1493**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1494 1495**Parameters** 1496 1497| Name | Type | Mandatory| Description | 1498| -------- | ------ | ---- | ---------------------------------------------------- | 1499| uri | string | Yes | File or folder URI obtained from [FileInfo](#fileinfo).| 1500| metaJson | string | Yes | Attribute [FILEKEY](#filekey10) to query. | 1501 1502**Return value** 1503 1504| Type | Description | 1505| :-------------------- | :------------------------------- | 1506| Promise<string> | Promise used to return the file attribute and the value obtained.| 1507 1508**Example** 1509 1510```ts 1511import { BusinessError } from '@ohos.base'; 1512async function getQuery01() { 1513 let imageFileRelativePath: string = "/storage/Users/currentUser/Download/queryTest/image/01.jpg"; 1514 let jsonStrSingleRelativepath: string = JSON.stringify({ [fileAccess.FileKey.RELATIVE_PATH]: "" }); 1515 try { 1516 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1517 let fileInfo = await fileAccessHelper.getFileInfoFromRelativePath(imageFileRelativePath); 1518 let queryResult = await fileAccessHelper.query(fileInfo.uri, jsonStrSingleRelativepath); 1519 console.log("query_file_single faf query, queryResult.relative_path: " + JSON.parse(queryResult).relative_path); 1520 } catch (err) { 1521 let error: BusinessError = err as BusinessError; 1522 console.error("query_file_single faf query failed, error.code :" + error.code + ", errorMessage :" + error.message); 1523 } 1524} 1525``` 1526 1527### query<sup>10+</sup> 1528 1529query(uri:string, metaJson: string, callback: AsyncCallback<string>) : void 1530 1531Queries the attribute information about a file or folder based on a URI. This API uses an asynchronous callback to return the result. 1532 1533**System capability**: SystemCapability.FileManagement.UserFileService 1534 1535**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1536 1537**Parameters** 1538 1539| Name | Type | Mandatory| Description | 1540| -------- | --------------------------- | ---- | ---------------------------------------------------- | 1541| uri | string | Yes | File or folder URI obtained from [FileInfo](#fileinfo).| 1542| metaJson | string | Yes | Attribute [FILEKEY](#filekey10) to query. | 1543| callback | AsyncCallback<string> | Yes | Callback used to return the file attribute and the value obtained. | 1544 1545**Example** 1546 1547```ts 1548import { BusinessError } from '@ohos.base'; 1549async function getQuery02() { 1550 let imageFileRelativePath: string = "/storage/Users/currentUser/Download/queryTest/image/01.jpg"; 1551 let jsonStrSingleRelativepath: string = JSON.stringify({ [fileAccess.FileKey.RELATIVE_PATH]: "" }); 1552 try { 1553 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1554 let fileInfo = await fileAccessHelper.getFileInfoFromRelativePath(imageFileRelativePath); 1555 fileAccessHelper.query(fileInfo.uri, jsonStrSingleRelativepath, (err: BusinessError, queryResult: string) => { 1556 if (err) { 1557 console.log("query_file_single faf query Failed, errCode:" + err.code + ", errMessage:" + err.message); 1558 return; 1559 } 1560 console.log("query_file_single faf query, queryResult.relative_path: " + JSON.parse(queryResult).relative_path); 1561 }) 1562 } catch (err) { 1563 let error: BusinessError = err as BusinessError; 1564 console.error("query_file_single faf query failed, error.code :" + error.code + ", errorMessage :" + error.message); 1565 } 1566} 1567``` 1568 1569### copy<sup>10+</sup> 1570 1571copy(sourceUri: string, destUri: string, force?: boolean) : Promise<Array<CopyResult>> 1572 1573Copies a file or folder. This API uses a promise to return the result. 1574 1575**System capability**: SystemCapability.FileManagement.UserFileService 1576 1577**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1578 1579**Parameters** 1580 1581| Name | Type | Mandatory| Description | 1582| --------- | ------- | ---- | ------------------------------------------------------------ | 1583| sourceUri | string | Yes | URI of the file or folder to copy, for example, **file://docs/storage/Users/currentUser/Download/1.txt**. | 1584| destUri | string | Yes | URI of the file or folder created, for example, **file://docs/storage/Users/currentUser/Download/test**. | 1585| force | boolean | No | Whether to forcibly overwrite the file with the same name. <br>If **force** is **true**, the file with the same name will be overwritten. If **force** is **false** or not specified, the file with the same name will not be overwritten.| 1586 1587**Return value** 1588 1589| Type | Description | 1590| :------------------------------------------------------ | :----------------------------------------------------------- | 1591| Promise<Array<[CopyResult](#copyresult10)>> | Promise used to return the result. If the file or folder is copied successfully, no information is returned. If the file copy fails, a **copyResult** array is returned.| 1592 1593Example 1: Copy a file with **force** unspecified. 1594 1595```ts 1596import { BusinessError } from '@ohos.base'; 1597// A built-in storage directory is used as an example. 1598// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 1599// You can use the URI obtained. 1600async function copyFunc01() { 1601 let sourceFile: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1602 let destFile: string = "file://docs/storage/Users/currentUser/Download/test"; 1603 try { 1604 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1605 let copyResult = await fileAccessHelper.copy(sourceFile, destFile); 1606 if (copyResult.length === 0) { 1607 console.log("copy success"); 1608 } else { 1609 for (let i = 0; i < copyResult.length; i++) { 1610 console.error("errCode" + copyResult[i].errCode); 1611 console.error("errMsg" + copyResult[i].errMsg); 1612 console.error("sourceUri" + copyResult[i].sourceUri); 1613 console.error("destUri" + copyResult[i].destUri); 1614 } 1615 } 1616 } catch (err) { 1617 let error: BusinessError = err as BusinessError; 1618 console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message); 1619 } 1620} 1621``` 1622 1623Example 2: Copy a file or folder when **force** set to **true**. 1624 1625```ts 1626import { BusinessError } from '@ohos.base'; 1627// A built-in storage directory is used as an example. 1628// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 1629// You can use the URI obtained. 1630async function copyFunc02() { 1631 let sourceFile: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1632 let destFile: string = "file://docs/storage/Users/currentUser/Download/test"; 1633 try { 1634 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1635 let copyResult = await fileAccessHelper.copy(sourceFile, destFile, true); 1636 if (copyResult.length === 0) { 1637 console.log("copy success"); 1638 } else { 1639 for (let i = 0; i < copyResult.length; i++) { 1640 console.error("errCode" + copyResult[i].errCode); 1641 console.error("errMsg" + copyResult[i].errMsg); 1642 console.error("sourceUri" + copyResult[i].sourceUri); 1643 console.error("destUri" + copyResult[i].destUri); 1644 } 1645 } 1646 } catch (err) { 1647 let error: BusinessError = err as BusinessError; 1648 console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message); 1649 } 1650} 1651``` 1652 1653### copy<sup>10+</sup> 1654 1655copy(sourceUri: string, destUri: string, callback: AsyncCallback<Array<CopyResult>>) : void 1656 1657Copies a file or folder. This API uses an asynchronous callback to return the result. 1658 1659**System capability**: SystemCapability.FileManagement.UserFileService 1660 1661**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1662 1663**Parameters** 1664 1665| Name | Type | Mandatory| Description | 1666| --------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 1667| sourceUri | string | Yes | URI of the file or folder to copy, for example, **file://docs/storage/Users/currentUser/Download/1.txt**. | 1668| destUri | string | Yes | URI of the file or folder created, for example, **file://docs/storage/Users/currentUser/Download/test**. | 1669| callback | AsyncCallback<Array<[CopyResult](#copyresult10)>> | Yes | Callback used to return the result. If the file or folder is copied successfully, no information is returned. If the file copy fails, a **copyResult** array is returned.| 1670 1671**Example** 1672 1673```ts 1674import { BusinessError } from '@ohos.base'; 1675// A built-in storage directory is used as an example. 1676// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 1677// You can use the URI obtained. 1678let sourceFile: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1679let destFile: string = "file://docs/storage/Users/currentUser/Download/test"; 1680try { 1681 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1682 fileAccessHelper.copy(sourceFile, destFile, async (err: BusinessError, copyResult: Array<fileAccess.CopyResult>) => { 1683 if (err) { 1684 console.error("copy failed, errCode:" + err.code + ", errMessage:" + err.message); 1685 } 1686 if (copyResult.length === 0) { 1687 console.log("copy success"); 1688 } else { 1689 for (let i = 0; i < copyResult.length; i++) { 1690 console.error("errCode" + copyResult[i].errCode); 1691 console.error("errMsg" + copyResult[i].errMsg); 1692 console.error("sourceUri" + copyResult[i].sourceUri); 1693 console.error("destUri" + copyResult[i].destUri); 1694 } 1695 } 1696 }); 1697} catch (err) { 1698 let error: BusinessError = err as BusinessError; 1699 console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message); 1700} 1701``` 1702 1703### copy<sup>10+</sup> 1704 1705copy(sourceUri: string, destUri: string, force: boolean, callback: AsyncCallback<Array<CopyResult>>) : void 1706 1707Copies a file or folder. This API uses an asynchronous callback to return the result. 1708 1709**System capability**: SystemCapability.FileManagement.UserFileService 1710 1711**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1712 1713**Parameters** 1714 1715| Name | Type | Mandatory| Description | 1716| --------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 1717| sourceUri | string | Yes | URI of the file or folder to copy, for example, **file://docs/storage/Users/currentUser/Download/1.txt**. | 1718| destUri | string | Yes | URI of the file or folder created, for example, **file://docs/storage/Users/currentUser/Download/test**. | 1719| force | boolean | Yes | Whether to forcibly overwrite the file with the same name. <br>If **force** is **true**, the file with the same name will be overwritten. If **force** is **false** or not specified, the file with the same name will not be overwritten.| 1720| callback | AsyncCallback<Array<[CopyResult](#copyresult10)>> | Yes | Callback used to return the result. If the file or folder is copied successfully, no information is returned. If the file copy fails, a **copyResult** array is returned.| 1721 1722**Example** 1723 1724```ts 1725import { BusinessError } from '@ohos.base'; 1726// A built-in storage directory is used as an example. 1727// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 1728// You can use the URI obtained. 1729let sourceFile: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1730let destFile: string = "file://docs/storage/Users/currentUser/Download/test"; 1731try { 1732 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1733 fileAccessHelper.copy(sourceFile, destFile, true, async (err: BusinessError, copyResult: Array<fileAccess.CopyResult>) => { 1734 if (err) { 1735 console.error("copy failed, errCode:" + err.code + ", errMessage:" + err.message); 1736 } 1737 if (copyResult.length === 0) { 1738 console.log("copy success"); 1739 } else { 1740 for (let i = 0; i < copyResult.length; i++) { 1741 console.error("errCode" + copyResult[i].errCode); 1742 console.error("errMsg" + copyResult[i].errMsg); 1743 console.error("sourceUri" + copyResult[i].sourceUri); 1744 console.error("destUri" + copyResult[i].destUri); 1745 } 1746 } 1747 }); 1748} catch (err) { 1749 let error: BusinessError = err as BusinessError; 1750 console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message); 1751} 1752``` 1753 1754### copyFile<sup>11+</sup> 1755 1756copyFile(sourceUri: string, destUri: string, fileName: string): Promise<string> 1757 1758Copies a file with an alternative file name. This API uses a promise to return the result. 1759 1760**Model restriction**: This API can be used only in the stage model. 1761 1762**System capability**: SystemCapability.FileManagement.UserFileService 1763 1764**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1765 1766**Parameters** 1767 1768| Name | Type | Mandatory| Description | 1769| --------- | ------- | ---- | ------------------------------------------------------------ | 1770| sourceUri | string | Yes | URI of the file or folder to copy, for example, **file://docs/storage/Users/currentUser/Download/1.txt**. | 1771| destUri | string | Yes | URI of the destination directory, for example, **file://docs/storage/Users/currentUser/Download/test**. | 1772| fileName | string | Yes | File name to use if there is a file with the same name as the source file in the destination directory.| 1773 1774**Return value** 1775 1776| Type | Description | 1777| :------------------------------------------------------ | :----------------------------------------------------------- | 1778| Promise<string> | URI of the file generated.| 1779 1780**Example** 1781 1782```ts 1783import { BusinessError } from '@ohos.base'; 1784// A built-in storage directory is used as an example. 1785// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 1786// You can use the URI obtained. 1787async function copyFunc01() { 1788 let sourceFile: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1789 let destFile: string = "file://docs/storage/Users/currentUser/Download/test"; 1790 let fileName: string = "2.txt"; 1791 try { 1792 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1793 let copyResult = await fileAccessHelper.copyFile(sourceFile, destFile, fileName); 1794 console.log("copyResult uri: " + copyResult); 1795 } catch (err) { 1796 let error: BusinessError = err as BusinessError; 1797 console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message); 1798 } 1799} 1800``` 1801 1802### copyFile<sup>11+</sup> 1803 1804copyFile(sourceUri: string, destUri: string, fileName: string, callback: AsyncCallback<string>) : void 1805 1806Copies a file with an alternative file name. This API uses an asynchronous callback to return the result. 1807 1808**Model restriction**: This API can be used only in the stage model. 1809 1810**System capability**: SystemCapability.FileManagement.UserFileService 1811 1812**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1813 1814**Parameters** 1815 1816| Name | Type | Mandatory| Description | 1817| --------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 1818| sourceUri | string | Yes | URI of the file or folder to copy, for example, **file://docs/storage/Users/currentUser/Download/1.txt**. | 1819| destUri | string | Yes | URI of the destination directory, for example, **file://docs/storage/Users/currentUser/Download/test**. | 1820| fileName | string | Yes | File name to use if there is a file with the same name as the source file in the destination directory.| 1821| callback | AsyncCallback<string> | Yes | URI of the file generated.| 1822 1823**Example** 1824 1825```ts 1826import { BusinessError } from '@ohos.base'; 1827// A built-in storage directory is used as an example. 1828// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 1829// You can use the URI obtained. 1830let sourceFile: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 1831let destFile: string = "file://docs/storage/Users/currentUser/Download/test"; 1832let fileName: string = "2.txt"; 1833 1834try { 1835 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1836 fileAccessHelper.copyFile(sourceFile, destFile, fileName, async (copyResult: string) => { 1837 console.log("copyResult uri: " + copyResult); 1838 }); 1839} catch (err) { 1840 let error: BusinessError = err as BusinessError; 1841 console.error("copy failed, errCode:" + error.code + ", errMessage:" + error.message); 1842} 1843``` 1844 1845### registerObserver<sup>10+</sup> 1846 1847registerObserver(uri: string, notifyForDescendants: boolean, callback: Callback<NotifyMessage>): void 1848 1849Registers a callback to listen for a URI. URIs and callbacks can be in many-to-many relationships. You are advised to use one callback to listen for one URI. 1850 1851**System capability**: SystemCapability.FileManagement.UserFileService 1852 1853**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 1854 1855**Parameters** 1856 1857| Name | Type | Mandatory| Description | 1858| -------------------- | ------------------------------------------------- | ---- | ------------------------------ | 1859| uri | string | Yes | URI of the file or folder to observe. | 1860| notifyForDescendants | boolean | Yes | Whether to observe changes of the files in the folder. The value **true** means to observe changes of the files in the folder; the value **false** means the opposite.| 1861| callback | Callback<[NotifyMessage](#notifymessage10)> | Yes | Callback used to return the notification. | 1862 1863**Example 1: Register a callback to listen for a URI.** 1864 1865```ts 1866import { BusinessError } from '@ohos.base'; 1867async function registerObserver01() { 1868 let DirUri: string = 'file://docs/storage/Users/currentUser/Documents'; 1869 try { 1870 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1871 let dirUri1 = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR1'); 1872 let dirUri2 = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR2'); 1873 // Two notifications are expected to receive because notifyForDescendants is set to true during registration. 1874 // The URI is 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR1/SUB_FILE', and the event type is NOTIFY_MOVED_FROM. 1875 // The URI is 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR1/SUB_FILE', and the event type is NOTIFY_MOVE_SELF. 1876 const callbackDir1 = (NotifyMessageDir: fileAccess.NotifyMessage) => { 1877 if (NotifyMessageDir != undefined) { 1878 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 1879 } else { 1880 console.error("NotifyMessageDir is undefined"); 1881 } 1882 } 1883 // The notification expected to receive is about the NOTIFY_MOVED_TO event of the URI 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR2/SUB_FILE'. 1884 const callbackDir2 = (NotifyMessageDir: fileAccess.NotifyMessage) => { 1885 if (NotifyMessageDir != undefined) { 1886 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 1887 } else { 1888 console.error("NotifyMessageDir is undefined"); 1889 } 1890 } 1891 // The notification expected to receive is about the NOTIFY_MOVE_SELF event of the URI 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR1/SUB_FILE'. 1892 // The notification expected to receive is about the NOTIFY_MOVED_FROM event of the URI 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR1/SUB_FILE'. 1893 const callbackFile = (NotifyMessageDir: fileAccess.NotifyMessage) => { 1894 if (NotifyMessageDir != undefined) { 1895 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 1896 } else { 1897 console.error("NotifyMessageDir is undefined"); 1898 } 1899 } 1900 let fileUri = await fileAccessHelper.createFile(dirUri1, 'SUB_FILE'); 1901 fileAccessHelper.registerObserver(dirUri1, true, callbackDir1); 1902 fileAccessHelper.registerObserver(dirUri2, true, callbackDir2); 1903 // If the moved file itself is not listened for, the NOTIFY_MOVE_SELF event will not be triggered. 1904 fileAccessHelper.registerObserver(fileUri, true, callbackFile); 1905 let moveFileUri = await fileAccessHelper.move(fileUri, dirUri2); 1906 // Do not unregister the callback immediately after the registration is complete, because the unregistration result may be returned before the notification is returned. If this occurs, the notification wll not be received. 1907 fileAccessHelper.unregisterObserver(dirUri1, callbackDir1); 1908 fileAccessHelper.unregisterObserver(dirUri2, callbackDir2); 1909 fileAccessHelper.unregisterObserver(fileUri, callbackFile); 1910 } catch (err) { 1911 let error: BusinessError = err as BusinessError; 1912 console.error("registerObserver failed, errCode:" + error.code + ", errMessage:" + error.message); 1913 } 1914} 1915``` 1916 1917Example 2: Use the same **uri**, **notifyForDescendants**, and **callback** to register repeatedly. 1918 1919```ts 1920import { BusinessError } from '@ohos.base'; 1921async function registerObserver02() { 1922 let DirUri: string = 'file://docs/storage/Users/currentUser/Documents'; 1923 try { 1924 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1925 let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); 1926 // The notification expected to receive is about the NOTIFY_ADD event of the URI 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR/SUB_DIR'. 1927 const callbackDir = (NotifyMessageDir: fileAccess.NotifyMessage) => { 1928 if (NotifyMessageDir != undefined) { 1929 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 1930 } else { 1931 console.error("NotifyMessageDir is undefined"); 1932 } 1933 } 1934 fileAccessHelper.registerObserver(dirUri, true, callbackDir); 1935 // A message is returned indicating that the registration is successful. Repeated registration is reported only in the log. 1936 fileAccessHelper.registerObserver(dirUri, true, callbackDir); 1937 let subDirUri = await fileAccessHelper.mkDir(dirUri, 'SUB_DIR'); 1938 // Do not unregister the callback immediately after the registration is complete, because the unregistration result may be returned before the notification is returned. If this occurs, the notification wll not be received. 1939 fileAccessHelper.unregisterObserver(dirUri, callbackDir); 1940 } catch (err) { 1941 let error: BusinessError = err as BusinessError; 1942 console.error("registerObserver failed, errCode:" + error.code + ", errMessage:" + error.message); 1943 } 1944} 1945``` 1946 1947Example 3: Use the same **uri** and **callback** but different **notifyForDescendants** for registration. In this case, **notifyForDescendants** will be reset. 1948 1949```ts 1950import { BusinessError } from '@ohos.base'; 1951async function registerObserver03() { 1952 let DirUri: string = 'file://docs/storage/Users/currentUser/Documents'; 1953 try { 1954 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 1955 let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); 1956 // The first notification expected to receive is about the NOTIFY_ADD event of the URI 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR/SUB_FILE_1'. 1957 // No second return is expected. 1958 const callbackDir = (NotifyMessageDir: fileAccess.NotifyMessage) => { 1959 if (NotifyMessageDir != undefined) { 1960 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 1961 } else { 1962 console.error("NotifyMessageDir is undefined"); 1963 } 1964 } 1965 fileAccessHelper.registerObserver(dirUri, true, callbackDir); 1966 let subFile1 = await fileAccessHelper.createFile(dirUri, 'SUB_FILE_1'); 1967 // After the registration is successful, change notifyForDescendants to false. 1968 fileAccessHelper.registerObserver(dirUri, false, callbackDir); 1969 let subFile2 = await fileAccessHelper.createFile(dirUri, 'SUB_FILE_2'); 1970 // Do not unregister the callback immediately after the registration is complete, because the unregistration result may be returned before the notification is returned. If this occurs, the notification wll not be received. 1971 fileAccessHelper.unregisterObserver(dirUri, callbackDir); 1972 } catch (err) { 1973 let error: BusinessError = err as BusinessError; 1974 console.error("registerObserver failed, errCode:" + error.code + ", errMessage:" + error.message); 1975 } 1976} 1977``` 1978 1979Example 4: Observe the device online/offline status. 1980 1981```ts 1982import { BusinessError } from '@ohos.base'; 1983async function UnregisterObserver03() { 1984 try { 1985 const callbackDir1 = (NotifyMessageDir: fileAccess.NotifyMessage) => { 1986 if (NotifyMessageDir != undefined) { 1987 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 1988 } else { 1989 console.error("NotifyMessageDir is undefined"); 1990 } 1991 } 1992 // Subscribe to the device online/offline status. 1993 fileAccessHelper.registerObserver(fileAccess.DEVICES_URI, true, callbackDir1); 1994 // Unsubscribe from the device online/offline status. 1995 fileAccessHelper.unregisterObserver(fileAccess.DEVICES_URI); 1996 } catch (err) { 1997 let error: BusinessError = err as BusinessError; 1998 console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message); 1999 } 2000} 2001``` 2002 2003### unregisterObserver<sup>10+</sup> 2004 2005 unregisterObserver(uri: string, callback?: Callback<NotifyMessage>): void 2006 2007Unregisters a callback that is used to listen for the specified URI. 2008 2009**System capability**: SystemCapability.FileManagement.UserFileService 2010 2011**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 2012 2013**Parameters** 2014 2015| Name | Type | Mandatory| Description | 2016| -------- | ------------------------------------------------- | ---- | ------------------------- | 2017| uri | string | Yes | URI of the file or folder. | 2018| callback | Callback<[NotifyMessage](#notifymessage10)> | No | Callback to unregister. If this parameter is not specified, all callbacks of the specified URI will be unregistered.| 2019 2020Example 1: Unregister a callback of the specified URI. 2021 2022```ts 2023import { BusinessError } from '@ohos.base'; 2024async function UnregisterObserver01() { 2025 let DirUri: string = 'file://docs/storage/Users/currentUser/Documents'; 2026 try { 2027 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 2028 let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); 2029 // The notification expected to receive is about the NOTIFY_DELETE event of the URI 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR'. 2030 const callbackDir = (NotifyMessageDir: fileAccess.NotifyMessage) => { 2031 if (NotifyMessageDir != undefined) { 2032 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 2033 } else { 2034 console.error("NotifyMessageDir is undefined"); 2035 } 2036 } 2037 fileAccessHelper.registerObserver(dirUri, true, callbackDir); 2038 await fileAccessHelper.delete(dirUri); 2039 // Do not unregister the callback immediately after the registration is complete, because the unregistration result may be returned before the notification is returned. If this occurs, the notification wll not be received. 2040 fileAccessHelper.unregisterObserver(dirUri, callbackDir); 2041 } catch (err) { 2042 let error: BusinessError = err as BusinessError; 2043 console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message); 2044 } 2045} 2046``` 2047 2048Example 2: Repeatedly unregister a callback of the specified URI. 2049 2050```ts 2051import { BusinessError } from '@ohos.base'; 2052async function UnregisterObserver02() { 2053 let DirUri: string = 'file://docs/storage/Users/currentUser/Documents'; 2054 try { 2055 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 2056 let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); 2057 // The notification expected to receive is about the NOTIFY_DELETE event of the URI 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR'. 2058 const callbackDir = (NotifyMessageDir: fileAccess.NotifyMessage) => { 2059 if (NotifyMessageDir != undefined) { 2060 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 2061 } else { 2062 console.error("NotifyMessageDir is undefined"); 2063 } 2064 } 2065 fileAccessHelper.registerObserver(dirUri, true, callbackDir); 2066 await fileAccessHelper.delete(dirUri); 2067 // Do not unregister the callback immediately after the registration is complete, because the unregistration result may be returned before the notification is returned. If this occurs, the notification wll not be received. 2068 fileAccessHelper.unregisterObserver(dirUri, callbackDir); 2069 // If the unregistration fails, throw the error code E_CAN_NOT_FIND_URI. 2070 fileAccessHelper.unregisterObserver(dirUri, callbackDir); 2071 } catch (err) { 2072 let error: BusinessError = err as BusinessError; 2073 console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message); 2074 } 2075} 2076``` 2077 2078Example 3: Unregister all callbacks of the specified URI. 2079 2080```ts 2081import { BusinessError } from '@ohos.base'; 2082async function UnregisterObserver03() { 2083 let DirUri: string = 'file://docs/storage/Users/currentUser/Documents'; 2084 try { 2085 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 2086 let dirUri = await fileAccessHelper.mkDir(DirUri, 'NOTIFY_DIR'); 2087 // The notification expected to receive is about the NOTIFY_MOVED_FROM event of the URI 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR/SUB_FILE'. 2088 // The notification expected to receive is about the NOTIFY_MOVED_TO event of the URI 'file://docs/storage/Users/currentUser/Documents/NOTIFY_DIR/RENAME_FILE'. 2089 const callbackDir1 = (NotifyMessageDir: fileAccess.NotifyMessage) => { 2090 if (NotifyMessageDir != undefined) { 2091 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 2092 } else { 2093 console.error("NotifyMessageDir is undefined"); 2094 } 2095 } 2096 // No notification is expected to receive. 2097 const callbackDir2 = (NotifyMessageDir: fileAccess.NotifyMessage) => { 2098 if (NotifyMessageDir != undefined) { 2099 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 2100 } else { 2101 console.error("NotifyMessageDir is undefined"); 2102 } 2103 } 2104 let fileUri = await fileAccessHelper.createFile(dirUri, 'SUB_FILE'); 2105 fileAccessHelper.registerObserver(dirUri, true, callbackDir1); 2106 // The registration does not include the events about the next-level directory. 2107 fileAccessHelper.registerObserver(dirUri, false, callbackDir2); 2108 let renameUri = await fileAccessHelper.rename(fileUri, 'RENAME_FILE'); 2109 // Unregister all callbacks (callbackDir1 and callbackDir2) of dirUri. 2110 // Do not unregister the callback immediately after the registration is complete, because the unregistration result may be returned before the notification is returned. If this occurs, the notification wll not be received. 2111 fileAccessHelper.unregisterObserver(dirUri); 2112 await fileAccessHelper.delete(dirUri); 2113 } catch (err) { 2114 let error: BusinessError = err as BusinessError; 2115 console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message); 2116 } 2117} 2118``` 2119 2120Example 4: Unregistger the device online/offline status. 2121 2122``` 2123import { BusinessError } from '@ohos.base'; 2124async function UnregisterObserver03() { 2125 try { 2126 const callbackDir1 = (NotifyMessageDir: fileAccess.NotifyMessage) => { 2127 if (NotifyMessageDir != undefined) { 2128 console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uris[0]); 2129 } else { 2130 console.error("NotifyMessageDir is undefined"); 2131 } 2132 } 2133 // Subscribe to the device online/offline status. 2134 fileAccessHelper.registerObserver(fileAccess.DEVICES_URI, true, callbackDir1); 2135 // Unsubscribe from the device online/offline status. 2136 fileAccessHelper.unregisterObserver(fileAccess.DEVICES_URI); 2137 } catch (err) { 2138 let error: BusinessError = err as BusinessError; 2139 console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message); 2140 } 2141} 2142``` 2143 2144### moveItem<sup>11+</sup> 2145 2146moveItem(sourceUri: string, destUri: string, force?: boolean) : Promise<Array<MoveResult>> 2147 2148Moves a file or folder. This API uses a promise to return the result. 2149 2150You can forcibly overwrite the file with the same name in the destination directory. 2151 2152Currently, this API does not support move of files or folders across devices. 2153 2154**Model restriction**: This API can be used only in the stage model. 2155 2156**System capability**: SystemCapability.FileManagement.UserFileService 2157 2158**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 2159 2160**Parameters** 2161 2162| Name | Type | Mandatory| Description | 2163| --------- | ------- | ---- | ------------------------------------------------------------ | 2164| sourceUri | string | Yes | URI of the file or folder to move. | 2165| destUri | string | Yes | URI of the destination directory, to which the file or folder is moved. | 2166| force | boolean | No | Whether to forcibly overwrite the file with the same name. The value **true** means to overwrite the file forcibly; the value **false** means the opposite. The default value is **false**.| 2167 2168**Return value** 2169 2170| Type | Description | 2171| ------------------------------------------------------- | ------------------------------------------------------------ | 2172| Promise<Array<[MoveResult](#moveresult11)>> | Promise used to return the result. If the operation is successful, no information is returned. If the operation fails, a **MoveResult** array is returned.| 2173 2174**Error codes** 2175 2176For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 2177 2178Example 1: Move a file with **force** unspecified. 2179 2180```ts 2181import { BusinessError } from '@ohos.base'; 2182// A built-in storage directory is used as an example. 2183// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 2184// You can use the URI obtained. 2185async function moveItemFunc01() { 2186 let sourceUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 2187 let destUri: string = "file://docs/storage/Users/currentUser/Download/test"; 2188 try { 2189 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 2190 let moveResult = await fileAccessHelper.moveItem(sourceUri, destUri); 2191 if (moveResult.length === 0) { 2192 console.log("moveItem success"); 2193 } else { 2194 for (let i = 0; i < moveResult.length; i++) { 2195 console.error("errCode" + moveResult[i].errCode); 2196 console.error("errMsg" + moveResult[i].errMsg); 2197 console.error("sourceUri" + moveResult[i].sourceUri); 2198 console.error("destUri" + moveResult[i].destUri); 2199 } 2200 } 2201 } catch (err) { 2202 let error: BusinessError = err as BusinessError; 2203 console.error("moveItem failed, errCode:" + error.code + ", errMessage:" + error.message); 2204 } 2205} 2206``` 2207 2208Example 2: Move a file or folder when **force** set to **true**. 2209 2210```ts 2211import { BusinessError } from '@ohos.base'; 2212// A built-in storage directory is used as an example. 2213// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 2214// You can use the URI obtained. 2215async function moveItemFunc02() { 2216 let sourceUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 2217 let destUri: string = "file://docs/storage/Users/currentUser/Download/test"; 2218 try { 2219 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 2220 let moveResult = await fileAccessHelper.moveItem(sourceUri, destUri, true); 2221 if (moveResult.length === 0) { 2222 console.log("moveItem success"); 2223 } else { 2224 for (let i = 0; i < moveResult.length; i++) { 2225 console.error("errCode" + moveResult[i].errCode); 2226 console.error("errMsg" + moveResult[i].errMsg); 2227 console.error("sourceUri" + moveResult[i].sourceUri); 2228 console.error("destUri" + moveResult[i].destUri); 2229 } 2230 } 2231 } catch (err) { 2232 let error: BusinessError = err as BusinessError; 2233 console.error("moveItem failed, errCode:" + error.code + ", errMessage:" + error.message); 2234 } 2235} 2236``` 2237 2238### moveItem<sup>11+</sup> 2239 2240moveItem(sourceUri: string, destUri: string, callback: AsyncCallback<Array<MoveResult>>) : void 2241 2242Moves a file or folder. This API uses an asynchronous callback to return the result. 2243 2244Currently, this API does not support move of files or folders across devices. 2245 2246**Model restriction**: This API can be used only in the stage model. 2247 2248**System capability**: SystemCapability.FileManagement.UserFileService 2249 2250**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 2251 2252**Parameters** 2253 2254| Name | Type | Mandatory| Description | 2255| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2256| sourceUri | string | Yes | URI of the file or folder to move. | 2257| destUri | string | Yes | URI of the destination directory, to which the file or folder is moved. | 2258| callback | AsyncCallback<Array<[MoveResult](#moveresult11)>> | Yes | Callback used to return the result. If the operation is successful, no information is returned. If the operation fails, a **moveResult** array is returned.| 2259 2260**Example** 2261 2262```ts 2263import { BusinessError } from '@ohos.base'; 2264// A built-in storage directory is used as an example. 2265// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 2266// You can use the URI obtained. 2267let sourceUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 2268let destUri: string = "file://docs/storage/Users/currentUser/Download/test"; 2269try { 2270 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 2271 fileAccessHelper.moveItem(sourceUri, destUri, async (err: BusinessError, copyResult: Array<fileAccess.MoveResult>) => { 2272 if (err) { 2273 console.error("moveItem failed, errCode:" + err.code + ", errMessage:" + err.message); 2274 } 2275 if (moveResult.length === 0) { 2276 console.log("moveItem success"); 2277 } else { 2278 for (let i = 0; i < moveResult.length; i++) { 2279 console.error("errCode" + moveResult[i].errCode); 2280 console.error("errMsg" + moveResult[i].errMsg); 2281 console.error("sourceUri" + moveResult[i].sourceUri); 2282 console.error("destUri" + moveResult[i].destUri); 2283 } 2284 } 2285 }); 2286} catch (err) { 2287 let error: BusinessError = err as BusinessError; 2288 console.error("moveItem failed, errCode:" + error.code + ", errMessage:" + error.message); 2289} 2290``` 2291 2292### moveItem<sup>11+</sup> 2293 2294moveItem(sourceUri: string, destUri: string, force: boolean, callback: AsyncCallback<Array<MoveResult>>) : void 2295 2296Moves a file or folder with the specified mode. This API uses an asynchronous callback to return the result. 2297 2298If a file with the same name exists in the destination directory, you can forcibly overwrite the file. 2299 2300Currently, this API does not support move of files or folders across devices. 2301 2302**Model restriction**: This API can be used only in the stage model. 2303 2304**System capability**: SystemCapability.FileManagement.UserFileService 2305 2306**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 2307 2308**Parameters** 2309 2310| Name | Type | Mandatory| Description | 2311| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2312| sourceUri | string | Yes | URI of the file or folder to move. | 2313| destUri | string | Yes | URI of the destination directory, to which the file or folder is moved. | 2314| force | boolean | Yes | Whether to forcibly overwrite the file with the same name. The value **true** means to overwrite the file forcibly; the value **false** means the opposite. The default value is **false**.| 2315| callback | AsyncCallback<Array<[MoveResult](#moveresult11)>> | Yes | Callback used to return the result. If the operation is successful, no information is returned. If the operation fails, a **moveResult** array is returned.| 2316 2317**Example** 2318 2319```ts 2320import { BusinessError } from '@ohos.base'; 2321// A built-in storage directory is used as an example. 2322// In the sample code, sourceFile indicates the file (directory) in the Download directory to copy, destFile indicates the destination directory in the Download directory, and uri is to URI in fileInfo. 2323// You can use the URI obtained. 2324let sourceUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 2325let destUri: string = "file://docs/storage/Users/currentUser/Download/test"; 2326try { 2327 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 2328 fileAccessHelper.moveItem(sourceUri, destUri, true, async (err: BusinessError, moveResult: Array<fileAccess.MoveResult>) => { 2329 if (err) { 2330 console.error("moveItem failed, errCode:" + err.code + ", errMessage:" + err.message); 2331 } 2332 if (moveResult.length === 0) { 2333 console.log("copy success"); 2334 } else { 2335 for (let i = 0; i < moveResult.length; i++) { 2336 console.error("errCode" + moveResult[i].errCode); 2337 console.error("errMsg" + moveResult[i].errMsg); 2338 console.error("sourceUri" + moveResult[i].sourceUri); 2339 console.error("destUri" + moveResult[i].destUri); 2340 } 2341 } 2342 }); 2343} catch (err) { 2344 let error: BusinessError = err as BusinessError; 2345 console.error("moveItem failed, errCode:" + error.code + ", errMessage:" + error.message); 2346} 2347``` 2348 2349### moveFile<sup>11+</sup> 2350 2351moveFile(sourceUri: string, destUri: string, fileName: string) : Promise<string> 2352 2353Moves a file, and renames it if a file with the same name already exists in the destination directory. This API uses a promise to return the result. 2354 2355 2356Currently, this API does not support move of files across devices. 2357 2358**Model restriction**: This API can be used only in the stage model. 2359 2360**System capability**: SystemCapability.FileManagement.UserFileService 2361 2362**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 2363 2364**Parameters** 2365 2366| Name | Type | Mandatory| Description | 2367| ---------- | ------ | ---- | ------------------- | 2368| sourceFile | string | Yes | URI of the file to move.| 2369| destFile | string | Yes | URI of the destination directory, to which the file is moved. | 2370| fileName | string | Yes | New name of the file. | 2371 2372**Return value** 2373 2374| Type | Description | 2375| --------------------- | ------------------- | 2376| Promise<string> | Promise used to return the URI of the file in the destination directory.| 2377 2378**Error codes** 2379 2380For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 2381 2382**Example** 2383 2384 ```ts 2385 import { BusinessError } from '@ohos.base'; 2386 async function moveFile01() { 2387 // A built-in storage directory is used as an example. 2388 // In the sample code, sourceUri and destUri indicate the files or directories in the Download directory. The URI is the URI in fileInfo. 2389 // You can use the URI obtained. 2390 let sourceUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 2391 let destUri: string = "file://docs/storage/Users/currentUser/Download/test"; 2392 let fileName: string; 2393 try { 2394 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 2395 let fileUri = await fileAccessHelper.moveFile(sourceUri, destUri, fileName); 2396 console.log("moveFile sucess, fileUri: " + JSON.stringify(fileUri)); 2397 } catch (err) { 2398 let error: BusinessError = err as BusinessError; 2399 console.error("moveFile failed, errCode:" + error.code + ", errMessage:" + error.message); 2400 } 2401 } 2402 ``` 2403 2404### moveFile<sup>11+</sup> 2405 2406moveFile(sourceUri: string, destUri: string, fileName: string, callback: AsyncCallback<string>) : void 2407 2408Moves a file, and renames it if a file with the same name already exists in the destination directory. This API uses an asynchronous callback to return the result. 2409 2410 2411 2412Currently, this API does not support move of files across devices. 2413 2414**Model restriction**: This API can be used only in the stage model. 2415 2416**System capability**: SystemCapability.FileManagement.UserFileService 2417 2418**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 2419 2420**Parameters** 2421 2422| Name | Type | Mandatory| Description | 2423| ---------- | --------------------------- | ---- | --------------------- | 2424| sourceFile | string | Yes | URI of the file to move.| 2425| destFile | string | Yes | URI of the destination directory, to which the file is moved. | 2426| fileName | string | Yes | New name of the file. | 2427| callback | AsyncCallback<string> | Yes | Callback used to return the URI of the file in the destination directory. | 2428 2429**Error codes** 2430 2431For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 2432 2433**Example** 2434 2435 ```ts 2436 import { BusinessError } from '@ohos.base'; 2437 // A built-in storage directory is used as an example. 2438 // In the sample code, sourceUri and destUri indicate the files or directories in the Download directory. The URI is the URI in fileInfo. 2439 // You can use the URI obtained. 2440 let sourceUri: string = "file://docs/storage/Users/currentUser/Download/1.txt"; 2441 let destUri: string = "file://docs/storage/Users/currentUser/Download/test"; 2442 let fileName: string; 2443 try { 2444 // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper. 2445 fileAccessHelper.moveFile(sourceUri, destUri, fileName, (err: BusinessError, fileUri: string) => { 2446 if (err) { 2447 console.error("Failed to moveFile in async, errCode:" + err.code + ", errMessage:" + err.message); 2448 } 2449 console.log("moveFile sucess, fileUri: " + JSON.stringify(fileUri)); 2450 }); 2451 } catch (err) { 2452 let error: BusinessError = err as BusinessError; 2453 console.error("moveFile failed, errCode:" + error.code + ", errMessage:" + error.message); 2454 } 2455 ``` 2456 2457## CopyResult<sup>10+</sup> 2458 2459Defines the information returned when the file copy operation fails. If the copy operation is successful, no information is returned. 2460 2461**System capability**: SystemCapability.FileManagement.UserFileService 2462 2463**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 2464 2465| Name | Type | Read-Only| Writable| Description | 2466| --------- | ------ | ---- | ---- | ----------------- | 2467| sourceUri | string | Yes | No | URI of the source file or folder. | 2468| destUri | string | Yes | No | URI of the conflicting file. If the error is not caused by a conflict, **destUri** is empty.| 2469| errCode | number | Yes | No | Error code. | 2470| errMsg | string | Yes | No | Error information. | 2471 2472## OPENFLAGS 2473 2474Enumerates the file open modes. 2475 2476**Model restriction**: This API can be used only in the stage model. 2477 2478**System capability**: SystemCapability.FileManagement.UserFileService 2479 2480| Name| Value| Description| 2481| ----- | ------ | ------ | 2482| READ | 0o0 | Read mode.| 2483| WRITE | 0o1 | Write mode.| 2484| WRITE_READ | 0o2 | Read/Write mode.| 2485 2486## FILEKEY<sup>10+</sup> 2487 2488Enumerates the keys of the file attributes to query. 2489 2490**Model restriction**: This API can be used only in the stage model. 2491 2492**System capability**: SystemCapability.FileManagement.UserFileService 2493 2494| Name | Value | Description | 2495| ------------- | ------------- | ----------------------------------- | 2496| DISPLAY_NAME | 'display_name' | Name of the file. | 2497| DATE_ADDED | 'date_added' | Date when the file was created, for example, **1501925454**. | 2498| DATE_MODIFIED | 'date_modified' | Date when the file was modified, for example, **1665310670**. | 2499| RELATIVE_PATH | 'relative_path' | Relative path of the file, for example, **Pictures/Screenshots/**.| 2500| FILE_SIZE | 'size' | Size of the file, in bytes. | 2501 2502## NotifyType<sup>10+</sup> 2503 2504Enumerates the notification types. 2505 2506**Model restriction**: This API can be used only in the stage model. 2507 2508**System capability**: SystemCapability.FileManagement.UserFileService 2509 2510| Name | Value | Description | 2511| ----------------- | ---- | ------------------------------------------------------------ | 2512| NOTIFY_ADD | 0 | File added.<br>See examples 2 and 3 of **registerObserver**. | 2513| NOTIFY_DELETE | 1 | File deleted.<br>See examples 1 and 2 of **unregisterObserver(uri: string, callback: Callback<NotifyMessage>)**. | 2514| NOTIFY_MOVED_TO | 2 | File or folder moved in (for example, a file or folder in the target directory is renamed, or a file or folder is moved to the target directory).<br>See example 1 of **registerObserver** and example 1 of **unregisterObserver(uri: string)**.| 2515| NOTIFY_MOVED_FROM | 3 | File or folder moved out (for example, a file or folder in the target directory is renamed and no longer in the target directory, or a file or folder is moved out from the target directory).<br>See example 1 of **registerObserver** and example 1 of **unregisterObserver(uri: string)**.| 2516| NOTIFY_MOVE_SELF | 4 | File moved (for example, the target file or folder is renamed or moved).<br>See example 1 **registerObserver**. | 2517| NOTIFY_DEVICE_ONLINE<sup>11+</sup> | 5 | Device goes online. | 2518| NOTIFY_DEVICE_OFFLINE<sup>11+</sup> | 6 | Device goes offline. | 2519 2520## NotifyMessage<sup>10+</sup> 2521 2522Represents the notification message. 2523 2524**Model restriction**: This API can be used only in the stage model. 2525 2526**System capability**: SystemCapability.FileManagement.UserFileService 2527 2528**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 2529 2530| Name| Type | Read-Only| Writable| Description | 2531| ---- | --------------------------- | ---- | ---- | --------------------------------------------------------- | 2532| type | [NotifyType](#notifytype10) | Yes | No | Notification type. | 2533| uris | Array<string> | Yes | No | URIs of the changed files. Currently, only one notification is supported. A collection of multiple notifications will be supported in later versions.| 2534 2535## MoveResult<sup>11+</sup> 2536 2537Represents the information returned when the move operation fails. If the operation is successful, no information is returned. 2538 2539**Model restriction**: This API can be used only in the stage model. 2540 2541**System capability**: SystemCapability.FileManagement.UserFileService 2542 2543**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER 2544 2545| Name | Type | Read-Only| Writable| Description | 2546| --------- | ------ | ---- | ---- | ------------------------------------------------------------ | 2547| sourceUri | string | Yes | No | URI of the source file or folder. | 2548| destUri | string | Yes | No | URI of the conflicting file. If the error is not caused by a file conflict, **destUri** is empty. | 2549| errCode | number | Yes | No | Error code. For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).| 2550| errMsg | string | Yes | No | Error message. | 2551