1# @system.file (File Storage) 2 3> **NOTE**<br> 4> - The APIs of this module are no longer maintained since API version 6. You are advised to use [`@ohos.fileio`](js-apis-fileio.md). 5> 6> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. 7 8 9## Modules to Import 10 11 12``` 13import file from '@system.file'; 14``` 15 16 17## file.move 18 19move(Object): void 20 21Moves a specified file to a given location. 22 23**System capability**: SystemCapability.FileManagement.File.FileIO 24 25**Parameters** 26 27| Name| Type| Mandatory| Description| 28| -------- | -------- | -------- | -------- | 29| srcUri | string | Yes| Uniform resource identifier (URI) of the file to move. <br/>The URI can contain a maximum of 128 characters, excluding the following characters: "\*+,:;<=>?[]\|\x7F | 30| dstUri | string | Yes| URI of the location to which the file is to move. <br/>The URI can contain a maximum of 128 characters, excluding the following characters: "\*+,:;<=>?[]\|\x7F | 31| success | Function | No| Called when the file is moved to the specified location. This API returns the URI of the destination location.| 32| fail | Function | No| Called when the file fails to be moved.| 33| complete | Function | No| Called when the execution is complete.| 34 35**Return value of fail()** 36 37| Error Code| Description| 38| -------- | -------- | 39| 202 | Incorrect parameters are detected.| 40| 300 | An I/O error occurs.| 41| 301 | The file or directory does not exist.| 42 43**Example** 44 45``` 46export default { 47 move() { 48 file.move({ 49 srcUri: 'internal://app/myfiles1', 50 dstUri: 'internal://app/myfiles2', 51 success: function(uri) { 52 console.log('call success callback success'); 53 }, 54 fail: function(data, code) { 55 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 56 }, 57 }); 58 } 59} 60``` 61 62 63## file.copy 64 65copy(Object): void 66 67Copies a file to the given URI. 68 69**System capability**: SystemCapability.FileManagement.File.FileIO 70 71**Parameters** 72 73| Name| Type| Mandatory| Description| 74| -------- | -------- | -------- | -------- | 75| srcUri | string | Yes| URI of the file to copy.| 76| dstUri | string | Yes| URI of the location to which the copy is to be saved.<br>The directory of application resources and URI of the **tmp** type are not supported.| 77| success | Function | No| Called when the file is copied and saved to the specified location. This API returns the URI of the destination location.| 78| fail | Function | No| Called when the file fails to be copied.| 79| complete | Function | No| Called when the execution is complete.| 80 81**Return value of fail()** 82 83| Error Code| Description| 84| -------- | -------- | 85| 202 | Incorrect parameters are detected.| 86| 300 | An I/O error occurs.| 87| 301 | The file or directory does not exist.| 88 89**Example** 90 91``` 92export default { 93 copy() { 94 file.copy({ 95 srcUri: 'internal://app/file.txt', 96 dstUri: 'internal://app/file_copy.txt', 97 success: function(uri) { 98 console.log('call success callback success'); 99 }, 100 fail: function(data, code) { 101 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 102 }, 103 }); 104 } 105} 106``` 107 108 109## file.list 110 111list(Object): void 112 113Obtains all files in the specified directory. 114 115**System capability**: SystemCapability.FileManagement.File.FileIO 116 117**Parameters** 118 119| Name| Type| Mandatory| Description| 120| -------- | -------- | -------- | -------- | 121| uri | string | Yes| URI of the directory. <br/>The URI can contain a maximum of 128 characters, excluding the following characters: "\*+,:;<=>?[]\|\x7F | 122| success | Function | No| Called when the file list is obtained.| 123| fail | Function | No| Called when the file list fails to be obtained.| 124| complete | Function | No| Called when the execution is complete.| 125 126**Return value of success()** 127 128| Name| Type| Description| 129| -------- | -------- | -------- | 130| fileList | Array<FileInfo> | File list. The format of each file is as follows:<br>{<br>uri:'file1',<br>lastModifiedTime:1589965924479,<br>length:10240,<br>type: 'file'<br>} | 131 132**Table 1** FileInfo 133 134| Name| Type| Description| 135| -------- | -------- | -------- | 136| uri | string | URI of the file.| 137| lastModifiedTime | number | Timestamp when the file is saved the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT.| 138| length | number | File size, in bytes.| 139| type | string | File type. Available values are as follows:<br>- **dir**: directory<br>- **file**: file | 140 141**Return value of fail()** 142 143| Error Code| Description| 144| -------- | -------- | 145| 202 | Incorrect parameters are detected.| 146| 300 | An I/O error occurs.| 147| 301 | The file or directory does not exist.| 148 149**Example** 150 151``` 152export default { 153 list() { 154 file.list({ 155 uri: 'internal://app/pic', 156 success: function(data) { 157 console.log(JSON.stringify(data.fileList)); 158 }, 159 fail: function(data, code) { 160 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 161 }, 162 }); 163 } 164} 165``` 166 167 168## file.get 169 170get(Object): void 171 172Obtains information about a local file. 173 174**System capability**: SystemCapability.FileManagement.File.FileIO 175 176**Parameters** 177 178| Name| Type| Mandatory| Description| 179| -------- | -------- | -------- | -------- | 180| uri | string | Yes| URI of the file.| 181| recursive | boolean | No| Whether to recursively obtain the file list under a subdirectory. The default value is **false**.| 182| success | Function | No| Called when the file information is obtained.| 183| fail | Function | No| Called when the file information fails to be obtained.| 184| complete | Function | No| Called when the execution is complete.| 185 186**Return value of success()** 187 188| Name| Type| Description| 189| -------- | -------- | -------- | 190| uri | string | URI of the file.| 191| length | number | File size, in bytes.| 192| lastModifiedTime | number | Timestamp when the file is saved the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT.| 193| type | string | File type. Available values are as follows:<br>- **dir**: directory<br>- **file**: file | 194| subFiles | Array | List of files.| 195 196**Return value of fail()** 197 198| Error Code| Description| 199| -------- | -------- | 200| 202 | Incorrect parameters are detected.| 201| 300 | An I/O error occurs.| 202| 301 | The file or directory does not exist.| 203 204**Example** 205 206``` 207export default { 208 get() { 209 file.get({ 210 uri: 'internal://app/file', 211 success: function(data) { 212 console.log(data.uri); 213 }, 214 fail: function(data, code) { 215 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 216 }, 217 }); 218 } 219} 220``` 221 222 223## file.delete 224 225delete(Object): void 226 227Deletes a local file. 228 229**System capability**: SystemCapability.FileManagement.File.FileIO 230 231**Parameters** 232 233| Name| Type| Mandatory| Description| 234| -------- | -------- | -------- | -------- | 235| uri | string | Yes| URI of the file to delete. It cannot be an application resource path.| 236| success | Function | No| Called when the file is deleted.| 237| fail | Function | No| Called when the file fails to be deleted.| 238| complete | Function | No| Called when the execution is complete.| 239 240**Return value of fail()** 241 242| Error Code| Description| 243| -------- | -------- | 244| 202 | Incorrect parameters are detected.| 245| 300 | An I/O error occurs.| 246| 301 | The file or directory does not exist.| 247 248**Example** 249 250``` 251export default { 252 delete() { 253 file.delete({ 254 uri: 'internal://app/my_file', 255 success: function() { 256 console.log('call delete success.'); 257 }, 258 fail: function(data, code) { 259 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 260 }, 261 }); 262 } 263} 264``` 265 266 267## file.writeText 268 269writeText(Object): void 270 271Writes text into a file. Only text files can be read and written. 272 273**System capability**: SystemCapability.FileManagement.File.FileIO 274 275**Parameters** 276 277| Name| Type| Mandatory| Description| 278| -------- | -------- | -------- | -------- | 279| uri | string | Yes| URI of a local file. If it does not exist, a file will be created.| 280| text | string | Yes| Text to write into the file. | 281| encoding | string | No| Encoding format. The default format is **UTF-8**.| 282| append | boolean | No| Whether to enable the append mode. The default value is **false**.| 283| success | Function | No| Called when the text is written into the specified file.| 284| fail | Function | No| Called when the text fails to be written into the file.| 285| complete | Function | No| Called when the execution is complete.| 286 287**Return value of fail()** 288 289| Error Code| Description| 290| -------- | -------- | 291| 202 | Incorrect parameters are detected.| 292| 300 | An I/O error occurs.| 293 294**Example** 295 296``` 297export default { 298 writeText() { 299 file.writeText({ 300 uri: 'internal://app/test.txt', 301 text: 'Text that just for test.', 302 success: function() { 303 console.log('call writeText success.'); 304 }, 305 fail: function(data, code) { 306 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 307 }, 308 }); 309 } 310} 311``` 312 313 314## file.writeArrayBuffer 315 316writeArrayBuffer(Object): void 317 318Writes buffer data into a file. Only text files can be read and written. 319 320**System capability**: SystemCapability.FileManagement.File.FileIO 321 322**Parameters** 323 324| Name| Type| Mandatory| Description| 325| -------- | -------- | -------- | -------- | 326| uri | string | Yes| URI of a local file. If it does not exist, a file will be created.| 327| buffer | Uint8Array | Yes| Buffer from which the data is derived.| 328| position | number | No| Offset to the position where the writing starts. The default value is **0**.| 329| append | boolean | No| Whether to enable the append mode. The default value is **false**. If the value is **true**, the **position** parameter will become invalid.| 330| success | Function | No| Called when buffer data is written into the file. | 331| fail | Function | No| Called when buffer data fails to be written into the file.| 332| complete | Function | No| Called when the execution is complete.| 333 334**Return value of fail()** 335 336| Error Code| Description| 337| -------- | -------- | 338| 202 | Incorrect parameters are detected.| 339| 300 | An I/O error occurs.| 340 341**Example** 342 343``` 344export default { 345 writeArrayBuffer() { 346 file.writeArrayBuffer({ 347 uri: 'internal://app/test', 348 buffer: new Uint8Array(8), // The buffer is of the Uint8Array type. 349 success: function() { 350 console.log('call writeArrayBuffer success.'); 351 }, 352 fail: function(data, code) { 353 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 354 }, 355 }); 356 } 357} 358``` 359 360 361## file.readText 362 363readText(Object): void 364 365Reads text from a file. Only text files can be read and written. 366 367**System capability**: SystemCapability.FileManagement.File.FileIO 368 369**Parameters** 370 371| Name| Type| Mandatory| Description| 372| -------- | -------- | -------- | -------- | 373| uri | string | Yes| URI of a local file.| 374| encoding | string | No| Encoding format. The default format is **UTF-8**.| 375| position | number | No| Position where the reading starts. The default value is the start position of the file.| 376| length | number | No| Length of the text to read, in bytes. The default value is **4096**.| 377| success | Function | No| Called when the text is read successfully.| 378| fail | Function | No| Called when the text failed to be read.| 379| complete | Function | No| Called when the execution is complete.| 380 381**Return value of success()** 382 383| Name| Type| Description| 384| -------- | -------- | -------- | 385| text | string | Text read from the specified file.| 386 387**Return value of fail()** 388 389| Error Code| Description| 390| -------- | -------- | 391| 202 | Incorrect parameters are detected.| 392| 300 | An I/O error occurs.| 393| 301 | The file or directory does not exist.| 394| 302 | The text to read exceeds 4 KB.| 395 396**Example** 397 398``` 399export default { 400 readText() { 401 file.readText({ 402 uri: 'internal://app/text.txt', 403 success: function(data) { 404 console.log('call readText success: ' + data.text); 405 }, 406 fail: function(data, code) { 407 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 408 }, 409 }); 410 } 411} 412``` 413 414 415## file.readArrayBuffer 416 417readArrayBuffer(Object): void 418 419Reads buffer data from a file. Only text files can be read and written. 420 421**System capability**: SystemCapability.FileManagement.File.FileIO 422 423**Parameters** 424 425| Name| Type| Mandatory| Description| 426| -------- | -------- | -------- | -------- | 427| uri | string | Yes| URI of a local file.| 428| position | number | No| Position where the reading starts. The default value is the start position of the file.| 429| length | number | No| Length of data to read. If this parameter is not set, the reading proceeds until the end of the file.| 430| success | Function | No| Called when the buffer data is read successfully.| 431| fail | Function | No| Called when the buffer data fails to be read.| 432| complete | Function | No| Called when the execution is complete.| 433 434**Return value of success()** 435 436| Name| Type| Description| 437| -------- | -------- | -------- | 438| buffer | Uint8Array | Data read.| 439 440**Return value of fail()** 441 442| Error Code| Description| 443| -------- | -------- | 444| 202 | Incorrect parameters are detected.| 445| 300 | An I/O error occurs.| 446| 301 | The file or directory does not exist.| 447 448**Example** 449 450``` 451export default { 452 readArrayBuffer() { 453 file.readArrayBuffer({ 454 uri: 'internal://app/test', 455 position: 10, 456 length: 200, 457 success: function(data) { 458 console.log('call readArrayBuffer success: ' + data.buffer); 459 }, 460 fail: function(data, code) { 461 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 462 }, 463 }); 464 } 465} 466``` 467 468 469## file.access 470 471access(Object): void 472 473Checks whether a file or directory exists. 474 475**System capability**: SystemCapability.FileManagement.File.FileIO 476 477**Parameters** 478 479| Name| Type| Mandatory| Description| 480| -------- | -------- | -------- | -------- | 481| uri | string | Yes| URI of the directory or file to check.| 482| success | Function | No| Called when the operation is successful.| 483| fail | Function | No| Called when the operation fails.| 484| complete | Function | No| Called when the execution is complete.| 485 486**Return value of fail()** 487 488| Error Code| Description| 489| -------- | -------- | 490| 202 | Incorrect parameters are detected.| 491| 300 | An I/O error occurs.| 492| 301 | The file or directory does not exist.| 493 494**Example** 495 496``` 497export default { 498 access() { 499 file.access({ 500 uri: 'internal://app/test', 501 success: function() { 502 console.log('call access success.'); 503 }, 504 fail: function(data, code) { 505 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 506 }, 507 }); 508 } 509} 510``` 511 512 513## file.mkdir 514 515mkdir(Object): void 516 517Creates a directory. 518 519**System capability**: SystemCapability.FileManagement.File.FileIO 520 521**Parameters** 522 523| Name| Type| Mandatory| Description| 524| -------- | -------- | -------- | -------- | 525| uri | string | Yes| URI of the directory to create.| 526| recursive | boolean | No| Whether to recursively create upper-level directories of the specified directory. The default value is **false**.| 527| success | Function | No| Called when the directory is created.| 528| fail | Function | No| Called when the directory fails to be created.| 529| complete | Function | No| Called when the execution is complete.| 530 531**Return value of fail()** 532 533| Error Code| Description| 534| -------- | -------- | 535| 202 | Incorrect parameters are detected.| 536| 300 | An I/O error occurs.| 537 538**Example** 539 540``` 541export default { 542 mkdir() { 543 file.mkdir({ 544 uri: 'internal://app/test_directory', 545 success: function() { 546 console.log('call mkdir success.'); 547 }, 548 fail: function(data, code) { 549 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 550 }, 551 }); 552 } 553} 554``` 555 556 557## file.rmdir 558 559rmdir(Object): void 560 561Deletes a directory. 562 563**System capability**: SystemCapability.FileManagement.File.FileIO 564 565**Parameters** 566 567| Name| Type| Mandatory| Description| 568| -------- | -------- | -------- | -------- | 569| uri | string | Yes| URI of the directory to delete.| 570| recursive | boolean | No| Whether to recursively delete files and subdirectories of the specified directory. The default value is **false**.| 571| success | Function | No| Called when the directory is deleted.| 572| fail | Function | No| Called when the directory fails to be deleted.| 573| complete | Function | No| Called when the execution is complete.| 574 575**Return value of fail()** 576 577| Error Code| Description| 578| -------- | -------- | 579| 202 | Incorrect parameters are detected.| 580| 300 | An I/O error occurs.| 581| 301 | The file or directory does not exist.| 582 583**Example** 584 585``` 586export default { 587 rmdir() { 588 file.rmdir({ 589 uri: 'internal://app/test_directory', 590 success: function() { 591 console.log('call rmdir success.'); 592 }, 593 fail: function(data, code) { 594 console.error('call fail callback fail, code: ' + code + ', data: ' + data); 595 }, 596 }); 597 } 598} 599``` 600