• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# File Management
2
3The fileio module provides APIs for file storage and management, including basic file management, directory management, file information statistics, and stream read and write.
4
5> **NOTE**<br>
6> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
7
8
9## Modules to Import
10
11```js
12import fileio from '@ohos.fileio';
13```
14
15
16## Guidelines
17
18Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows:
19
20Stage Model
21
22 ```js
23import Ability from '@ohos.application.Ability';
24class MainAbility extends Ability {
25    onWindowStageCreate(windowStage) {
26        let context = this.context;
27        let path = context.filesDir;
28    }
29}
30 ```
31
32 For details about how to obtain the stage model context, see [Stage Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-ability-context.md#abilitycontext).
33
34FA Model
35
36 ```js
37 import featureAbility from '@ohos.ability.featureAbility';
38 let context = featureAbility.getContext();
39 context.getFilesDir().then((data) => {
40      let path = data;
41 })
42 ```
43
44 For details about how to obtain the context of the FA model, see [FA Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-Context.md#context).
45
46## fileio.stat
47
48stat(path: string): Promise&lt;Stat&gt;
49
50Obtains file information. This API uses a promise to return the result.
51
52**System capability**: SystemCapability.FileManagement.File.FileIO
53
54**Parameters**
55
56| Name| Type  | Mandatory| Description                      |
57| ------ | ------ | ---- | -------------------------- |
58| path   | string | Yes  | Application sandbox path of the file.|
59
60**Return value**
61
62  | Type                          | Description        |
63  | ---------------------------- | ---------- |
64  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information obtained.|
65
66**Example**
67
68  ```js
69  fileio.stat(path).then(function(stat){
70      console.info("Got file info:"+ JSON.stringify(stat));
71  }).catch(function(err){
72      console.info("Failed to get file info. Error:"+ err);
73  });
74  ```
75
76
77## fileio.stat
78
79stat(path:string, callback:AsyncCallback&lt;Stat&gt;): void
80
81Obtains file information. This API uses an asynchronous callback to return the result.
82
83**System capability**: SystemCapability.FileManagement.File.FileIO
84
85**Parameters**
86
87| Name  | Type                              | Mandatory| Description                          |
88| -------- | ---------------------------------- | ---- | ------------------------------ |
89| path     | string                             | Yes  | Application sandbox path of the file.    |
90| callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes  | Callback invoked to return the file information obtained.|
91
92**Example**
93
94  ```js
95  fileio.stat(path, function (err, stat) {
96      // Example code in Stat
97  });
98  ```
99
100
101## fileio.statSync
102
103statSync(path:string): Stat
104
105Synchronously obtains file information.
106
107**System capability**: SystemCapability.FileManagement.File.FileIO
108
109**Parameters**
110
111| Name| Type  | Mandatory| Description                      |
112| ------ | ------ | ---- | -------------------------- |
113| path   | string | Yes  | Application sandbox path of the file.|
114
115
116**Return value**
117
118  | Type           | Description        |
119  | ------------- | ---------- |
120  | [Stat](#stat) | File information obtained.|
121
122**Example**
123
124  ```js
125  let stat = fileio.statSync(path);
126  // Example code in Stat
127  ```
128
129
130## fileio.opendir
131
132opendir(path: string): Promise&lt;Dir&gt;
133
134Opens a file directory. This API uses a promise to return the result.
135
136**System capability**: SystemCapability.FileManagement.File.FileIO
137
138**Parameters**
139
140| Name| Type  | Mandatory| Description                          |
141| ------ | ------ | ---- | ------------------------------ |
142| path   | string | Yes  | Application sandbox path of the directory to open.|
143
144**Return value**
145
146  | Type                        | Description      |
147  | -------------------------- | -------- |
148  | Promise&lt;[Dir](#dir)&gt; | Promise used to return the **Dir** object.|
149
150**Example**
151
152  ```js
153  fileio.opendir(path).then(function(dir){
154      console.info("Directory opened:"+ JSON.stringify(dir));
155  }).catch(function(err){
156      console.info("Failed to open the directory. Error:"+ err);
157  });
158  ```
159
160
161## fileio.opendir
162
163opendir(path: string, callback: AsyncCallback&lt;Dir&gt;): void
164
165Opens a file directory. This API uses an asynchronous callback to return the result.
166
167**System capability**: SystemCapability.FileManagement.File.FileIO
168
169**Parameters**
170
171| Name  | Type                            | Mandatory| Description                          |
172| -------- | -------------------------------- | ---- | ------------------------------ |
173| path     | string                           | Yes  | Application sandbox path of the directory to open.|
174| callback | AsyncCallback&lt;[Dir](#dir)&gt; | Yes  | Callback invoked when the directory is open asynchronously.  |
175
176**Example**
177
178  ```js
179  fileio.opendir(path, function (err, dir) {
180      // Example code in Dir struct
181      // Use read/readSync/close.
182  });
183  ```
184
185
186## fileio.opendirSync
187
188opendirSync(path: string): Dir
189
190Synchronously opens a directory.
191
192**System capability**: SystemCapability.FileManagement.File.FileIO
193
194
195**Parameters**
196
197| Name| Type  | Mandatory| Description                          |
198| ------ | ------ | ---- | ------------------------------ |
199| path   | string | Yes  | Application sandbox path of the directory to open.|
200
201**Return value**
202
203  | Type         | Description      |
204  | ----------- | -------- |
205  | [Dir](#dir) | A **Dir** instance corresponding to the directory.|
206
207**Example**
208
209  ```js
210  let dir = fileio.opendirSync(path);
211  // Example code in Dir struct
212  // Use read/readSync/close.
213  ```
214
215
216## fileio.access
217
218access(path: string, mode?: number): Promise&lt;void&gt;
219
220Checks whether the current process can access a file. This API uses a promise to return the result.
221
222**System capability**: SystemCapability.FileManagement.File.FileIO
223
224**Parameters**
225
226| Name| Type  | Mandatory| Description                                                        |
227| ------ | ------ | ---- | ------------------------------------------------------------ |
228| path   | string | Yes  | Application sandbox path of the file.                                  |
229| mode   | number | No  | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (&#124;). The default value is **0**.<br>The options are as follows:<br>-&nbsp;**0**: check whether the file exists.<br>-&nbsp;**1**: check whether the current process has the execute permission on the file.<br>-&nbsp;**2**: check whether the current process has the write permission on the file.<br>-&nbsp;**4**: check whether the current process has the read permission on the file.|
230
231**Return value**
232
233  | Type                 | Description                          |
234  | ------------------- | ---------------------------- |
235  | Promise&lt;void&gt; | Promise that returns no value.|
236
237**Example**
238
239  ```js
240  fileio.access(path).then(function() {
241      console.info("Access successful");
242  }).catch(function(err){
243      console.info("Access failed. Error:"+ err);
244  });
245  ```
246
247
248## fileio.access
249
250access(path: string, mode: number, callback: AsyncCallback&lt;void&gt;): void
251
252Checks whether the current process can access a file. This API uses an asynchronous callback to return the result.
253
254**System capability**: SystemCapability.FileManagement.File.FileIO
255
256**Parameters**
257
258| Name  | Type                     | Mandatory| Description                                                        |
259| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
260| path     | string                    | Yes  | Application sandbox path of the file.                                  |
261| mode     | number                    | No  | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (&#124;). The default value is **0**.<br>The options are as follows:<br>-&nbsp;**0**: check whether the file exists.<br>-&nbsp;**1**: check whether the current process has the execute permission on the file.<br>-&nbsp;**2**: check whether the current process has the write permission on the file.<br>-&nbsp;**4**: check whether the current process has the read permission on the file.|
262| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously checked.                |
263
264**Example**
265
266  ```js
267  fileio.access(path, function (err) {
268      // Do something.
269  });
270  ```
271
272
273## fileio.accessSync
274
275accessSync(path: string, mode?: number): void
276
277Synchronously checks whether the current process can access the specified file.
278
279**System capability**: SystemCapability.FileManagement.File.FileIO
280
281**Parameters**
282
283| Name| Type  | Mandatory| Description                                                        |
284| ------ | ------ | ---- | ------------------------------------------------------------ |
285| path   | string | Yes  | Application sandbox path of the file.                                  |
286| mode   | number | No  | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (&#124;). The default value is **0**.<br>The options are as follows:<br>-&nbsp;**0**: check whether the file exists.<br>-&nbsp;**1**: check whether the current process has the execute permission on the file.<br>-&nbsp;**2**: check whether the current process has the write permission on the file.<br>-&nbsp;**4**: check whether the current process has the read permission on the file.|
287
288**Example**
289
290  ```js
291  try {
292      fileio.accessSync(path);
293  } catch(err) {
294      console.info("accessSync failed. Error:"+ err);
295  }
296  ```
297
298
299## fileio.close<sup>7+</sup>
300
301close(fd: number):Promise&lt;void&gt;
302
303Closes a file. This API uses a promise to return the result.
304
305**System capability**: SystemCapability.FileManagement.File.FileIO
306
307**Parameters**
308
309  | Name | Type    | Mandatory  | Description          |
310  | ---- | ------ | ---- | ------------ |
311  | fd   | number | Yes   | File descriptor of the file to close.|
312
313**Return value**
314
315  | Type                 | Description                          |
316  | ------------------- | ---------------------------- |
317  | Promise&lt;void&gt; | Promise that returns no value.|
318
319**Example**
320
321  ```js
322  let fd = fileio.openSync(path);
323  fileio.close(fd).then(function(){
324      console.info("File closed");
325  }).catch(function(err){
326      console.info("Failed to close the file. Error:"+ err);
327  });
328  ```
329
330
331## fileio.close<sup>7+</sup>
332
333close(fd: number, callback:AsyncCallback&lt;void&gt;): void
334
335Closes a file. This API uses an asynchronous callback to return the result.
336
337**System capability**: SystemCapability.FileManagement.File.FileIO
338
339**Parameters**
340
341  | Name     | Type                       | Mandatory  | Description          |
342  | -------- | ------------------------- | ---- | ------------ |
343  | fd       | number                    | Yes   | File descriptor of the file to close.|
344  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is closed asynchronously.|
345
346**Example**
347
348  ```js
349  let fd = fileio.openSync(path);
350  fileio.close(fd, function (err) {
351      // Do something.
352  });
353  ```
354
355
356## fileio.closeSync
357
358closeSync(fd: number): void
359
360Synchronously closes a file.
361
362**System capability**: SystemCapability.FileManagement.File.FileIO
363
364**Parameters**
365
366  | Name | Type    | Mandatory  | Description          |
367  | ---- | ------ | ---- | ------------ |
368  | fd   | number | Yes   | File descriptor of the file to close.|
369
370**Example**
371
372  ```js
373  let fd = fileio.openSync(path);
374  fileio.closeSync(fd);
375  ```
376
377
378## fileio.copyFile
379
380copyFile(src:string | number, dest:string | number, mode?:number):Promise&lt;void&gt;
381
382Copies a file. This API uses a promise to return the result.
383
384**System capability**: SystemCapability.FileManagement.File.FileIO
385
386**Parameters**
387
388  | Name | Type                        | Mandatory  | Description                                      |
389  | ---- | -------------------------- | ---- | ---------------------------------------- |
390  | src  | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the file to copy.                     |
391  | dest | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the new file.                         |
392  | mode | number                     | No   | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
393
394**Return value**
395
396  | Type                 | Description                          |
397  | ------------------- | ---------------------------- |
398  | Promise&lt;void&gt; | Promise that returns no value.|
399
400**Example**
401
402  ```js
403  let src = path;
404  let dest = src + 'tgt';
405  fileio.copyFile(src, dest).then(function(){
406      console.info("File copied");
407  }).catch(function(err){
408      console.info("Failed to copy the file. Error:"+ err);
409  });
410  ```
411
412
413## fileio.copyFile
414
415copyFile(src: string | number, dest: string | number, mode: number, callback: AsyncCallback&lt;void&gt;): void
416
417Copies a file. This API uses an asynchronous callback to return the result.
418
419**System capability**: SystemCapability.FileManagement.File.FileIO
420
421**Parameters**
422
423  | Name     | Type                        | Mandatory  | Description                                      |
424  | -------- | -------------------------- | ---- | ---------------------------------------- |
425  | src      | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the file to copy.                     |
426  | dest     | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the new file.                         |
427  | mode     | number                     | No   | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
428  | callback | AsyncCallback&lt;void&gt;  | Yes   | Callback invoked when the file is copied asynchronously.                            |
429
430**Example**
431
432  ```js
433  let src = path;
434  let dest = src + 'tgt';
435  fileio.copyFile(src, dest, function (err) {
436      // Do something.
437  });
438  ```
439
440
441## fileio.copyFileSync
442
443copyFileSync(src: string | number, dest: string | number, mode?: number): void
444
445Synchronously copies a file.
446
447**System capability**: SystemCapability.FileManagement.File.FileIO
448
449**Parameters**
450
451  | Name | Type                        | Mandatory  | Description                                      |
452  | ---- | -------------------------- | ---- | ---------------------------------------- |
453  | src  | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the file to copy.                     |
454  | dest | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the new file.                         |
455  | mode | number                     | No   | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
456
457**Example**
458
459  ```js
460  let src = path;
461  let dest = src + 'tgt';
462  fileio.copyFileSync(src, dest);
463  ```
464
465
466## fileio.mkdir
467
468mkdir(path:string, mode?: number): Promise&lt;void&gt;
469
470Creates a directory. This API uses a promise to return the result.
471
472**System capability**: SystemCapability.FileManagement.File.FileIO
473
474**Parameters**
475
476| Name| Type  | Mandatory| Description                                                        |
477| ------ | ------ | ---- | ------------------------------------------------------------ |
478| path   | string | Yes  | Application sandbox path of the directory.                                  |
479| mode   | number | No  | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o775**.<br>-&nbsp;**0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
480
481**Return value**
482
483  | Type                 | Description                          |
484  | ------------------- | ---------------------------- |
485  | Promise&lt;void&gt; | Promise that returns no value.|
486
487**Example**
488
489  ```js
490  fileio.mkdir(path).then(function() {
491      console.info("Directory created");
492  }).catch(function (error){
493      console.info("Failed to create the directory. Error:"+ error);
494  });
495  ```
496
497
498## fileio.mkdir
499
500mkdir(path: string, mode: number, callback: AsyncCallback&lt;void&gt;): void
501
502Creates a directory. This API uses an asynchronous callback to return the result.
503
504**System capability**: SystemCapability.FileManagement.File.FileIO
505
506**Parameters**
507
508| Name  | Type                     | Mandatory| Description                                                        |
509| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
510| path     | string                    | Yes  | Application sandbox path of the directory.                                  |
511| mode     | number                    | No  | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o775**.<br>-&nbsp;**0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
512| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is created asynchronously.                            |
513
514**Example**
515
516  ```js
517  fileio.mkdir(path, function(err) {
518    console.info("Directory created");
519  });
520  ```
521
522
523## fileio.mkdirSync
524
525mkdirSync(path: string, mode?: number): void
526
527Synchronously creates a directory.
528
529**System capability**: SystemCapability.FileManagement.File.FileIO
530
531**Parameters**
532
533| Name| Type  | Mandatory| Description                                                        |
534| ------ | ------ | ---- | ------------------------------------------------------------ |
535| path   | string | Yes  | Application sandbox path of the directory.                                  |
536| mode   | number | No  | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o775**.<br>-&nbsp;**0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
537
538**Example**
539
540  ```js
541  fileio.mkdirSync(path);
542  ```
543
544
545## fileio.open<sup>7+</sup>
546
547open(path: string, flags?: number, mode?: number): Promise&lt;number&gt;
548
549Opens a file. This API uses a promise to return the result.
550
551**System capability**: SystemCapability.FileManagement.File.FileIO
552
553**Parameters**
554
555| Name| Type  | Mandatory| Description                                                        |
556| ------ | ------ | ---- | ------------------------------------------------------------ |
557| path   | string | Yes  | Application sandbox path of the file.                                  |
558| flags  | number | No  | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>-&nbsp;**0o200000**: If **path** does not point to a directory, throw an exception.<br><br>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
559| mode   | number | No  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>-&nbsp;**0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
560
561**Return value**
562
563  | Type                   | Description         |
564  | --------------------- | ----------- |
565  | Promise&lt;number&gt; | Promise used to return the file descriptor of the file opened.|
566
567**Example**
568
569  ```js
570  fileio.open(path, 0o1, 0o0200).then(function(number){
571      console.info("File opened");
572  }).catch(function(err){
573      console.info("Failed to open the file. Error:"+ err);
574  });
575  ```
576
577
578## fileio.open<sup>7+</sup>
579
580open(path: string, flags: number, mode: number, callback: AsyncCallback&lt;number&gt;): void
581
582Opens a file. This API uses an asynchronous callback to return the result.
583
584**System capability**: SystemCapability.FileManagement.File.FileIO
585
586**Parameters**
587
588| Name  | Type                           | Mandatory| Description                                                        |
589| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
590| path     | string                          | Yes  | Application sandbox path of the file.                                  |
591| flags    | number                          | Yes  | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>-&nbsp;**0o200000**: If **path** does not point to a directory, throw an exception.<br><br>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
592| mode     | number                          | Yes  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>-&nbsp;**0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
593| callback | AsyncCallback&nbsp;&lt;void&gt; | Yes  | Callback invoked when the file is open asynchronously.                                    |
594
595**Example**
596
597  ```js
598  fileio.open(path, 0, function(err, fd) {
599      // Do something.
600  });
601  ```
602
603
604## fileio.openSync
605
606openSync(path:string, flags?:number, mode?:number): number
607
608Synchronously opens a file.
609
610**System capability**: SystemCapability.FileManagement.File.FileIO
611
612**Parameters**
613
614| Name| Type  | Mandatory| Description                                                        |
615| ------ | ------ | ---- | ------------------------------------------------------------ |
616| path   | string | Yes  | Application sandbox path of the file.                                  |
617| flags  | number | No  | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>-&nbsp;**0o200000**: If **path** does not point to a directory, throw an exception.<br><br>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
618| mode   | number | No  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>-&nbsp;**0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>-&nbsp;**0o640**: The owner has the read and write permissions, and the user group has the read permission.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.<br>The file permissions on newly created files are affected by umask, which is set as the process starts. Currently, the modification of umask is not open.|
619
620**Return value**
621
622  | Type    | Description         |
623  | ------ | ----------- |
624  | number | File descriptor of the file opened.|
625
626**Example**
627
628  ```js
629  let fd = fileio.openSync(path, 0o102, 0o640);
630  ```
631  ```js
632  let fd = fileio.openSync(path, 0o102, 0o666);
633  fileio.writeSync(fd, 'hello world');
634  let fd1 = fileio.openSync(path, 0o2002);
635  fileio.writeSync(fd1, 'hello world');
636  let num = fileio.readSync(fd1, new ArrayBuffer(4096), {position: 0});
637  console.info("num == " + num);
638  ```
639
640
641## fileio.read
642
643read(fd: number, buffer: ArrayBuffer, options?: {
644    offset?: number;
645    length?: number;
646    position?: number;
647}): Promise&lt;ReadOut&gt;
648
649Reads data from a file. This API uses a promise to return the result.
650
651**System capability**: SystemCapability.FileManagement.File.FileIO
652
653**Parameters**
654
655| Name | Type       | Mandatory| Description                                                        |
656| ------- | ----------- | ---- | ------------------------------------------------------------ |
657| fd      | number      | Yes  | File descriptor of the file to read.                                    |
658| buffer  | ArrayBuffer | Yes  | Buffer used to store the file data read.                          |
659| options | Object      | No  | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size|
660
661**Return value**
662
663  | Type                                | Description    |
664  | ---------------------------------- | ------ |
665  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
666
667**Example**
668
669  ```js
670  let fd = fileio.openSync(path, 0o2);
671  let buf = new ArrayBuffer(4096);
672  fileio.read(fd, buf).then(function(readOut){
673      console.info("Read file data successfully");
674      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
675  }).catch(function(err){
676      console.info("Failed to read file data. Error:"+ err);
677  });
678  ```
679
680
681## fileio.read
682
683read(fd: number, buffer: ArrayBuffer, options: {
684    offset?: number;
685    length?: number;
686    position?: number;
687}, callback: AsyncCallback&lt;ReadOut&gt;): void
688
689Reads data from a file. This API uses an asynchronous callback to return the result.
690
691**System capability**: SystemCapability.FileManagement.File.FileIO
692
693**Parameters**
694
695  | Name     | Type                                      | Mandatory  | Description                                      |
696  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
697  | fd       | number                                   | Yes   | File descriptor of the file to read.                            |
698  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file data read.                       |
699  | options  | Object                                   | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
700  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously.                            |
701
702**Example**
703
704  ```js
705  let fd = fileio.openSync(path, 0o2);
706  let buf = new ArrayBuffer(4096);
707  fileio.read(fd, buf, function (err, readOut) {
708      if (readOut) {
709          console.info("Read file data successfully");
710          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
711      }
712  });
713  ```
714
715
716## fileio.readSync
717
718readSync(fd: number, buffer: ArrayBuffer, options?: {
719    offset?: number;
720    length?: number;
721    position?: number;
722}): number
723
724Synchronously reads data from a file.
725
726**System capability**: SystemCapability.FileManagement.File.FileIO
727
728**Parameters**
729
730  | Name    | Type         | Mandatory  | Description                                      |
731  | ------- | ----------- | ---- | ---------------------------------------- |
732  | fd      | number      | Yes   | File descriptor of the file to read.                            |
733  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file data read.                       |
734  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
735
736**Return value**
737
738  | Type    | Description      |
739  | ------ | -------- |
740  | number | Length of the data read.|
741
742**Example**
743
744  ```js
745  let fd = fileio.openSync(path, 0o2);
746  let buf = new ArrayBuffer(4096);
747  let num = fileio.readSync(fd, buf);
748  ```
749
750
751## fileio.rmdir<sup>7+</sup>
752
753rmdir(path: string): Promise&lt;void&gt;
754
755Deletes a directory. This API uses a promise to return the result.
756
757**System capability**: SystemCapability.FileManagement.File.FileIO
758
759**Parameters**
760
761| Name| Type  | Mandatory| Description                      |
762| ------ | ------ | ---- | -------------------------- |
763| path   | string | Yes  | Application sandbox path of the directory.|
764
765**Return value**
766
767  | Type                 | Description                          |
768  | ------------------- | ---------------------------- |
769  | Promise&lt;void&gt; | Promise that returns no value.|
770
771**Example**
772
773  ```js
774  fileio.rmdir(path).then(function() {
775      console.info("Directory deleted");
776  }).catch(function(err){
777      console.info("Failed to delete the directory. Error:"+ err);
778  });
779  ```
780
781
782## fileio.rmdir<sup>7+</sup>
783
784rmdir(path: string, callback:AsyncCallback&lt;void&gt;): void
785
786Deletes a directory. This API uses an asynchronous callback to return the result.
787
788**System capability**: SystemCapability.FileManagement.File.FileIO
789
790**Parameters**
791
792| Name  | Type                     | Mandatory| Description                      |
793| -------- | ------------------------- | ---- | -------------------------- |
794| path     | string                    | Yes  | Application sandbox path of the directory.|
795| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is deleted asynchronously.  |
796
797**Example**
798
799  ```js
800  fileio.rmdir(path, function(err){
801      // Do something.
802      console.info("Directory deleted");
803  });
804  ```
805
806
807## fileio.rmdirSync<sup>7+</sup>
808
809rmdirSync(path: string): void
810
811Synchronously deletes a directory.
812
813**System capability**: SystemCapability.FileManagement.File.FileIO
814
815**Parameters**
816
817| Name| Type  | Mandatory| Description                      |
818| ------ | ------ | ---- | -------------------------- |
819| path   | string | Yes  | Application sandbox path of the directory.|
820
821**Example**
822
823  ```js
824  fileio.rmdirSync(path);
825  ```
826
827
828## fileio.unlink
829
830unlink(path:string): Promise&lt;void&gt;
831
832Deletes a file. This API uses a promise to return the result.
833
834**System capability**: SystemCapability.FileManagement.File.FileIO
835
836**Parameters**
837
838| Name| Type  | Mandatory| Description                      |
839| ------ | ------ | ---- | -------------------------- |
840| path   | string | Yes  | Application sandbox path of the file.|
841
842**Return value**
843
844  | Type                 | Description                          |
845  | ------------------- | ---------------------------- |
846  | Promise&lt;void&gt; | Promise that returns no value.|
847
848**Example**
849
850  ```js
851  fileio.unlink(path).then(function(){
852      console.info("File deleted");
853  }).catch(function(error){
854      console.info("Failed to delete the file. Error:"+ error);
855  });
856  ```
857
858
859## fileio.unlink
860
861unlink(path:string, callback:AsyncCallback&lt;void&gt;): void
862
863Deletes a file. This API uses an asynchronous callback to return the result.
864
865**System capability**: SystemCapability.FileManagement.File.FileIO
866
867**Parameters**
868
869| Name  | Type                     | Mandatory| Description                      |
870| -------- | ------------------------- | ---- | -------------------------- |
871| path     | string                    | Yes  | Application sandbox path of the file.|
872| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is deleted asynchronously.  |
873
874**Example**
875
876  ```js
877  fileio.unlink(path, function(err) {
878      console.info("File deleted");
879  });
880  ```
881
882
883## fileio.unlinkSync
884
885unlinkSync(path: string): void
886
887Synchronously deletes a file.
888
889**System capability**: SystemCapability.FileManagement.File.FileIO
890
891**Parameters**
892
893| Name| Type  | Mandatory| Description                      |
894| ------ | ------ | ---- | -------------------------- |
895| path   | string | Yes  | Application sandbox path of the file.|
896
897**Example**
898
899  ```js
900  fileio.unlinkSync(path);
901  ```
902
903
904## fileio.write
905
906write(fd: number, buffer: ArrayBuffer | string, options?: {
907    offset?: number;
908    length?: number;
909    position?: number;
910    encoding?: string;
911}): Promise&lt;number&gt;
912
913Writes data into a file. This API uses a promise to return the result.
914
915**System capability**: SystemCapability.FileManagement.File.FileIO
916
917**Parameters**
918
919  | Name    | Type                             | Mandatory  | Description                                      |
920  | ------- | ------------------------------- | ---- | ---------------------------------------- |
921  | fd      | number                          | Yes   | File descriptor of the file to write.                            |
922  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
923  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
924
925**Return value**
926
927  | Type                   | Description      |
928  | --------------------- | -------- |
929  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
930
931**Example**
932
933  ```js
934  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
935  fileio.write(fd, "hello, world").then(function(number){
936       console.info("Data written to the file. Size is:"+ number);
937  }).catch(function(err){
938      console.info("Failed to write data to the file. Error:"+ err);
939  });
940  ```
941
942
943## fileio.write
944
945write(fd: number, buffer: ArrayBuffer | string, options: {
946    offset?: number;
947    length?: number;
948    position?: number;
949    encoding?: string;
950}, callback: AsyncCallback&lt;number&gt;): void
951
952Writes data into a file. This API uses an asynchronous callback to return the result.
953
954**System capability**: SystemCapability.FileManagement.File.FileIO
955
956**Parameters**
957
958  | Name     | Type                             | Mandatory  | Description                                      |
959  | -------- | ------------------------------- | ---- | ---------------------------------------- |
960  | fd       | number                          | Yes   | File descriptor of the file to write.                            |
961  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
962  | options  | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
963  | callback | AsyncCallback&lt;number&gt;     | Yes   | Callback invoked when the data is written asynchronously.                      |
964
965**Example**
966
967  ```js
968  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
969  fileio.write(fd, "hello, world", function (err, bytesWritten) {
970      if (bytesWritten) {
971         console.info("Data written to the file. Size is:"+ bytesWritten);
972      }
973  });
974  ```
975
976
977## fileio.writeSync
978
979writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
980    offset?: number;
981    length?: number;
982    position?: number;
983    encoding?: string;
984}): number
985
986Synchronously writes data into a file.
987
988**System capability**: SystemCapability.FileManagement.File.FileIO
989
990**Parameters**
991
992  | Name    | Type                             | Mandatory  | Description                                      |
993  | ------- | ------------------------------- | ---- | ---------------------------------------- |
994  | fd      | number                          | Yes   | File descriptor of the file to write.                            |
995  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
996  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
997
998**Return value**
999
1000  | Type    | Description      |
1001  | ------ | -------- |
1002  | number | Length of the data written in the file.|
1003
1004**Example**
1005
1006  ```js
1007  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
1008  let num = fileio.writeSync(fd, "hello, world");
1009  ```
1010
1011
1012## fileio.hash
1013
1014hash(path: string, algorithm: string): Promise&lt;string&gt;
1015
1016Calculates the hash value of a file. This API uses a promise to return the result.
1017
1018**System capability**: SystemCapability.FileManagement.File.FileIO
1019
1020**Parameters**
1021
1022| Name   | Type  | Mandatory| Description                                                        |
1023| --------- | ------ | ---- | ------------------------------------------------------------ |
1024| path      | string | Yes  | Application sandbox path of the file.                            |
1025| algorithm | string | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
1026
1027**Return value**
1028
1029  | Type                   | Description                        |
1030  | --------------------- | -------------------------- |
1031  | Promise&lt;string&gt; | Promise used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
1032
1033**Example**
1034
1035  ```js
1036  fileio.hash(path, "sha256").then(function(str){
1037      console.info("Calculated file hash:"+ str);
1038  }).catch(function(err){
1039      console.info("Failed to calculate the file hash. Error:"+ err);
1040  });
1041  ```
1042
1043
1044## fileio.hash
1045
1046hash(path: string, algorithm: string, callback: AsyncCallback&lt;string&gt;): void
1047
1048Calculates the hash value of a file. This API uses an asynchronous callback to return the result.
1049
1050**System capability**: SystemCapability.FileManagement.File.FileIO
1051
1052**Parameters**
1053
1054| Name   | Type                       | Mandatory| Description                                                        |
1055| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
1056| path      | string                      | Yes  | Application sandbox path of the file.                            |
1057| algorithm | string                      | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
1058| callback  | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
1059
1060**Example**
1061
1062  ```js
1063  fileio.hash(path, "sha256", function(err, hashStr) {
1064      if (hashStr) {
1065          console.info("Calculated file hash:"+ hashStr);
1066      }
1067  });
1068  ```
1069
1070
1071## fileio.chmod<sup>7+</sup>
1072
1073chmod(path: string, mode: number):Promise&lt;void&gt;
1074
1075Changes file permissions. This API uses a promise to return the result.
1076
1077**System capability**: SystemCapability.FileManagement.File.FileIO
1078
1079**Parameters**
1080
1081| Name| Type  | Mandatory| Description                                                        |
1082| ------ | ------ | ---- | ------------------------------------------------------------ |
1083| path   | string | Yes  | Application sandbox path of the file.                              |
1084| mode   | number | Yes  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
1085
1086**Return value**
1087
1088  | Type                 | Description                          |
1089  | ------------------- | ---------------------------- |
1090  | Promise&lt;void&gt; | Promise that returns no value.|
1091
1092**Example**
1093
1094  ```js
1095  fileio.chmod(path, 0o700).then(function() {
1096      console.info("File permissions changed");
1097  }).catch(function(err){
1098      console.info("Failed to change file permissions. Error:"+ err);
1099  });
1100  ```
1101
1102
1103## fileio.chmod<sup>7+</sup>
1104
1105chmod(path: string, mode: number, callback: AsyncCallback&lt;void&gt;): void
1106
1107Changes file permissions. This API uses an asynchronous callback to return the result.
1108
1109**System capability**: SystemCapability.FileManagement.File.FileIO
1110
1111**Parameters**
1112
1113| Name  | Type                     | Mandatory| Description                                                        |
1114| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
1115| path     | string                    | Yes  | Application sandbox path of the file.                              |
1116| mode     | number                    | Yes  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
1117| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file permissions are changed asynchronously.                                |
1118
1119**Example**
1120
1121  ```js
1122  fileio.chmod(path, 0o700, function (err) {
1123      // Do something.
1124  });
1125  ```
1126
1127
1128## fileio.chmodSync<sup>7+</sup>
1129
1130chmodSync(path: string, mode: number): void
1131
1132Synchronously changes file permissions.
1133
1134**System capability**: SystemCapability.FileManagement.File.FileIO
1135
1136**Parameters**
1137
1138| Name| Type  | Mandatory| Description                                                        |
1139| ------ | ------ | ---- | ------------------------------------------------------------ |
1140| path   | string | Yes  | Application sandbox path of the file.                              |
1141| mode   | number | Yes  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
1142
1143**Example**
1144
1145  ```js
1146  fileio.chmodSync(path, 0o700);
1147  ```
1148
1149
1150## fileio.fstat<sup>7+</sup>
1151
1152fstat(fd: number): Promise&lt;Stat&gt;
1153
1154Obtains file information based on the file descriptor. This API uses a promise to return the result.
1155
1156**System capability**: SystemCapability.FileManagement.File.FileIO
1157
1158**Parameters**
1159
1160  | Name | Type    | Mandatory  | Description          |
1161  | ---- | ------ | ---- | ------------ |
1162  | fd   | number | Yes   | File descriptor of the target file.|
1163
1164**Return value**
1165
1166  | Type                          | Description        |
1167  | ---------------------------- | ---------- |
1168  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information.|
1169
1170**Example**
1171
1172  ```js
1173  let fd = fileio.openSync(path);
1174  fileio.fstat(fd).then(function(stat){
1175      console.info("Obtained file info:"+ JSON.stringify(stat));
1176  }).catch(function(err){
1177      console.info("Failed to obtain file info. Error:"+ err);
1178  });
1179  ```
1180
1181
1182## fileio.fstat<sup>7+</sup>
1183
1184fstat(fd: number, callback: AsyncCallback&lt;Stat&gt;): void
1185
1186Obtains file information based on the file descriptor. This API uses an asynchronous callback to return the result.
1187
1188**System capability**: SystemCapability.FileManagement.File.FileIO
1189
1190**Parameters**
1191
1192  | Name     | Type                                | Mandatory  | Description              |
1193  | -------- | ---------------------------------- | ---- | ---------------- |
1194  | fd       | number                             | Yes   | File descriptor of the target file.    |
1195  | callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes   | Callback invoked to return the file information obtained.|
1196
1197**Example**
1198
1199  ```js
1200  let fd = fileio.openSync(path);
1201  fileio.fstat(fd, function (err) {
1202      // Do something.
1203  });
1204  ```
1205
1206
1207## fileio.fstatSync<sup>7+</sup>
1208
1209fstatSync(fd: number): Stat
1210
1211Synchronously obtains file information based on the file descriptor.
1212
1213**System capability**: SystemCapability.FileManagement.File.FileIO
1214
1215**Parameters**
1216
1217  | Name | Type    | Mandatory  | Description          |
1218  | ---- | ------ | ---- | ------------ |
1219  | fd   | number | Yes   | File descriptor of the target file.|
1220
1221**Return value**
1222
1223  | Type           | Description        |
1224  | ------------- | ---------- |
1225  | [Stat](#stat) | File information obtained.|
1226
1227**Example**
1228
1229  ```js
1230  let fd = fileio.openSync(path);
1231  let stat = fileio.fstatSync(fd);
1232  ```
1233
1234
1235## fileio.ftruncate<sup>7+</sup>
1236
1237ftruncate(fd: number, len?: number): Promise&lt;void&gt;
1238
1239Truncates a file based on the file descriptor. This API uses a promise to return the result.
1240
1241**System capability**: SystemCapability.FileManagement.File.FileIO
1242
1243**Parameters**
1244
1245  | Name | Type    | Mandatory  | Description              |
1246  | ---- | ------ | ---- | ---------------- |
1247  | fd   | number | Yes   | File descriptor of the file to truncate.    |
1248  | len  | number | No   | File length, in bytes, after truncation.|
1249
1250**Return value**
1251
1252  | Type                 | Description                          |
1253  | ------------------- | ---------------------------- |
1254  | Promise&lt;void&gt; | Promise that returns no value.|
1255
1256**Example**
1257
1258  ```js
1259  let fd = fileio.openSync(path);
1260  fileio.ftruncate(fd, 5).then(function(err) {
1261      console.info("File truncated");
1262  }).catch(function(err){
1263      console.info("Failed to truncate the file. Error:"+ err);
1264  });
1265  ```
1266
1267
1268## fileio.ftruncate<sup>7+</sup>
1269
1270ftruncate(fd: number, len: number, callback:AsyncCallback&lt;void&gt;): void
1271
1272Truncates a file based on the file descriptor. This API uses an asynchronous callback to return the result.
1273
1274**System capability**: SystemCapability.FileManagement.File.FileIO
1275
1276**Parameters**
1277
1278  | Name     | Type                       | Mandatory  | Description              |
1279  | -------- | ------------------------- | ---- | ---------------- |
1280  | fd       | number                    | Yes   | File descriptor of the file to truncate.    |
1281  | len      | number                    | Yes   | File length, in bytes, after truncation.|
1282  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback that returns no value.|
1283
1284**Example**
1285
1286  ```js
1287  let fd = fileio.openSync(path);
1288  let len = 5;
1289  fileio.ftruncate(fd, 5, function(err){
1290      // Do something.
1291  });
1292  ```
1293
1294
1295## fileio.ftruncateSync<sup>7+</sup>
1296
1297ftruncateSync(fd: number, len?: number): void
1298
1299Synchronously truncates a file based on the file descriptor.
1300
1301**System capability**: SystemCapability.FileManagement.File.FileIO
1302
1303**Parameters**
1304
1305  | Name | Type    | Mandatory  | Description              |
1306  | ---- | ------ | ---- | ---------------- |
1307  | fd   | number | Yes   | File descriptor of the file to truncate.    |
1308  | len  | number | No   | File length, in bytes, after truncation.|
1309
1310**Example**
1311
1312  ```js
1313  let fd = fileio.openSync(path);
1314  let len = 5;
1315  fileio.ftruncateSync(fd, len);
1316  ```
1317
1318
1319## fileio.truncate<sup>7+</sup>
1320
1321truncate(path: string, len?: number): Promise&lt;void&gt;
1322
1323Truncates a file based on the file path. This API uses a promise to return the result.
1324
1325**System capability**: SystemCapability.FileManagement.File.FileIO
1326
1327**Parameters**
1328
1329| Name| Type  | Mandatory| Description                            |
1330| ------ | ------ | ---- | -------------------------------- |
1331| path   | string | Yes  | Application sandbox path of the file to truncate.|
1332| len    | number | No  | File length, in bytes, after truncation.|
1333
1334**Return value**
1335
1336  | Type                 | Description                          |
1337  | ------------------- | ---------------------------- |
1338  | Promise&lt;void&gt; | Promise that returns no value.|
1339
1340**Example**
1341
1342  ```js
1343  let len = 5;
1344  fileio.truncate(path, len).then(function(){
1345      console.info("File truncated");
1346  }).catch(function(err){
1347      console.info("Failed to truncate the file. Error:"+ err);
1348  });
1349  ```
1350
1351
1352## fileio.truncate<sup>7+</sup>
1353
1354truncate(path: string, len: number, callback:AsyncCallback&lt;void&gt;): void
1355
1356Truncates a file based on the file path. This API uses an asynchronous callback to return the result.
1357
1358**System capability**: SystemCapability.FileManagement.File.FileIO
1359
1360**Parameters**
1361
1362| Name  | Type                     | Mandatory| Description                            |
1363| -------- | ------------------------- | ---- | -------------------------------- |
1364| path     | string                    | Yes  | Application sandbox path of the file to truncate.|
1365| len      | number                    | Yes  | File length, in bytes, after truncation.|
1366| callback | AsyncCallback&lt;void&gt; | Yes  | Callback that returns no value.|
1367
1368**Example**
1369
1370  ```js
1371  let len = 5;
1372  fileio.truncate(path, len, function(err){
1373      // Do something.
1374  });
1375  ```
1376
1377
1378## fileio.truncateSync<sup>7+</sup>
1379
1380truncateSync(path: string, len?: number): void
1381
1382Synchronously truncates a file based on the file path.
1383
1384**System capability**: SystemCapability.FileManagement.File.FileIO
1385
1386**Parameters**
1387
1388| Name| Type  | Mandatory| Description                            |
1389| ------ | ------ | ---- | -------------------------------- |
1390| path   | string | Yes  | Application sandbox path of the file to truncate.|
1391| len    | number | No  | File length, in bytes, after truncation.|
1392
1393**Example**
1394
1395  ```js
1396  let len = 5;
1397  fileio.truncateSync(path, len);
1398  ```
1399
1400
1401## fileio.readText<sup>7+</sup>
1402
1403readText(filePath: string, options?: {
1404    position?: number;
1405    length?: number;
1406    encoding?: string;
1407}): Promise&lt;string&gt;
1408
1409Reads the text content of a file. This API uses a promise to return the result.
1410
1411**System capability**: SystemCapability.FileManagement.File.FileIO
1412
1413**Parameters**
1414
1415| Name  | Type  | Mandatory| Description                                                        |
1416| -------- | ------ | ---- | ------------------------------------------------------------ |
1417| filePath | string | Yes  | Application sandbox path of the file to read.                                  |
1418| options  | Object | No  | The options are as follows:<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**encoding** (string): format of the data (string) to be encoded. The default value is **utf-8**, which is the only value supported.|
1419
1420**Return value**
1421
1422  | Type                   | Description        |
1423  | --------------------- | ---------- |
1424  | Promise&lt;string&gt; | Promise used to return the content read.|
1425
1426**Example**
1427
1428  ```js
1429  fileio.readText(path).then(function(str) {
1430      console.info("Read text successfully:"+ str);
1431  }).catch(function(err){
1432      console.info("Failed to read the text. Error:"+ err);
1433  });
1434  ```
1435
1436
1437## fileio.readText<sup>7+</sup>
1438
1439readText(filePath: string, options: {
1440    position?: number;
1441    length?: number;
1442    encoding?: string;
1443}, callback: AsyncCallback&lt;string&gt;): void
1444
1445Reads the text content of a file. This API uses an asynchronous callback to return the result.
1446
1447**System capability**: SystemCapability.FileManagement.File.FileIO
1448
1449**Parameters**
1450
1451| Name  | Type                       | Mandatory| Description                                                        |
1452| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
1453| filePath | string                      | Yes  | Application sandbox path of the file to read.                                  |
1454| options  | Object                      | Yes  | The options are as follows:<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- &nbsp;**encoding**: format of the string to be encoded. The default value is &nbsp;**utf-8**, which is the only value supported.|
1455| callback | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the content read.                        |
1456
1457**Example**
1458
1459  ```js
1460  fileio.readText(path, { position: 1, encoding: 'UTF-8' }, function(err, str){
1461      // Do something.
1462  });
1463  ```
1464
1465
1466## fileio.readTextSync<sup>7+</sup>
1467
1468readTextSync(filePath: string, options?: {
1469    position?: number;
1470    length?: number;
1471    encoding?: string;
1472}): string
1473
1474Synchronously reads the text of a file.
1475
1476**System capability**: SystemCapability.FileManagement.File.FileIO
1477
1478**Parameters**
1479
1480| Name  | Type  | Mandatory| Description                                                        |
1481| -------- | ------ | ---- | ------------------------------------------------------------ |
1482| filePath | string | Yes  | Application sandbox path of the file to read.                                  |
1483| options  | Object | No  | The options are as follows:<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**encoding** (string): format of the data (string) to be encoded. The default value is **utf-8**, which is the only value supported.|
1484
1485**Return value**
1486
1487  | Type  | Description                |
1488  | ------ | -------------------- |
1489  | string | Promise used to return the content of the file read.|
1490
1491**Example**
1492
1493  ```js
1494  let str = fileio.readTextSync(path, {position: 1, length: 3});
1495  ```
1496
1497
1498## fileio.lstat<sup>7+</sup>
1499
1500lstat(path: string): Promise&lt;Stat&gt;
1501
1502Obtains link information. This API uses a promise to return the result.
1503
1504**System capability**: SystemCapability.FileManagement.File.FileIO
1505
1506**Parameters**
1507
1508| Name| Type  | Mandatory| Description                                  |
1509| ------ | ------ | ---- | -------------------------------------- |
1510| path   | string | Yes  | Application sandbox path of the target file.|
1511
1512**Return value**
1513
1514  | Type                          | Description        |
1515  | ---------------------------- | ---------- |
1516  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the link information obtained. For details, see [Stat](#stat).|
1517
1518**Example**
1519
1520  ```js
1521  fileio.lstat(path).then(function(stat){
1522      console.info("Got link info:"+ JSON.stringify(stat));
1523  }).catch(function(err){
1524      console.info("Failed to obtain link info. Error:"+ err);
1525  });
1526  ```
1527
1528
1529## fileio.lstat<sup>7+</sup>
1530
1531lstat(path:string, callback:AsyncCallback&lt;Stat&gt;): void
1532
1533Obtains link information. This API uses an asynchronous callback to return the result.
1534
1535**System capability**: SystemCapability.FileManagement.File.FileIO
1536
1537**Parameters**
1538
1539| Name  | Type                              | Mandatory| Description                                  |
1540| -------- | ---------------------------------- | ---- | -------------------------------------- |
1541| path     | string                             | Yes  | Application sandbox path of the target file.|
1542| callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes  | Callback used to return the link information obtained.      |
1543
1544**Example**
1545
1546  ```js
1547  fileio.lstat(path, function (err, stat) {
1548      // Do something.
1549  });
1550  ```
1551
1552
1553## fileio.lstatSync<sup>7+</sup>
1554
1555lstatSync(path:string): Stat
1556
1557Synchronously obtains the link information.
1558
1559**System capability**: SystemCapability.FileManagement.File.FileIO
1560
1561**Parameters**
1562
1563| Name| Type  | Mandatory| Description                                  |
1564| ------ | ------ | ---- | -------------------------------------- |
1565| path   | string | Yes  | Application sandbox path of the target file.|
1566
1567**Return value**
1568
1569  | Type           | Description        |
1570  | ------------- | ---------- |
1571  | [Stat](#stat) | Link information obtained.|
1572
1573**Example**
1574
1575  ```js
1576  let stat = fileio.lstatSync(path);
1577  ```
1578
1579
1580## fileio.rename<sup>7+</sup>
1581
1582rename(oldPath: string, newPath: string): Promise&lt;void&gt;
1583
1584Renames a file. This API uses a promise to return the result.
1585
1586**System capability**: SystemCapability.FileManagement.File.FileIO
1587
1588**Parameters**
1589
1590| Name | Type  | Mandatory| Description                        |
1591| ------- | ------ | ---- | ---------------------------- |
1592| oldPath | string | Yes  | Application sandbox path of the file to rename.|
1593| newPath | String | Yes  | Application sandbox path of the file renamed.  |
1594
1595**Return value**
1596
1597  | Type                 | Description                          |
1598  | ------------------- | ---------------------------- |
1599  | Promise&lt;void&gt; | Promise that returns no value.|
1600
1601**Example**
1602
1603  ```js
1604  let oldPath = path;
1605  let newPath = oldPath + '123';
1606  fileio.rename(oldPath, newPath).then(function() {
1607      console.info("File renamed");
1608  }).catch(function(err){
1609      console.info("Failed to rename the file. Error:"+ err);
1610  });
1611  ```
1612
1613
1614## fileio.rename<sup>7+</sup>
1615
1616rename(oldPath: string, newPath: string, callback: AsyncCallback&lt;void&gt;): void
1617
1618Renames a file. This API uses an asynchronous callback to return the result.
1619
1620**System capability**: SystemCapability.FileManagement.File.FileIO
1621
1622**Parameters**
1623
1624| Name  | Type                     | Mandatory| Description                        |
1625| -------- | ------------------------- | ---- | ---------------------------- |
1626| oldPath  | string                    | Yes  | Application sandbox path of the file to rename.|
1627| newPath  | String                    | Yes  | Application sandbox path of the file renamed.  |
1628| Callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously renamed.  |
1629
1630**Example**
1631
1632  ```js
1633  let oldPath = path;
1634  let newPath = oldPath + '123';
1635  fileio.rename(oldPath, newPath, function(err){
1636  });
1637  ```
1638
1639
1640## fileio.renameSync<sup>7+</sup>
1641
1642renameSync(oldPath: string, newPath: string): void
1643
1644Synchronously renames a file.
1645
1646**System capability**: SystemCapability.FileManagement.File.FileIO
1647
1648**Parameters**
1649
1650| Name | Type  | Mandatory| Description                        |
1651| ------- | ------ | ---- | ---------------------------- |
1652| oldPath | string | Yes  | Application sandbox path of the file to rename.|
1653| newPath | String | Yes  | Application sandbox path of the file renamed.  |
1654
1655**Example**
1656
1657  ```js
1658  let oldPath = path;
1659  let newPath = oldPath + '123';
1660  fileio.renameSync(oldPath, newPath);
1661  ```
1662
1663
1664## fileio.fsync<sup>7+</sup>
1665
1666fsync(fd: number): Promise&lt;void&gt;
1667
1668Flushes data of a file to disk. This API uses a promise to return the result.
1669
1670**System capability**: SystemCapability.FileManagement.File.FileIO
1671
1672**Parameters**
1673
1674  | Name | Type    | Mandatory  | Description          |
1675  | ---- | ------ | ---- | ------------ |
1676  | fd   | number | Yes   | File descriptor of the file to flush.|
1677
1678**Return value**
1679
1680  | Type                 | Description                          |
1681  | ------------------- | ---------------------------- |
1682  | Promise&lt;void&gt; | Promise that returns no value.|
1683
1684**Example**
1685
1686  ```js
1687  let fd = fileio.openSync(path);
1688  fileio.fsync(fd).then(function(){
1689      console.info("Data flushed");
1690  }).catch(function(err){
1691      console.info("Failed to flush data. Error:"+ err);
1692  });
1693  ```
1694
1695
1696## fileio.fsync<sup>7+</sup>
1697
1698fsync(fd: number, callback: AsyncCallback&lt;void&gt;): void
1699
1700Flushes data of a file to disk. This API uses an asynchronous callback to return the result.
1701
1702**System capability**: SystemCapability.FileManagement.File.FileIO
1703
1704**Parameters**
1705
1706  | Name     | Type                       | Mandatory  | Description             |
1707  | -------- | ------------------------- | ---- | --------------- |
1708  | fd       | number                    | Yes   | File descriptor of the file to flush.   |
1709  | Callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is synchronized in asynchronous mode.|
1710
1711**Example**
1712
1713  ```js
1714  let fd = fileio.openSync(path);
1715  fileio.fsync(fd, function(err){
1716      // Do something.
1717  });
1718  ```
1719
1720
1721## fileio.fsyncSync<sup>7+</sup>
1722
1723fsyncSync(fd: number): void
1724
1725Flushes data of a file to disk in synchronous mode.
1726
1727**System capability**: SystemCapability.FileManagement.File.FileIO
1728
1729**Parameters**
1730
1731  | Name | Type    | Mandatory  | Description          |
1732  | ---- | ------ | ---- | ------------ |
1733  | fd   | number | Yes   | File descriptor of the file to flush.|
1734
1735**Example**
1736
1737  ```js
1738  let fd = fileio.openSync(path);
1739  fileio.fsyncSync(fd);
1740  ```
1741
1742
1743## fileio.fdatasync<sup>7+</sup>
1744
1745fdatasync(fd: number): Promise&lt;void&gt;
1746
1747Flushes data of a file to disk. This API uses a promise to return the result. **fdatasync()** is similar to **fsync()**, but does not flush modified metadata unless that metadata is needed.
1748
1749**System capability**: SystemCapability.FileManagement.File.FileIO
1750
1751**Parameters**
1752
1753  | Name | Type    | Mandatory  | Description          |
1754  | ---- | ------ | ---- | ------------ |
1755  | fd   | number | Yes   | File descriptor of the file to flush.|
1756
1757**Return value**
1758
1759  | Type                 | Description                          |
1760  | ------------------- | ---------------------------- |
1761  | Promise&lt;void&gt; | Promise that returns no value.|
1762
1763**Example**
1764
1765  ```js
1766  let fd = fileio.openSync(path);
1767  fileio.fdatasync(fd).then(function(err) {
1768      console.info("Data flushed");
1769  }).catch(function(err){
1770      console.info("Failed to flush data. Error:"+ err);
1771  });
1772  ```
1773
1774
1775## fileio.fdatasync<sup>7+</sup>
1776
1777fdatasync(fd: number, callback:AsyncCallback&lt;void&gt;): void
1778
1779Flushes data of a file to disk. This API uses an asynchronous callback to return the result.
1780
1781**System capability**: SystemCapability.FileManagement.File.FileIO
1782
1783**Parameters**
1784
1785  | Name     | Type                             | Mandatory  | Description               |
1786  | -------- | ------------------------------- | ---- | ----------------- |
1787  | fd       | number                          | Yes   | File descriptor of the file to synchronize.     |
1788  | callback | AsyncCallback&nbsp;&lt;void&gt; | Yes   | Callback invoked when the file data is synchronized in asynchronous mode.|
1789
1790**Example**
1791
1792  ```js
1793  let fd = fileio.openSync(path);
1794  fileio.fdatasync (fd, function (err) {
1795      // Do something.
1796  });
1797  ```
1798
1799
1800## fileio.fdatasyncSync<sup>7+</sup>
1801
1802fdatasyncSync(fd: number): void
1803
1804Synchronizes data in a file in synchronous mode.
1805
1806**System capability**: SystemCapability.FileManagement.File.FileIO
1807
1808**Parameters**
1809
1810  | Name | Type    | Mandatory  | Description          |
1811  | ---- | ------ | ---- | ------------ |
1812  | fd   | number | Yes   | File descriptor of the file to flush.|
1813
1814**Example**
1815
1816  ```js
1817  let fd = fileio.openSync(path);
1818  let stat = fileio.fdatasyncSync(fd);
1819  ```
1820
1821
1822## fileio.symlink<sup>7+</sup>
1823
1824symlink(target: string, srcPath: string): Promise&lt;void&gt;
1825
1826Creates a symbolic link based on the file path. This API uses a promise to return the result.
1827
1828**System capability**: SystemCapability.FileManagement.File.FileIO
1829
1830**Parameters**
1831
1832| Name | Type  | Mandatory| Description                        |
1833| ------- | ------ | ---- | ---------------------------- |
1834| target  | string | Yes  | Application sandbox path of the target file.    |
1835| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
1836
1837**Return value**
1838
1839  | Type                 | Description                          |
1840  | ------------------- | ---------------------------- |
1841  | Promise&lt;void&gt; | Promise that returns no value.|
1842
1843**Example**
1844
1845  ```js
1846  let target = path;
1847  let srcPath = target + 'aaa';
1848  fileio.symlink(target, srcPath).then(function() {
1849      console.info("Symbolic link created");
1850  }).catch(function(err){
1851      console.info("Failed to create the symbolic link. Error:"+ err);
1852  });
1853  ```
1854
1855
1856## fileio.symlink<sup>7+</sup>
1857
1858symlink(target: string, srcPath: string, callback: AsyncCallback&lt;void&gt;): void
1859
1860Creates a symbolic link based on the file path. This API uses an asynchronous callback to return the result.
1861
1862**System capability**: SystemCapability.FileManagement.File.FileIO
1863
1864**Parameters**
1865
1866| Name  | Type                     | Mandatory| Description                            |
1867| -------- | ------------------------- | ---- | -------------------------------- |
1868| target   | string                    | Yes  | Application sandbox path of the target file.        |
1869| srcPath  | string                    | Yes  | Application sandbox path of the symbolic link file.    |
1870| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the symbolic link is created asynchronously.|
1871
1872**Example**
1873
1874  ```js
1875  let target = path;
1876  let srcPath = target + 'aaa';
1877  fileio.symlink(target, srcPath, function (err) {
1878      // Do something.
1879  });
1880  ```
1881
1882
1883## fileio.symlinkSync<sup>7+</sup>
1884
1885symlinkSync(target: string, srcPath: string): void
1886
1887Synchronously creates a symbolic link based on a specified path.
1888
1889**System capability**: SystemCapability.FileManagement.File.FileIO
1890
1891**Parameters**
1892
1893| Name | Type  | Mandatory| Description                        |
1894| ------- | ------ | ---- | ---------------------------- |
1895| target  | string | Yes  | Application sandbox path of the target file.    |
1896| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
1897
1898**Example**
1899
1900  ```js
1901  let target = path;
1902  let srcPath = target + 'aaa';
1903  fileio.symlinkSync(target, srcPath);
1904  ```
1905
1906
1907## fileio.chown<sup>7+</sup>
1908
1909chown(path: string, uid: number, gid: number): Promise&lt;void&gt;
1910
1911Changes the file owner based on the file path. This API uses a promise to return the result.
1912
1913**System capability**: SystemCapability.FileManagement.File.FileIO
1914
1915**Parameters**
1916
1917| Name| Type  | Mandatory| Description                      |
1918| ------ | ------ | ---- | -------------------------- |
1919| path   | string | Yes  | Application sandbox path of the file.|
1920| uid    | number | Yes  | New user ID (UID).       |
1921| gid    | number | Yes  | New group ID (GID).      |
1922
1923**Return value**
1924
1925  | Type                 | Description                          |
1926  | ------------------- | ---------------------------- |
1927  | Promise&lt;void&gt; | Promise that returns no value.|
1928
1929**Example**
1930
1931  ```js
1932  let stat = fileio.statSync(path);
1933  fileio.chown(path, stat.uid, stat.gid).then(function(){
1934      console.info("File owner changed");
1935  }).catch(function(err){
1936      console.info("Failed to change the file owner. Error:"+ err);
1937  });
1938  ```
1939
1940
1941## fileio.chown<sup>7+</sup>
1942
1943chown(path: string, uid: number, gid: number, callback: AsyncCallback&lt;void&gt;): void
1944
1945Changes the file owner based on the file path. This API uses an asynchronous callback to return the result.
1946
1947**System capability**: SystemCapability.FileManagement.File.FileIO
1948
1949**Parameters**
1950
1951| Name  | Type                     | Mandatory| Description                          |
1952| -------- | ------------------------- | ---- | ------------------------------ |
1953| path     | string                    | Yes  | Application sandbox path of the file.    |
1954| uid      | number                    | Yes  | New UID.                     |
1955| gid      | number                    | Yes  | New GID.                     |
1956| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file owner is changed asynchronously.|
1957
1958**Example**
1959
1960  ```js
1961  let stat = fileio.statSync(path)
1962  fileio.chown(path, stat.uid, stat.gid, function (err){
1963      // Do something.
1964  });
1965  ```
1966
1967
1968## fileio.chownSync<sup>7+</sup>
1969
1970chownSync(path: string, uid: number, gid: number): void
1971
1972Synchronously changes the file owner based on its path.
1973
1974**System capability**: SystemCapability.FileManagement.File.FileIO
1975
1976**Parameters**
1977
1978| Name| Type  | Mandatory| Description                      |
1979| ------ | ------ | ---- | -------------------------- |
1980| path   | string | Yes  | Application sandbox path of the file.|
1981| uid    | number | Yes  | New UID.                 |
1982| gid    | number | Yes  | New GID.                 |
1983
1984**Example**
1985
1986  ```js
1987  let stat = fileio.statSync(path)
1988  fileio.chownSync(path, stat.uid, stat.gid);
1989  ```
1990
1991
1992## fileio.mkdtemp<sup>7+</sup>
1993
1994mkdtemp(prefix: string): Promise&lt;string&gt;
1995
1996Creates a temporary directory. This API uses a promise to return the result.
1997
1998**System capability**: SystemCapability.FileManagement.File.FileIO
1999
2000**Parameters**
2001
2002  | Name   | Type    | Mandatory  | Description                         |
2003  | ------ | ------ | ---- | --------------------------- |
2004  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
2005
2006**Return value**
2007
2008  | Type                  | Description        |
2009  | --------------------- | ---------- |
2010  | Promise&lt;string&gt; | Promise used to return the unique directory generated.|
2011
2012**Example**
2013
2014  ```js
2015  fileio.mkdtemp(path + "XXXX").then(function(path){
2016      console.info("Temporary directory created:"+ path);
2017  }).catch(function(err){
2018      console.info("Failed to create the temporary directory. Error:"+ err);
2019  });
2020  ```
2021
2022
2023## fileio.mkdtemp<sup>7+</sup>
2024
2025mkdtemp(prefix: string, callback: AsyncCallback&lt;string&gt;): void
2026
2027Creates a temporary directory. This API uses an asynchronous callback to return the result.
2028
2029**System capability**: SystemCapability.FileManagement.File.FileIO
2030
2031**Parameters**
2032
2033  | Name     | Type                         | Mandatory  | Description                         |
2034  | -------- | --------------------------- | ---- | --------------------------- |
2035  | prefix   | string                      | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
2036  | callback | AsyncCallback&lt;string&gt; | Yes   | Callback invoked when a temporary directory is created asynchronously.             |
2037
2038**Example**
2039
2040  ```js
2041  fileio.mkdtemp(path + "XXXX", function (err, res) {
2042      // Do something.
2043  });
2044  ```
2045
2046
2047## fileio.mkdtempSync<sup>7+</sup>
2048
2049mkdtempSync(prefix: string): string
2050
2051Synchronously creates a temporary directory.
2052
2053**System capability**: SystemCapability.FileManagement.File.FileIO
2054
2055**Parameters**
2056
2057  | Name   | Type    | Mandatory  | Description                         |
2058  | ------ | ------ | ---- | --------------------------- |
2059  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
2060
2061**Return value**
2062
2063  | Type   | Description        |
2064  | ------ | ---------- |
2065  | string | Unique path generated.|
2066
2067**Example**
2068
2069  ```js
2070  let res = fileio.mkdtempSync(path + "XXXX");
2071  ```
2072
2073
2074## fileio.fchmod<sup>7+</sup>
2075
2076fchmod(fd: number, mode: number): Promise&lt;void&gt;
2077
2078Changes file permissions based on the file descriptor. This API uses a promise to return the result.
2079
2080**System capability**: SystemCapability.FileManagement.File.FileIO
2081
2082**Parameters**
2083
2084  | Name | Type    | Mandatory  | Description                                      |
2085  | ---- | ------ | ---- | ---------------------------------------- |
2086  | fd   | number | Yes   | File descriptor of the target file.                            |
2087  | mode | number | Yes   | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
2088
2089**Return value**
2090
2091  | Type                | Description                          |
2092  | ------------------- | ---------------------------- |
2093  | Promise&lt;void&gt; | Promise that returns no value.|
2094
2095**Example**
2096
2097  ```js
2098  let fd = fileio.openSync(path);
2099  let mode = 0o700;
2100  fileio.fchmod(fd, mode).then(function() {
2101      console.info("File permissions changed");
2102  }).catch(function(err){
2103      console.info("Failed to change file permissions. Error:"+ err);
2104  });
2105  ```
2106
2107
2108## fileio.fchmod<sup>7+</sup>
2109
2110fchmod(fd: number, mode: number, callback: AsyncCallback&lt;void&gt;): void
2111
2112Changes file permissions based on the file descriptor. This API uses an asynchronous callback to return the result.
2113
2114**System capability**: SystemCapability.FileManagement.File.FileIO
2115
2116**Parameters**
2117
2118  | Name     | Type                             | Mandatory  | Description                                      |
2119  | -------- | ------------------------------- | ---- | ---------------------------------------- |
2120  | fd       | number                          | Yes   | File descriptor of the target file.                            |
2121  | mode     | number                          | Yes   | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
2122  | callback | AsyncCallback&nbsp;&lt;void&gt; | Yes   | Callback invoked when the file permissions are changed asynchronously.                          |
2123
2124**Example**
2125
2126  ```js
2127  let fd = fileio.openSync(path);
2128  let mode = 0o700;
2129  fileio.fchmod(fd, mode, function (err) {
2130      // Do something.
2131  });
2132  ```
2133
2134
2135## fileio.fchmodSync<sup>7+</sup>
2136
2137fchmodSync(fd: number, mode: number): void
2138
2139Synchronously changes the file permissions based on the file descriptor.
2140
2141**System capability**: SystemCapability.FileManagement.File.FileIO
2142
2143**Parameters**
2144
2145  | Name | Type    | Mandatory  | Description                                      |
2146  | ---- | ------ | ---- | ---------------------------------------- |
2147  | fd   | number | Yes   | File descriptor of the target file.                            |
2148  | mode | number | Yes   | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
2149
2150**Example**
2151
2152  ```js
2153  let fd = fileio.openSync(path);
2154  let mode = 0o700;
2155   fileio.fchmodSync(fd, mode);
2156  ```
2157
2158
2159## fileio.createStream<sup>7+</sup>
2160
2161createStream(path: string, mode: string): Promise&lt;Stream&gt;
2162
2163Opens a file stream based on the file path. This API uses a promise to return the result.
2164
2165**System capability**: SystemCapability.FileManagement.File.FileIO
2166
2167**Parameters**
2168
2169| Name| Type  | Mandatory| Description                                                        |
2170| ------ | ------ | ---- | ------------------------------------------------------------ |
2171| path   | string | Yes  | Application sandbox path of the file.                                  |
2172| mode   | string | Yes  | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
2173
2174**Return value**
2175
2176  | Type                               | Description       |
2177  | --------------------------------- | --------- |
2178  | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
2179
2180**Example**
2181
2182  ```js
2183  fileio.createStream(path, "r+").then(function(stream){
2184      console.info("Stream opened");
2185  }).catch(function(err){
2186      console.info("Failed to create the stream. Error:"+ err);
2187  });
2188  ```
2189
2190
2191## fileio.createStream<sup>7+</sup>
2192
2193createStream(path: string, mode: string, callback: AsyncCallback&lt;Stream&gt;): void
2194
2195Opens a file stream based on the file path. This API uses an asynchronous callback to return the result.
2196
2197**System capability**: SystemCapability.FileManagement.File.FileIO
2198
2199**Parameters**
2200
2201| Name  | Type                                   | Mandatory| Description                                                        |
2202| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
2203| path     | string                                  | Yes  | Application sandbox path of the file.                                  |
2204| mode     | string                                  | Yes  | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
2205| callback | AsyncCallback&lt;[Stream](#stream)&gt; | Yes  | Callback invoked when the stream is open asynchronously.                                  |
2206
2207**Example**
2208
2209  ```js
2210  fileio.createStream(path, "r+", function(err, stream){
2211      // Do something.
2212  });
2213  ```
2214
2215
2216## fileio.createStreamSync<sup>7+</sup>
2217
2218createStreamSync(path: string, mode: string): Stream
2219
2220Synchronously opens a stream based on the file path.
2221
2222**System capability**: SystemCapability.FileManagement.File.FileIO
2223
2224**Parameters**
2225
2226| Name| Type  | Mandatory| Description                                                        |
2227| ------ | ------ | ---- | ------------------------------------------------------------ |
2228| path   | string | Yes  | Application sandbox path of the file.                                  |
2229| mode   | string | Yes  | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
2230
2231**Return value**
2232
2233  | Type               | Description       |
2234  | ------------------ | --------- |
2235  | [Stream](#stream) | Stream opened.|
2236
2237**Example**
2238
2239  ```js
2240  let ss = fileio.createStreamSync(path, "r+");
2241  ```
2242
2243
2244## fileio.fdopenStream<sup>7+</sup>
2245
2246fdopenStream(fd: number, mode: string): Promise&lt;Stream&gt;
2247
2248Opens a file stream based on the file descriptor. This API uses a promise to return the result.
2249
2250**System capability**: SystemCapability.FileManagement.File.FileIO
2251
2252**Parameters**
2253
2254  | Name | Type    | Mandatory  | Description                                      |
2255  | ---- | ------ | ---- | ---------------------------------------- |
2256  | fd   | number | Yes   | File descriptor of the target file.                            |
2257  | mode | string | Yes   | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
2258
2259**Return value**
2260
2261  | Type                              | Description       |
2262  | --------------------------------- | --------- |
2263  | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
2264
2265**Example**
2266
2267  ```js
2268  let fd = fileio.openSync(path);
2269  fileio.fdopenStream(fd, "r+").then(function(stream){
2270      console.info("Stream opened");
2271  }).catch(function(err){
2272      console.info("Failed to open the stream. Error:"+ err);
2273  });
2274  ```
2275
2276
2277## fileio.fdopenStream<sup>7+</sup>
2278
2279fdopenStream(fd: number, mode: string, callback: AsyncCallback&lt;Stream&gt;): void
2280
2281Opens a file stream based on the file descriptor. This API uses an asynchronous callback to return the result.
2282
2283**System capability**: SystemCapability.FileManagement.File.FileIO
2284
2285**Parameters**
2286
2287  | Name     | Type                                      | Mandatory  | Description                                      |
2288  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
2289  | fd       | number                                   | Yes   | File descriptor of the target file.                            |
2290  | mode     | string                                   | Yes   | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
2291  | callback | AsyncCallback&nbsp;&lt;[Stream](#stream)&gt; | Yes   | Callback invoked when the stream is open asynchronously.                           |
2292
2293**Example**
2294
2295  ```js
2296  let fd = fileio.openSync(path);
2297  fileio.fdopenStream(fd, "r+", function (err, stream) {
2298      // Do something.
2299  });
2300  ```
2301
2302
2303## fileio.fdopenStreamSync<sup>7+</sup>
2304
2305fdopenStreamSync(fd: number, mode: string): Stream
2306
2307Synchronously opens a stream based on the file descriptor.
2308
2309**System capability**: SystemCapability.FileManagement.File.FileIO
2310
2311**Parameters**
2312
2313  | Name | Type    | Mandatory  | Description                                      |
2314  | ---- | ------ | ---- | ---------------------------------------- |
2315  | fd   | number | Yes   | File descriptor of the target file.                            |
2316  | mode | string | Yes   | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
2317
2318**Return value**
2319
2320  | Type               | Description       |
2321  | ------------------ | --------- |
2322  | [Stream](#stream) | Stream opened.|
2323
2324**Example**
2325
2326  ```js
2327  let fd = fileio.openSync(path);
2328  let ss = fileio.fdopenStreamSync(fd, "r+");
2329  ```
2330
2331
2332## fileio.fchown<sup>7+</sup>
2333
2334fchown(fd: number, uid: number, gid: number): Promise&lt;void&gt;
2335
2336Changes the file owner based on the file descriptor. This API uses a promise to return the result.
2337
2338**System capability**: SystemCapability.FileManagement.File.FileIO
2339
2340**Parameters**
2341
2342  | Name | Type    | Mandatory  | Description          |
2343  | ---- | ------ | ---- | ------------ |
2344  | fd   | number | Yes   | File descriptor of the target file.|
2345  | uid  | number | Yes   | New UID.  |
2346  | gid  | number | Yes   | New GID.  |
2347
2348**Return value**
2349
2350  | Type                 | Description                          |
2351  | ------------------- | ---------------------------- |
2352  | Promise&lt;void&gt; | Promise that returns no value.|
2353
2354**Example**
2355
2356  ```js
2357  let fd = fileio.openSync(path);
2358  let stat = fileio.statSync(path);
2359  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
2360      console.info("File owner changed");
2361  }).catch(function(err){
2362      console.info("Failed to change the file owner. Error:"+ err);
2363  });
2364  ```
2365
2366
2367## fileio.fchown<sup>7+</sup>
2368
2369fchown(fd: number, uid: number, gid: number, callback: AsyncCallback&lt;void&gt;): void
2370
2371Changes the file owner based on the file descriptor. This API uses an asynchronous callback to return the result.
2372
2373**System capability**: SystemCapability.FileManagement.File.FileIO
2374
2375**Parameters**
2376
2377  | Name     | Type                       | Mandatory  | Description             |
2378  | -------- | ------------------------- | ---- | --------------- |
2379  | fd       | number                    | Yes   | File descriptor of the target file.   |
2380  | uid      | number                    | Yes   | New UID.     |
2381  | gid      | number                    | Yes   | New GID.     |
2382  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file owner is changed asynchronously.|
2383
2384**Example**
2385
2386  ```js
2387  let fd = fileio.openSync(path);
2388  let stat = fileio.statSync(path);
2389  fileio.fchown(fd, stat.uid, stat.gid, function (err){
2390      // Do something.
2391  });
2392  ```
2393
2394
2395## fileio.fchownSync<sup>7+</sup>
2396
2397fchownSync(fd: number, uid: number, gid: number): void
2398
2399Synchronously changes the file owner based on the file descriptor.
2400
2401**System capability**: SystemCapability.FileManagement.File.FileIO
2402
2403**Parameters**
2404
2405  | Name | Type    | Mandatory  | Description          |
2406  | ---- | ------ | ---- | ------------ |
2407  | fd   | number | Yes   | File descriptor of the target file.|
2408  | uid  | number | Yes   | New UID.  |
2409  | gid  | number | Yes   | New GID.  |
2410
2411**Example**
2412
2413  ```js
2414  let fd = fileio.openSync(path);
2415  let stat = fileio.statSync(path);
2416  fileio.fchownSync(fd, stat.uid, stat.gid);
2417  ```
2418
2419
2420## fileio.lchown<sup>7+</sup>
2421
2422lchown(path: string, uid: number, gid: number): Promise&lt;void&gt;
2423
2424Changes the file owner (owner of the symbolic link, not the file referred to by the symbolic link) based on the file path. This API uses a promise to return the result.
2425
2426**System capability**: SystemCapability.FileManagement.File.FileIO
2427
2428**Parameters**
2429
2430| Name| Type  | Mandatory| Description                      |
2431| ------ | ------ | ---- | -------------------------- |
2432| path   | string | Yes  | Application sandbox path of the file.|
2433| uid    | number | Yes  | New UID.                 |
2434| gid    | number | Yes  | New GID.                 |
2435
2436**Return value**
2437
2438  | Type                 | Description                          |
2439  | ------------------- | ---------------------------- |
2440  | Promise&lt;void&gt; | Promise that returns no value.|
2441
2442**Example**
2443
2444  ```js
2445  let stat = fileio.statSync(path);
2446  fileio.lchown(path, stat.uid, stat.gid).then(function() {
2447      console.info("File owner changed");
2448  }).catch(function(err){
2449      console.info("Failed to change the file owner. Error:"+ err);
2450  });
2451  ```
2452
2453
2454## fileio.lchown<sup>7+</sup>
2455
2456lchown(path: string, uid: number, gid: number, callback: AsyncCallback&lt;void&gt;): void
2457
2458Changes the file owner (owner of the symbolic link, not the file referred to by the symbolic link) based on the file path. This API uses an asynchronous callback to return the result.
2459
2460**System capability**: SystemCapability.FileManagement.File.FileIO
2461
2462**Parameters**
2463
2464| Name  | Type                     | Mandatory| Description                          |
2465| -------- | ------------------------- | ---- | ------------------------------ |
2466| path     | string                    | Yes  | Application sandbox path of the file.    |
2467| uid      | number                    | Yes  | New UID.                     |
2468| gid      | number                    | Yes  | New GID.                     |
2469| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file owner is changed asynchronously.|
2470
2471**Example**
2472
2473  ```js
2474  let stat = fileio.statSync(path);
2475  fileio.lchown(path, stat.uid, stat.gid, function (err){
2476      // Do something.
2477  });
2478  ```
2479
2480
2481## fileio.lchownSync<sup>7+</sup>
2482
2483lchownSync(path: string, uid: number, gid: number): void
2484
2485Synchronously changes the file owner based on the file path and changes the owner of the symbolic link (not the referenced file).
2486
2487**System capability**: SystemCapability.FileManagement.File.FileIO
2488
2489**Parameters**
2490
2491| Name| Type  | Mandatory| Description                      |
2492| ------ | ------ | ---- | -------------------------- |
2493| path   | string | Yes  | Application sandbox path of the file.|
2494| uid    | number | Yes  | New UID.                 |
2495| gid    | number | Yes  | New GID.                 |
2496
2497**Example**
2498
2499  ```js
2500  let stat = fileio.statSync(path);
2501  fileio.lchownSync(path, stat.uid, stat.gid);
2502  ```
2503
2504
2505## fileio.createWatcher<sup>7+</sup>
2506
2507createWatcher(filename: string, events: number, callback: AsyncCallback&lt;number&gt;): Watcher
2508
2509Listens for file or directory changes. This API uses an asynchronous callback to return the result.
2510
2511**System capability**: SystemCapability.FileManagement.File.FileIO
2512
2513**Parameters**
2514
2515| Name  | Type                             | Mandatory| Description                                                        |
2516| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
2517| filename | string                            | Yes  | Application sandbox path of the file.                                  |
2518| events   | Number                            | Yes  | -&nbsp;**1**: The file or directory is renamed.<br>-&nbsp;**2**: The file or directory is modified.<br>-&nbsp;**3**: The file or directory is modified and renamed.|
2519| callback | AsyncCallback&lt;number&nbsp;&gt; | Yes  | Called each time a change is detected.                            |
2520
2521**Return value**
2522
2523  | Type                 | Description        |
2524  | -------------------- | ---------- |
2525  | [Watcher](#watcher7) | Promise used to return the **Watcher** instance.|
2526
2527**Example**
2528
2529  ```js
2530  let filename = path +"/test.txt";
2531  fileio.createWatcher(filename, 1, function(number){
2532     console.info("Monitoring times: "+number);
2533  });
2534
2535  ```
2536
2537
2538## Readout
2539
2540Obtains the file read result. This class applies only to the **read()** method.
2541
2542**System capability**: SystemCapability.FileManagement.File.FileIO
2543
2544| Name       | Type      | Readable  | Writable  | Description               |
2545| --------- | ---------- | ---- | ---- | ----------------- |
2546| bytesRead | number     | Yes   | Yes   | Length of the data read.          |
2547| offset    | number     | Yes   | Yes   | Position of the buffer to which the data will be read in reference to the start address of the buffer.|
2548| buffer    | ArrayBufer | Yes   | Yes   | Buffer for storing the data read.      |
2549
2550
2551## Stat
2552
2553Provides detailed file information. Before calling a method of the **Stat** class, use the [stat()](#fileiostat) method synchronously or asynchronously to create a **Stat** instance.
2554
2555**System capability**: SystemCapability.FileManagement.File.FileIO
2556
2557### Attributes
2558
2559| Name    | Type  | Readable  | Writable  | Description                                      |
2560| ------ | ------ | ---- | ---- | ---------------------------------------- |
2561| dev    | number | Yes   | No   | Major device number.                           |
2562| ino    | number | Yes   | No   | File ID. Different files on the same device have different **ino**s.                |
2563| mode   | number | Yes   | No   | File type and permissions. The first four bits indicate the file type, and the last 12 bits indicate the permissions. The bit fields are described as follows:<br>-&nbsp;**0o170000**: mask used to obtain the file type.<br>-&nbsp;**0o140000**: The file is a socket.<br>-&nbsp;**0o120000**: The file is a symbolic link.<br>-&nbsp;**0o100000**: The file is a regular file.<br>-&nbsp;**0o060000**: The file is a block device.<br>-&nbsp;**0o040000**: The file is a directory.<br>-&nbsp;**0o020000**: The file is a character device.<br>-&nbsp;**0o010000**: The file is a named pipe (FIFO).<br>-&nbsp;**0o0700**: mask used to obtain the owner permissions.<br>-&nbsp;**0o0400**: The owner has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0100**: The owner has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0070**: mask used to obtain the user group permissions.<br>-&nbsp;**0o0040**: The user group has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0020**: The user group has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0010**: The user group has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0007**: mask used to obtain the permissions of other users.<br>-&nbsp;**0o0004**: Other users have the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0002**: Other users have the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0001**: Other users have the permission to execute a regular file or search for the specified path in a directory.|
2564| nlink  | number | Yes   | No   | Number of hard links in the file.                                |
2565| uid    | number | Yes   | No   | User ID, that is ID of the file owner.                               |
2566| gid    | number | Yes   | No   | Group ID, that is, ID of the user group of the file.                               |
2567| rdev   | number | Yes   | No   | Minor device number.                           |
2568| size   | number | Yes   | No   | File size, in bytes. This parameter is valid only for regular files.                  |
2569| blocks | number | Yes   | No   | Number of blocks occupied by a file. Each block is 512 bytes.                  |
2570| atime  | number | Yes   | No   | Time of the last access to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.       |
2571| mtime  | number | Yes   | No   | Time of the last modification to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.       |
2572| ctime  | number | Yes   | No   | Time of the last status change of the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.      |
2573
2574
2575### isBlockDevice
2576
2577isBlockDevice(): boolean
2578
2579Checks whether this file is a block special file. A block special file supports access by block only, and it is cached when accessed.
2580
2581**System capability**: SystemCapability.FileManagement.File.FileIO
2582
2583**Return value**
2584
2585  | Type     | Description              |
2586  | ------- | ---------------- |
2587  | boolean | Whether the file is a block special file.|
2588
2589**Example**
2590
2591  ```js
2592  let isBLockDevice = fileio.statSync(path).isBlockDevice();
2593  ```
2594
2595
2596### isCharacterDevice
2597
2598isCharacterDevice(): boolean
2599
2600Checks whether this file is a character special file. A character special file supports random access, and it is not cached when accessed.
2601
2602**System capability**: SystemCapability.FileManagement.File.FileIO
2603
2604**Return value**
2605
2606  | Type     | Description               |
2607  | ------- | ----------------- |
2608  | boolean | Whether the file is a character special file.|
2609
2610**Example**
2611
2612  ```js
2613  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
2614  ```
2615
2616
2617### isDirectory
2618
2619isDirectory(): boolean
2620
2621Checks whether this file is a directory.
2622
2623**System capability**: SystemCapability.FileManagement.File.FileIO
2624
2625**Return value**
2626
2627  | Type     | Description           |
2628  | ------- | ------------- |
2629  | boolean | Whether the file is a directory.|
2630
2631**Example**
2632
2633  ```js
2634  let isDirectory = fileio.statSync(path).isDirectory();
2635  ```
2636
2637
2638### isFIFO
2639
2640isFIFO(): boolean
2641
2642Checks whether this file is a named pipe (or FIFO). Named pipes are used for inter-process communication.
2643
2644**System capability**: SystemCapability.FileManagement.File.FileIO
2645
2646**Return value**
2647
2648  | Type     | Description                   |
2649  | ------- | --------------------- |
2650  | boolean | Whether the file is an FIFO.|
2651
2652**Example**
2653
2654  ```js
2655  let isFIFO = fileio.statSync(path).isFIFO();
2656  ```
2657
2658
2659### isFile
2660
2661isFile(): boolean
2662
2663Checks whether this file is a regular file.
2664
2665**System capability**: SystemCapability.FileManagement.File.FileIO
2666
2667**Return value**
2668
2669  | Type     | Description             |
2670  | ------- | --------------- |
2671  | boolean | Whether the file is a regular file.|
2672
2673**Example**
2674
2675  ```js
2676  let isFile = fileio.statSync(path).isFile();
2677  ```
2678
2679
2680### isSocket
2681
2682isSocket(): boolean
2683
2684Checks whether this file is a socket.
2685
2686**System capability**: SystemCapability.FileManagement.File.FileIO
2687
2688**Return value**
2689
2690  | Type     | Description            |
2691  | ------- | -------------- |
2692  | boolean | Whether the file is a socket.|
2693
2694**Example**
2695
2696  ```js
2697  let isSocket = fileio.statSync(path).isSocket();
2698  ```
2699
2700
2701### isSymbolicLink
2702
2703isSymbolicLink(): boolean
2704
2705Checks whether this file is a symbolic link.
2706
2707**System capability**: SystemCapability.FileManagement.File.FileIO
2708
2709**Return value**
2710
2711  | Type     | Description             |
2712  | ------- | --------------- |
2713  | boolean | Whether the file is a symbolic link.|
2714
2715**Example**
2716
2717  ```js
2718  let isSymbolicLink = fileio.statSync(path).isSymbolicLink();
2719  ```
2720
2721
2722## Watcher<sup>7+</sup>
2723
2724Listens for the changes of a file. You can call the **Watcher.stop()** method synchronously or asynchronously to stop the listening.
2725
2726
2727### stop<sup>7+</sup>
2728
2729stop(): Promise&lt;void&gt;
2730
2731Stops the **watcher** instance. This API uses a promise to return the result.
2732
2733**System capability**: SystemCapability.FileManagement.File.FileIO
2734
2735**Example**
2736
2737  ```js
2738  let filename = path +"/test.txt";
2739  let watcher = fileio.createWatcher(filename, 1, function(number){
2740      console.info("Monitoring times: "+number);
2741  });
2742  watcher.stop().then(function(){
2743       console.info("Watcher stopped");
2744  });
2745  ```
2746
2747
2748### stop<sup>7+</sup>
2749
2750stop(callback: AsyncCallback&lt;void&gt;): void
2751
2752Stops the **watcher** instance. This API uses an asynchronous callback to return the result.
2753
2754**System capability**: SystemCapability.FileManagement.File.FileIO
2755
2756**Parameters**
2757
2758  | Name     | Type                       | Mandatory  | Description                    |
2759  | -------- | ------------------------- | ---- | ---------------------- |
2760  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when **watcher** is stopped asynchronously.|
2761
2762**Example**
2763
2764  ```js
2765  let filename = path +"/test.txt";
2766  let watcher = fileio.createWatcher(filename, 1, function(number){
2767      console.info("Monitoring times: "+number);
2768  });
2769  watcher.stop(function(){
2770      console.info("Watcher stopped");
2771  })
2772  ```
2773
2774## Stream
2775
2776Provides file stream management. Before calling a method of the **Stream** class, use the **createStream()** method synchronously or asynchronously to create a **Stream** instance.
2777
2778
2779### close<sup>7+</sup>
2780
2781close(): Promise&lt;void&gt;
2782
2783Closes the stream. This API uses a promise to return the result.
2784
2785**System capability**: SystemCapability.FileManagement.File.FileIO
2786
2787**Return value**
2788
2789  | Type                 | Description           |
2790  | ------------------- | ------------- |
2791  | Promise&lt;void&gt; | Promise used to return the stream close result.|
2792
2793**Example**
2794
2795  ```js
2796  let ss= fileio.createStreamSync(path, "r+");
2797  ss.close().then(function(){
2798      console.info("File stream closed");
2799  }).catch(function(err){
2800      console.info("Failed to close the file stream. Error:"+ err);
2801  });
2802  ```
2803
2804
2805### close<sup>7+</sup>
2806
2807close(callback: AsyncCallback&lt;void&gt;): void
2808
2809Closes the stream. This API uses an asynchronous callback to return the result.
2810
2811**System capability**: SystemCapability.FileManagement.File.FileIO
2812
2813**Parameters**
2814
2815  | Name     | Type                       | Mandatory  | Description           |
2816  | -------- | ------------------------- | ---- | ------------- |
2817  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|
2818
2819**Example**
2820
2821  ```js
2822  let ss= fileio.createStreamSync(path, "r+");
2823  ss.close(function (err) {
2824      // Do something
2825  });
2826  ```
2827
2828
2829### closeSync
2830
2831closeSync(): void
2832
2833Synchronously closes the stream.
2834
2835**System capability**: SystemCapability.FileManagement.File.FileIO
2836
2837**Example**
2838
2839  ```js
2840  let ss= fileio.createStreamSync(path, "r+");
2841  ss.closeSync();
2842  ```
2843
2844
2845### flush<sup>7+</sup>
2846
2847flush(): Promise&lt;void&gt;
2848
2849Flushes the stream. This API uses a promise to return the result.
2850
2851**System capability**: SystemCapability.FileManagement.File.FileIO
2852
2853**Return value**
2854
2855  | Type                 | Description           |
2856  | ------------------- | ------------- |
2857  | Promise&lt;void&gt; | Promise used to return the stream flushing result.|
2858
2859**Example**
2860
2861  ```js
2862  let ss= fileio.createStreamSync(path, "r+");
2863  ss.flush().then(function (){
2864      console.info("Stream flushed");
2865  }).catch(function(err){
2866      console.info("Failed to flush the stream. Error:"+ err);
2867  });
2868  ```
2869
2870
2871### flush<sup>7+</sup>
2872
2873flush(callback: AsyncCallback&lt;void&gt;): void
2874
2875Flushes the stream. This API uses an asynchronous callback to return the result.
2876
2877**System capability**: SystemCapability.FileManagement.File.FileIO
2878
2879**Parameters**
2880
2881  | Name     | Type                       | Mandatory  | Description            |
2882  | -------- | ------------------------- | ---- | -------------- |
2883  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is asynchronously flushed.|
2884
2885**Example**
2886
2887  ```js
2888  let ss= fileio.createStreamSync(path, "r+");
2889  ss.flush(function (err) {
2890      // Do something
2891  });
2892  ```
2893
2894
2895### flushSync<sup>7+</sup>
2896
2897flushSync(): void
2898
2899Synchronously flushes the stream.
2900
2901**System capability**: SystemCapability.FileManagement.File.FileIO
2902
2903**Example**
2904
2905  ```js
2906  let ss= fileio.createStreamSync(path, "r+");
2907  ss.flushSync();
2908  ```
2909
2910
2911### write<sup>7+</sup>
2912
2913write(buffer: ArrayBuffer | string, options?: {
2914    offset?: number;
2915    length?: number;
2916    position?: number;
2917    encoding?: string;
2918}): Promise&lt;number&gt;
2919
2920Writes data into the stream. This API uses a promise to return the result.
2921
2922**System capability**: SystemCapability.FileManagement.File.FileIO
2923
2924**Parameters**
2925
2926  | Name    | Type                             | Mandatory  | Description                                      |
2927  | ------- | ------------------------------- | ---- | ---------------------------------------- |
2928  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
2929  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
2930
2931**Return value**
2932
2933  | Type                   | Description      |
2934  | --------------------- | -------- |
2935  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
2936
2937**Example**
2938
2939  ```js
2940  let ss= fileio.createStreamSync(path, "r+");
2941  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
2942      console.info("Data written to the stream. Size is:"+ number);
2943  }).catch(function(err){
2944      console.info("Failed to write data to the stream. Error:"+ err);
2945  });
2946  ```
2947
2948
2949### write<sup>7+</sup>
2950
2951write(buffer: ArrayBuffer | string, options: {
2952    offset?: number;
2953    length?: number;
2954    position?: number;
2955    encoding?: string;
2956}, callback: AsyncCallback&lt;number&gt;): void
2957
2958Writes data into the stream. This API uses an asynchronous callback to return the result.
2959
2960**System capability**: SystemCapability.FileManagement.File.FileIO
2961
2962**Parameters**
2963
2964  | Name  | Type                           | Mandatory| Description                                                        |
2965  | -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
2966  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | Yes  | Data to write. It can be a string or data from a buffer.                    |
2967  | options  | Object                          | No  | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
2968  | callback | AsyncCallback&lt;number&gt;     | Yes  | Callback invoked when the data is written asynchronously.                              |
2969
2970**Example**
2971
2972  ```js
2973  let ss= fileio.createStreamSync(path, "r+");
2974  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
2975      if (bytesWritten) {
2976         // Do something
2977         console.info("Data written to the stream. Size is:"+ bytesWritten);
2978      }
2979  });
2980  ```
2981
2982
2983### writeSync<sup>7+</sup>
2984
2985writeSync(buffer: ArrayBuffer | string, options?: {
2986    offset?: number;
2987    length?: number;
2988    position?: number;
2989    encoding?: string;
2990}): number
2991
2992Synchronously writes data into the stream.
2993
2994**System capability**: SystemCapability.FileManagement.File.FileIO
2995
2996**Parameters**
2997
2998  | Name    | Type                             | Mandatory  | Description                                      |
2999  | ------- | ------------------------------- | ---- | ---------------------------------------- |
3000  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
3001  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
3002
3003**Return value**
3004
3005  | Type    | Description      |
3006  | ------ | -------- |
3007  | number | Length of the data written in the file.|
3008
3009**Example**
3010
3011  ```js
3012  let ss= fileio.createStreamSync(path,"r+");
3013  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
3014  ```
3015
3016
3017### read<sup>7+</sup>
3018
3019read(buffer: ArrayBuffer, options?: {
3020    position?: number;
3021    offset?: number;
3022    length?: number;
3023}): Promise&lt;ReadOut&gt;
3024
3025Reads data from the stream. This API uses a promise to return the result.
3026
3027**System capability**: SystemCapability.FileManagement.File.FileIO
3028
3029**Parameters**
3030
3031  | Name    | Type         | Mandatory  | Description                                      |
3032  | ------- | ----------- | ---- | ---------------------------------------- |
3033  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
3034  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
3035
3036**Return value**
3037
3038  | Type                                | Description    |
3039  | ---------------------------------- | ------ |
3040  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
3041
3042**Example**
3043
3044  ```js
3045  let ss = fileio.createStreamSync(path, "r+");
3046  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut){
3047      console.info("Read data successfully");
3048      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
3049  }).catch(function(err){
3050      console.info("Failed to read data. Error:"+ err);
3051  });
3052  ```
3053
3054
3055### read<sup>7+</sup>
3056
3057read(buffer: ArrayBuffer, options: {
3058    position?: number;
3059    offset?: number;
3060    length?: number;
3061}, callback: AsyncCallback&lt;ReadOut&gt;): void
3062
3063Reads data from the stream. This API uses an asynchronous callback to return the result.
3064
3065**System capability**: SystemCapability.FileManagement.File.FileIO
3066
3067**Parameters**
3068
3069  | Name     | Type                                      | Mandatory  | Description                                      |
3070  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
3071  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file read.                             |
3072  | options  | Object                                   | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
3073  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when data is read asynchronously from the stream.                        |
3074
3075**Example**
3076
3077  ```js
3078  let ss = fileio.createStreamSync(path, "r+");
3079  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
3080      if (readOut) {
3081          console.info("Read data successfully");
3082          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
3083      }
3084  });
3085  ```
3086
3087
3088### readSync<sup>7+</sup>
3089
3090readSync(buffer: ArrayBuffer, options?: {
3091    position?: number;
3092    offset?: number;
3093    length?: number;
3094}): number
3095
3096Synchronously reads data from the stream.
3097
3098**System capability**: SystemCapability.FileManagement.File.FileIO
3099
3100**Parameters**
3101
3102  | Name    | Type         | Mandatory  | Description                                      |
3103  | ------- | ----------- | ---- | ---------------------------------------- |
3104  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
3105  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
3106
3107**Return value**
3108
3109  | Type    | Description      |
3110  | ------ | -------- |
3111  | number | Length of the data read.|
3112
3113**Example**
3114
3115  ```js
3116  let ss = fileio.createStreamSync(path, "r+");
3117  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
3118  ```
3119
3120
3121## Dir
3122
3123Manages directories. Before calling a method of the **Dir** class, use the **opendir()** method synchronously or asynchronously to create a **Dir** instance.
3124
3125
3126### read
3127
3128read(): Promise&lt;Dirent&gt;
3129
3130Reads the next directory entry. This API uses a promise to return the result.
3131
3132**System capability**: SystemCapability.FileManagement.File.FileIO
3133
3134**Return value**
3135
3136  | Type                              | Description           |
3137  | -------------------------------- | ------------- |
3138  | Promise&lt;[Dirent](#dirent)&gt; | Promise used to return the directory entry read.|
3139
3140**Example**
3141
3142  ```js
3143  dir.read().then(function (dirent){
3144      console.log("Read the next directory entry:"+JSON.stringify(dirent));
3145  }).catch(function(err){
3146      console.info("Failed to read the next directory entry. Error:"+ err);
3147  });
3148  ```
3149
3150
3151### read
3152
3153read(callback: AsyncCallback&lt;Dirent&gt;): void
3154
3155Reads the next directory entry. This API uses an asynchronous callback to return the result.
3156
3157**System capability**: SystemCapability.FileManagement.File.FileIO
3158
3159**Parameters**
3160
3161  | Name     | Type                                    | Mandatory  | Description              |
3162  | -------- | -------------------------------------- | ---- | ---------------- |
3163  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | Yes   | Callback invoked when the next directory entry is asynchronously read.|
3164
3165**Example**
3166
3167  ```js
3168  dir.read(function (err, dirent) {
3169      if (dirent) {
3170          // Do something
3171          console.log("Read the next directory entry:"+JSON.stringify(dirent));
3172      }
3173  });
3174  ```
3175
3176
3177### readSync
3178
3179readSync(): Dirent
3180
3181Synchronously reads the next directory entry.
3182
3183**System capability**: SystemCapability.FileManagement.File.FileIO
3184
3185**Return value**
3186
3187  | Type               | Description      |
3188  | ----------------- | -------- |
3189  | [Dirent](#dirent) | Directory entry read.|
3190
3191**Example**
3192
3193  ```js
3194  let dirent = dir.readSync();
3195  ```
3196
3197
3198### close<sup>7+</sup>
3199
3200close(): Promise&lt;void&gt;
3201
3202Closes a directory. This API uses a promise to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir.
3203
3204**System capability**: SystemCapability.FileManagement.File.FileIO
3205
3206**Example**
3207
3208  ```js
3209  dir.close().then(function(err){
3210      console.info("close dir successfully");
3211  });
3212  ```
3213
3214
3215  ### close<sup>7+</sup>
3216
3217close(callback: AsyncCallback&lt;void&gt;): void
3218
3219Closes a directory. This API uses an asynchronous callback to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir.
3220
3221**System capability**: SystemCapability.FileManagement.File.FileIO
3222
3223**Example**
3224
3225  ```js
3226  dir.close(function(err){
3227      console.info("close dir successfully");
3228  });
3229  ```
3230
3231
3232### closeSync
3233
3234closeSync(): void
3235
3236Closes a directory. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir.
3237
3238**System capability**: SystemCapability.FileManagement.File.FileIO
3239
3240**Example**
3241
3242  ```js
3243  dir.closeSync();
3244  ```
3245
3246
3247## Dirent
3248
3249Provides information about files and directories. Before calling a method of the **Dirent** class, use the [dir.read()](#read) method synchronously or asynchronously to create a **Dirent** instance.
3250
3251**System capability**: SystemCapability.FileManagement.File.FileIO
3252
3253### Attributes
3254
3255| Name  | Type  | Readable  | Writable  | Description     |
3256| ---- | ------ | ---- | ---- | ------- |
3257| name | string | Yes   | No   | Directory entry name.|
3258
3259
3260### isBlockDevice
3261
3262isBlockDevice(): boolean
3263
3264Checks whether this directory entry is a block special file. A block special file supports access by block only, and it is cached when accessed.
3265
3266**System capability**: SystemCapability.FileManagement.File.FileIO
3267
3268**Return value**
3269
3270  | Type     | Description              |
3271  | ------- | ---------------- |
3272  | boolean | Whether the directory entry is a block special file.|
3273
3274**Example**
3275
3276  ```js
3277  let dir = fileio.opendirSync(path);
3278  let isBLockDevice = dir.readSync().isBlockDevice();
3279  ```
3280
3281
3282### isCharacterDevice
3283
3284isCharacterDevice(): boolean
3285
3286Checks whether a directory entry is a character special file. A character special file supports random access, and it is not cached when accessed.
3287
3288**System capability**: SystemCapability.FileManagement.File.FileIO
3289
3290**Return value**
3291
3292  | Type     | Description               |
3293  | ------- | ----------------- |
3294  | boolean | Whether the directory entry is a character special file.|
3295
3296**Example**
3297
3298  ```js
3299  let dir = fileio.opendirSync(path);
3300  let isCharacterDevice = dir.readSync().isCharacterDevice();
3301  ```
3302
3303
3304### isDirectory
3305
3306isDirectory(): boolean
3307
3308Checks whether a directory entry is a directory.
3309
3310**System capability**: SystemCapability.FileManagement.File.FileIO
3311
3312**Return value**
3313
3314  | Type     | Description           |
3315  | ------- | ------------- |
3316  | boolean | Whether the directory entry is a directory.|
3317
3318**Example**
3319
3320  ```js
3321  let dir = fileio.opendirSync(path);
3322  let isDirectory = dir.readSync().isDirectory();
3323  ```
3324
3325
3326### isFIFO
3327
3328isFIFO(): boolean
3329
3330Checks whether this directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication.
3331
3332**System capability**: SystemCapability.FileManagement.File.FileIO
3333
3334**Return value**
3335
3336  | Type     | Description             |
3337  | ------- | --------------- |
3338  | boolean | Whether the directory entry is a FIFO.|
3339
3340**Example**
3341
3342  ```js
3343  let dir = fileio.opendirSync(path);
3344  let isFIFO = dir.readSync().isFIFO();
3345  ```
3346
3347
3348### isFile
3349
3350isFile(): boolean
3351
3352Checks whether a directory entry is a regular file.
3353
3354**System capability**: SystemCapability.FileManagement.File.FileIO
3355
3356**Return value**
3357
3358  | Type     | Description             |
3359  | ------- | --------------- |
3360  | boolean | Whether the directory entry is a regular file.|
3361
3362**Example**
3363
3364  ```js
3365  let dir = fileio.opendirSync(path);
3366  let isFile = dir.readSync().isFile();
3367  ```
3368
3369
3370### isSocket
3371
3372isSocket(): boolean
3373
3374Checks whether a directory entry is a socket.
3375
3376**System capability**: SystemCapability.FileManagement.File.FileIO
3377
3378**Return value**
3379
3380  | Type     | Description            |
3381  | ------- | -------------- |
3382  | boolean | Whether the directory entry is a socket.|
3383
3384**Example**
3385
3386  ```js
3387  let dir = fileio.opendirSync(path);
3388  let isSocket = dir.readSync().isSocket();
3389  ```
3390
3391
3392### isSymbolicLink
3393
3394isSymbolicLink(): boolean
3395
3396Checks whether a directory entry is a symbolic link.
3397
3398**System capability**: SystemCapability.FileManagement.File.FileIO
3399
3400**Return value**
3401
3402  | Type     | Description             |
3403  | ------- | --------------- |
3404  | boolean | Whether the directory entry is a symbolic link.|
3405
3406**Example**
3407
3408  ```js
3409  let dir = fileio.opendirSync(path);
3410  let isSymbolicLink = dir.readSync().isSymbolicLink();
3411  ```
3412