js-apis-fileio.md 133.4 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.fileio (File Management)
A
Annie_wang 已提交
2

A
Annie_wang 已提交
3
The **fileio** module provides APIs for file storage and management, including basic file management, directory management, file information statistics, and stream read and write.
Z
zengyawen 已提交
4

A
Annie_wang 已提交
5
> **NOTE**
A
Annie_wang 已提交
6
> 
A
Annie_wang 已提交
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.
A
Annie_wang 已提交
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).
A
Annie_wang 已提交
9

Z
zengyawen 已提交
10 11
## Modules to Import

A
annie_wangli 已提交
12
```js
Z
zengyawen 已提交
13 14 15
import fileio from '@ohos.fileio';
```

A
annie_wangli 已提交
16

A
annie_wangli 已提交
17
## Guidelines
Z
zengyawen 已提交
18

A
Annie_wang 已提交
19
Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows:
A
Annie_wang 已提交
20

A
Annie_wang 已提交
21
**Stage Model**
A
Annie_wang 已提交
22 23

 ```js
A
Annie_wang 已提交
24 25 26
import UIAbility from '@ohos.app.ability.UIAbility';

export default class EntryAbility extends UIAbility {
A
Annie_wang 已提交
27 28
    onWindowStageCreate(windowStage) {
        let context = this.context;
A
Annie_wang 已提交
29
        let pathDir = context.filesDir;
A
Annie_wang 已提交
30 31 32 33
    }
}
 ```

A
Annie_wang 已提交
34
 For details about how to obtain the stage model context, see [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md).
A
Annie_wang 已提交
35

A
Annie_wang 已提交
36
**FA Model**
A
Annie_wang 已提交
37

A
Annie_wang 已提交
38 39
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
A
Annie_wang 已提交
40
 
A
Annie_wang 已提交
41 42
 let context = featureAbility.getContext();
 context.getFilesDir().then((data) => {
A
Annie_wang 已提交
43
      let pathDir = data;
A
Annie_wang 已提交
44 45
 })
 ```
A
Annie_wang 已提交
46 47

 For details about how to obtain the FA model context, see [Contex](js-apis-inner-app-context.md#context).
Z
zengyawen 已提交
48

A
annie_wangli 已提交
49 50 51 52
## fileio.stat

stat(path: string): Promise<Stat>

A
Annie_wang 已提交
53
Obtains file information. This API uses a promise to return the result.
A
annie_wangli 已提交
54

A
annie_wangli 已提交
55 56
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
57 58
**Parameters**

A
Annie_wang 已提交
59 60
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
61
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
62

A
Annie_wang 已提交
63 64
**Return value**

A
Annie_wang 已提交
65 66 67
  | Type                          | Description        |
  | ---------------------------- | ---------- |
  | Promise<[Stat](#stat)> | Promise used to return the file information obtained.|
A
annie_wangli 已提交
68

A
Annie_wang 已提交
69
**Example**
A
Annie_wang 已提交
70

A
annie_wangli 已提交
71
  ```js
A
Annie_wang 已提交
72
  let filePath = pathDir + "test.txt";
A
Annie_wang 已提交
73 74 75 76
  fileio.stat(filePath).then(function (stat) {
      console.info("getFileInfo succeed, the size of file is " + stat.size);
  }).catch(function (err) {
      console.info("getFileInfo failed with error:" + err);
A
annie_wangli 已提交
77 78 79 80 81 82
  });
  ```


## fileio.stat

A
Annie_wang 已提交
83
stat(path: string, callback: AsyncCallback<Stat>): void
A
annie_wangli 已提交
84

A
Annie_wang 已提交
85
Obtains file information. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
86

A
annie_wangli 已提交
87 88
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
89
**Parameters**
A
Annie_wang 已提交
90

A
Annie_wang 已提交
91 92
| Name  | Type                              | Mandatory| Description                          |
| -------- | ---------------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
93
| path     | string                             | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
94
| callback | AsyncCallback<[Stat](#stat)> | Yes  | Callback invoked to return the file information obtained.|
A
annie_wangli 已提交
95

A
Annie_wang 已提交
96
**Example**
A
Annie_wang 已提交
97

A
annie_wangli 已提交
98
  ```js
A
Annie_wang 已提交
99
  fileio.stat(pathDir, function (err, stat) {
A
annie_wangli 已提交
100 101 102 103 104 105 106
      // Example code in Stat
  });
  ```


## fileio.statSync

A
Annie_wang 已提交
107
statSync(path: string): Stat
Z
zengyawen 已提交
108 109 110

Synchronously obtains file information.

A
annie_wangli 已提交
111 112
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
113
**Parameters**
A
Annie_wang 已提交
114

A
Annie_wang 已提交
115 116
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
117
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
118 119


A
Annie_wang 已提交
120
**Return value**
A
Annie_wang 已提交
121

A
Annie_wang 已提交
122 123 124
  | Type           | Description        |
  | ------------- | ---------- |
  | [Stat](#stat) | File information obtained.|
A
annie_wangli 已提交
125

A
Annie_wang 已提交
126
**Example**
A
Annie_wang 已提交
127

A
annie_wangli 已提交
128
  ```js
A
Annie_wang 已提交
129
  let stat = fileio.statSync(pathDir);
A
annie_wangli 已提交
130 131 132 133 134 135 136 137
  // Example code in Stat
  ```


## fileio.opendir

opendir(path: string): Promise<Dir>

A
Annie_wang 已提交
138
Opens a file directory. This API uses a promise to return the result.
A
annie_wangli 已提交
139

A
annie_wangli 已提交
140 141
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
142
**Parameters**
A
Annie_wang 已提交
143

A
Annie_wang 已提交
144 145 146
| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
147

A
Annie_wang 已提交
148
**Return value**
A
Annie_wang 已提交
149

A
Annie_wang 已提交
150 151 152
  | Type                        | Description      |
  | -------------------------- | -------- |
  | Promise<[Dir](#dir)> | Promise used to return the **Dir** object.|
A
annie_wangli 已提交
153

A
Annie_wang 已提交
154
**Example**
A
Annie_wang 已提交
155

A
annie_wangli 已提交
156
  ```js
A
Annie_wang 已提交
157
  let dirPath = pathDir + "/testDir";
A
Annie_wang 已提交
158 159 160 161
  fileio.opendir(dirPath).then(function (dir) {
      console.info("opendir succeed");
  }).catch(function (err) {
      console.info("opendir failed with error:" + err);
A
annie_wangli 已提交
162 163 164 165 166 167 168 169
  });
  ```


## fileio.opendir

opendir(path: string, callback: AsyncCallback<Dir>): void

A
Annie_wang 已提交
170
Opens a file directory. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
171

A
annie_wangli 已提交
172 173
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
174 175
**Parameters**

A
Annie_wang 已提交
176 177 178 179
| Name  | Type                            | Mandatory| Description                          |
| -------- | -------------------------------- | ---- | ------------------------------ |
| path     | string                           | Yes  | Application sandbox path of the directory to open.|
| callback | AsyncCallback<[Dir](#dir)> | Yes  | Callback invoked when the directory is open asynchronously.  |
A
annie_wangli 已提交
180

A
Annie_wang 已提交
181
**Example**
A
Annie_wang 已提交
182

A
annie_wangli 已提交
183
  ```js
A
Annie_wang 已提交
184
  fileio.opendir(pathDir, function (err, dir) { 
A
annie_wangli 已提交
185
      // Example code in Dir struct
A
annie_wangli 已提交
186
      // Use read/readSync/close.
A
annie_wangli 已提交
187 188 189 190 191 192 193
  });
  ```


## fileio.opendirSync

opendirSync(path: string): Dir
Z
zengyawen 已提交
194 195 196

Synchronously opens a directory.

A
annie_wangli 已提交
197 198
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
199

A
Annie_wang 已提交
200 201
**Parameters**

A
Annie_wang 已提交
202 203 204
| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
205

A
Annie_wang 已提交
206
**Return value**
A
Annie_wang 已提交
207

A
Annie_wang 已提交
208 209 210
  | Type         | Description      |
  | ----------- | -------- |
  | [Dir](#dir) | A **Dir** instance corresponding to the directory.|
A
annie_wangli 已提交
211

A
Annie_wang 已提交
212
**Example**
A
Annie_wang 已提交
213

A
annie_wangli 已提交
214
  ```js
A
Annie_wang 已提交
215
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
216 217 218 219 220 221 222 223 224
  // Example code in Dir struct
  // Use read/readSync/close.
  ```


## fileio.access

access(path: string, mode?: number): Promise<void>

A
Annie_wang 已提交
225
Checks whether the current process can access a file. This API uses a promise to return the result.
A
annie_wangli 已提交
226

A
annie_wangli 已提交
227 228
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
229 230
**Parameters**

A
Annie_wang 已提交
231 232
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
233
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
234
| 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. |
A
annie_wangli 已提交
235

A
Annie_wang 已提交
236
**Return value**
A
Annie_wang 已提交
237

A
Annie_wang 已提交
238 239 240
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
241

A
Annie_wang 已提交
242
**Example**
A
Annie_wang 已提交
243

A
annie_wangli 已提交
244
  ```js
A
Annie_wang 已提交
245
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
246
  fileio.access(filePath).then(function () {
A
Annie_wang 已提交
247
      console.info("Access successful");
A
Annie_wang 已提交
248 249
  }).catch(function (err) {
      console.info("access failed with error:" + err);
A
annie_wangli 已提交
250 251 252 253 254 255
  });
  ```


## fileio.access

A
Annie_wang 已提交
256
access(path: string, mode?: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
257

A
Annie_wang 已提交
258
Checks whether the current process can access a file. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
259

A
annie_wangli 已提交
260 261
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
262
**Parameters**
A
Annie_wang 已提交
263

A
Annie_wang 已提交
264 265
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
266
| path     | string                    | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
267
| 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. |
A
Annie_wang 已提交
268
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously checked.                |
A
annie_wangli 已提交
269

A
Annie_wang 已提交
270
**Example**
A
Annie_wang 已提交
271

A
annie_wangli 已提交
272
  ```js
A
Annie_wang 已提交
273 274
  let filePath = pathDir + "/test.txt";
  fileio.access(filePath, function (err) {
A
annie_wangli 已提交
275 276 277 278 279 280 281 282 283 284 285
      // Do something.
  });
  ```


## fileio.accessSync

accessSync(path: string, mode?: number): void

Synchronously checks whether the current process can access the specified file.

A
annie_wangli 已提交
286
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
287

A
Annie_wang 已提交
288
**Parameters**
A
Annie_wang 已提交
289

A
Annie_wang 已提交
290 291
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
292
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
293
| 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. |
A
annie_wangli 已提交
294

A
Annie_wang 已提交
295
**Example**
A
Annie_wang 已提交
296

A
annie_wangli 已提交
297
  ```js
A
Annie_wang 已提交
298
  let filePath = pathDir + "/test.txt";
A
annie_wangli 已提交
299
  try {
A
Annie_wang 已提交
300
      fileio.accessSync(filePath);
A
annie_wangli 已提交
301
  } catch(err) {
A
Annie_wang 已提交
302
      console.info("accessSync failed with error:" + err);
A
annie_wangli 已提交
303 304 305 306 307 308
  }
  ```


## fileio.close<sup>7+</sup>

A
Annie_wang 已提交
309
close(fd: number): Promise&lt;void&gt;
A
annie_wangli 已提交
310

A
Annie_wang 已提交
311
Closes a file. This API uses a promise to return the result.
A
annie_wangli 已提交
312

A
annie_wangli 已提交
313 314
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
315
**Parameters**
A
Annie_wang 已提交
316

A
Annie_wang 已提交
317 318 319
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
320

A
Annie_wang 已提交
321
**Return value**
A
Annie_wang 已提交
322

A
Annie_wang 已提交
323 324 325
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
326

A
Annie_wang 已提交
327
**Example**
A
Annie_wang 已提交
328

A
annie_wangli 已提交
329
  ```js
A
Annie_wang 已提交
330 331
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
332
  fileio.close(fd).then(function () {
A
Annie_wang 已提交
333
      console.info("File closed");
A
Annie_wang 已提交
334 335
  }).catch(function (err) {
      console.info("close file failed with error:" + err);
A
annie_wangli 已提交
336 337 338 339 340 341
  });
  ```


## fileio.close<sup>7+</sup>

A
Annie_wang 已提交
342
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
343

A
Annie_wang 已提交
344
Closes a file. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
345

A
annie_wangli 已提交
346 347
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
348
**Parameters**
A
Annie_wang 已提交
349

A
Annie_wang 已提交
350 351 352 353
  | Name     | Type                       | Mandatory  | Description          |
  | -------- | ------------------------- | ---- | ------------ |
  | fd       | number                    | Yes   | File descriptor of the file to close.|
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is closed asynchronously.|
A
annie_wangli 已提交
354

A
Annie_wang 已提交
355
**Example**
A
Annie_wang 已提交
356

A
annie_wangli 已提交
357
  ```js
A
Annie_wang 已提交
358 359
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
360 361 362 363 364 365 366 367 368
  fileio.close(fd, function (err) {
      // Do something.
  });
  ```


## fileio.closeSync

closeSync(fd: number): void
Z
zengyawen 已提交
369 370 371

Synchronously closes a file.

A
annie_wangli 已提交
372 373
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
374
**Parameters**
A
Annie_wang 已提交
375

A
Annie_wang 已提交
376 377 378
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
379

A
Annie_wang 已提交
380
**Example**
A
Annie_wang 已提交
381

A
annie_wangli 已提交
382
  ```js
A
Annie_wang 已提交
383 384
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
385 386 387 388 389 390
  fileio.closeSync(fd);
  ```


## fileio.copyFile

A
Annie_wang 已提交
391
copyFile(src: string|number, dest: string|number, mode?: number): Promise&lt;void&gt;
A
annie_wangli 已提交
392

A
Annie_wang 已提交
393
Copies a file. This API uses a promise to return the result.
A
annie_wangli 已提交
394

A
annie_wangli 已提交
395 396
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
397
**Parameters**
A
Annie_wang 已提交
398

A
Annie_wang 已提交
399 400 401 402 403
  | Name | Type                        | Mandatory  | Description                                      |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
  | src  | string\|number | Yes   | Path or file descriptor of the file to copy.                     |
  | dest | string\|number | Yes   | Path or file descriptor of the new file.                         |
  | 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.|
A
annie_wangli 已提交
404

A
Annie_wang 已提交
405
**Return value**
A
Annie_wang 已提交
406

A
Annie_wang 已提交
407 408 409
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
410

A
Annie_wang 已提交
411
**Example**
A
Annie_wang 已提交
412

A
annie_wangli 已提交
413
  ```js
A
Annie_wang 已提交
414 415
  let srcPath = pathDir + "srcDir/test.txt";
  let dstPath = pathDir + "dstDir/test.txt";
A
Annie_wang 已提交
416
  fileio.copyFile(srcPath, dstPath).then(function () {
A
Annie_wang 已提交
417
      console.info("File copied");
A
Annie_wang 已提交
418 419
  }).catch(function (err) {
      console.info("copyFile failed with error:" + err);
A
annie_wangli 已提交
420 421 422 423 424 425
  });
  ```


## fileio.copyFile

A
Annie_wang 已提交
426
copyFile(src: string|number, dest: string|number, mode: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
427

A
Annie_wang 已提交
428
Copies a file. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
429

A
annie_wangli 已提交
430 431
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
432
**Parameters**
A
Annie_wang 已提交
433

A
Annie_wang 已提交
434 435 436 437 438 439
  | Name     | Type                        | Mandatory  | Description                                      |
  | -------- | -------------------------- | ---- | ---------------------------------------- |
  | src      | string\|number | Yes   | Path or file descriptor of the file to copy.                     |
  | dest     | string\|number | Yes   | Path or file descriptor of the new file.                         |
  | 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.|
  | callback | AsyncCallback&lt;void&gt;  | Yes   | Callback invoked when the file is copied asynchronously.                            |
A
annie_wangli 已提交
440

A
Annie_wang 已提交
441
**Example**
A
Annie_wang 已提交
442

A
annie_wangli 已提交
443
  ```js
