1/* 2 * Copyright (c) 2020 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16export interface FileResponse { 17 /** 18 * File URI. 19 * @syscap SystemCapability.FileManagement.File.FileIO 20 * @since 3 21 */ 22 uri: string; 23 24 /** 25 * File size, in bytes. 26 * If type is dir, the length value is fixed to 0. 27 * @syscap SystemCapability.FileManagement.File.FileIO 28 * @since 3 29 */ 30 length: number; 31 32 /** 33 * Timestamp when the file is stored, which is the number of milliseconds elapsed since 1970/01/01 00:00:00. 34 * For lite wearables, the value is fixed to 0, because this parameter is restricted by the underlying file system. 35 * @syscap SystemCapability.FileManagement.File.FileIO 36 * @since 3 37 */ 38 lastModifiedTime: number; 39 40 /** 41 * File type. The values are as follows: 42 * dir: directory 43 * file: file 44 * @syscap SystemCapability.FileManagement.File.FileIO 45 * @since 3 46 */ 47 type: "dir" | "file"; 48 49 /** 50 * File list. When the recursive value is true and the type is dir, the file information under the subdirectory will be returned. 51 * Otherwise, no value will be returned. 52 * @syscap SystemCapability.FileManagement.File.FileIO 53 * @since 3 54 */ 55 subFiles?: Array<FileResponse>; 56} 57 58/** 59 * @syscap SystemCapability.FileManagement.File.FileIO 60 * @since 3 61 */ 62export interface FileMoveOption { 63 /** 64 * URI of the file to move. 65 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 66 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 67 * 2. The maximum number of characters allowed is 128. 68 * @syscap SystemCapability.FileManagement.File.FileIO 69 * @since 3 70 */ 71 srcUri: string; 72 73 /** 74 * URI of the file moved to the target location. 75 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 76 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 77 * 2. The maximum number of characters allowed is 128. 78 * @syscap SystemCapability.FileManagement.File.FileIO 79 * @since 3 80 */ 81 dstUri: string; 82 83 /** 84 * Called when the source file is moved to the specified location successfully. 85 * This function returns the URI of the file moved to the target location. 86 * @syscap SystemCapability.FileManagement.File.FileIO 87 * @since 3 88 */ 89 success?: (uri: string) => void; 90 91 /** 92 * Called when moving fails. 93 * @syscap SystemCapability.FileManagement.File.FileIO 94 * @since 3 95 */ 96 fail?: (data: string, code: number) => void; 97 98 /** 99 * Called when the execution is completed. 100 * @syscap SystemCapability.FileManagement.File.FileIO 101 * @since 3 102 */ 103 complete?: () => void; 104} 105 106/** 107 * @syscap SystemCapability.FileManagement.File.FileIO 108 * @since 3 109 */ 110export interface FileListResponse { 111 /** 112 * @syscap SystemCapability.FileManagement.File.FileIO 113 * @since 3 114 */ 115 fileList: Array<FileResponse>; 116} 117 118/** 119 * @syscap SystemCapability.FileManagement.File.FileIO 120 * @since 3 121 */ 122export interface FileListOption { 123 /** 124 * URI of the directory. 125 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 126 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 127 * 2. The maximum number of characters allowed is 128. 128 * @syscap SystemCapability.FileManagement.File.FileIO 129 * @since 3 130 */ 131 uri: string; 132 133 /** 134 * Called when the list is obtained successfully. 135 * @syscap SystemCapability.FileManagement.File.FileIO 136 * @since 3 137 */ 138 success?: (data: FileListResponse) => void; 139 140 /** 141 * Called when the list fails to be obtained. 142 * @syscap SystemCapability.FileManagement.File.FileIO 143 * @since 3 144 */ 145 fail?: (data: string, code: number) => void; 146 147 /** 148 * Called when the execution is completed. 149 * @syscap SystemCapability.FileManagement.File.FileIO 150 * @since 3 151 */ 152 complete?: () => void; 153} 154 155export interface FileCopyOption { 156 /** 157 * URI of the file to copy. 158 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 159 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 160 * 2. The maximum number of characters allowed is 128. 161 * @syscap SystemCapability.FileManagement.File.FileIO 162 * @since 3 163 */ 164 srcUri: string; 165 166 /** 167 * URI of the file moved to the target location. 168 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 169 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 170 * 2. The maximum number of characters allowed is 128. 171 * @syscap SystemCapability.FileManagement.File.FileIO 172 * @since 3 173 */ 174 dstUri: string; 175 176 /** 177 * Called when the copy file is saved successful. 178 * This function returns the URI of the file saved to the target location. 179 * @syscap SystemCapability.FileManagement.File.FileIO 180 * @since 3 181 */ 182 success?: (uri: string) => void; 183 184 /** 185 * Called when the copy or save operation fails. 186 * @syscap SystemCapability.FileManagement.File.FileIO 187 * @since 3 188 */ 189 fail?: (data: string, code: number) => void; 190 191 /** 192 * Called when the execution is completed. 193 * @syscap SystemCapability.FileManagement.File.FileIO 194 * @since 3 195 */ 196 complete?: () => void; 197} 198 199export interface FileGetOption { 200 /** 201 * File URI, which cannot be an application resource path. 202 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 203 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 204 * 2. The maximum number of characters allowed is 128. 205 * @syscap SystemCapability.FileManagement.File.FileIO 206 * @since 3 207 */ 208 uri: string; 209 210 /** 211 * Whether to recursively obtain the file list under a subdirectory. 212 * The default value is false. 213 * @syscap SystemCapability.FileManagement.File.FileIO 214 * @since 3 215 */ 216 recursive?: boolean; 217 218 /** 219 * Called when file information is obtained successfully. 220 * @syscap SystemCapability.FileManagement.File.FileIO 221 * @since 3 222 */ 223 success?: (file: FileResponse) => void; 224 225 /** 226 * Called when file information fails to be obtained. 227 * @syscap SystemCapability.FileManagement.File.FileIO 228 * @since 3 229 */ 230 fail?: (data: string, code: number) => void; 231 232 /** 233 * Called when the execution is completed. 234 * @syscap SystemCapability.FileManagement.File.FileIO 235 * @since 3 236 */ 237 complete?: () => void; 238} 239 240export interface FileDeleteOption { 241 /** 242 * URI of the file to be deleted, which cannot be an application resource path. 243 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 244 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 245 * 2. The maximum number of characters allowed is 128. 246 * @syscap SystemCapability.FileManagement.File.FileIO 247 * @since 3 248 */ 249 uri: string; 250 251 /** 252 * Called when local files are deleted successfully. 253 * @syscap SystemCapability.FileManagement.File.FileIO 254 * @since 3 255 */ 256 success?: () => void; 257 258 /** 259 * Called when the deletion fails. 260 * @syscap SystemCapability.FileManagement.File.FileIO 261 * @since 3 262 */ 263 fail?: (data: string, code: number) => void; 264 265 /** 266 * Called when the execution is completed. 267 * @syscap SystemCapability.FileManagement.File.FileIO 268 * @since 3 269 */ 270 complete?: () => void; 271} 272 273/** 274 * @syscap SystemCapability.FileManagement.File.FileIO 275 * @since 3 276 */ 277export interface FileWriteTextOption { 278 /** 279 * URI of a local file. If it does not exist, a file will be created. 280 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 281 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 282 * 2. The maximum number of characters allowed is 128. 283 * @syscap SystemCapability.FileManagement.File.FileIO 284 * @since 3 285 */ 286 uri: string; 287 288 /** 289 * Character string to write into the local file. 290 * @syscap SystemCapability.FileManagement.File.FileIO 291 * @since 3 292 */ 293 text: string; 294 295 /** 296 * Encoding format. The default format is UTF-8. 297 * @syscap SystemCapability.FileManagement.File.FileIO 298 * @since 3 299 */ 300 encoding?: string; 301 302 /** 303 * Whether to enable the append mode. The default value is false. 304 * @syscap SystemCapability.FileManagement.File.FileIO 305 * @since 3 306 */ 307 append?: boolean; 308 309 /** 310 * Called when texts are written into a file successfully. 311 * @syscap SystemCapability.FileManagement.File.FileIO 312 * @since 3 313 */ 314 success?: () => void; 315 316 /** 317 * Called when texts fail to be written into a file. 318 * @syscap SystemCapability.FileManagement.File.FileIO 319 * @since 3 320 */ 321 fail?: (data: string, code: number) => void; 322 323 /** 324 * Called when the execution is completed. 325 * @syscap SystemCapability.FileManagement.File.FileIO 326 * @since 3 327 */ 328 complete?: () => void; 329} 330 331/** 332 * @syscap SystemCapability.FileManagement.File.FileIO 333 * @since 3 334 */ 335export interface FileReadTextResponse { 336 /** 337 * @syscap SystemCapability.FileManagement.File.FileIO 338 * @since 3 339 */ 340 text: string; 341} 342 343export interface FileReadTextOption { 344 /** 345 * URI of a local file. 346 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 347 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 348 * 2. The maximum number of characters allowed is 128. 349 * @syscap SystemCapability.FileManagement.File.FileIO 350 * @since 3 351 */ 352 uri: string; 353 354 /** 355 * Encoding format. The default format is UTF-8. 356 * Currently, only UTF-8 is supported. 357 * @syscap SystemCapability.FileManagement.File.FileIO 358 * @since 3 359 */ 360 encoding?: string; 361 362 /** 363 * Position where the reading starts. 364 * The default value is the start position of the file. 365 * @syscap SystemCapability.FileManagement.File.FileIO 366 * @since 3 367 */ 368 position?: number; 369 370 /** 371 * Position where the reading starts. 372 * The default value is the start position of the file. 373 * @syscap SystemCapability.FileManagement.File.FileIO 374 * @since 3 375 */ 376 length?: number; 377 378 /** 379 * Called when texts are read successfully. 380 * @syscap SystemCapability.FileManagement.File.FileIO 381 * @since 3 382 */ 383 success?: (data: FileReadTextResponse) => void; 384 385 /** 386 * Called when texts fail to be read. 387 * @syscap SystemCapability.FileManagement.File.FileIO 388 * @since 3 389 */ 390 fail?: (data: string, code: number) => void; 391 392 /** 393 * Called when the execution is completed. 394 * @syscap SystemCapability.FileManagement.File.FileIO 395 * @since 3 396 */ 397 complete?: () => void; 398} 399 400/** 401 * @syscap SystemCapability.FileManagement.File.FileIO 402 * @since 3 403 */ 404export interface FileWriteArrayBufferOption { 405 /** 406 * URI of a local file. If it does not exist, a file will be created. 407 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 408 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 409 * 2. The maximum number of characters allowed is 128. 410 * @syscap SystemCapability.FileManagement.File.FileIO 411 * @since 3 412 */ 413 uri: string; 414 415 /** 416 * Buffer from which the data is derived. 417 * @syscap SystemCapability.FileManagement.File.FileIO 418 * @since 3 419 */ 420 buffer: Uint8Array; 421 422 /** 423 * Offset to the position where the writing starts. The default value is 0. 424 * @syscap SystemCapability.FileManagement.File.FileIO 425 * @since 3 426 */ 427 position?: number; 428 429 /** 430 * Whether to enable the append mode. 431 * The default value is false. If the value is true, the position parameter will become invalid. 432 * @syscap SystemCapability.FileManagement.File.FileIO 433 * @since 3 434 */ 435 append?: boolean; 436 437 /** 438 * Called when data from a buffer is written into a file successfully. 439 * @syscap SystemCapability.FileManagement.File.FileIO 440 * @since 3 441 */ 442 success?: () => void; 443 444 /** 445 * Called when data from a buffer fails to be written into a file. 446 * @syscap SystemCapability.FileManagement.File.FileIO 447 * @since 3 448 */ 449 fail?: (data: string, code: number) => void; 450 451 /** 452 * Called when the execution is completed. 453 * @syscap SystemCapability.FileManagement.File.FileIO 454 * @since 3 455 */ 456 complete?: () => void; 457} 458 459/** 460 * @syscap SystemCapability.FileManagement.File.FileIO 461 * @since 3 462 */ 463export interface FileReadArrayBufferResponse { 464 /** 465 * @syscap SystemCapability.FileManagement.File.FileIO 466 * @since 3 467 */ 468 buffer: Uint8Array; 469} 470 471/** 472 * @syscap SystemCapability.FileManagement.File.FileIO 473 * @since 3 474 */ 475export interface FileReadArrayBufferOption { 476 /** 477 * URI of a local file. 478 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 479 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 480 * 2. The maximum number of characters allowed is 128. 481 * @syscap SystemCapability.FileManagement.File.FileIO 482 * @since 3 483 */ 484 uri: string; 485 486 /** 487 * Position where the reading starts. 488 * The default value is the start position of the file. 489 * @syscap SystemCapability.FileManagement.File.FileIO 490 * @since 3 491 */ 492 position?: number; 493 494 /** 495 * Length of the content to read. 496 * If this parameter is not set, all content of the file will be read. 497 * @syscap SystemCapability.FileManagement.File.FileIO 498 * @since 3 499 */ 500 length?: number; 501 502 /** 503 * Called when the buffer data is read successfully. 504 * @syscap SystemCapability.FileManagement.File.FileIO 505 * @since 3 506 */ 507 success?: (data: FileReadArrayBufferResponse) => void; 508 509 /** 510 * Called when the buffer data fails to be read. 511 * @syscap SystemCapability.FileManagement.File.FileIO 512 * @since 3 513 */ 514 fail?: (data: string, code: number) => void; 515 516 /** 517 * Called when the execution is completed. 518 * @syscap SystemCapability.FileManagement.File.FileIO 519 * @since 3 520 */ 521 complete?: () => void; 522} 523 524/** 525 * @syscap SystemCapability.FileManagement.File.FileIO 526 * @since 3 527 */ 528export interface FileAccessOption { 529 /** 530 * URI of the directory or file. 531 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 532 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 533 * 2. The maximum number of characters allowed is 128. 534 * @syscap SystemCapability.FileManagement.File.FileIO 535 * @since 3 536 */ 537 uri: string; 538 539 /** 540 * Called when the check result is obtained successfully. 541 * @syscap SystemCapability.FileManagement.File.FileIO 542 * @since 3 543 */ 544 success?: () => void; 545 546 /** 547 * Called when the check fails. 548 * @syscap SystemCapability.FileManagement.File.FileIO 549 * @since 3 550 */ 551 fail?: (data: string, code: number) => void; 552 553 /** 554 * Called when the execution is completed. 555 * @syscap SystemCapability.FileManagement.File.FileIO 556 * @since 3 557 */ 558 complete?: () => void; 559} 560 561/** 562 * @syscap SystemCapability.FileManagement.File.FileIO 563 * @since 3 564 */ 565export interface FileMkdirOption { 566 /** 567 * URI of the directory. 568 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 569 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 570 * 2. The maximum number of characters allowed is 128. 571 * 3. A maximum of five recursions are allowed for creating the directory. 572 * @syscap SystemCapability.FileManagement.File.FileIO 573 * @since 3 574 */ 575 uri: string; 576 577 /** 578 * Whether to create the directory after creating its upper-level directory recursively. 579 * The default value is false. 580 * @syscap SystemCapability.FileManagement.File.FileIO 581 * @since 3 582 */ 583 recursive?: boolean; 584 585 /** 586 * Called when the directory is created successfully. 587 * @syscap SystemCapability.FileManagement.File.FileIO 588 * @since 3 589 */ 590 success?: () => void; 591 592 /** 593 * Called when the creation fails. 594 * @syscap SystemCapability.FileManagement.File.FileIO 595 * @since 3 596 */ 597 fail?: (data: string, code: number) => void; 598 599 /** 600 * Called when the execution is completed. 601 * @syscap SystemCapability.FileManagement.File.FileIO 602 * @since 3 603 */ 604 complete?: () => void; 605} 606 607/** 608 * @syscap SystemCapability.FileManagement.File.FileIO 609 * @since 3 610 */ 611export interface FileRmdirOption { 612 /** 613 * URI of the directory. 614 * Restricted by the underlying file system of lite wearables, the value must meet the following requirements: 615 * 1. The URI cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. 616 * 2. The maximum number of characters allowed is 128. 617 * @syscap SystemCapability.FileManagement.File.FileIO 618 * @since 3 619 */ 620 uri: string; 621 622 /** 623 * Whether to delete files and subdirectories recursively. 624 * The default value is false. 625 * @syscap SystemCapability.FileManagement.File.FileIO 626 * @since 3 627 */ 628 recursive?: boolean; 629 630 /** 631 * Called when the directory is deleted successfully. 632 * @syscap SystemCapability.FileManagement.File.FileIO 633 * @since 3 634 */ 635 success?: () => void; 636 637 /** 638 * Called when the deletion fails. 639 * @syscap SystemCapability.FileManagement.File.FileIO 640 * @since 3 641 */ 642 fail?: (data: string, code: number) => void; 643 644 /** 645 * Called when the execution is completed. 646 * @syscap SystemCapability.FileManagement.File.FileIO 647 * @since 3 648 */ 649 complete?: () => void; 650} 651 652/** 653 * @syscap SystemCapability.FileManagement.File.FileIO 654 * @since 3 655 */ 656export default class File { 657 /** 658 * Moves the source file to a specified location. 659 * @syscap SystemCapability.FileManagement.File.FileIO 660 * @since 3 661 * @param options Options. 662 */ 663 static move(options: FileMoveOption): void; 664 665 /** 666 * Copies a source file and save the copy to a specified location. 667 * @syscap SystemCapability.FileManagement.File.FileIO 668 * @since 3 669 * @param options Options. 670 */ 671 static copy(options: FileCopyOption): void; 672 673 /** 674 * Obtains the list of files in a specified directory. 675 * @syscap SystemCapability.FileManagement.File.FileIO 676 * @since 3 677 * @param options Options. 678 */ 679 static list(options: FileListOption): void; 680 681 /** 682 * Obtains information about a local file. 683 * @syscap SystemCapability.FileManagement.File.FileIO 684 * @since 3 685 * @param options Options. 686 */ 687 static get(options: FileGetOption): void; 688 689 /** 690 * Deletes local files. 691 * @syscap SystemCapability.FileManagement.File.FileIO 692 * @since 3 693 * @param options Options. 694 */ 695 static delete(options: FileDeleteOption): void; 696 697 /** 698 * Writes texts into a file. 699 * @syscap SystemCapability.FileManagement.File.FileIO 700 * @since 3 701 * @param options Options. 702 */ 703 static writeText(options: FileWriteTextOption): void; 704 705 /** 706 * Reads texts from a file. 707 * @syscap SystemCapability.FileManagement.File.FileIO 708 * @since 3 709 * @param options Options. 710 */ 711 static readText(options: FileReadTextOption): void; 712 713 /** 714 * Writes data from a buffer into a file. 715 * @syscap SystemCapability.FileManagement.File.FileIO 716 * @since 3 717 * @param options Options. 718 */ 719 static writeArrayBuffer(options: FileWriteArrayBufferOption): void; 720 721 /** 722 * Reads buffer data from a file. 723 * @syscap SystemCapability.FileManagement.File.FileIO 724 * @since 3 725 * @param options Options. 726 */ 727 static readArrayBuffer(options: FileReadArrayBufferOption): void; 728 729 /** 730 * Checks whether a file or directory exists. 731 * @syscap SystemCapability.FileManagement.File.FileIO 732 * @since 3 733 * @param options Options. 734 */ 735 static access(options: FileAccessOption): void; 736 737 /** 738 * Creates a directory. 739 * @syscap SystemCapability.FileManagement.File.FileIO 740 * @since 3 741 * @param options Options. 742 */ 743 static mkdir(options: FileMkdirOption): void; 744 745 /** 746 * Deletes a directory. 747 * @syscap SystemCapability.FileManagement.File.FileIO 748 * @since 3 749 * @param options Options. 750 */ 751 static rmdir(options: FileRmdirOption): void; 752} 753