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