A
Annie_wang 已提交
444 445 446
  let srcPath = pathDir + "srcDir/test.txt";
  let dstPath = pathDir + "dstDir/test.txt";
  fileio.copyFile(srcPath, dstPath, function (err) {
A
annie_wangli 已提交
447 448 449 450 451 452 453
      // Do something.
  });
  ```


## fileio.copyFileSync

A
Annie_wang 已提交
454
copyFileSync(src: string|number, dest: string|number, mode?: number): void
Z
zengyawen 已提交
455 456 457

Synchronously copies a file.

A
annie_wangli 已提交
458 459
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
460
**Parameters**
A
Annie_wang 已提交
461

A
Annie_wang 已提交
462 463 464 465 466
  | Name | Type                        | Mandatory  | Description                                      |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
  | src  | string\|number | Yes   | Path or file descriptor of the file to copy.                     |
  | dest | string\|number | Yes   | Path or file descriptor of the new file.                         |
  | 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.|
A
annie_wangli 已提交
467

A
Annie_wang 已提交
468
**Example**
A
Annie_wang 已提交
469

A
annie_wangli 已提交
470
  ```js
A
Annie_wang 已提交
471 472 473
  let srcPath = pathDir + "srcDir/test.txt";
  let dstPath = pathDir + "dstDir/test.txt";
  fileio.copyFileSync(srcPath, dstPath);
A
annie_wangli 已提交
474 475 476 477 478
  ```


## fileio.mkdir

A
Annie_wang 已提交
479
mkdir(path: string, mode?: number): Promise&lt;void&gt;
A
annie_wangli 已提交
480

A
Annie_wang 已提交
481
Creates a directory. This API uses a promise to return the result.
A
annie_wangli 已提交
482

A
annie_wangli 已提交
483
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
484

A
Annie_wang 已提交
485
**Parameters**
A
Annie_wang 已提交
486

A
Annie_wang 已提交
487 488
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
489
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
490
| 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. |
A
annie_wangli 已提交
491

A
Annie_wang 已提交
492
**Return value**
A
Annie_wang 已提交
493

A
Annie_wang 已提交
494 495 496
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
497

A
Annie_wang 已提交
498
**Example**
A
Annie_wang 已提交
499

A
annie_wangli 已提交
500
  ```js
A
Annie_wang 已提交
501
  let dirPath = pathDir + '/testDir';
A
Annie_wang 已提交
502
  fileio.mkdir(dirPath).then(function () {
A
Annie_wang 已提交
503
      console.info("Directory created");
A
Annie_wang 已提交
504 505
  }).catch(function (error) {
      console.info("mkdir failed with error:" + error);
A
annie_wangli 已提交
506 507 508 509 510 511
  });
  ```


## fileio.mkdir

A
annie_wangli 已提交
512
mkdir(path: string, mode: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
513

A
Annie_wang 已提交
514
Creates a directory. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
515

A
annie_wangli 已提交
516 517
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
518
**Parameters**
A
Annie_wang 已提交
519

A
Annie_wang 已提交
520 521
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
522
| path     | string                    | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
523
| 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. |
A
Annie_wang 已提交
524
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is created asynchronously.                            |
A
annie_wangli 已提交
525

A
Annie_wang 已提交
526
**Example**
A
Annie_wang 已提交
527

A
annie_wangli 已提交
528
  ```js
A
Annie_wang 已提交
529
  let dirPath = pathDir + '/testDir';
A
Annie_wang 已提交
530
  fileio.mkdir(dirPath, function (err) {
A
Annie_wang 已提交
531
    console.info("Directory created");
A
annie_wangli 已提交
532 533 534 535 536 537
  });
  ```


## fileio.mkdirSync

A
annie_wangli 已提交
538
mkdirSync(path: string, mode?: number): void
Z
zengyawen 已提交
539 540 541

Synchronously creates a directory.

A
annie_wangli 已提交
542 543
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
544
**Parameters**
A
Annie_wang 已提交
545

A
Annie_wang 已提交
546 547
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
548
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
549
| 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. |
A
annie_wangli 已提交
550

A
Annie_wang 已提交
551
**Example**
A
Annie_wang 已提交
552

A
annie_wangli 已提交
553
  ```js
A
Annie_wang 已提交
554 555
  let dirPath = path + '/testDir';
  fileio.mkdirSync(dirPath);
A
annie_wangli 已提交
556 557 558 559 560 561 562
  ```


## fileio.open<sup>7+</sup>

open(path: string, flags?: number, mode?: number): Promise&lt;number&gt;

A
Annie_wang 已提交
563
Opens a file. This API uses a promise to return the result.
A
annie_wangli 已提交
564

A
annie_wangli 已提交
565 566
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
567
**Parameters**
A
Annie_wang 已提交
568

A
Annie_wang 已提交
569 570
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
571
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
572 573
| 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.|
| mode   | number | No  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>- **0o666**: The owner, user group, and other users 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. |
A
annie_wangli 已提交
574

A
Annie_wang 已提交
575
**Return value**
A
Annie_wang 已提交
576

A
Annie_wang 已提交
577 578 579
  | Type                   | Description         |
  | --------------------- | ----------- |
  | Promise&lt;number&gt; | Promise used to return the file descriptor of the file opened.|
A
annie_wangli 已提交
580

A
Annie_wang 已提交
581
**Example**
A
Annie_wang 已提交
582

A
annie_wangli 已提交
583
  ```js
A
Annie_wang 已提交
584
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
585
  fileio.open(filePath, 0o1, 0o0200).then(function (number) {
A
Annie_wang 已提交
586
      console.info("File opened");
A
Annie_wang 已提交
587 588
  }).catch(function (err) {
      console.info("open file failed with error:" + err);
A
annie_wangli 已提交
589 590 591 592 593 594 595 596
  });
  ```


## fileio.open<sup>7+</sup>

open(path: string, flags: number, mode: number, callback: AsyncCallback&lt;number&gt;): void

A
Annie_wang 已提交
597
Opens a file. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
598

A
annie_wangli 已提交
599 600
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
601
**Parameters**
A
Annie_wang 已提交
602

A
Annie_wang 已提交
603 604
| Name  | Type                           | Mandatory| Description                                                        |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
605
| path     | string                          | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
606 607
| 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.|
| mode     | number                          | No  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>- **0o666**: The owner, user group, and other users 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. |
A
Annie_wang 已提交
608
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback invoked when the file is open asynchronously.                                    |
A
annie_wangli 已提交
609

A
Annie_wang 已提交
610
**Example**
A
Annie_wang 已提交
611

A
annie_wangli 已提交
612
  ```js
A
Annie_wang 已提交
613
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
614
  fileio.open(filePath, 0, function (err, fd) {
A
annie_wangli 已提交
615 616 617 618 619 620 621
      // Do something.
  });
  ```


## fileio.openSync

A
Annie_wang 已提交
622
openSync(path: string, flags?: number, mode?: number): number
Z
zengyawen 已提交
623 624 625

Synchronously opens a file.

A
annie_wangli 已提交
626 627
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
628
**Parameters**
A
Annie_wang 已提交
629

A
Annie_wang 已提交
630 631
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
632
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
633 634
| 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.|
| mode   | number | No  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>- **0o666**: The owner, user group, and other users 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. |
A
annie_wangli 已提交
635

A
Annie_wang 已提交
636
**Return value**
A
Annie_wang 已提交
637

A
Annie_wang 已提交
638 639 640
  | Type    | Description         |
  | ------ | ----------- |
  | number | File descriptor of the file opened.|
A
annie_wangli 已提交
641

A
Annie_wang 已提交
642
**Example**
A
Annie_wang 已提交
643

A
annie_wangli 已提交
644
  ```js
A
Annie_wang 已提交
645 646
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o102, 0o640);
A
Annie_wang 已提交
647 648
  ```
  ```js
A
Annie_wang 已提交
649 650
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o102, 0o666);
A
Annie_wang 已提交
651
  fileio.writeSync(fd, 'hello world');
A
Annie_wang 已提交
652
  let fd1 = fileio.openSync(filePath, 0o2002);
A
Annie_wang 已提交
653 654 655
  fileio.writeSync(fd1, 'hello world');
  let num = fileio.readSync(fd1, new ArrayBuffer(4096), {position: 0});
  console.info("num == " + num);
A
annie_wangli 已提交
656 657 658 659 660
  ```


## fileio.read

A
Annie_wang 已提交
661
read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): Promise&lt;ReadOut&gt;
A
annie_wangli 已提交
662

A
Annie_wang 已提交
663
Reads data from a file. This API uses a promise to return the result.
A
annie_wangli 已提交
664

A
annie_wangli 已提交
665 666
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
667
**Parameters**
A
Annie_wang 已提交
668

A
Annie_wang 已提交
669 670 671 672
| Name | Type       | Mandatory| Description                                                        |
| ------- | ----------- | ---- | ------------------------------------------------------------ |
| fd      | number      | Yes  | File descriptor of the file to read.                                    |
| buffer  | ArrayBuffer | Yes  | Buffer used to store the file data read.                          |
A
Annie_wang 已提交
673
| 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|
A
Annie_wang 已提交
674 675

**Return value**
A
annie_wangli 已提交
676

A
Annie_wang 已提交
677 678 679
  | Type                                | Description    |
  | ---------------------------------- | ------ |
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
680

A
Annie_wang 已提交
681
**Example**
A
Annie_wang 已提交
682

A
annie_wangli 已提交
683
  ```js
A
Annie_wang 已提交
684 685
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
A
annie_wangli 已提交
686
  let buf = new ArrayBuffer(4096);
A
Annie_wang 已提交
687
  fileio.read(fd, buf).then(function (readOut) {
A
Annie_wang 已提交
688
      console.info("Read file data successfully");
A
Annie_wang 已提交
689
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
Annie_wang 已提交
690 691
  }).catch(function (err) {
      console.info("read file data failed with error:" + err);
A
annie_wangli 已提交
692 693 694 695 696 697
  });
  ```


## fileio.read

A
Annie_wang 已提交
698
read(fd: number, buffer: ArrayBuffer, options: { offset?: number; length?: number; position?: number; }, callback: AsyncCallback&lt;ReadOut&gt;): void
A
annie_wangli 已提交
699

A
Annie_wang 已提交
700
Reads data from a file. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
701

A
annie_wangli 已提交
702 703
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
704
**Parameters**
A
Annie_wang 已提交
705

A
Annie_wang 已提交
706 707 708 709 710 711
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                                   | Yes   | File descriptor of the file to read.                            |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file data read.                       |
  | 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 |
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously.                            |
A
annie_wangli 已提交
712

A
Annie_wang 已提交
713
**Example**
A
Annie_wang 已提交
714

A
annie_wangli 已提交
715
  ```js
A
Annie_wang 已提交
716 717
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
A
annie_wangli 已提交
718 719
  let buf = new ArrayBuffer(4096);
  fileio.read(fd, buf, function (err, readOut) {
A
Annie_wang 已提交
720
      if (readOut) {
A
Annie_wang 已提交
721
          console.info("Read file data successfully");
A
Annie_wang 已提交
722
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
723 724 725 726 727 728 729
      }
  });
  ```


## fileio.readSync

A
Annie_wang 已提交
730
readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): number
Z
zengyawen 已提交
731 732 733

Synchronously reads data from a file.

A
annie_wangli 已提交
734 735
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
736
**Parameters**
A
Annie_wang 已提交
737

A
Annie_wang 已提交
738 739 740 741 742
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | fd      | number      | Yes   | File descriptor of the file to read.                            |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file data read.                       |
  | 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 |
A
annie_wangli 已提交
743

A
Annie_wang 已提交
744
**Return value**
A
Annie_wang 已提交
745

A
Annie_wang 已提交
746 747 748
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data read.|
A
annie_wangli 已提交
749

A
Annie_wang 已提交
750
**Example**
A
Annie_wang 已提交
751

A
annie_wangli 已提交
752
  ```js
A
Annie_wang 已提交
753 754
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
A
annie_wangli 已提交
755 756 757 758 759 760 761 762 763
  let buf = new ArrayBuffer(4096);
  let num = fileio.readSync(fd, buf);
  ```


## fileio.rmdir<sup>7+</sup>

rmdir(path: string): Promise&lt;void&gt;

A
Annie_wang 已提交
764
Deletes a directory. This API uses a promise to return the result.
A
annie_wangli 已提交
765

A
annie_wangli 已提交
766 767
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
768
**Parameters**
A
Annie_wang 已提交
769

A
Annie_wang 已提交
770 771
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
772
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
773

A
Annie_wang 已提交
774
**Return value**
A
Annie_wang 已提交
775

A
Annie_wang 已提交
776 777 778
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
779

A
Annie_wang 已提交
780
**Example**
A
Annie_wang 已提交
781

A
annie_wangli 已提交
782
  ```js
A
Annie_wang 已提交
783
  let dirPath = pathDir + '/testDir';
A
Annie_wang 已提交
784
  fileio.rmdir(dirPath).then(function () {
A
Annie_wang 已提交
785
      console.info("Directory deleted");
A
Annie_wang 已提交
786 787
  }).catch(function (err) {
      console.info("rmdir failed with error:" + err);
A
annie_wangli 已提交
788 789 790 791 792 793
  });
  ```


## fileio.rmdir<sup>7+</sup>

A
Annie_wang 已提交
794
rmdir(path: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
795

A
Annie_wang 已提交
796
Deletes a directory. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
797

A
annie_wangli 已提交
798 799
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
800
**Parameters**
A
Annie_wang 已提交
801

A
Annie_wang 已提交
802 803
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
804
| path     | string                    | Yes  | Application sandbox path of the directory.|
A
Annie_wang 已提交
805
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is deleted asynchronously.  |
A
annie_wangli 已提交
806

A
Annie_wang 已提交
807
**Example**
A
Annie_wang 已提交
808

A
annie_wangli 已提交
809
  ```js
A
Annie_wang 已提交
810
  let dirPath = pathDir + '/testDir';
A
Annie_wang 已提交
811
  fileio.rmdir(dirPath, function (err) {
A
annie_wangli 已提交
812
      // Do something.
A
Annie_wang 已提交
813
      console.info("Directory deleted");
A
annie_wangli 已提交
814 815 816 817
  });
  ```


A
annie_wangli 已提交
818
## fileio.rmdirSync<sup>7+</sup>
A
annie_wangli 已提交
819

A
annie_wangli 已提交
820
rmdirSync(path: string): void
A
annie_wangli 已提交
821 822 823

Synchronously deletes a directory.

A
annie_wangli 已提交
824 825
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
826
**Parameters**
A
Annie_wang 已提交
827

A
Annie_wang 已提交
828 829
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
830
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
831

A
Annie_wang 已提交
832
**Example**
A
Annie_wang 已提交
833

A
annie_wangli 已提交
834
  ```js
A
Annie_wang 已提交
835 836
  let dirPath = pathDir + '/testDir';
  fileio.rmdirSync(dirPath);
A
annie_wangli 已提交
837 838 839 840 841
  ```


## fileio.unlink

A
Annie_wang 已提交
842
unlink(path: string): Promise&lt;void&gt;
A
annie_wangli 已提交
843

A
Annie_wang 已提交
844
Deletes a file. This API uses a promise to return the result.
A
annie_wangli 已提交
845

A
annie_wangli 已提交
846 847
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
848
**Parameters**
A
Annie_wang 已提交
849

A
Annie_wang 已提交
850 851
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
852
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
853

A
Annie_wang 已提交
854
**Return value**
A
Annie_wang 已提交
855

A
Annie_wang 已提交
856 857 858
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
859

A
Annie_wang 已提交
860
**Example**
A
Annie_wang 已提交
861

A
annie_wangli 已提交
862
  ```js
