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