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