A
Annie_wang 已提交
863
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
864
  fileio.unlink(filePath).then(function () {
A
Annie_wang 已提交
865
      console.info("File deleted");
A
Annie_wang 已提交
866 867
  }).catch(function (error) {
      console.info("remove file failed with error:" + error);
A
annie_wangli 已提交
868 869 870 871 872 873
  });
  ```


## fileio.unlink

A
Annie_wang 已提交
874
unlink(path: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
875

A
Annie_wang 已提交
876
Deletes a file. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
877

A
annie_wangli 已提交
878 879
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
880
**Parameters**
A
Annie_wang 已提交
881

A
Annie_wang 已提交
882 883
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
884
| path     | string                    | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
885
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is deleted asynchronously.  |
A
annie_wangli 已提交
886

A
Annie_wang 已提交
887
**Example**
A
Annie_wang 已提交
888

A
annie_wangli 已提交
889
  ```js
A
Annie_wang 已提交
890
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
891
  fileio.unlink(filePath, function (err) {
A
Annie_wang 已提交
892
      console.info("File deleted");
A
annie_wangli 已提交
893 894 895 896 897 898 899
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void
Z
zengyawen 已提交
900 901 902

Synchronously deletes a file.

A
annie_wangli 已提交
903 904
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
905
**Parameters**
A
Annie_wang 已提交
906

A
Annie_wang 已提交
907 908
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
909
| path   | string | Yes  | Application sandbox path of the file.|
Z
zengyawen 已提交
910

A
Annie_wang 已提交
911
**Example**
A
Annie_wang 已提交
912

A
annie_wangli 已提交
913
  ```js
A
Annie_wang 已提交
914 915
  let filePath = pathDir + "/test.txt";
  fileio.unlinkSync(filePath);
A
annie_wangli 已提交
916
  ```
Z
zengyawen 已提交
917 918


A
annie_wangli 已提交
919
## fileio.write
Z
zengyawen 已提交
920

A
Annie_wang 已提交
921
write(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise&lt;number&gt;
Z
zengyawen 已提交
922

A
Annie_wang 已提交
923
Writes data into a file. This API uses a promise to return the result.
Z
zengyawen 已提交
924

A
annie_wangli 已提交
925 926
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
927
**Parameters**
A
Annie_wang 已提交
928

A
Annie_wang 已提交
929 930 931 932 933
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer  | ArrayBuffer\|string | Yes   | Data to write. It can be a string or data from a buffer.                    |
  | 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|
Z
zengyawen 已提交
934

A
Annie_wang 已提交
935
**Return value**
A
Annie_wang 已提交
936

A
Annie_wang 已提交
937 938 939
  | Type                   | Description      |
  | --------------------- | -------- |
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
Z
zengyawen 已提交
940

A
Annie_wang 已提交
941
**Example**
A
Annie_wang 已提交
942

A
annie_wangli 已提交
943
  ```js
A
Annie_wang 已提交
944 945
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
A
Annie_wang 已提交
946 947 948 949
  fileio.write(fd, "hello, world").then(function (number) {
       console.info("write data to file succeed and size is:" + number);
  }).catch(function (err) {
      console.info("write data to file failed with error:" + err);
A
annie_wangli 已提交
950 951
  });
  ```
Z
zengyawen 已提交
952 953


A
annie_wangli 已提交
954
## fileio.write
Z
zengyawen 已提交
955

A
Annie_wang 已提交
956
write(fd: number, buffer: ArrayBuffer|string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
957

A
Annie_wang 已提交
958
Writes data into a file. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
959

A
annie_wangli 已提交
960 961
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
962
**Parameters**
A
Annie_wang 已提交
963

A
Annie_wang 已提交
964 965 966 967 968 969
  | Name     | Type                             | Mandatory  | Description                                      |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer   | ArrayBuffer\|string | Yes   | Data to write. It can be a string or data from a buffer.                    |
  | 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|
  | callback | AsyncCallback&lt;number&gt;     | Yes   | Callback invoked when the data is written asynchronously.                      |
Z
zengyawen 已提交
970

A
Annie_wang 已提交
971
**Example**
A
Annie_wang 已提交
972

A
annie_wangli 已提交
973
  ```js
A
Annie_wang 已提交
974 975
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
A
annie_wangli 已提交
976
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
A
Annie_wang 已提交
977
      if (bytesWritten) {
A
Annie_wang 已提交
978
         console.info("write data to file succeed and size is:" + bytesWritten);
A
annie_wangli 已提交
979 980 981
      }
  });
  ```
Z
zengyawen 已提交
982 983


A
annie_wangli 已提交
984
## fileio.writeSync
Z
zengyawen 已提交
985

A
Annie_wang 已提交
986
writeSync(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number
Z
zengyawen 已提交
987

A
annie_wangli 已提交
988
Synchronously writes data into a file.
Z
zengyawen 已提交
989

A
annie_wangli 已提交
990 991
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
992
**Parameters**
A
Annie_wang 已提交
993

A
Annie_wang 已提交
994 995 996 997 998
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer  | ArrayBuffer\|string | Yes   | Data to write. It can be a string or data from a buffer.                    |
  | 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|
Z
zengyawen 已提交
999

A
Annie_wang 已提交
1000
**Return value**
A
Annie_wang 已提交
1001

A
Annie_wang 已提交
1002 1003 1004
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data written in the file.|
Z
zengyawen 已提交
1005

A
Annie_wang 已提交
1006
**Example**
A
Annie_wang 已提交
1007

A
annie_wangli 已提交
1008
  ```js
A
Annie_wang 已提交
1009 1010
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
A
annie_wangli 已提交
1011 1012
  let num = fileio.writeSync(fd, "hello, world");
  ```
Z
zengyawen 已提交
1013 1014


A
annie_wangli 已提交
1015
## fileio.hash
Z
zengyawen 已提交
1016

A
annie_wangli 已提交
1017
hash(path: string, algorithm: string): Promise&lt;string&gt;
Z
zengyawen 已提交
1018

A
Annie_wang 已提交
1019
Calculates the hash value of a file. This API uses a promise to return the result.
Z
zengyawen 已提交
1020

A
annie_wangli 已提交
1021 1022
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1023
**Parameters**
A
Annie_wang 已提交
1024

A
Annie_wang 已提交
1025 1026
| Name   | Type  | Mandatory| Description                                                        |
| --------- | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1027
| path      | string | Yes  | Application sandbox path of the file.                            |
A
Annie_wang 已提交
1028
| algorithm | string | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
Z
zengyawen 已提交
1029

A
Annie_wang 已提交
1030
**Return value**
A
Annie_wang 已提交
1031

A
Annie_wang 已提交
1032 1033 1034
  | Type                   | Description                        |
  | --------------------- | -------------------------- |
  | 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.|
Z
zengyawen 已提交
1035

A
Annie_wang 已提交
1036
**Example**
A
Annie_wang 已提交
1037

A
annie_wangli 已提交
1038
  ```js
A
Annie_wang 已提交
1039
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1040 1041 1042 1043
  fileio.hash(filePath, "sha256").then(function (str) {
      console.info("calculate file hash succeed:" + str);
  }).catch(function (err) {
      console.info("calculate file hash failed with error:" + err);
A
annie_wangli 已提交
1044 1045
  });
  ```
Z
zengyawen 已提交
1046 1047


A
annie_wangli 已提交
1048
## fileio.hash
Z
zengyawen 已提交
1049

A
annie_wangli 已提交
1050
hash(path: string, algorithm: string, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1051

A
Annie_wang 已提交
1052
Calculates the hash value of a file. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
1053

A
annie_wangli 已提交
1054 1055
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1056
**Parameters**
A
Annie_wang 已提交
1057

A
Annie_wang 已提交
1058 1059
| Name   | Type                       | Mandatory| Description                                                        |
| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1060
| path      | string                      | Yes  | Application sandbox path of the file.                            |
A
Annie_wang 已提交
1061
| algorithm | string                      | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
A
Annie_wang 已提交
1062
| 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.|
Z
zengyawen 已提交
1063

A
Annie_wang 已提交
1064
**Example**
A
Annie_wang 已提交
1065

A
annie_wangli 已提交
1066
  ```js
A
Annie_wang 已提交
1067
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1068
  fileio.hash(filePath, "sha256", function (err, hashStr) {
A
Annie_wang 已提交
1069
      if (hashStr) {
A
Annie_wang 已提交
1070
          console.info("calculate file hash succeed:" + hashStr);
A
annie_wangli 已提交
1071 1072 1073
      }
  });
  ```
Z
zengyawen 已提交
1074 1075


A
annie_wangli 已提交
1076
## fileio.chmod<sup>7+</sup>
Z
zengyawen 已提交
1077

A
Annie_wang 已提交
1078
chmod(path: string, mode: number): Promise&lt;void&gt;
Z
zengyawen 已提交
1079

A
Annie_wang 已提交
1080
Changes file permissions. This API uses a promise to return the result.
Z
zengyawen 已提交
1081

A
annie_wangli 已提交
1082 1083
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1084
**Parameters**
A
Annie_wang 已提交
1085

A
Annie_wang 已提交
1086 1087
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1088
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1089
| 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. |
Z
zengyawen 已提交
1090

A
Annie_wang 已提交
1091
**Return value**
A
Annie_wang 已提交
1092

A
Annie_wang 已提交
1093 1094 1095
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1096

A
Annie_wang 已提交
1097
**Example**
A
Annie_wang 已提交
1098

A
annie_wangli 已提交
1099
  ```js
A
Annie_wang 已提交
1100
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1101
  fileio.chmod(filePath, 0o700).then(function () {
A
Annie_wang 已提交
1102
      console.info("File permissions changed");
A
Annie_wang 已提交
1103 1104
  }).catch(function (err) {
      console.info("chmod failed with error:" + err);
A
annie_wangli 已提交
1105 1106
  });
  ```
Z
zengyawen 已提交
1107 1108


A
annie_wangli 已提交
1109
## fileio.chmod<sup>7+</sup>
Z
zengyawen 已提交
1110

A
annie_wangli 已提交
1111
chmod(path: string, mode: number, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
1112

A
Annie_wang 已提交
1113
Changes file permissions. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
1114

A
annie_wangli 已提交
1115 1116
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1117
**Parameters**
A
Annie_wang 已提交
1118

A
Annie_wang 已提交
1119 1120
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1121
| path     | string                    | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1122
| 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. |
A
Annie_wang 已提交
1123
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file permissions are changed asynchronously.                                |
Z
zengyawen 已提交
1124

A
Annie_wang 已提交
1125
**Example**
A
Annie_wang 已提交
1126

A
annie_wangli 已提交
1127
  ```js
A
Annie_wang 已提交
1128 1129
  let filePath = pathDir + "/test.txt";
  fileio.chmod(filePath, 0o700, function (err) {
A
annie_wangli 已提交
1130 1131 1132
      // Do something.
  });
  ```
Z
zengyawen 已提交
1133 1134


A
annie_wangli 已提交
1135
## fileio.chmodSync<sup>7+</sup>
Z
zengyawen 已提交
1136

A
annie_wangli 已提交
1137
chmodSync(path: string, mode: number): void
Z
zengyawen 已提交
1138

A
Annie_wang 已提交
1139
Synchronously changes file permissions.
Z
zengyawen 已提交
1140

A
annie_wangli 已提交
1141 1142
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1143
**Parameters**
A
Annie_wang 已提交
1144

A
Annie_wang 已提交
1145 1146
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1147
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1148
| 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. |
Z
zengyawen 已提交
1149

A
Annie_wang 已提交
1150
**Example**
A
Annie_wang 已提交
1151

A
annie_wangli 已提交
1152
  ```js
A
Annie_wang 已提交
1153 1154
  let filePath = pathDir + "/test.txt";
  fileio.chmodSync(filePath, 0o700);
A
annie_wangli 已提交
1155
  ```
Z
zengyawen 已提交
1156 1157


A
annie_wangli 已提交
1158
## fileio.fstat<sup>7+</sup>
Z
zengyawen 已提交
1159

A
annie_wangli 已提交
1160
fstat(fd: number): Promise&lt;Stat&gt;
Z
zengyawen 已提交
1161

A
Annie_wang 已提交
1162
Obtains file information based on the file descriptor. This API uses a promise to return the result.
Z
zengyawen 已提交
1163

A
annie_wangli 已提交
1164 1165
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1166
**Parameters**
A
Annie_wang 已提交
1167

A
Annie_wang 已提交
1168 1169 1170
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | Descriptor of the target file.|
Z
zengyawen 已提交
1171

A
Annie_wang 已提交
1172
**Return value**
A
Annie_wang 已提交
1173

A
Annie_wang 已提交
1174 1175 1176
  | Type                          | Description        |
  | ---------------------------- | ---------- |
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information obtained.|
Z
zengyawen 已提交
1177

A
Annie_wang 已提交
1178
**Example**
A
Annie_wang 已提交
1179

A
annie_wangli 已提交
1180
  ```js
A
Annie_wang 已提交
1181 1182
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1183 1184 1185 1186
  fileio.fstat(fd).then(function (stat) {
      console.info("fstat succeed, the size of file is " + stat.size);
  }).catch(function (err) {
      console.info("fstat failed with error:" + err);
A
annie_wangli 已提交
1187 1188
  });
  ```
Z
zengyawen 已提交
1189 1190


A
annie_wangli 已提交
1191
## fileio.fstat<sup>7+</sup>
Z
zengyawen 已提交
1192

A
annie_wangli 已提交
1193
fstat(fd: number, callback: AsyncCallback&lt;Stat&gt;): void
Z
zengyawen 已提交
1194

A
Annie_wang 已提交
1195
Obtains file information based on the file descriptor. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
1196

A
annie_wangli 已提交
1197 1198
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1199
**Parameters**
A
Annie_wang 已提交
1200

A
Annie_wang 已提交
1201 1202 1203 1204
  | Name     | Type                                | Mandatory  | Description              |
  | -------- | ---------------------------------- | ---- | ---------------- |
  | fd       | number                             | Yes   | File descriptor of the target file.    |
  | callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes   | Callback invoked to return the file information obtained.|
Z
zengyawen 已提交
1205

A
Annie_wang 已提交
1206
**Example**
A
Annie_wang 已提交
1207

A
annie_wangli 已提交
1208
  ```js
A
Annie_wang 已提交
1209 1210
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1211 1212 1213 1214
  fileio.fstat(fd, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
1215 1216


A
annie_wangli 已提交
1217
## fileio.fstatSync<sup>7+</sup>
Z
zengyawen 已提交
1218

A
annie_wangli 已提交
1219
fstatSync(fd: number): Stat
Z
zengyawen 已提交
1220

A
Annie_wang 已提交
1221
Synchronously obtains file information based on the file descriptor.
Z
zengyawen 已提交
1222

A
annie_wangli 已提交
1223 1224
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1225
**Parameters**
A
Annie_wang 已提交
1226

A
Annie_wang 已提交
1227 1228 1229
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1230

A
Annie_wang 已提交
1231
**Return value**
A
Annie_wang 已提交
1232

A
Annie_wang 已提交
1233 1234 1235
  | Type           | Description        |
  | ------------- | ---------- |
  | [Stat](#stat) | File information obtained.|
Z
zengyawen 已提交
1236

A
Annie_wang 已提交
1237
**Example**
A
Annie_wang 已提交
1238

A
annie_wangli 已提交
1239
  ```js
A
Annie_wang 已提交
1240 1241
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1242 1243
  let stat = fileio.fstatSync(fd);
  ```
Z
zengyawen 已提交
1244 1245


A
annie_wangli 已提交
1246
## fileio.ftruncate<sup>7+</sup>
Z
zengyawen 已提交
1247

A
annie_wangli 已提交
1248
ftruncate(fd: number, len?: number): Promise&lt;void&gt;
Z
zengyawen 已提交
1249

A
Annie_wang 已提交
1250
Truncates a file based on the file descriptor. This API uses a promise to return the result.
Z
zengyawen 已提交
1251

A
annie_wangli 已提交
1252 1253
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1254
**Parameters**
A
Annie_wang 已提交
1255

A
Annie_wang 已提交
1256 1257 1258 1259
  | Name | Type    | Mandatory  | Description              |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | Yes   | File descriptor of the file to truncate.    |
  | len  | number | No   | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1260

A
Annie_wang 已提交
1261
**Return value**
A
Annie_wang 已提交
1262

A
Annie_wang 已提交
1263 1264 1265
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1266

A
Annie_wang 已提交
1267
**Example**
A
Annie_wang 已提交
1268

A
annie_wangli 已提交
1269
  ```js
A
Annie_wang 已提交
1270 1271
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1272
  fileio.ftruncate(fd, 5).then(function (err) {    
A
Annie_wang 已提交
1273
      console.info("File truncated");
A
Annie_wang 已提交
1274 1275
  }).catch(function (err) {
      console.info("truncate file failed with error:" + err);
A
annie_wangli 已提交
1276 1277
  });
  ```
Z
zengyawen 已提交
1278 1279


A
annie_wangli 已提交
1280
## fileio.ftruncate<sup>7+</sup>
Z
zengyawen 已提交
1281

A
Annie_wang 已提交
1282
ftruncate(fd: number, len?: number, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
1283

A
Annie_wang 已提交
1284
Truncates a file based on the file descriptor. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
1285

A
annie_wangli 已提交
1286 1287
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1288
**Parameters**
A
Annie_wang 已提交
1289

A
Annie_wang 已提交
1290 1291 1292 1293 1294
  | Name     | Type                       | Mandatory  | Description              |
  | -------- | ------------------------- | ---- | ---------------- |
  | fd       | number                    | Yes   | File descriptor of the file to truncate.    |
  | len      | number                    | No   | File length, in bytes, after truncation.|
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback that returns no value. |
Z
zengyawen 已提交
1295

A
Annie_wang 已提交
1296
**Example**
A
Annie_wang 已提交
1297

A
annie_wangli 已提交
1298
  ```js
A
Annie_wang 已提交
1299 1300
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1301
  let len = 5;
A
Annie_wang 已提交
1302
  fileio.ftruncate(fd, 5, function (err) {
A
annie_wangli 已提交
1303 1304 1305
      // Do something.
  });
  ```
Z
zengyawen 已提交
1306 1307


A
annie_wangli 已提交
1308
## fileio.ftruncateSync<sup>7+</sup>
Z
zengyawen 已提交
1309

A
annie_wangli 已提交
1310
ftruncateSync(fd: number, len?: number): void
Z
zengyawen 已提交
1311

A
annie_wangli 已提交
1312
Synchronously truncates a file based on the file descriptor.
Z
zengyawen 已提交
1313

A
annie_wangli 已提交
1314 1315
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1316
**Parameters**
A
Annie_wang 已提交
1317

A
Annie_wang 已提交
1318 1319 1320 1321
  | Name | Type    | Mandatory  | Description              |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | Yes   | File descriptor of the file to truncate.    |
  | len  | number | No   | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1322

A
Annie_wang 已提交
1323
**Example**
A
Annie_wang 已提交
1324

A
annie_wangli 已提交
1325
  ```js
A
Annie_wang 已提交
1326 1327
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1328
  let len = 5;
P
Peter_1988 已提交
1329
  fileio.ftruncateSync(fd, len);
A
annie_wangli 已提交
1330
  ```
Z
zengyawen 已提交
1331 1332


A
annie_wangli 已提交
1333
## fileio.truncate<sup>7+</sup>
Z
zengyawen 已提交
1334

A
annie_wangli 已提交
1335
truncate(path: string, len?: number): Promise&lt;void&gt;
Z
zengyawen 已提交
1336

A
Annie_wang 已提交
1337
Truncates a file based on the file path. This API uses a promise to return the result.
Z
zengyawen 已提交
1338

A
annie_wangli 已提交
1339 1340
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1341
**Parameters**
A
Annie_wang 已提交
1342

A
Annie_wang 已提交
1343 1344
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
A
Annie_wang 已提交
1345
| path   | string | Yes  | Application sandbox path of the file to truncate.      |
A
Annie_wang 已提交
1346
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1347

A
Annie_wang 已提交
1348
**Return value**
A
Annie_wang 已提交
1349

A
Annie_wang 已提交
1350 1351 1352
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1353

A
Annie_wang 已提交
1354
**Example**
A
Annie_wang 已提交
1355

A
annie_wangli 已提交
1356
  ```js
A
Annie_wang 已提交
1357
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1358
  let len = 5;
A
Annie_wang 已提交
1359
  fileio.truncate(filePath, len).then(function () {
A
Annie_wang 已提交
1360
      console.info("File truncated");
A
Annie_wang 已提交
1361 1362
  }).catch(function (err) {
      console.info("truncate file failed with error:" + err);
A
annie_wangli 已提交
1363 1364
  });
  ```
Z
zengyawen 已提交
1365 1366


A
annie_wangli 已提交
1367
## fileio.truncate<sup>7+</sup>
Z
zengyawen 已提交
1368

A
Annie_wang 已提交
1369
truncate(path: string, len?: number, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
1370

A
Annie_wang 已提交
1371
Truncates a file based on the file path. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
1372

A
annie_wangli 已提交
1373 1374
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1375
**Parameters**
A
Annie_wang 已提交
1376

A
Annie_wang 已提交
1377 1378
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1379
| path     | string                    | Yes  | Application sandbox path of the file to truncate.      |
A
Annie_wang 已提交
1380 1381
| len      | number                    | No  | File length, in bytes, after truncation.|
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback that returns no value.  |
Z
zengyawen 已提交
1382

A
Annie_wang 已提交
1383
**Example**
A
Annie_wang 已提交
1384

A
annie_wangli 已提交
1385
  ```js
A
Annie_wang 已提交
1386
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1387
  let len = 5;
A
Annie_wang 已提交
1388
  fileio.truncate(filePath, len, function (err) {
A
annie_wangli 已提交
1389 1390 1391
      // Do something.
  });
  ```
Z
zengyawen 已提交
1392 1393


A
annie_wangli 已提交
1394
## fileio.truncateSync<sup>7+</sup>
Z
zengyawen 已提交
1395

A
annie_wangli 已提交
1396
truncateSync(path: string, len?: number): void
Z
zengyawen 已提交
1397

A
annie_wangli 已提交
1398
Synchronously truncates a file based on the file path.
Z
zengyawen 已提交
1399

A
annie_wangli 已提交
1400 1401
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1402
**Parameters**
A
Annie_wang 已提交
1403

A
Annie_wang 已提交
1404 1405
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
A
Annie_wang 已提交
1406
| path   | string | Yes  | Application sandbox path of the file to truncate.      |
A
Annie_wang 已提交
1407
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1408

A
Annie_wang 已提交
1409
**Example**
A
Annie_wang 已提交
1410

A
annie_wangli 已提交
1411
  ```js
A
Annie_wang 已提交
1412
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1413
  let len = 5;
A
Annie_wang 已提交
1414
  fileio.truncateSync(filePath, len);
A
annie_wangli 已提交
1415
  ```
Z
zengyawen 已提交
1416 1417


A
annie_wangli 已提交
1418
## fileio.readText<sup>7+</sup>
Z
zengyawen 已提交
1419

A
Annie_wang 已提交
1420
readText(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): Promise&lt;string&gt;
Z
zengyawen 已提交
1421

A
Annie_wang 已提交
1422
Reads the text content of a file. This API uses a promise to return the result.
Z
zengyawen 已提交
1423

A
annie_wangli 已提交
1424 1425
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1426
**Parameters**
A
Annie_wang 已提交
1427

A
Annie_wang 已提交
1428 1429 1430
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filePath | string | Yes  | Application sandbox path of the file to read.                                  |
A
Annie_wang 已提交
1431
| 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.|
Z
zengyawen 已提交
1432

A
Annie_wang 已提交
1433
**Return value**
A
Annie_wang 已提交
1434

A
Annie_wang 已提交
1435 1436 1437
  | Type                   | Description        |
  | --------------------- | ---------- |
  | Promise&lt;string&gt; | Promise used to return the content read.|
Z
zengyawen 已提交
1438

A
Annie_wang 已提交
1439
**Example**
A
Annie_wang 已提交
1440

A
annie_wangli 已提交
1441
  ```js
A
Annie_wang 已提交
1442
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1443 1444 1445 1446
  fileio.readText(filePath).then(function (str) {
      console.info("readText succeed:" + str);
  }).catch(function (err) {
      console.info("readText failed with error:" + err);
A
annie_wangli 已提交
1447 1448
  });
  ```
Z
zengyawen 已提交
1449 1450


A
annie_wangli 已提交
1451
## fileio.readText<sup>7+</sup>
Z
zengyawen 已提交
1452

A
Annie_wang 已提交
1453
readText(filePath: string, options: { position?: number; length?: number; encoding?: string; }, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1454

A
Annie_wang 已提交
1455
Reads the text content of a file. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
1456

A
annie_wangli 已提交
1457 1458
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1459
**Parameters**
A
Annie_wang 已提交
1460

A
Annie_wang 已提交
1461 1462 1463
| Name  | Type                       | Mandatory| Description                                                        |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| filePath | string                      | Yes  | Application sandbox path of the file to read.                                  |
A
Annie_wang 已提交
1464
| 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.|
A
Annie_wang 已提交
1465
| callback | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the content read.                        |
Z
zengyawen 已提交
1466

A
Annie_wang 已提交
1467
**Example**
A
Annie_wang 已提交
1468

A
annie_wangli 已提交
1469
  ```js
A
Annie_wang 已提交
1470
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1471
  fileio.readText(filePath, { position: 1, encoding: 'UTF-8' }, function (err, str) {
A
annie_wangli 已提交
1472 1473 1474
      // Do something.
  });
  ```
Z
zengyawen 已提交
1475 1476


A
annie_wangli 已提交
1477 1478
## fileio.readTextSync<sup>7+</sup>

A
Annie_wang 已提交
1479
readTextSync(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): string
A
annie_wangli 已提交
1480 1481 1482

Synchronously reads the text of a file. 

A
annie_wangli 已提交
1483 1484
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1485
**Parameters**
A
Annie_wang 已提交
1486

A
Annie_wang 已提交
1487 1488
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1489
| filePath | string | Yes  | Application sandbox path of the file to read.                                  |
A
Annie_wang 已提交
1490
| 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.|
A
annie_wangli 已提交
1491

A
Annie_wang 已提交
1492
**Return value**
A
Annie_wang 已提交
1493

A
Annie_wang 已提交
1494 1495 1496
  | Type  | Description                |
  | ------ | -------------------- |
  | string | Promise used to return the content of the file read.|
A
annie_wangli 已提交
1497

A
Annie_wang 已提交
1498
**Example**
A
Annie_wang 已提交
1499

A
annie_wangli 已提交
1500
  ```js
A
Annie_wang 已提交
1501 1502
  let filePath = pathDir + "/test.txt";
  let str = fileio.readTextSync(filePath, {position: 1, length: 3});
A
annie_wangli 已提交
1503 1504 1505 1506 1507 1508 1509
  ```


## fileio.lstat<sup>7+</sup>

lstat(path: string): Promise&lt;Stat&gt;

A
Annie_wang 已提交
1510
Obtains link information. This API uses a promise to return the result.
A
annie_wangli 已提交
1511

A
annie_wangli 已提交
1512 1513
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1514
**Parameters**
A
Annie_wang 已提交
1515

A
Annie_wang 已提交
1516 1517
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1518
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1519

A
Annie_wang 已提交
1520
**Return value**
A
Annie_wang 已提交
1521

A
Annie_wang 已提交
1522 1523 1524
  | Type                          | Description        |
  | ---------------------------- | ---------- |
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the link information obtained. For details, see [Stat](#stat).|
A
annie_wangli 已提交
1525

A
Annie_wang 已提交
1526
**Example**
A
Annie_wang 已提交
1527

A
annie_wangli 已提交
1528
  ```js
A
Annie_wang 已提交
1529
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1530 1531 1532 1533
  fileio.lstat(filePath).then(function (stat) {
      console.info("get link status succeed, the size of file is" + stat.size);
  }).catch(function (err) {
      console.info("get link status failed with error:" + err);
A
annie_wangli 已提交
1534 1535 1536 1537 1538 1539
  });
  ```


## fileio.lstat<sup>7+</sup>

A
Annie_wang 已提交
1540
lstat(path: string, callback: AsyncCallback&lt;Stat&gt;): void
A
annie_wangli 已提交
1541

A
Annie_wang 已提交
1542
Obtains link information. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1543

A
annie_wangli 已提交
1544 1545
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1546
**Parameters**
A
Annie_wang 已提交
1547

A
Annie_wang 已提交
1548 1549
| Name  | Type                              | Mandatory| Description                                  |
| -------- | ---------------------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1550 1551
| path     | string                             | Yes  | Application sandbox path of the target file.|
| callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes  | Callback used to return the link information obtained.      |
A
annie_wangli 已提交
1552

A
Annie_wang 已提交
1553
**Example**
A
Annie_wang 已提交
1554

A
annie_wangli 已提交
1555
  ```js
A
Annie_wang 已提交
1556 1557
  let filePath = pathDir + "/test.txt";
  fileio.lstat(filePath, function (err, stat) {
A
annie_wangli 已提交
1558
      // Do something.
A
annie_wangli 已提交
1559
  });
A
annie_wangli 已提交
1560 1561 1562 1563 1564
  ```


## fileio.lstatSync<sup>7+</sup>

A
Annie_wang 已提交
1565
lstatSync(path: string): Stat
A
annie_wangli 已提交
1566

A
Annie_wang 已提交
1567
Synchronously obtains the link information.
A
annie_wangli 已提交
1568

A
annie_wangli 已提交
1569 1570
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1571
**Parameters**
A
Annie_wang 已提交
1572

A
Annie_wang 已提交
1573 1574
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1575
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1576

A
Annie_wang 已提交
1577
**Return value**
A
Annie_wang 已提交
1578

A
Annie_wang 已提交
1579 1580 1581
  | Type           | Description        |
  | ------------- | ---------- |
  | [Stat](#stat) | Link information obtained.|
A
annie_wangli 已提交
1582

A
Annie_wang 已提交
1583
**Example**
A
Annie_wang 已提交
1584

A
annie_wangli 已提交
1585
  ```js
A
Annie_wang 已提交
1586 1587
  let filePath = pathDir + "/test.txt";
  let stat = fileio.lstatSync(filePath);
A
annie_wangli 已提交
1588 1589 1590 1591 1592 1593 1594
  ```


## fileio.rename<sup>7+</sup>

rename(oldPath: string, newPath: string): Promise&lt;void&gt;

A
Annie_wang 已提交
1595
Renames a file. This API uses a promise to return the result.
A
annie_wangli 已提交
1596

A
annie_wangli 已提交
1597 1598
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1599
**Parameters**
A
Annie_wang 已提交
1600

A
Annie_wang 已提交
1601 1602 1603
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes  | Application sandbox path of the file to rename.|
A
Annie_wang 已提交
1604
| newPath | string | Yes  | Application sandbox path of the file renamed.  |
A
annie_wangli 已提交
1605

A
Annie_wang 已提交
1606
**Return value**
A
Annie_wang 已提交
1607

A
Annie_wang 已提交
1608 1609 1610
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1611

A
Annie_wang 已提交
1612
**Example**
A
Annie_wang 已提交
1613

A
annie_wangli 已提交
1614
  ```js
A
Annie_wang 已提交
1615 1616
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
A
Annie_wang 已提交
1617
  fileio.rename(srcFile, dstFile).then(function () {
A
Annie_wang 已提交
1618
      console.info("File renamed");
A
Annie_wang 已提交
1619 1620
  }).catch(function (err) {
      console.info("rename failed with error:" + err);
A
annie_wangli 已提交
1621 1622 1623 1624 1625 1626 1627 1628
  });
  ```


## fileio.rename<sup>7+</sup>

rename(oldPath: string, newPath: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
1629
Renames a file. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1630

A
annie_wangli 已提交
1631 1632
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1633
**Parameters**
A
Annie_wang 已提交
1634

A
Annie_wang 已提交
1635 1636 1637
| Name  | Type                     | Mandatory| Description                        |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | Yes  | Application sandbox path of the file to rename.|
A
Annie_wang 已提交
1638 1639
| newPath  | string                    | Yes  | Application sandbox path of the file renamed.  |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously renamed.  |
A
annie_wangli 已提交
1640

A
Annie_wang 已提交
1641
**Example**
A
Annie_wang 已提交
1642

A
annie_wangli 已提交
1643
  ```js
A
Annie_wang 已提交
1644 1645
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
A
Annie_wang 已提交
1646
  fileio.rename(srcFile, dstFile, function (err) {
A
annie_wangli 已提交
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
  });
  ```


## fileio.renameSync<sup>7+</sup>

renameSync(oldPath: string, newPath: string): void

Synchronously renames a file.

A
annie_wangli 已提交
1657 1658
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1659
**Parameters**
A
Annie_wang 已提交
1660

A
Annie_wang 已提交
1661 1662 1663
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes  | Application sandbox path of the file to rename.|
A
Annie_wang 已提交
1664
| newPath | string | Yes  | Application sandbox path of the file renamed.  |
A
annie_wangli 已提交
1665

A
Annie_wang 已提交
1666
**Example**
A
Annie_wang 已提交
1667

A
annie_wangli 已提交
1668
  ```js
A
Annie_wang 已提交
1669 1670 1671
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
  fileio.renameSync(srcFile, dstFile);
A
annie_wangli 已提交
1672 1673 1674 1675 1676 1677 1678
  ```


## fileio.fsync<sup>7+</sup>

fsync(fd: number): Promise&lt;void&gt;

A
Annie_wang 已提交
1679
Flushes data of a file to disk. This API uses a promise to return the result.
A
annie_wangli 已提交
1680

A
annie_wangli 已提交
1681 1682
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1683
**Parameters**
A
Annie_wang 已提交
1684

A
Annie_wang 已提交
1685 1686 1687
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1688

A
Annie_wang 已提交
1689
**Return value**
A
Annie_wang 已提交
1690

A
Annie_wang 已提交
1691 1692 1693
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1694

A
Annie_wang 已提交
1695
**Example**
A
Annie_wang 已提交
1696

A
annie_wangli 已提交
1697
  ```js
A
Annie_wang 已提交
1698 1699
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1700
  fileio.fsync(fd).then(function () {
A
Annie_wang 已提交
1701
      console.info("Data flushed");
A
Annie_wang 已提交
1702 1703
  }).catch(function (err) {
      console.info("sync data failed with error:" + err);
A
annie_wangli 已提交
1704 1705 1706 1707 1708 1709 1710 1711
  });
  ```


## fileio.fsync<sup>7+</sup>

fsync(fd: number, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
1712
Flushes data of a file to disk. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1713

A
annie_wangli 已提交
1714 1715
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1716
**Parameters**
A
Annie_wang 已提交
1717

A
Annie_wang 已提交
1718 1719 1720 1721
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | Yes   | File descriptor of the file to flush.   |
  | Callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1722

A
Annie_wang 已提交
1723
**Example**
A
Annie_wang 已提交
1724

A
annie_wangli 已提交
1725
  ```js
A
Annie_wang 已提交
1726 1727
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1728
  fileio.fsync(fd, function (err) {
A
annie_wangli 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737
      // Do something.
  });
  ```


## fileio.fsyncSync<sup>7+</sup>

fsyncSync(fd: number): void

A
Annie_wang 已提交
1738
Flushes data of a file to disk in synchronous mode.
A
annie_wangli 已提交
1739 1740

**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
1741

A
Annie_wang 已提交
1742
**Parameters**
A
Annie_wang 已提交
1743

A
Annie_wang 已提交
1744 1745 1746
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1747

A
Annie_wang 已提交
1748
**Example**
A
Annie_wang 已提交
1749

A
annie_wangli 已提交
1750
  ```js
A
Annie_wang 已提交
1751 1752
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1753
  fileio.fsyncSync(fd);
A
annie_wangli 已提交
1754 1755 1756 1757 1758 1759 1760
  ```


## fileio.fdatasync<sup>7+</sup>

fdatasync(fd: number): Promise&lt;void&gt;

A
Annie_wang 已提交
1761
Flushes 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.
A
annie_wangli 已提交
1762

A
annie_wangli 已提交
1763 1764
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1765
**Parameters**
A
Annie_wang 已提交
1766

A
Annie_wang 已提交
1767 1768 1769
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1770

A
Annie_wang 已提交
1771
**Return value**
A
Annie_wang 已提交
1772

A
Annie_wang 已提交
1773 1774 1775
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1776

A
Annie_wang 已提交
1777
**Example**
A
Annie_wang 已提交
1778

A
annie_wangli 已提交
1779
  ```js
A
Annie_wang 已提交
1780 1781
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1782
  fileio.fdatasync(fd).then(function (err) {
A
Annie_wang 已提交
1783
      console.info("Data flushed");
A
Annie_wang 已提交
1784 1785
  }).catch(function (err) {
      console.info("sync data failed with error:" + err);
A
annie_wangli 已提交
1786 1787 1788 1789 1790 1791
  });
  ```


## fileio.fdatasync<sup>7+</sup>

A
Annie_wang 已提交
1792
fdatasync(fd: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
1793

A
Annie_wang 已提交
1794
Flushes data of a file to disk. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1795

A
annie_wangli 已提交
1796 1797
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1798
**Parameters**
A
Annie_wang 已提交
1799

A
Annie_wang 已提交
1800 1801 1802 1803
  | Name     | Type                             | Mandatory  | Description               |
  | -------- | ------------------------------- | ---- | ----------------- |
  | fd       | number                          | Yes   | File descriptor of the file to synchronize.     |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file data is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1804

A
Annie_wang 已提交
1805
**Example**
A
Annie_wang 已提交
1806

A
annie_wangli 已提交
1807
  ```js
A
Annie_wang 已提交
1808 1809
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
  fileio.fdatasync (fd, function (err) {
      // Do something.
  });
  ```


## fileio.fdatasyncSync<sup>7+</sup>

fdatasyncSync(fd: number): void

Synchronizes data in a file in synchronous mode.

A
annie_wangli 已提交
1822 1823
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1824
**Parameters**
A
Annie_wang 已提交
1825

A
Annie_wang 已提交
1826 1827 1828
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1829

A
Annie_wang 已提交
1830
**Example**
A
Annie_wang 已提交
1831

A
annie_wangli 已提交
1832
  ```js
A
Annie_wang 已提交
1833 1834
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1835 1836 1837 1838 1839 1840 1841 1842
  let stat = fileio.fdatasyncSync(fd);
  ```


## fileio.symlink<sup>7+</sup>

symlink(target: string, srcPath: string): Promise&lt;void&gt;

A
Annie_wang 已提交
1843
Creates a symbolic link based on the file path. This API uses a promise to return the result.
A
annie_wangli 已提交
1844

A
annie_wangli 已提交
1845 1846
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1847
**Parameters**
A
Annie_wang 已提交
1848

A
Annie_wang 已提交
1849 1850
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1851 1852
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1853

A
Annie_wang 已提交
1854
**Return value**
A
Annie_wang 已提交
1855

A
Annie_wang 已提交
1856 1857 1858
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1859

A
Annie_wang 已提交
1860
**Example**
A
Annie_wang 已提交
1861

A
annie_wangli 已提交
1862
  ```js
A
Annie_wang 已提交
1863 1864
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
A
Annie_wang 已提交
1865
  fileio.symlink(srcFile, dstFile).then(function () {
A
Annie_wang 已提交
1866
      console.info("Symbolic link created");
A
Annie_wang 已提交
1867 1868
  }).catch(function (err) {
      console.info("symlink failed with error:" + err);
A
annie_wangli 已提交
1869 1870 1871 1872 1873 1874 1875 1876
  });
  ```


## fileio.symlink<sup>7+</sup>

symlink(target: string, srcPath: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
1877
Creates a symbolic link based on the file path. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1878

A
annie_wangli 已提交
1879 1880
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1881
**Parameters**
A
Annie_wang 已提交
1882

A
Annie_wang 已提交
1883 1884
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1885 1886
| target   | string                    | Yes  | Application sandbox path of the target file.        |
| srcPath  | string                    | Yes  | Application sandbox path of the symbolic link file.    |
A
Annie_wang 已提交
1887
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the symbolic link is created asynchronously.|
A
annie_wangli 已提交
1888

A
Annie_wang 已提交
1889
**Example**
A
Annie_wang 已提交
1890

A
annie_wangli 已提交
1891
  ```js
A
Annie_wang 已提交
1892 1893 1894
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlink(srcFile, dstFile, function (err) {
A
annie_wangli 已提交
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
      // Do something.
  });
  ```


## fileio.symlinkSync<sup>7+</sup>

symlinkSync(target: string, srcPath: string): void

Synchronously creates a symbolic link based on a specified path.

A
annie_wangli 已提交
1906 1907
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1908
**Parameters**
A
Annie_wang 已提交
1909

A
Annie_wang 已提交
1910 1911
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1912 1913
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1914

A
Annie_wang 已提交
1915
**Example**
A
Annie_wang 已提交
1916

A
annie_wangli 已提交
1917
  ```js
A
Annie_wang 已提交
1918 1919 1920
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlinkSync(srcFile, dstFile);
A
annie_wangli 已提交
1921 1922 1923 1924 1925 1926 1927
  ```


## fileio.chown<sup>7+</sup>

chown(path: string, uid: number, gid: number): Promise&lt;void&gt;

A
Annie_wang 已提交
1928
Changes the file owner based on the file path. This API uses a promise to return the result.
A
annie_wangli 已提交
1929

A
annie_wangli 已提交
1930 1931
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1932
**Parameters**
A
Annie_wang 已提交
1933

A
Annie_wang 已提交
1934 1935
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1936
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
1937 1938
| uid    | number | Yes  | New user ID (UID).       |
| gid    | number | Yes  | New group ID (GID).      |
A
annie_wangli 已提交
1939

A
Annie_wang 已提交
1940
**Return value**
A
Annie_wang 已提交
1941

A
Annie_wang 已提交
1942 1943 1944
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1945

A
Annie_wang 已提交
1946
**Example**
A
Annie_wang 已提交
1947

A
annie_wangli 已提交
1948
  ```js
A
Annie_wang 已提交
1949 1950
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
A
Annie_wang 已提交
1951
  fileio.chown(filePath, stat.uid, stat.gid).then(function () {
A
Annie_wang 已提交
1952
      console.info("File owner changed");
A
Annie_wang 已提交
1953 1954
  }).catch(function (err) {
      console.info("chown failed with error:" + err);
A
annie_wangli 已提交
1955 1956 1957 1958 1959 1960 1961 1962
  });
  ```


## fileio.chown<sup>7+</sup>

chown(path: string, uid: number, gid: number, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
1963
Changes the file owner based on the file path. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1964

A
annie_wangli 已提交
1965 1966
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1967
**Parameters**
A
Annie_wang 已提交
1968

A
Annie_wang 已提交
1969 1970
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
1971
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
1972 1973 1974
| uid      | number                    | Yes  | New UID.                     |
| gid      | number                    | Yes  | New GID.                     |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file owner is changed asynchronously.|
A
annie_wangli 已提交
1975

A
Annie_wang 已提交
1976
**Example**
A
Annie_wang 已提交
1977

A
annie_wangli 已提交
1978
  ```js
A
Annie_wang 已提交
1979 1980
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath)
A
Annie_wang 已提交
1981
  fileio.chown(filePath, stat.uid, stat.gid, function (err) {
A
annie_wangli 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
      // Do something.
  });
  ```


## fileio.chownSync<sup>7+</sup>

chownSync(path: string, uid: number, gid: number): void

Synchronously changes the file owner based on its path.

A
annie_wangli 已提交
1993 1994
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1995
**Parameters**
A
Annie_wang 已提交
1996

A
Annie_wang 已提交
1997 1998
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1999
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2000 2001
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2002

A
Annie_wang 已提交
2003
**Example**
A
Annie_wang 已提交
2004

A
annie_wangli 已提交
2005
  ```js
A
Annie_wang 已提交
2006 2007 2008
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath)
  fileio.chownSync(filePath, stat.uid, stat.gid);
A
annie_wangli 已提交
2009 2010 2011 2012 2013 2014 2015
  ```


## fileio.mkdtemp<sup>7+</sup>

mkdtemp(prefix: string): Promise&lt;string&gt;

A
Annie_wang 已提交
2016
Creates a temporary directory. This API uses a promise to return the result.
A
annie_wangli 已提交
2017

A
annie_wangli 已提交
2018 2019
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2020
**Parameters**
A
Annie_wang 已提交
2021

A
Annie_wang 已提交
2022 2023 2024
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
2025

A
Annie_wang 已提交
2026
**Return value**
A
Annie_wang 已提交
2027

A
Annie_wang 已提交
2028 2029 2030
  | Type                  | Description        |
  | --------------------- | ---------- |
  | Promise&lt;string&gt; | Promise used to return the unique directory generated.|
A
annie_wangli 已提交
2031

A
Annie_wang 已提交
2032
**Example**
A
Annie_wang 已提交
2033

A
annie_wangli 已提交
2034
  ```js
A
Annie_wang 已提交
2035 2036 2037 2038
  fileio.mkdtemp(pathDir + "/XXXXXX").then(function (pathDir) {
      console.info("mkdtemp succeed:" + pathDir);
  }).catch(function (err) {
      console.info("mkdtemp failed with error:" + err);
A
annie_wangli 已提交
2039 2040 2041 2042 2043 2044 2045 2046
  });
  ```


## fileio.mkdtemp<sup>7+</sup>

mkdtemp(prefix: string, callback: AsyncCallback&lt;string&gt;): void

A
Annie_wang 已提交
2047
Creates a temporary directory. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2048

A
annie_wangli 已提交
2049 2050
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2051
**Parameters**
A
Annie_wang 已提交
2052

A
Annie_wang 已提交
2053 2054 2055 2056
  | Name     | Type                         | Mandatory  | Description                         |
  | -------- | --------------------------- | ---- | --------------------------- |
  | prefix   | string                      | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
  | callback | AsyncCallback&lt;string&gt; | Yes   | Callback invoked when a temporary directory is created asynchronously.             |
A
annie_wangli 已提交
2057

A
Annie_wang 已提交
2058
**Example**
A
Annie_wang 已提交
2059

A
annie_wangli 已提交
2060
  ```js
A
Annie_wang 已提交
2061
  fileio.mkdtemp(pathDir + "/XXXXXX", function (err, res) {
A
annie_wangli 已提交
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
      // Do something.
  });
  ```


## fileio.mkdtempSync<sup>7+</sup>

mkdtempSync(prefix: string): string

Synchronously creates a temporary directory.

A
annie_wangli 已提交
2073 2074
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2075
**Parameters**
A
Annie_wang 已提交
2076

A
Annie_wang 已提交
2077 2078 2079
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
2080

A
Annie_wang 已提交
2081
**Return value**
A
Annie_wang 已提交
2082

A
Annie_wang 已提交
2083 2084 2085
  | Type   | Description        |
  | ------ | ---------- |
  | string | Unique path generated.|
A
annie_wangli 已提交
2086

A
Annie_wang 已提交
2087
**Example**
A
Annie_wang 已提交
2088

A
annie_wangli 已提交
2089
  ```js
A
Annie_wang 已提交
2090
  let res = fileio.mkdtempSync(pathDir + "/XXXXXX");
A
annie_wangli 已提交
2091 2092 2093 2094 2095 2096 2097
  ```


## fileio.fchmod<sup>7+</sup>

fchmod(fd: number, mode: number): Promise&lt;void&gt;

A
Annie_wang 已提交
2098
Changes file permissions based on the file descriptor. This API uses a promise to return the result.
A
annie_wangli 已提交
2099

A
annie_wangli 已提交
2100 2101
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2102
**Parameters**
A
Annie_wang 已提交
2103

A
Annie_wang 已提交
2104 2105 2106 2107
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | Yes   | File descriptor of the target file.                            |
| 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. |
A
annie_wangli 已提交
2108

A
Annie_wang 已提交
2109
**Return value**
A
Annie_wang 已提交
2110

A
Annie_wang 已提交
2111 2112 2113
  | Type                | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2114

A
Annie_wang 已提交
2115
**Example**
A
Annie_wang 已提交
2116

A
annie_wangli 已提交
2117
  ```js
A
Annie_wang 已提交
2118 2119
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2120
  let mode = 0o700;
A
Annie_wang 已提交
2121
  fileio.fchmod(fd, mode).then(function () {
A
Annie_wang 已提交
2122
      console.info("File permissions changed");
A
Annie_wang 已提交
2123 2124
  }).catch(function (err) {
      console.info("chmod failed with error:" + err);
A
annie_wangli 已提交
2125 2126 2127 2128 2129 2130 2131 2132
  });
  ```


## fileio.fchmod<sup>7+</sup>

fchmod(fd: number, mode: number, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
2133
Changes file permissions based on the file descriptor. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2134

A
annie_wangli 已提交
2135 2136
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2137
**Parameters**
A
Annie_wang 已提交
2138

A
Annie_wang 已提交
2139 2140 2141 2142 2143
  | Name     | Type                             | Mandatory  | Description                                      |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | Yes   | File descriptor of the target file.                            |
| 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. |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file permissions are changed asynchronously.                          |
A
annie_wangli 已提交
2144

A
Annie_wang 已提交
2145
**Example**
A
Annie_wang 已提交
2146

A
annie_wangli 已提交
2147
  ```js
A
Annie_wang 已提交
2148 2149
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2150
  let mode = 0o700;
A
annie_wangli 已提交
2151 2152 2153 2154 2155 2156 2157 2158
  fileio.fchmod(fd, mode, function (err) {
      // Do something.
  });
  ```


## fileio.fchmodSync<sup>7+</sup>

A
annie_wangli 已提交
2159
fchmodSync(fd: number, mode: number): void
A
annie_wangli 已提交
2160 2161 2162

Synchronously changes the file permissions based on the file descriptor.

A
annie_wangli 已提交
2163 2164
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2165
**Parameters**
A
Annie_wang 已提交
2166

A
Annie_wang 已提交
2167 2168 2169 2170
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | Yes   | File descriptor of the target file.                            |
| 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. |
A
annie_wangli 已提交
2171

A
Annie_wang 已提交
2172
**Example**
A
Annie_wang 已提交
2173

A
annie_wangli 已提交
2174
  ```js
A
Annie_wang 已提交
2175 2176
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2177
  let mode = 0o700;
A
annie_wangli 已提交
2178 2179 2180 2181 2182 2183 2184 2185
   fileio.fchmodSync(fd, mode);
  ```


## fileio.createStream<sup>7+</sup>

createStream(path: string, mode: string): Promise&lt;Stream&gt;

A
Annie_wang 已提交
2186
Opens a file stream based on the file path. This API uses a promise to return the result.
A
annie_wangli 已提交
2187

A
annie_wangli 已提交
2188 2189
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2190
**Parameters**
A
Annie_wang 已提交
2191

A
Annie_wang 已提交
2192 2193
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2194
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2195
| 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).|
A
annie_wangli 已提交
2196

A
Annie_wang 已提交
2197
**Return value**
A
Annie_wang 已提交
2198

A
Annie_wang 已提交
2199 2200 2201
  | Type                               | Description       |
  | --------------------------------- | --------- |
  | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
A
annie_wangli 已提交
2202

A
Annie_wang 已提交
2203
**Example**
A
Annie_wang 已提交
2204

A
annie_wangli 已提交
2205
  ```js
A
Annie_wang 已提交
2206
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
2207
  fileio.createStream(filePath, "r+").then(function (stream) {
A
Annie_wang 已提交
2208
      console.info("Stream created");
A
Annie_wang 已提交
2209 2210
  }).catch(function (err) {
      console.info("createStream failed with error:" + err);
A
annie_wangli 已提交
2211 2212 2213 2214 2215 2216 2217 2218
  });
  ```


## fileio.createStream<sup>7+</sup>

createStream(path: string, mode: string, callback: AsyncCallback&lt;Stream&gt;): void

A
Annie_wang 已提交
2219
Opens a file stream based on the file path. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2220

A
annie_wangli 已提交
2221 2222
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2223
**Parameters**
A
Annie_wang 已提交
2224

A
Annie_wang 已提交
2225 2226
| Name  | Type                                   | Mandatory| Description                                                        |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2227
| path     | string                                  | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2228
| 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).|
A
Annie_wang 已提交
2229
| callback | AsyncCallback&lt;[Stream](#stream)&gt; | Yes  | Callback invoked when the stream is open asynchronously.                                  |
A
annie_wangli 已提交
2230

A
Annie_wang 已提交
2231
**Example**
A
Annie_wang 已提交
2232

A
annie_wangli 已提交
2233
  ```js
A
Annie_wang 已提交
2234
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
2235
  fileio.createStream(filePath, "r+", function (err, stream) {
A
annie_wangli 已提交
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
      // Do something.
  });
  ```


## fileio.createStreamSync<sup>7+</sup>

createStreamSync(path: string, mode: string): Stream

Synchronously opens a stream based on the file path.

A
annie_wangli 已提交
2247 2248
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2249
**Parameters**
A
Annie_wang 已提交
2250

A
Annie_wang 已提交
2251 2252
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2253
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2254
| 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).|
A
annie_wangli 已提交
2255

A
Annie_wang 已提交
2256
**Return value**
A
Annie_wang 已提交
2257

A
Annie_wang 已提交
2258 2259 2260
  | Type               | Description       |
  | ------------------ | --------- |
  | [Stream](#stream) | Stream opened.|
A
annie_wangli 已提交
2261

A
Annie_wang 已提交
2262
**Example**
A
Annie_wang 已提交
2263

A
annie_wangli 已提交
2264
  ```js
A
Annie_wang 已提交
2265 2266
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2267 2268 2269 2270 2271 2272 2273
  ```


## fileio.fdopenStream<sup>7+</sup>

fdopenStream(fd: number, mode: string): Promise&lt;Stream&gt;

A
Annie_wang 已提交
2274
Opens a file stream based on the file descriptor. This API uses a promise to return the result.
A
annie_wangli 已提交
2275

A
annie_wangli 已提交
2276 2277
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2278
**Parameters**
A
Annie_wang 已提交
2279

A
Annie_wang 已提交
2280 2281 2282 2283
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | Yes   | File descriptor of the target file.                            |
  | 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).|
A
annie_wangli 已提交
2284

A
Annie_wang 已提交
2285
**Return value**
A
Annie_wang 已提交
2286

A
Annie_wang 已提交
2287 2288 2289
  | Type                              | Description       |
  | --------------------------------- | --------- |
  | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
A
annie_wangli 已提交
2290

A
Annie_wang 已提交
2291
**Example**
A
Annie_wang 已提交
2292

A
annie_wangli 已提交
2293
  ```js
A
Annie_wang 已提交
2294 2295
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2296
  fileio.fdopenStream(fd, "r+").then(function (stream) {
A
Annie_wang 已提交
2297
      console.info("Stream opened");
A
Annie_wang 已提交
2298 2299
  }).catch(function (err) {
      console.info("openStream failed with error:" + err);
A
annie_wangli 已提交
2300 2301 2302 2303 2304 2305 2306 2307
  });
  ```


## fileio.fdopenStream<sup>7+</sup>

fdopenStream(fd: number, mode: string, callback: AsyncCallback&lt;Stream&gt;): void

A
Annie_wang 已提交
2308
Opens a file stream based on the file descriptor. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2309

A
annie_wangli 已提交
2310 2311
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2312
**Parameters**
A
Annie_wang 已提交
2313

A
Annie_wang 已提交
2314 2315 2316 2317 2318
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                                   | Yes   | File descriptor of the target file.                            |
  | 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).|
  | callback | AsyncCallback&lt;[Stream](#stream)&gt; | Yes   | Callback invoked when the stream is open asynchronously.                           |
A
annie_wangli 已提交
2319

A
Annie_wang 已提交
2320
**Example**
A
Annie_wang 已提交
2321

A
annie_wangli 已提交
2322
  ```js
A
Annie_wang 已提交
2323 2324
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2325
  fileio.fdopenStream(fd, "r+", function (err, stream) {
A
annie_wangli 已提交
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
      // Do something.
  });
  ```


## fileio.fdopenStreamSync<sup>7+</sup>

fdopenStreamSync(fd: number, mode: string): Stream

Synchronously opens a stream based on the file descriptor.

A
annie_wangli 已提交
2337 2338
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2339
**Parameters**
A
Annie_wang 已提交
2340

A
Annie_wang 已提交
2341 2342 2343 2344
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | Yes   | File descriptor of the target file.                            |
  | 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).|
A
annie_wangli 已提交
2345

A
Annie_wang 已提交
2346
**Return value**
A
Annie_wang 已提交
2347

A
Annie_wang 已提交
2348 2349 2350
  | Type               | Description       |
  | ------------------ | --------- |
  | [Stream](#stream) | Stream opened.|
A
annie_wangli 已提交
2351

A
Annie_wang 已提交
2352
**Example**
A
Annie_wang 已提交
2353

A
annie_wangli 已提交
2354
  ```js
A
Annie_wang 已提交
2355 2356
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
2357 2358 2359 2360 2361 2362 2363 2364
  let ss = fileio.fdopenStreamSync(fd, "r+");
  ```


## fileio.fchown<sup>7+</sup>

fchown(fd: number, uid: number, gid: number): Promise&lt;void&gt;

A
Annie_wang 已提交
2365
Changes the file owner based on the file descriptor. This API uses a promise to return the result.
A
annie_wangli 已提交
2366

A
annie_wangli 已提交
2367 2368
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2369
**Parameters**
A
Annie_wang 已提交
2370

A
Annie_wang 已提交
2371 2372 2373 2374 2375
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
  | uid  | number | Yes   | New UID.  |
  | gid  | number | Yes   | New GID.  |
A
annie_wangli 已提交
2376

A
Annie_wang 已提交
2377
**Return value**
A
Annie_wang 已提交
2378

A
Annie_wang 已提交
2379 2380 2381
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2382

A
Annie_wang 已提交
2383
**Example**
A
Annie_wang 已提交
2384

A
annie_wangli 已提交
2385
  ```js
A
Annie_wang 已提交
2386 2387 2388
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
A
Annie_wang 已提交
2389
  fileio.fchown(fd, stat.uid, stat.gid).then(function () {
A
Annie_wang 已提交
2390
      console.info("File owner changed");
A
Annie_wang 已提交
2391 2392
  }).catch(function (err) {
      console.info("chown failed with error:" + err);
A
annie_wangli 已提交
2393 2394 2395 2396 2397 2398 2399 2400
  });
  ```


## fileio.fchown<sup>7+</sup>

fchown(fd: number, uid: number, gid: number, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
2401
Changes the file owner based on the file descriptor. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2402

A
annie_wangli 已提交
2403 2404
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2405
**Parameters**
A
Annie_wang 已提交
2406

A
Annie_wang 已提交
2407 2408 2409 2410 2411 2412
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | Yes   | File descriptor of the target file.   |
  | uid      | number                    | Yes   | New UID.     |
  | gid      | number                    | Yes   | New GID.     |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file owner is changed asynchronously.|
A
annie_wangli 已提交
2413

A
Annie_wang 已提交
2414
**Example**
A
Annie_wang 已提交
2415

A
annie_wangli 已提交
2416
  ```js
A
Annie_wang 已提交
2417 2418 2419
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
A
Annie_wang 已提交
2420
  fileio.fchown(fd, stat.uid, stat.gid, function (err) {
A
annie_wangli 已提交
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
      // Do something.
  });
  ```


## fileio.fchownSync<sup>7+</sup>

fchownSync(fd: number, uid: number, gid: number): void

Synchronously changes the file owner based on the file descriptor.

A
annie_wangli 已提交
2432 2433
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2434
**Parameters**
A
Annie_wang 已提交
2435

A
Annie_wang 已提交
2436 2437 2438 2439 2440
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
  | uid  | number | Yes   | New UID.  |
  | gid  | number | Yes   | New GID.  |
A
annie_wangli 已提交
2441

A
Annie_wang 已提交
2442
**Example**
A
Annie_wang 已提交
2443

A
annie_wangli 已提交
2444
  ```js
A
Annie_wang 已提交
2445 2446 2447
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
A
annie_wangli 已提交
2448 2449 2450 2451 2452 2453 2454 2455
  fileio.fchownSync(fd, stat.uid, stat.gid);
  ```


## fileio.lchown<sup>7+</sup>

lchown(path: string, uid: number, gid: number): Promise&lt;void&gt;

A
Annie_wang 已提交
2456
Changes 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.
A
annie_wangli 已提交
2457

A
annie_wangli 已提交
2458 2459
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2460
**Parameters**
A
Annie_wang 已提交
2461

A
Annie_wang 已提交
2462 2463
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2464
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2465 2466
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2467

A
Annie_wang 已提交
2468
**Return value**
A
Annie_wang 已提交
2469

A
Annie_wang 已提交
2470 2471 2472
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2473

A
Annie_wang 已提交
2474
**Example**
A
Annie_wang 已提交
2475

A
annie_wangli 已提交
2476
  ```js
A
Annie_wang 已提交
2477 2478
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
A
Annie_wang 已提交
2479
  fileio.lchown(filePath, stat.uid, stat.gid).then(function () {
A
Annie_wang 已提交
2480
      console.info("File owner changed");
A
Annie_wang 已提交
2481 2482
  }).catch(function (err) {
      console.info("chown failed with error:" + err);
A
annie_wangli 已提交
2483 2484 2485 2486 2487 2488 2489 2490
  });
  ```


## fileio.lchown<sup>7+</sup>

lchown(path: string, uid: number, gid: number, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
2491
Changes 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.
A
annie_wangli 已提交
2492

A
annie_wangli 已提交
2493 2494
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2495
**Parameters**
A
Annie_wang 已提交
2496

A
Annie_wang 已提交
2497 2498
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
2499
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
2500 2501 2502
| uid      | number                    | Yes  | New UID.                     |
| gid      | number                    | Yes  | New GID.                     |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file owner is changed asynchronously.|
A
annie_wangli 已提交
2503

A
Annie_wang 已提交
2504
**Example**
A
Annie_wang 已提交
2505

A
annie_wangli 已提交
2506
  ```js
A
Annie_wang 已提交
2507 2508
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
A
Annie_wang 已提交
2509
  fileio.lchown(filePath, stat.uid, stat.gid, function (err) {
A
annie_wangli 已提交
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520
      // Do something.
  });
  ```


## fileio.lchownSync<sup>7+</sup>

lchownSync(path: string, uid: number, gid: number): void

Synchronously changes the file owner based on the file path and changes the owner of the symbolic link (not the referenced file).

A
annie_wangli 已提交
2521 2522
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2523
**Parameters**
A
Annie_wang 已提交
2524

A
Annie_wang 已提交
2525 2526
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2527
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2528 2529
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2530

A
Annie_wang 已提交
2531
**Example**
A
Annie_wang 已提交
2532

A
annie_wangli 已提交
2533
  ```js
A
Annie_wang 已提交
2534 2535 2536
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchownSync(filePath, stat.uid, stat.gid);
A
annie_wangli 已提交
2537 2538 2539 2540 2541 2542 2543
  ```


## fileio.createWatcher<sup>7+</sup>

createWatcher(filename: string, events: number, callback: AsyncCallback&lt;number&gt;): Watcher

A
Annie_wang 已提交
2544
Listens for file or directory changes. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2545

A
annie_wangli 已提交
2546 2547
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2548
**Parameters**
A
Annie_wang 已提交
2549

A
Annie_wang 已提交
2550 2551
| Name  | Type                             | Mandatory| Description                                                        |
| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2552
| filePath | string                            | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2553
| 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.|
A
Annie_wang 已提交
2554
| callback | AsyncCallback&lt;number&gt; | Yes  | Called each time a change is detected.                            |
A
annie_wangli 已提交
2555

A
Annie_wang 已提交
2556
**Return value**
A
Annie_wang 已提交
2557

A
Annie_wang 已提交
2558 2559 2560
  | Type                 | Description        |
  | -------------------- | ---------- |
  | [Watcher](#watcher7) | Promise used to return the **Watcher** instance.|
A
annie_wangli 已提交
2561

A
Annie_wang 已提交
2562
**Example**
A
Annie_wang 已提交
2563

A
annie_wangli 已提交
2564
  ```js
A
Annie_wang 已提交
2565
  let filePath = pathDir +"/test.txt";
A
Annie_wang 已提交
2566 2567
  fileio.createWatcher(filePath, 1, function (number) {
     console.info("Monitoring times: " +number);
A
annie_wangli 已提交
2568
  });
A
Annie_wang 已提交
2569
  
A
annie_wangli 已提交
2570 2571 2572 2573 2574 2575 2576
  ```


## Readout

Obtains the file read result. This class applies only to the **read()** method.

A
annie_wangli 已提交
2577 2578
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2579 2580 2581 2582
| Name       | Type      | Readable  | Writable  | Description               |
| --------- | ---------- | ---- | ---- | ----------------- |
| bytesRead | number     | Yes   | Yes   | Length of the data read.          |
| offset    | number     | Yes   | Yes   | Position of the buffer to which the data will be read in reference to the start address of the buffer.|
A
Annie_wang 已提交
2583
| buffer    | ArrayBuffer | Yes   | Yes   | Buffer for storing the data read.      |
A
annie_wangli 已提交
2584 2585 2586 2587 2588 2589


## Stat

Provides detailed file information. Before calling a method of the **Stat** class, use the [stat()](#fileiostat) method synchronously or asynchronously to create a **Stat** instance.

A
annie_wangli 已提交
2590
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
2591 2592 2593

### Attributes

A
annie_wangli 已提交
2594 2595 2596 2597
| Name    | Type  | Readable  | Writable  | Description                                      |
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| dev    | number | Yes   | No   | Major device number.                           |
| ino    | number | Yes   | No   | File ID. Different files on the same device have different **ino**s.                |
A
Annie_wang 已提交
2598
| 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.|
A
annie_wangli 已提交
2599 2600 2601 2602 2603 2604 2605 2606 2607
| nlink  | number | Yes   | No   | Number of hard links in the file.                                |
| uid    | number | Yes   | No   | User ID, that is ID of the file owner.                               |
| gid    | number | Yes   | No   | Group ID, that is, ID of the user group of the file.                               |
| rdev   | number | Yes   | No   | Minor device number.                           |
| size   | number | Yes   | No   | File size, in bytes. This parameter is valid only for regular files.                  |
| blocks | number | Yes   | No   | Number of blocks occupied by a file. Each block is 512 bytes.                  |
| 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.       |
| 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.       |
| 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.      |
A
annie_wangli 已提交
2608 2609 2610 2611 2612 2613


### isBlockDevice

isBlockDevice(): boolean

A
Annie_wang 已提交
2614
Checks whether this file is a block special file. A block special file supports access by block only, and it is cached when accessed.
A
annie_wangli 已提交
2615

A
annie_wangli 已提交
2616 2617
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2618
**Return value**
A
Annie_wang 已提交
2619

A
Annie_wang 已提交
2620 2621 2622
  | Type     | Description              |
  | ------- | ---------------- |
  | boolean | Whether the file is a block special file.|
A
annie_wangli 已提交
2623

A
Annie_wang 已提交
2624
**Example**
A
Annie_wang 已提交
2625

A
annie_wangli 已提交
2626
  ```js
A
Annie_wang 已提交
2627 2628
  let filePath = pathDir + "/test.txt";
  let isBLockDevice = fileio.statSync(filePath).isBlockDevice();
A
annie_wangli 已提交
2629 2630 2631 2632 2633 2634 2635
  ```


### isCharacterDevice

isCharacterDevice(): boolean

A
Annie_wang 已提交
2636
Checks whether this file is a character special file. A character special file supports random access, and it is not cached when accessed.
A
annie_wangli 已提交
2637

A
annie_wangli 已提交
2638 2639
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2640
**Return value**
A
Annie_wang 已提交
2641

A
Annie_wang 已提交
2642 2643 2644
  | Type     | Description               |
  | ------- | ----------------- |
  | boolean | Whether the file is a character special file.|
A
annie_wangli 已提交
2645

A
Annie_wang 已提交
2646
**Example**
A
Annie_wang 已提交
2647

A
annie_wangli 已提交
2648
  ```js
A
Annie_wang 已提交
2649 2650
  let filePath = pathDir + "/test.txt";
  let isCharacterDevice = fileio.statSync(filePath).isCharacterDevice();
A
annie_wangli 已提交
2651 2652 2653 2654 2655 2656 2657
  ```


### isDirectory

isDirectory(): boolean

A
Annie_wang 已提交
2658
Checks whether this file is a directory.
A
annie_wangli 已提交
2659

A
annie_wangli 已提交
2660 2661
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2662
**Return value**
A
Annie_wang 已提交
2663

A
Annie_wang 已提交
2664 2665 2666
  | Type     | Description           |
  | ------- | ------------- |
  | boolean | Whether the file is a directory.|
A
annie_wangli 已提交
2667

A
Annie_wang 已提交
2668
**Example**
A
Annie_wang 已提交
2669

A
annie_wangli 已提交
2670
  ```js
A
Annie_wang 已提交
2671 2672
  let dirPath = pathDir + "/test";
  let isDirectory = fileio.statSync(dirPath).isDirectory(); 
A
annie_wangli 已提交
2673 2674 2675 2676 2677 2678 2679
  ```


### isFIFO

isFIFO(): boolean

A
Annie_wang 已提交
2680
Checks whether this file is a named pipe (or FIFO). Named pipes are used for inter-process communication.
A
annie_wangli 已提交
2681

A
annie_wangli 已提交
2682 2683
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2684
**Return value**
A
Annie_wang 已提交
2685

A
Annie_wang 已提交
2686 2687 2688
  | Type     | Description                   |
  | ------- | --------------------- |
  | boolean | Whether the file is an FIFO.|
A
annie_wangli 已提交
2689

A
Annie_wang 已提交
2690
**Example**
A
Annie_wang 已提交
2691

A
annie_wangli 已提交
2692
  ```js
A
Annie_wang 已提交
2693 2694
  let filePath = pathDir + "/test.txt";
  let isFIFO = fileio.statSync(filePath).isFIFO(); 
A
annie_wangli 已提交
2695 2696 2697 2698 2699 2700 2701
  ```


### isFile

isFile(): boolean

A
Annie_wang 已提交
2702
Checks whether this file is a regular file.
A
annie_wangli 已提交
2703

A
annie_wangli 已提交
2704 2705
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2706
**Return value**
A
Annie_wang 已提交
2707

A
Annie_wang 已提交
2708 2709 2710
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the file is a regular file.|
A
annie_wangli 已提交
2711

A
Annie_wang 已提交
2712
**Example**
A
Annie_wang 已提交
2713

A
annie_wangli 已提交
2714
  ```js
A
Annie_wang 已提交
2715 2716
  let filePath = pathDir + "/test.txt";
  let isFile = fileio.statSync(filePath).isFile();
A
annie_wangli 已提交
2717 2718 2719 2720 2721 2722 2723
  ```


### isSocket

isSocket(): boolean

A
Annie_wang 已提交
2724
Checks whether this file is a socket.
A
annie_wangli 已提交
2725

A
annie_wangli 已提交
2726 2727
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2728
**Return value**
A
Annie_wang 已提交
2729

A
Annie_wang 已提交
2730 2731 2732
  | Type     | Description            |
  | ------- | -------------- |
  | boolean | Whether the file is a socket.|
A
annie_wangli 已提交
2733

A
Annie_wang 已提交
2734
**Example**
A
Annie_wang 已提交
2735

A
annie_wangli 已提交
2736
  ```js
A
Annie_wang 已提交
2737 2738
  let filePath = pathDir + "/test.txt";
  let isSocket = fileio.statSync(filePath).isSocket(); 
A
annie_wangli 已提交
2739 2740 2741 2742 2743 2744 2745
  ```


### isSymbolicLink

isSymbolicLink(): boolean

A
Annie_wang 已提交
2746
Checks whether this file is a symbolic link.
A
annie_wangli 已提交
2747

A
annie_wangli 已提交
2748 2749
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2750
**Return value**
A
Annie_wang 已提交
2751

A
Annie_wang 已提交
2752 2753 2754
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the file is a symbolic link.|
A
annie_wangli 已提交
2755

A
Annie_wang 已提交
2756
**Example**
A
Annie_wang 已提交
2757

A
annie_wangli 已提交
2758
  ```js
A
Annie_wang 已提交
2759 2760
  let filePath = pathDir + "/test";
  let isSymbolicLink = fileio.statSync(filePath).isSymbolicLink(); 
A
annie_wangli 已提交
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
  ```


## Watcher<sup>7+</sup>

Listens for the changes of a file. You can call the **Watcher.stop()** method synchronously or asynchronously to stop the listening.


### stop<sup>7+</sup>

A
annie_wangli 已提交
2771
stop(): Promise&lt;void&gt;
A
annie_wangli 已提交
2772

A
Annie_wang 已提交
2773
Stops the **watcher** instance. This API uses a promise to return the result.
A
annie_wangli 已提交
2774

A
annie_wangli 已提交
2775 2776
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2777
**Example**
A
Annie_wang 已提交
2778

A
annie_wangli 已提交
2779
  ```js
A
Annie_wang 已提交
2780
  let filePath = path + "/test.txt";
A
Annie_wang 已提交
2781 2782
  let watcher = fileio.createWatcher(filePath, 1, function (number) {
      console.info("Monitoring times: " +number);
A
Annie_wang 已提交
2783
  });
A
Annie_wang 已提交
2784
  watcher.stop().then(function () {
A
Annie_wang 已提交
2785 2786
       console.info("Watcher stopped");
  });
A
annie_wangli 已提交
2787 2788 2789 2790 2791
  ```


### stop<sup>7+</sup>

A
annie_wangli 已提交
2792
stop(callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2793

A
Annie_wang 已提交
2794
Stops the **watcher** instance. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2795

A
annie_wangli 已提交
2796 2797
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2798
**Parameters**
A
Annie_wang 已提交
2799

A
Annie_wang 已提交
2800 2801 2802
  | Name     | Type                       | Mandatory  | Description                    |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when **watcher** is stopped asynchronously.|
A
annie_wangli 已提交
2803

A
Annie_wang 已提交
2804
**Example**
A
Annie_wang 已提交
2805

A
annie_wangli 已提交
2806
  ```js
A
Annie_wang 已提交
2807
  let filePath = path +"/test.txt";
A
Annie_wang 已提交
2808 2809
  let watcher = fileio.createWatcher(filePath, 1, function (number) {
      console.info("Monitoring times: " +number);
A
annie_wangli 已提交
2810
  });
A
Annie_wang 已提交
2811
  watcher.stop(function () {
A
Annie_wang 已提交
2812 2813
      console.info("Watcher stopped");
  })
A
annie_wangli 已提交
2814
  ```
A
annie_wangli 已提交
2815

A
Annie_wang 已提交
2816

A
Annie_wang 已提交
2817
## Stream
A
annie_wangli 已提交
2818

A
Annie_wang 已提交
2819
Provides file stream management. Before calling a method of the **Stream** class, use the **createStream()** method synchronously or asynchronously to create a **Stream** instance.
A
annie_wangli 已提交
2820 2821 2822 2823 2824 2825


### close<sup>7+</sup>

close(): Promise&lt;void&gt;

A
Annie_wang 已提交
2826
Closes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2827 2828 2829

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2830
**Return value**
A
Annie_wang 已提交
2831

A
Annie_wang 已提交
2832 2833 2834
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream close result.|
A
annie_wangli 已提交
2835

A
Annie_wang 已提交
2836
**Example**
A
Annie_wang 已提交
2837

A
annie_wangli 已提交
2838
  ```js
A
Annie_wang 已提交
2839 2840
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
Annie_wang 已提交
2841
  ss.close().then(function () {
A
Annie_wang 已提交
2842
      console.info("File stream closed");
A
Annie_wang 已提交
2843 2844
  }).catch(function (err) {
      console.info("close fileStream  failed with error:" + err);
A
annie_wangli 已提交
2845 2846 2847 2848 2849 2850 2851 2852
  });
  ```


### close<sup>7+</sup>

close(callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
2853
Closes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2854 2855 2856

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2857
**Parameters**
A
Annie_wang 已提交
2858

A
Annie_wang 已提交
2859 2860 2861
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|
A
annie_wangli 已提交
2862

A
Annie_wang 已提交
2863
**Example**
A
Annie_wang 已提交
2864

A
annie_wangli 已提交
2865
  ```js
A
Annie_wang 已提交
2866 2867
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2868
  ss.close(function (err) {
A
Annie_wang 已提交
2869
      // Do something.
A
annie_wangli 已提交
2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881
  });
  ```


### closeSync

closeSync(): void

Synchronously closes the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2882
**Example**
A
Annie_wang 已提交
2883

A
annie_wangli 已提交
2884
  ```js
A
Annie_wang 已提交
2885 2886
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2887 2888 2889 2890 2891 2892 2893 2894
  ss.closeSync();
  ```


### flush<sup>7+</sup>

flush(): Promise&lt;void&gt;

A
Annie_wang 已提交
2895
Flushes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2896 2897 2898

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2899
**Return value**
A
Annie_wang 已提交
2900

A
Annie_wang 已提交
2901 2902 2903
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream flushing result.|
A
annie_wangli 已提交
2904

A
Annie_wang 已提交
2905
**Example**
A
Annie_wang 已提交
2906

A
annie_wangli 已提交
2907
  ```js
A
Annie_wang 已提交
2908 2909
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
Annie_wang 已提交
2910
  ss.flush().then(function () {
A
Annie_wang 已提交
2911
      console.info("Stream flushed");
A
Annie_wang 已提交
2912 2913
  }).catch(function (err) {
      console.info("flush failed with error:" + err);
A
annie_wangli 已提交
2914 2915 2916 2917 2918 2919 2920 2921
  });
  ```


### flush<sup>7+</sup>

flush(callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
2922
Flushes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2923 2924 2925

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2926
**Parameters**
A
Annie_wang 已提交
2927

A
Annie_wang 已提交
2928 2929 2930
  | Name     | Type                       | Mandatory  | Description            |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is asynchronously flushed.|
A
annie_wangli 已提交
2931

A
Annie_wang 已提交
2932
**Example**
A
Annie_wang 已提交
2933

A
annie_wangli 已提交
2934
  ```js
A
Annie_wang 已提交
2935 2936
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2937
  ss.flush(function (err) {
A
Annie_wang 已提交
2938
      // Do something.
A
annie_wangli 已提交
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
  });
  ```


### flushSync<sup>7+</sup>

flushSync(): void

Synchronously flushes the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2951
**Example**
A
Annie_wang 已提交
2952

A
annie_wangli 已提交
2953
  ```js
A
Annie_wang 已提交
2954 2955
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2956 2957 2958 2959 2960 2961
  ss.flushSync();
  ```


### write<sup>7+</sup>

A
Annie_wang 已提交
2962
write(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise&lt;number&gt;
A
annie_wangli 已提交
2963

A
Annie_wang 已提交
2964
Writes data into the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2965 2966 2967

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2968
**Parameters**
A
Annie_wang 已提交
2969

A
Annie_wang 已提交
2970 2971 2972 2973
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer\|string | Yes   | Data to write. It can be a string or data from a buffer.                    |
  | 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 |
A
annie_wangli 已提交
2974

A
Annie_wang 已提交
2975
**Return value**
A
Annie_wang 已提交
2976

A
Annie_wang 已提交
2977 2978 2979
  | Type                   | Description      |
  | --------------------- | -------- |
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
A
annie_wangli 已提交
2980

A
Annie_wang 已提交
2981
**Example**
A
Annie_wang 已提交
2982

A
annie_wangli 已提交
2983
  ```js
A
Annie_wang 已提交
2984 2985
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
Annie_wang 已提交
2986 2987 2988 2989
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number) {
      console.info("write succeed and size is:" + number);
  }).catch(function (err) {
      console.info("write failed with error:" + err);
A
annie_wangli 已提交
2990 2991 2992 2993 2994 2995
  });
  ```


### write<sup>7+</sup>

A
Annie_wang 已提交
2996
write(buffer: ArrayBuffer|string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback&lt;number&gt;): void
A
annie_wangli 已提交
2997

A
Annie_wang 已提交
2998
Writes data into the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2999 3000 3001

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3002
**Parameters**
A
Annie_wang 已提交
3003

A
Annie_wang 已提交
3004 3005 3006 3007 3008
  | Name  | Type                           | Mandatory| Description                                                        |
  | -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
  | buffer   | ArrayBuffer\|string | Yes  | Data to write. It can be a string or data from a buffer.                    |
  | 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|
  | callback | AsyncCallback&lt;number&gt;     | Yes  | Callback invoked when the data is written asynchronously.                              |
A
annie_wangli 已提交
3009

A
Annie_wang 已提交
3010
**Example**
A
Annie_wang 已提交
3011

A
annie_wangli 已提交
3012
  ```js
A
Annie_wang 已提交
3013 3014
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
3015
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
A
Annie_wang 已提交
3016
      if (bytesWritten) {
A
Annie_wang 已提交
3017
         // Do something.
A
Annie_wang 已提交
3018
         console.info("write succeed and size is:" + bytesWritten);
A
annie_wangli 已提交
3019 3020 3021 3022 3023 3024 3025
      }
  });
  ```


### writeSync<sup>7+</sup>

A
Annie_wang 已提交
3026
writeSync(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number
A
annie_wangli 已提交
3027 3028 3029 3030 3031

Synchronously writes data into the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3032
**Parameters**
A
Annie_wang 已提交
3033

A
Annie_wang 已提交
3034 3035 3036 3037
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer\|string | Yes   | Data to write. It can be a string or data from a buffer.                    |
  | 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 |
A
annie_wangli 已提交
3038

A
Annie_wang 已提交
3039
**Return value**
A
Annie_wang 已提交
3040

A
Annie_wang 已提交
3041 3042 3043
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data written in the file.|
A
annie_wangli 已提交
3044

A
Annie_wang 已提交
3045
**Example**
A
Annie_wang 已提交
3046

A
annie_wangli 已提交
3047
  ```js
A
Annie_wang 已提交
3048 3049
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath,"r+");
A
annie_wangli 已提交
3050 3051 3052 3053 3054 3055
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


### read<sup>7+</sup>

A
Annie_wang 已提交
3056
read(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): Promise&lt;ReadOut&gt;
A
annie_wangli 已提交
3057

A
Annie_wang 已提交
3058
Reads data from the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
3059 3060 3061

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3062
**Parameters**
A
Annie_wang 已提交
3063

A
Annie_wang 已提交
3064 3065 3066 3067
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
  | 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 |
A
annie_wangli 已提交
3068

A
Annie_wang 已提交
3069
**Return value**
A
Annie_wang 已提交
3070

A
Annie_wang 已提交
3071 3072 3073
  | Type                                | Description    |
  | ---------------------------------- | ------ |
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
3074

A
Annie_wang 已提交
3075
**Example**
A
Annie_wang 已提交
3076

A
annie_wangli 已提交
3077
  ```js
A
Annie_wang 已提交
3078 3079
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
A
Annie_wang 已提交
3080
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut) {
A
Annie_wang 已提交
3081
      console.info("Read data successfully");
A
Annie_wang 已提交
3082
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
Annie_wang 已提交
3083 3084
  }).catch(function (err) {
      console.info("read data failed with error:" + err);
A
annie_wangli 已提交
3085 3086 3087 3088 3089 3090
  });
  ```


### read<sup>7+</sup>

A
Annie_wang 已提交
3091
read(buffer: ArrayBuffer, options: { position?: number; offset?: number; length?: number; }, callback: AsyncCallback&lt;ReadOut&gt;): void
A
annie_wangli 已提交
3092

A
Annie_wang 已提交
3093
Reads data from the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3094 3095 3096

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3097
**Parameters**
A
Annie_wang 已提交
3098

A
Annie_wang 已提交
3099 3100 3101 3102 3103
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file read.                             |
  | 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 |
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when data is read asynchronously from the stream.                        |
A
annie_wangli 已提交
3104

A
Annie_wang 已提交
3105
**Example**
A
Annie_wang 已提交
3106

A
annie_wangli 已提交
3107
  ```js
A
Annie_wang 已提交
3108 3109
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
3110
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
A
Annie_wang 已提交
3111
      if (readOut) {
A
Annie_wang 已提交
3112
          console.info("Read data successfully");
A
Annie_wang 已提交
3113
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
3114 3115 3116 3117 3118 3119 3120
      }
  });
  ```


### readSync<sup>7+</sup>

A
Annie_wang 已提交
3121
readSync(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): number
A
annie_wangli 已提交
3122 3123 3124 3125 3126

Synchronously reads data from the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3127 3128
**Parameters**

A
Annie_wang 已提交
3129 3130 3131 3132
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
  | 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 |
A
Annie_wang 已提交
3133 3134

**Return value**
A
annie_wangli 已提交
3135

A
Annie_wang 已提交
3136 3137 3138
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data read.|
A
annie_wangli 已提交
3139

A
Annie_wang 已提交
3140
**Example**
A
Annie_wang 已提交
3141

A
annie_wangli 已提交
3142
  ```js
A
Annie_wang 已提交
3143 3144
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
3145 3146 3147 3148 3149 3150
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
  ```


## Dir

A
Annie_wang 已提交
3151
Manages directories. Before calling a method of the **Dir** class, use the **opendir()** method synchronously or asynchronously to create a **Dir** instance.
A
annie_wangli 已提交
3152 3153 3154 3155 3156 3157


### read

read(): Promise&lt;Dirent&gt;

A
Annie_wang 已提交
3158
Reads the next directory entry. This API uses a promise to return the result.
A
annie_wangli 已提交
3159 3160 3161

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3162
**Return value**
A
Annie_wang 已提交
3163

A
Annie_wang 已提交
3164 3165 3166
  | Type                              | Description           |
  | -------------------------------- | ------------- |
  | Promise&lt;[Dirent](#dirent)&gt; | Promise used to return the directory entry read.|
A
annie_wangli 已提交
3167

A
Annie_wang 已提交
3168
**Example**
A
Annie_wang 已提交
3169

A
annie_wangli 已提交
3170
  ```js
A
Annie_wang 已提交
3171 3172 3173 3174
  dir.read().then(function (dirent) {
      console.log("read succeed, the name of dirent is " + dirent.name);
  }).catch(function (err) {
      console.info("read failed with error:" + err);
A
annie_wangli 已提交
3175 3176 3177 3178 3179 3180 3181 3182
  });
  ```


### read

read(callback: AsyncCallback&lt;Dirent&gt;): void

A
Annie_wang 已提交
3183
Reads the next directory entry. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3184 3185 3186

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3187
**Parameters**
A
Annie_wang 已提交
3188

A
Annie_wang 已提交
3189 3190 3191
  | Name     | Type                                    | Mandatory  | Description              |
  | -------- | -------------------------------------- | ---- | ---------------- |
  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | Yes   | Callback invoked when the next directory entry is asynchronously read.|
A
annie_wangli 已提交
3192

A
Annie_wang 已提交
3193
**Example**
A
Annie_wang 已提交
3194

A
annie_wangli 已提交
3195 3196
  ```js
  dir.read(function (err, dirent) {
A
Annie_wang 已提交
3197
      if (dirent) {
A
Annie_wang 已提交
3198
          // Do something.
A
Annie_wang 已提交
3199
          console.log("read succeed, the name of file is " + dirent.name);
A
annie_wangli 已提交
3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212
      }
  });
  ```


### readSync

readSync(): Dirent

Synchronously reads the next directory entry.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3213
**Return value**
A
Annie_wang 已提交
3214

A
Annie_wang 已提交
3215 3216 3217
  | Type               | Description      |
  | ----------------- | -------- |
  | [Dirent](#dirent) | Directory entry read.|
A
annie_wangli 已提交
3218

A
Annie_wang 已提交
3219
**Example**
A
Annie_wang 已提交
3220

A
annie_wangli 已提交
3221 3222 3223 3224 3225
  ```js
  let dirent = dir.readSync();
  ```


A
Annie_wang 已提交
3226 3227 3228 3229 3230 3231 3232 3233 3234
### close<sup>7+</sup>

close(): Promise&lt;void&gt;

Closes 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.

**System capability**: SystemCapability.FileManagement.File.FileIO

**Example**
A
Annie_wang 已提交
3235

A
Annie_wang 已提交
3236
  ```js
A
Annie_wang 已提交
3237
  dir.close().then(function (err) {
A
Annie_wang 已提交
3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
      console.info("close dir successfully");
  });
  ```


  ### close<sup>7+</sup>

close(callback: AsyncCallback&lt;void&gt;): void

Closes 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.

**System capability**: SystemCapability.FileManagement.File.FileIO

**Example**
A
Annie_wang 已提交
3252

A
Annie_wang 已提交
3253
  ```js
A
Annie_wang 已提交
3254
  dir.close(function (err) {
A
Annie_wang 已提交
3255 3256 3257 3258 3259
      console.info("close dir successfully");
  });
  ```


A
annie_wangli 已提交
3260 3261 3262 3263 3264 3265 3266 3267
### closeSync

closeSync(): void

Closes a directory. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3268
**Example**
A
Annie_wang 已提交
3269

A
annie_wangli 已提交
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291
  ```js
  dir.closeSync();
  ```


## Dirent

Provides 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.

**System capability**: SystemCapability.FileManagement.File.FileIO

### Attributes

| Name  | Type  | Readable  | Writable  | Description     |
| ---- | ------ | ---- | ---- | ------- |
| name | string | Yes   | No   | Directory entry name.|


### isBlockDevice

isBlockDevice(): boolean

A
Annie_wang 已提交
3292
Checks whether this directory entry is a block special file. A block special file supports access by block only, and it is cached when accessed.
A
annie_wangli 已提交
3293 3294 3295

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3296
**Return value**
A
Annie_wang 已提交
3297

A
Annie_wang 已提交
3298 3299 3300
  | Type     | Description              |
  | ------- | ---------------- |
  | boolean | Whether the directory entry is a block special file.|
A
annie_wangli 已提交
3301

A
Annie_wang 已提交
3302
**Example**
A
Annie_wang 已提交
3303

A
annie_wangli 已提交
3304
  ```js
A
Annie_wang 已提交
3305
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
  let isBLockDevice = dir.readSync().isBlockDevice();
  ```


### isCharacterDevice

isCharacterDevice(): boolean

Checks whether a directory entry is a character special file. A character special file supports random access, and it is not cached when accessed.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3318
**Return value**
A
Annie_wang 已提交
3319

A
Annie_wang 已提交
3320 3321 3322
  | Type     | Description               |
  | ------- | ----------------- |
  | boolean | Whether the directory entry is a character special file.|
A
annie_wangli 已提交
3323

A
Annie_wang 已提交
3324
**Example**
A
Annie_wang 已提交
3325

A
annie_wangli 已提交
3326
  ```js
A
Annie_wang 已提交
3327
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339
  let isCharacterDevice = dir.readSync().isCharacterDevice(); 
  ```


### isDirectory

isDirectory(): boolean

Checks whether a directory entry is a directory.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3340
**Return value**
A
Annie_wang 已提交
3341

A
Annie_wang 已提交
3342 3343 3344
  | Type     | Description           |
  | ------- | ------------- |
  | boolean | Whether the directory entry is a directory.|
A
annie_wangli 已提交
3345

A
Annie_wang 已提交
3346
**Example**
A
Annie_wang 已提交
3347

A
annie_wangli 已提交
3348
  ```js
A
Annie_wang 已提交
3349
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3350 3351 3352 3353 3354 3355 3356 3357
  let isDirectory = dir.readSync().isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

A
Annie_wang 已提交
3358
Checks whether this directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication.
A
annie_wangli 已提交
3359 3360 3361

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3362
**Return value**
A
Annie_wang 已提交
3363

A
Annie_wang 已提交
3364 3365 3366
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a FIFO.|
A
annie_wangli 已提交
3367

A
Annie_wang 已提交
3368
**Example**
A
Annie_wang 已提交
3369

A
annie_wangli 已提交
3370
  ```js
A
Annie_wang 已提交
3371
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383
  let isFIFO = dir.readSync().isFIFO(); 
  ```


### isFile

isFile(): boolean

Checks whether a directory entry is a regular file.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3384
**Return value**
A
Annie_wang 已提交
3385

A
Annie_wang 已提交
3386 3387 3388
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a regular file.|
A
annie_wangli 已提交
3389

A
Annie_wang 已提交
3390
**Example**
A
Annie_wang 已提交
3391

A
annie_wangli 已提交
3392
  ```js
A
Annie_wang 已提交
3393
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405
  let isFile = dir.readSync().isFile(); 
  ```


### isSocket

isSocket(): boolean

Checks whether a directory entry is a socket.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3406
**Return value**
A
Annie_wang 已提交
3407

A
Annie_wang 已提交
3408 3409 3410
  | Type     | Description            |
  | ------- | -------------- |
  | boolean | Whether the directory entry is a socket.|
A
annie_wangli 已提交
3411

A
Annie_wang 已提交
3412
**Example**
A
Annie_wang 已提交
3413

A
annie_wangli 已提交
3414
  ```js
A
Annie_wang 已提交
3415
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427
  let isSocket = dir.readSync().isSocket(); 
  ```


### isSymbolicLink

isSymbolicLink(): boolean

Checks whether a directory entry is a symbolic link.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3428
**Return value**
A
Annie_wang 已提交
3429

A
Annie_wang 已提交
3430 3431 3432
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a symbolic link.|
A
annie_wangli 已提交
3433

A
Annie_wang 已提交
3434
**Example**
A
Annie_wang 已提交
3435

A
annie_wangli 已提交
3436
  ```js
A
Annie_wang 已提交
3437
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3438 3439
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```