js-apis-fileio.md 133.5 KB
Newer Older
A
Annie_wang 已提交
1 2 3
# File Management

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**<br>
A
annie_wangli 已提交
6
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
7

A
Annie_wang 已提交
8

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

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

A
annie_wangli 已提交
15

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

A
Annie_wang 已提交
18 19 20 21 22 23 24 25 26
Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows:
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
 let context = featureAbility.getContext();
 let path = '';
 context.getFilesDir().then((data) => {
      path = data;
 })
 ```
Z
zengyawen 已提交
27 28


A
annie_wangli 已提交
29 30 31 32
## fileio.stat

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

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

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

A
Annie_wang 已提交
37 38
**Parameters**

A
Annie_wang 已提交
39 40
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
41
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
42

A
Annie_wang 已提交
43 44
**Return value**

A
annie_wangli 已提交
45 46
  | Type                          | Description        |
  | ---------------------------- | ---------- |
A
annie_wangli 已提交
47 48
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information obtained.|

A
Annie_wang 已提交
49
**Example**
A
Annie_wang 已提交
50

A
annie_wangli 已提交
51 52
  ```js
  fileio.stat(path).then(function(stat){
A
Annie_wang 已提交
53
      console.info("Got file info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
54
  }).catch(function(err){
A
Annie_wang 已提交
55
      console.info("Failed to get file info. Error:"+ err);
A
annie_wangli 已提交
56 57 58 59 60 61 62 63
  });
  ```


## fileio.stat

stat(path:string, callback:AsyncCallback&lt;Stat&gt;): void

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

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

A
Annie_wang 已提交
68
**Parameters**
A
Annie_wang 已提交
69

A
Annie_wang 已提交
70 71
| Name  | Type                              | Mandatory| Description                          |
| -------- | ---------------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
72
| path     | string                             | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
73
| callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes  | Callback invoked to return the file information obtained.|
A
annie_wangli 已提交
74

A
Annie_wang 已提交
75
**Example**
A
Annie_wang 已提交
76

A
annie_wangli 已提交
77 78 79 80 81 82 83 84 85 86
  ```js
  fileio.stat(path, function (err, stat) {
      // Example code in Stat
  });
  ```


## fileio.statSync

statSync(path:string): Stat
Z
zengyawen 已提交
87 88 89

Synchronously obtains file information.

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

A
Annie_wang 已提交
92
**Parameters**
A
Annie_wang 已提交
93

A
Annie_wang 已提交
94 95
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
96
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
97 98


A
Annie_wang 已提交
99
**Return value**
A
Annie_wang 已提交
100

A
annie_wangli 已提交
101 102
  | Type           | Description        |
  | ------------- | ---------- |
A
annie_wangli 已提交
103 104
  | [Stat](#stat) | File information obtained.|

A
Annie_wang 已提交
105
**Example**
A
Annie_wang 已提交
106

A
annie_wangli 已提交
107 108 109 110 111 112 113 114 115 116
  ```js
  let stat = fileio.statSync(path);
  // Example code in Stat
  ```


## fileio.opendir

opendir(path: string): Promise&lt;Dir&gt;

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

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

A
Annie_wang 已提交
121
**Parameters**
A
Annie_wang 已提交
122

A
Annie_wang 已提交
123 124 125
| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
126

A
Annie_wang 已提交
127
**Return value**
A
Annie_wang 已提交
128

A
annie_wangli 已提交
129 130
  | Type                        | Description      |
  | -------------------------- | -------- |
A
Annie_wang 已提交
131
  | Promise&lt;[Dir](#dir)&gt; | Promise used to return the **Dir** object.|
A
annie_wangli 已提交
132

A
Annie_wang 已提交
133
**Example**
A
Annie_wang 已提交
134

A
annie_wangli 已提交
135 136
  ```js
  fileio.opendir(path).then(function(dir){
A
Annie_wang 已提交
137
      console.info("Directory opened:"+ JSON.stringify(dir));
A
annie_wangli 已提交
138
  }).catch(function(err){
A
Annie_wang 已提交
139
      console.info("Failed to open the directory. Error:"+ err);
A
annie_wangli 已提交
140 141 142 143 144 145 146 147
  });
  ```


## fileio.opendir

opendir(path: string, callback: AsyncCallback&lt;Dir&gt;): void

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

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

A
Annie_wang 已提交
152 153
**Parameters**

A
Annie_wang 已提交
154 155 156 157
| Name  | Type                            | Mandatory| Description                          |
| -------- | -------------------------------- | ---- | ------------------------------ |
| path     | string                           | Yes  | Application sandbox path of the directory to open.|
| callback | AsyncCallback&lt;[Dir](#dir)&gt; | Yes  | Callback invoked when the directory is open asynchronously.  |
A
annie_wangli 已提交
158

A
Annie_wang 已提交
159
**Example**
A
Annie_wang 已提交
160

A
annie_wangli 已提交
161 162 163
  ```js
  fileio.opendir(path, function (err, dir) { 
      // Example code in Dir struct
A
annie_wangli 已提交
164
      // Use read/readSync/close.
A
annie_wangli 已提交
165 166 167 168 169 170 171
  });
  ```


## fileio.opendirSync

opendirSync(path: string): Dir
Z
zengyawen 已提交
172 173 174

Synchronously opens a directory.

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

A
annie_wangli 已提交
177

A
Annie_wang 已提交
178 179
**Parameters**

A
Annie_wang 已提交
180 181 182
| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
183

A
Annie_wang 已提交
184
**Return value**
A
Annie_wang 已提交
185

A
annie_wangli 已提交
186 187
  | Type         | Description      |
  | ----------- | -------- |
A
annie_wangli 已提交
188 189
  | [Dir](#dir) | A **Dir** instance corresponding to the directory.|

A
Annie_wang 已提交
190
**Example**
A
Annie_wang 已提交
191

A
annie_wangli 已提交
192 193 194 195 196 197 198 199 200 201 202
  ```js
  let dir = fileio.opendirSync(path);
  // Example code in Dir struct
  // Use read/readSync/close.
  ```


## fileio.access

access(path: string, mode?: number): Promise&lt;void&gt;

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

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

A
Annie_wang 已提交
207 208
**Parameters**

A
Annie_wang 已提交
209 210
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
211
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
212
| mode   | number | No  | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (&#124;). The default value is **0**.<br>The options are as follows:<br>-&nbsp;**0**: check whether the file exists.<br>-&nbsp;**1**: check whether the current process has the execute permission on the file.<br>-&nbsp;**2**: check whether the current process has the write permission on the file.<br>-&nbsp;**4**: check whether the current process has the read permission on the file.|
A
annie_wangli 已提交
213

A
Annie_wang 已提交
214
**Return value**
A
Annie_wang 已提交
215

A
annie_wangli 已提交
216 217
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
218
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
219

A
Annie_wang 已提交
220
**Example**
A
Annie_wang 已提交
221

A
annie_wangli 已提交
222 223
  ```js
  fileio.access(path).then(function() {
A
Annie_wang 已提交
224
      console.info("Access successful");
A
annie_wangli 已提交
225
  }).catch(function(err){
A
Annie_wang 已提交
226
      console.info("Access failed. Error:"+ err);
A
annie_wangli 已提交
227 228 229 230 231 232
  });
  ```


## fileio.access

A
annie_wangli 已提交
233
access(path: string, mode: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
234

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

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

A
Annie_wang 已提交
239
**Parameters**
A
Annie_wang 已提交
240

A
Annie_wang 已提交
241 242
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
243
| path     | string                    | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
244 245
| mode     | number                    | No  | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (&#124;). The default value is **0**.<br>The options are as follows:<br>-&nbsp;**0**: check whether the file exists.<br>-&nbsp;**1**: check whether the current process has the execute permission on the file.<br>-&nbsp;**2**: check whether the current process has the write permission on the file.<br>-&nbsp;**4**: check whether the current process has the read permission on the file.|
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously checked.                |
A
annie_wangli 已提交
246

A
Annie_wang 已提交
247
**Example**
A
Annie_wang 已提交
248

A
annie_wangli 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261
  ```js
  fileio.access(path, function (err) {
      // Do something.
  });
  ```


## fileio.accessSync

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

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

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

A
Annie_wang 已提交
264
**Parameters**
A
Annie_wang 已提交
265

A
Annie_wang 已提交
266 267
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
268
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
269
| mode   | number | No  | Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (&#124;). The default value is **0**.<br>The options are as follows:<br>-&nbsp;**0**: check whether the file exists.<br>-&nbsp;**1**: check whether the current process has the execute permission on the file.<br>-&nbsp;**2**: check whether the current process has the write permission on the file.<br>-&nbsp;**4**: check whether the current process has the read permission on the file.|
A
annie_wangli 已提交
270

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

A
annie_wangli 已提交
273 274 275 276
  ```js
  try {
      fileio.accessSync(path);
  } catch(err) {
A
Annie_wang 已提交
277
      console.info("accessSync failed. Error:"+ err);
A
annie_wangli 已提交
278 279 280 281 282 283 284 285
  }
  ```


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

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

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

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

A
Annie_wang 已提交
290
**Parameters**
A
Annie_wang 已提交
291

A
annie_wangli 已提交
292 293 294
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
295

A
Annie_wang 已提交
296
**Return value**
A
Annie_wang 已提交
297

A
annie_wangli 已提交
298 299
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
300
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
301

A
Annie_wang 已提交
302
**Example**
A
Annie_wang 已提交
303

A
annie_wangli 已提交
304 305 306
  ```js
  let fd = fileio.openSync(path);
  fileio.close(fd).then(function(){
A
Annie_wang 已提交
307
      console.info("File closed");
A
annie_wangli 已提交
308
  }).catch(function(err){
A
Annie_wang 已提交
309
      console.info("Failed to close the file. Error:"+ err);
A
annie_wangli 已提交
310 311 312 313 314 315 316 317
  });
  ```


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

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

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

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

A
Annie_wang 已提交
322
**Parameters**
A
Annie_wang 已提交
323

A
annie_wangli 已提交
324 325 326 327
  | 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 已提交
328

A
Annie_wang 已提交
329
**Example**
A
Annie_wang 已提交
330

A
annie_wangli 已提交
331 332 333 334 335 336 337 338 339 340 341
  ```js
  let fd = fileio.openSync(path);
  fileio.close(fd, function (err) {
      // Do something.
  });
  ```


## fileio.closeSync

closeSync(fd: number): void
Z
zengyawen 已提交
342 343 344

Synchronously closes a file.

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

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

A
annie_wangli 已提交
349 350 351
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
352

A
Annie_wang 已提交
353
**Example**
A
Annie_wang 已提交
354

A
annie_wangli 已提交
355 356 357 358 359 360 361 362 363
  ```js
  fileio.closeSync(fd);
  ```


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

close(): Promise&lt;void&gt;

A
Annie_wang 已提交
364
Closes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
365

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

A
Annie_wang 已提交
368
**Return value**
A
Annie_wang 已提交
369

A
annie_wangli 已提交
370 371
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
372
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
373

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

A
annie_wangli 已提交
376 377
  ```js
  fileio.close().then(function(){
A
Annie_wang 已提交
378
      console.info("File stream closed");
A
annie_wangli 已提交
379
  }).catch(function(err){
A
Annie_wang 已提交
380
      console.info("Failed to close the file stream. Error:"+ err);
A
annie_wangli 已提交
381 382 383 384 385 386 387 388
  });
  ```


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

close(callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
389
Closes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
390

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

A
Annie_wang 已提交
393
**Parameters**
A
Annie_wang 已提交
394

A
annie_wangli 已提交
395 396 397
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|
A
annie_wangli 已提交
398

A
Annie_wang 已提交
399
**Example**
A
Annie_wang 已提交
400

A
annie_wangli 已提交
401 402 403 404 405 406 407 408 409 410 411
  ```js
  fileio.close(function(err){
      // Do something.
  });
  ```


## fileio.copyFile

copyFile(src:string | number, dest:string | number, mode?:number):Promise&lt;void&gt;

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

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

A
Annie_wang 已提交
416
**Parameters**
A
Annie_wang 已提交
417

A
annie_wangli 已提交
418 419 420 421 422
  | Name | Type                        | Mandatory  | Description                                      |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
  | src  | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the file to copy.                     |
  | dest | string&nbsp;\|&nbsp;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 已提交
423

A
Annie_wang 已提交
424
**Return value**
A
Annie_wang 已提交
425

A
annie_wangli 已提交
426 427
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
428
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
429

A
Annie_wang 已提交
430
**Example**
A
Annie_wang 已提交
431

A
annie_wangli 已提交
432 433
  ```js
  fileio.copyFile(src, dest).then(function(){
A
Annie_wang 已提交
434
      console.info("File copied");
A
annie_wangli 已提交
435
  }).catch(function(err){
A
Annie_wang 已提交
436
      console.info("Failed to copy the file. Error:"+ err);
A
annie_wangli 已提交
437 438 439 440 441 442
  });
  ```


## fileio.copyFile

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

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

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

A
Annie_wang 已提交
449
**Parameters**
A
Annie_wang 已提交
450

A
annie_wangli 已提交
451 452 453 454 455 456
  | Name     | Type                        | Mandatory  | Description                                      |
  | -------- | -------------------------- | ---- | ---------------------------------------- |
  | src      | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the file to copy.                     |
  | dest     | string&nbsp;\|&nbsp;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 已提交
457

A
Annie_wang 已提交
458
**Example**
A
Annie_wang 已提交
459

A
annie_wangli 已提交
460 461 462 463 464 465 466 467 468
  ```js
  fileio.copyFile(src, dest, function (err) {
      // Do something.
  });
  ```


## fileio.copyFileSync

A
annie_wangli 已提交
469
copyFileSync(src: string | number, dest: string | number, mode?: number): void
Z
zengyawen 已提交
470 471 472

Synchronously copies a file.

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

A
Annie_wang 已提交
475
**Parameters**
A
Annie_wang 已提交
476

A
annie_wangli 已提交
477 478 479 480 481
  | Name | Type                        | Mandatory  | Description                                      |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
  | src  | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the file to copy.                     |
  | dest | string&nbsp;\|&nbsp;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 已提交
482

A
Annie_wang 已提交
483
**Example**
A
Annie_wang 已提交
484

A
annie_wangli 已提交
485 486 487 488 489 490 491 492 493
  ```js
  fileio.copyFileSync(src, dest);
  ```


## fileio.mkdir

mkdir(path:string, mode?: number): Promise&lt;void&gt;

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

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

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

A
Annie_wang 已提交
500 501
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
502
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
503
| mode   | number | No  | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o775**.<br>-&nbsp;**0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
A
annie_wangli 已提交
504

A
Annie_wang 已提交
505
**Return value**
A
Annie_wang 已提交
506

A
annie_wangli 已提交
507 508
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
509
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
510

A
Annie_wang 已提交
511
**Example**
A
Annie_wang 已提交
512

A
annie_wangli 已提交
513 514
  ```js
  fileio.mkdir(path).then(function() {
A
Annie_wang 已提交
515
      console.info("Directory created");
A
annie_wangli 已提交
516
  }).catch(function (error){
A
Annie_wang 已提交
517
      console.info("Failed to create the directory. Error:"+ error);
A
annie_wangli 已提交
518 519 520 521 522 523
  });
  ```


## fileio.mkdir

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

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

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

A
Annie_wang 已提交
530
**Parameters**
A
Annie_wang 已提交
531

A
Annie_wang 已提交
532 533
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
534
| path     | string                    | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
535 536
| mode     | number                    | No  | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o775**.<br>-&nbsp;**0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is created asynchronously.                            |
A
annie_wangli 已提交
537

A
Annie_wang 已提交
538
**Example**
A
Annie_wang 已提交
539

A
annie_wangli 已提交
540 541
  ```js
  fileio.mkdir(path, function(err) {
A
Annie_wang 已提交
542
    console.info("Directory created");
A
annie_wangli 已提交
543 544 545 546 547 548
  });
  ```


## fileio.mkdirSync

A
annie_wangli 已提交
549
mkdirSync(path: string, mode?: number): void
Z
zengyawen 已提交
550 551 552

Synchronously creates a directory.

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

A
Annie_wang 已提交
555
**Parameters**
A
Annie_wang 已提交
556

A
Annie_wang 已提交
557 558
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
559
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
560
| mode   | number | No  | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o775**.<br>-&nbsp;**0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
A
annie_wangli 已提交
561

A
Annie_wang 已提交
562
**Example**
A
Annie_wang 已提交
563

A
annie_wangli 已提交
564 565 566 567 568 569 570 571 572
  ```js
  fileio.mkdirSync(path);
  ```


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

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

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

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

A
Annie_wang 已提交
577
**Parameters**
A
Annie_wang 已提交
578

A
Annie_wang 已提交
579 580
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
581 582
| path   | string | Yes  | Application sandbox path of the file.                                  |
| flags  | number | No  | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>-&nbsp;**0o200000**: If **path** does not point to a directory, throw an exception.<br><br>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
A
Annie_wang 已提交
583
| mode   | number | No  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>-&nbsp;**0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
A
annie_wangli 已提交
584

A
Annie_wang 已提交
585
**Return value**
A
Annie_wang 已提交
586

A
annie_wangli 已提交
587 588
  | Type                   | Description         |
  | --------------------- | ----------- |
A
Annie_wang 已提交
589
  | Promise&lt;number&gt; | Promise used to return the file descriptor of the file opened.|
A
annie_wangli 已提交
590

A
Annie_wang 已提交
591
**Example**
A
Annie_wang 已提交
592

A
annie_wangli 已提交
593 594
  ```js
  fileio.open(path, 0o1, 0o0200).then(function(number){
A
Annie_wang 已提交
595 596 597
      console.info("File opened");
  }).catch(function(err){
      console.info("Failed to open the file. Error:"+ err);
A
annie_wangli 已提交
598 599 600 601 602 603 604 605
  });
  ```


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

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

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

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

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

A
Annie_wang 已提交
612 613
| Name  | Type                           | Mandatory| Description                                                        |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
614 615
| path     | string                          | Yes  | Application sandbox path of the file.                                  |
| flags    | number                          | Yes  | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>-&nbsp;**0o200000**: If **path** does not point to a directory, throw an exception.<br><br>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
A
Annie_wang 已提交
616 617
| mode     | number                          | Yes  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>-&nbsp;**0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
| callback | AsyncCallback&nbsp;&lt;void&gt; | Yes  | Callback invoked when the file is open asynchronously.                                    |
A
annie_wangli 已提交
618

A
Annie_wang 已提交
619
**Example**
A
Annie_wang 已提交
620

A
annie_wangli 已提交
621 622 623 624 625 626 627 628 629 630
  ```js
  fileio.open(path, 0, function(err, fd) {
      // Do something.
  });
  ```


## fileio.openSync

openSync(path:string, flags?:number, mode?:number): number
Z
zengyawen 已提交
631 632 633

Synchronously opens a file.

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

A
Annie_wang 已提交
636
**Parameters**
A
Annie_wang 已提交
637

A
Annie_wang 已提交
638 639
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
640 641 642
| path   | string | Yes  | Application sandbox path of the file.                                  |
| flags  | number | No  | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>-&nbsp;**0o200000**: If **path** does not point to a directory, throw an exception.<br><br>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
| mode   | number | No  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>-&nbsp;**0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>-&nbsp;**0o640**: The owner has the read and write permissions, and the user group has the read permission.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.<br>The file permissions on newly created files are affected by umask, which is set as the process starts. Currently, the modification of umask is not open.|
A
annie_wangli 已提交
643

A
Annie_wang 已提交
644
**Return value**
A
Annie_wang 已提交
645

A
annie_wangli 已提交
646 647
  | Type    | Description         |
  | ------ | ----------- |
A
annie_wangli 已提交
648 649
  | number | File descriptor of the file opened.|

A
Annie_wang 已提交
650
**Example**
A
Annie_wang 已提交
651

A
annie_wangli 已提交
652
  ```js
A
Annie_wang 已提交
653 654 655 656 657 658 659 660 661
  let fd = fileio.openSync(path, 0o102, 0o640);
  ```
  ```js
  let fd = fileio.openSync(path, 0o102, 0o666);
  fileio.writeSync(fd, 'hello world');
  let fd1 = fileio.openSync(path, 0o2002);
  fileio.writeSync(fd1, 'hello world');
  let num = fileio.readSync(fd1, new ArrayBuffer(4096), {position: 0});
  console.info("num == " + num);
A
annie_wangli 已提交
662 663 664 665 666
  ```


## fileio.read

A
annie_wangli 已提交
667 668 669 670 671
read(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): Promise&lt;ReadOut&gt;
A
annie_wangli 已提交
672

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

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

A
Annie_wang 已提交
677
**Parameters**
A
Annie_wang 已提交
678

A
Annie_wang 已提交
679 680 681 682 683 684 685
| 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>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size|

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

A
annie_wangli 已提交
687 688
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
689
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
690

A
Annie_wang 已提交
691
**Example**
A
Annie_wang 已提交
692

A
annie_wangli 已提交
693 694 695
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
A
Annie_wang 已提交
696 697
  fileio.read(fd, buf).then(function(readOut){
      console.info("Read file data");
A
Annie_wang 已提交
698
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
Annie_wang 已提交
699 700
  }).catch(function(err){
      console.info("Failed to read file data. Error:"+ err);
A
annie_wangli 已提交
701 702 703 704 705 706
  });
  ```


## fileio.read

A
annie_wangli 已提交
707 708 709 710 711
read(fd: number, buffer: ArrayBuffer, options: {
    offset?: number;
    length?: number;
    position?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
A
annie_wangli 已提交
712

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

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

A
Annie_wang 已提交
717
**Parameters**
A
Annie_wang 已提交
718

A
annie_wangli 已提交
719 720 721 722
  | 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 已提交
723
  | options  | Object                                   | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
A
annie_wangli 已提交
724
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously.                            |
A
annie_wangli 已提交
725

A
Annie_wang 已提交
726
**Example**
A
Annie_wang 已提交
727

A
annie_wangli 已提交
728 729 730 731
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
  fileio.read(fd, buf, function (err, readOut) {
A
Annie_wang 已提交
732
      if (readOut) {
A
Annie_wang 已提交
733
          console.info("Read file data");
A
Annie_wang 已提交
734
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
735 736 737 738 739 740 741
      }
  });
  ```


## fileio.readSync

A
annie_wangli 已提交
742 743 744 745 746
readSync(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): number
Z
zengyawen 已提交
747 748 749

Synchronously reads data from a file.

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

A
Annie_wang 已提交
752
**Parameters**
A
Annie_wang 已提交
753

A
annie_wangli 已提交
754 755 756 757
  | 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 已提交
758
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
A
annie_wangli 已提交
759

A
Annie_wang 已提交
760
**Return value**
A
Annie_wang 已提交
761

A
annie_wangli 已提交
762 763
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
764 765
  | number | Length of the data read.|

A
Annie_wang 已提交
766
**Example**
A
Annie_wang 已提交
767

A
annie_wangli 已提交
768 769 770 771 772 773 774 775 776 777 778
  ```js
  let fd = fileio.openSync(path, 0o2);
  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 已提交
779
Deletes a directory. This API uses a promise to return the result.
A
annie_wangli 已提交
780

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

A
Annie_wang 已提交
783
**Parameters**
A
Annie_wang 已提交
784

A
Annie_wang 已提交
785 786
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
787
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
788

A
Annie_wang 已提交
789
**Return value**
A
Annie_wang 已提交
790

A
annie_wangli 已提交
791 792
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
793
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
794

A
Annie_wang 已提交
795
**Example**
A
Annie_wang 已提交
796

A
annie_wangli 已提交
797 798
  ```js
  fileio.rmdir(path).then(function() {
A
Annie_wang 已提交
799
      console.info("Directory deleted");
A
annie_wangli 已提交
800
  }).catch(function(err){
A
Annie_wang 已提交
801
      console.info("Failed to delete the directory. Error:"+ err);
A
annie_wangli 已提交
802 803 804 805 806 807 808 809
  });
  ```


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

rmdir(path: string, callback:AsyncCallback&lt;void&gt;): void

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

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

A
Annie_wang 已提交
814
**Parameters**
A
Annie_wang 已提交
815

A
Annie_wang 已提交
816 817
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
818
| path     | string                    | Yes  | Application sandbox path of the directory.|
A
Annie_wang 已提交
819
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is deleted asynchronously.  |
A
annie_wangli 已提交
820

A
Annie_wang 已提交
821
**Example**
A
Annie_wang 已提交
822

A
annie_wangli 已提交
823 824 825
  ```js
  fileio.rmdir(path, function(err){
      // Do something.
A
Annie_wang 已提交
826
      console.info("Directory deleted");
A
annie_wangli 已提交
827 828 829 830
  });
  ```


A
annie_wangli 已提交
831
## fileio.rmdirSync<sup>7+</sup>
A
annie_wangli 已提交
832

A
annie_wangli 已提交
833
rmdirSync(path: string): void
A
annie_wangli 已提交
834 835 836

Synchronously deletes a directory.

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

A
Annie_wang 已提交
839
**Parameters**
A
Annie_wang 已提交
840

A
Annie_wang 已提交
841 842
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
843
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
844

A
Annie_wang 已提交
845
**Example**
A
Annie_wang 已提交
846

A
annie_wangli 已提交
847 848 849 850 851 852 853 854 855
  ```js
  fileio.rmdirSync(path);
  ```


## fileio.unlink

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

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

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

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

A
Annie_wang 已提交
862 863
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
864
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
865

A
Annie_wang 已提交
866
**Return value**
A
Annie_wang 已提交
867

A
annie_wangli 已提交
868 869
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
870
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
871

A
Annie_wang 已提交
872
**Example**
A
Annie_wang 已提交
873

A
annie_wangli 已提交
874 875
  ```js
  fileio.unlink(path).then(function(){
A
Annie_wang 已提交
876
      console.info("File deleted");
A
annie_wangli 已提交
877
  }).catch(function(error){
A
Annie_wang 已提交
878
      console.info("Failed to delete the file. Error:"+ error);
A
annie_wangli 已提交
879 880 881 882 883 884 885 886
  });
  ```


## fileio.unlink

unlink(path:string, callback:AsyncCallback&lt;void&gt;): void

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

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

A
Annie_wang 已提交
891
**Parameters**
A
Annie_wang 已提交
892

A
Annie_wang 已提交
893 894
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
895
| path     | string                    | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
896
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is deleted asynchronously.  |
A
annie_wangli 已提交
897

A
Annie_wang 已提交
898
**Example**
A
Annie_wang 已提交
899

A
annie_wangli 已提交
900 901
  ```js
  fileio.unlink(path, function(err) {
A
Annie_wang 已提交
902
      console.info("File deleted");
A
annie_wangli 已提交
903 904 905 906 907 908 909
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void
Z
zengyawen 已提交
910 911 912

Synchronously deletes a file.

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

A
Annie_wang 已提交
915
**Parameters**
A
Annie_wang 已提交
916

A
Annie_wang 已提交
917 918
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
919
| path   | string | Yes  | Application sandbox path of the file.|
Z
zengyawen 已提交
920

A
Annie_wang 已提交
921
**Example**
A
Annie_wang 已提交
922

A
annie_wangli 已提交
923 924 925
  ```js
  fileio.unlinkSync(path);
  ```
Z
zengyawen 已提交
926 927


A
annie_wangli 已提交
928
## fileio.write
Z
zengyawen 已提交
929

A
annie_wangli 已提交
930 931 932 933 934 935
write(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
936

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

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

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

A
annie_wangli 已提交
943 944 945 946
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
947
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
Z
zengyawen 已提交
948

A
Annie_wang 已提交
949
**Return value**
A
Annie_wang 已提交
950

A
annie_wangli 已提交
951 952
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
953
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
Z
zengyawen 已提交
954

A
Annie_wang 已提交
955
**Example**
A
Annie_wang 已提交
956

A
annie_wangli 已提交
957
  ```js
A
Annie_wang 已提交
958
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
A
annie_wangli 已提交
959
  fileio.write(fd, "hello, world").then(function(number){
A
Annie_wang 已提交
960
       console.info("Data written to file and size is:"+ number);
A
annie_wangli 已提交
961
  }).catch(function(err){
A
Annie_wang 已提交
962
      console.info("Failed to write data to the file. Error:"+ err);
A
annie_wangli 已提交
963 964
  });
  ```
Z
zengyawen 已提交
965 966


A
annie_wangli 已提交
967
## fileio.write
Z
zengyawen 已提交
968

A
annie_wangli 已提交
969 970 971 972 973 974
write(fd: number, buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
975

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

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

A
Annie_wang 已提交
980
**Parameters**
A
Annie_wang 已提交
981

A
annie_wangli 已提交
982 983 984 985
  | Name     | Type                             | Mandatory  | Description                                      |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
986
  | options  | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
A
annie_wangli 已提交
987
  | callback | AsyncCallback&lt;number&gt;     | Yes   | Callback invoked when the data is written asynchronously.                      |
Z
zengyawen 已提交
988

A
Annie_wang 已提交
989
**Example**
A
Annie_wang 已提交
990

A
annie_wangli 已提交
991 992 993
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
A
Annie_wang 已提交
994
      if (bytesWritten) {
A
Annie_wang 已提交
995
         console.info("Data written to file and size is:"+ bytesWritten);
A
annie_wangli 已提交
996 997 998
      }
  });
  ```
Z
zengyawen 已提交
999 1000


A
annie_wangli 已提交
1001
## fileio.writeSync
Z
zengyawen 已提交
1002

A
annie_wangli 已提交
1003 1004 1005 1006 1007 1008
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
1009

A
annie_wangli 已提交
1010
Synchronously writes data into a file.
Z
zengyawen 已提交
1011

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

A
Annie_wang 已提交
1014
**Parameters**
A
Annie_wang 已提交
1015

A
annie_wangli 已提交
1016 1017 1018 1019
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
1020
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
Z
zengyawen 已提交
1021

A
Annie_wang 已提交
1022
**Return value**
A
Annie_wang 已提交
1023

A
annie_wangli 已提交
1024 1025
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
1026
  | number | Length of the data written in the file.|
Z
zengyawen 已提交
1027

A
Annie_wang 已提交
1028
**Example**
A
Annie_wang 已提交
1029

A
annie_wangli 已提交
1030 1031 1032 1033
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  let num = fileio.writeSync(fd, "hello, world");
  ```
Z
zengyawen 已提交
1034 1035


A
annie_wangli 已提交
1036
## fileio.hash
Z
zengyawen 已提交
1037

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

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

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

A
Annie_wang 已提交
1044
**Parameters**
A
Annie_wang 已提交
1045

A
Annie_wang 已提交
1046 1047
| Name   | Type  | Mandatory| Description                                                        |
| --------- | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1048
| path      | string | Yes  | Application sandbox path of the file.                            |
A
Annie_wang 已提交
1049
| 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 已提交
1050

A
Annie_wang 已提交
1051
**Return value**
A
Annie_wang 已提交
1052

A
annie_wangli 已提交
1053 1054
  | Type                   | Description                        |
  | --------------------- | -------------------------- |
A
Annie_wang 已提交
1055
  | 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 已提交
1056

A
Annie_wang 已提交
1057
**Example**
A
Annie_wang 已提交
1058

A
annie_wangli 已提交
1059 1060
  ```js
  fileio.hash(path, "sha256").then(function(str){
A
Annie_wang 已提交
1061
      console.info("Calculated file hash:"+ str);
A
annie_wangli 已提交
1062
  }).catch(function(error){
A
Annie_wang 已提交
1063
      console.info("Failed to calculate the file hash. Error:"+ err);
A
annie_wangli 已提交
1064 1065
  });
  ```
Z
zengyawen 已提交
1066 1067


A
annie_wangli 已提交
1068
## fileio.hash
Z
zengyawen 已提交
1069

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

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

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

A
Annie_wang 已提交
1076
**Parameters**
A
Annie_wang 已提交
1077

A
Annie_wang 已提交
1078 1079
| Name   | Type                       | Mandatory| Description                                                        |
| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1080
| path      | string                      | Yes  | Application sandbox path of the file.                            |
A
Annie_wang 已提交
1081
| 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 已提交
1082
| 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 已提交
1083

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

A
annie_wangli 已提交
1086
  ```js
A
Annie_wang 已提交
1087
  fileio.hash(path, "sha256", function(err, hashStr) {
A
Annie_wang 已提交
1088
      if (hashStr) {
A
Annie_wang 已提交
1089
          console.info("Calculated file hash:"+ hashStr);
A
annie_wangli 已提交
1090 1091 1092
      }
  });
  ```
Z
zengyawen 已提交
1093 1094


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

A
annie_wangli 已提交
1097
chmod(path: string, mode: number):Promise&lt;void&gt;
Z
zengyawen 已提交
1098

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

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

A
Annie_wang 已提交
1103
**Parameters**
A
Annie_wang 已提交
1104

A
Annie_wang 已提交
1105 1106
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1107
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1108
| mode   | number | Yes  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
Z
zengyawen 已提交
1109

A
Annie_wang 已提交
1110
**Return value**
A
Annie_wang 已提交
1111

A
annie_wangli 已提交
1112 1113
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1114
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1115

A
Annie_wang 已提交
1116
**Example**
A
Annie_wang 已提交
1117

A
annie_wangli 已提交
1118 1119
  ```js
  fileio.chmod(path, mode).then(function() {
A
Annie_wang 已提交
1120
      console.info("File permissions changed");
A
annie_wangli 已提交
1121
  }).catch(function(err){
A
Annie_wang 已提交
1122
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
1123 1124
  });
  ```
Z
zengyawen 已提交
1125 1126


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

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

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

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

A
Annie_wang 已提交
1135
**Parameters**
A
Annie_wang 已提交
1136

A
Annie_wang 已提交
1137 1138
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1139
| path     | string                    | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1140 1141
| mode     | number                    | Yes  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file permissions are changed asynchronously.                                |
Z
zengyawen 已提交
1142

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

A
annie_wangli 已提交
1145 1146 1147 1148 1149
  ```js
  fileio.chmod(path, mode, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
1150 1151


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

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

A
Annie_wang 已提交
1156
Synchronously changes file permissions.
Z
zengyawen 已提交
1157

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

A
Annie_wang 已提交
1160
**Parameters**
A
Annie_wang 已提交
1161

A
Annie_wang 已提交
1162 1163
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1164
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1165
| mode   | number | Yes  | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
Z
zengyawen 已提交
1166

A
Annie_wang 已提交
1167
**Example**
A
Annie_wang 已提交
1168

A
annie_wangli 已提交
1169
  ```js
A
Annie_wang 已提交
1170
  fileio.chmodSync(path, mode);
A
annie_wangli 已提交
1171
  ```
Z
zengyawen 已提交
1172 1173


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

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

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

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

A
Annie_wang 已提交
1182
**Parameters**
A
Annie_wang 已提交
1183

A
annie_wangli 已提交
1184 1185 1186
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1187

A
Annie_wang 已提交
1188
**Return value**
A
Annie_wang 已提交
1189

A
annie_wangli 已提交
1190
  | Type                          | Description        |
A
Annie_wang 已提交
1191
  | ---------------------------- | ---------- |
A
Annie_wang 已提交
1192
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information.|
Z
zengyawen 已提交
1193

A
Annie_wang 已提交
1194
**Example**
A
Annie_wang 已提交
1195

A
annie_wangli 已提交
1196 1197
  ```js
  fileio.fstat(fd).then(function(stat){
A
Annie_wang 已提交
1198
      console.info("File information obtained:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1199
  }).catch(function(err){
A
Annie_wang 已提交
1200
      console.info("Failed to obtain file information. Error:"+ err);
A
annie_wangli 已提交
1201 1202
  });
  ```
Z
zengyawen 已提交
1203 1204


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

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

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

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

A
Annie_wang 已提交
1213
**Parameters**
A
Annie_wang 已提交
1214

A
annie_wangli 已提交
1215 1216 1217
  | Name     | Type                                | Mandatory  | Description              |
  | -------- | ---------------------------------- | ---- | ---------------- |
  | fd       | number                             | Yes   | File descriptor of the target file.    |
A
Annie_wang 已提交
1218
  | callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes   | Callback invoked to return the file information obtained.|
Z
zengyawen 已提交
1219

A
Annie_wang 已提交
1220
**Example**
A
Annie_wang 已提交
1221

A
annie_wangli 已提交
1222 1223 1224 1225 1226 1227
  ```js
  let fd = fileio.openSync(path);
  fileio.fstat(fd, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
1228 1229


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

A
annie_wangli 已提交
1232
fstatSync(fd: number): Stat
Z
zengyawen 已提交
1233

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

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

A
Annie_wang 已提交
1238
**Parameters**
A
Annie_wang 已提交
1239

A
annie_wangli 已提交
1240 1241 1242
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1243

A
Annie_wang 已提交
1244
**Return value**
A
Annie_wang 已提交
1245

A
annie_wangli 已提交
1246 1247
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1248
  | [Stat](#stat) | File information obtained.|
Z
zengyawen 已提交
1249

A
Annie_wang 已提交
1250
**Example**
A
Annie_wang 已提交
1251

A
annie_wangli 已提交
1252 1253 1254 1255
  ```js
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```
Z
zengyawen 已提交
1256 1257


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

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

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

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

A
Annie_wang 已提交
1266
**Parameters**
A
Annie_wang 已提交
1267

A
annie_wangli 已提交
1268 1269 1270
  | Name | Type    | Mandatory  | Description              |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | Yes   | File descriptor of the file to truncate.    |
A
Annie_wang 已提交
1271
  | len  | number | No   | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1272

A
Annie_wang 已提交
1273
**Return value**
A
Annie_wang 已提交
1274

A
annie_wangli 已提交
1275 1276
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1277
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1278

A
Annie_wang 已提交
1279
**Example**
A
Annie_wang 已提交
1280

A
annie_wangli 已提交
1281 1282 1283
  ```js
  let fd = fileio.openSync(path);
  fileio.ftruncate(fd, 5).then(function(err) {    
A
Annie_wang 已提交
1284
      console.info("File truncated");
A
annie_wangli 已提交
1285 1286 1287 1288
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1289 1290


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

A
annie_wangli 已提交
1293
ftruncate(fd: number, len: number, callback:AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
1294

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

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

A
Annie_wang 已提交
1299
**Parameters**
A
Annie_wang 已提交
1300

A
annie_wangli 已提交
1301 1302 1303 1304
  | Name     | Type                       | Mandatory  | Description              |
  | -------- | ------------------------- | ---- | ---------------- |
  | fd       | number                    | Yes   | File descriptor of the file to truncate.    |
  | len      | number                    | Yes   | File length, in bytes, after truncation.|
A
Annie_wang 已提交
1305
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback that returns no value. |
Z
zengyawen 已提交
1306

A
Annie_wang 已提交
1307
**Example**
A
Annie_wang 已提交
1308

A
annie_wangli 已提交
1309 1310 1311 1312 1313
  ```js
  fileio.ftruncate(fd, len, function(err){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1314 1315


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

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

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

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

A
Annie_wang 已提交
1324
**Parameters**
A
Annie_wang 已提交
1325

A
annie_wangli 已提交
1326 1327 1328 1329
  | 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 已提交
1330

A
Annie_wang 已提交
1331
**Example**
A
Annie_wang 已提交
1332

A
annie_wangli 已提交
1333
  ```js
P
Peter_1988 已提交
1334
  fileio.ftruncateSync(fd, len);
A
annie_wangli 已提交
1335
  ```
Z
zengyawen 已提交
1336 1337


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

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

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

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

A
Annie_wang 已提交
1346
**Parameters**
A
Annie_wang 已提交
1347

A
Annie_wang 已提交
1348 1349 1350
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
| path   | string | Yes  | Application sandbox path of the file to truncate.      |
A
Annie_wang 已提交
1351
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1352

A
Annie_wang 已提交
1353
**Return value**
A
Annie_wang 已提交
1354

A
annie_wangli 已提交
1355 1356
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1357
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1358

A
Annie_wang 已提交
1359
**Example**
A
Annie_wang 已提交
1360

A
annie_wangli 已提交
1361 1362
  ```js
  fileio.truncate(path, len).then(function(){
A
Annie_wang 已提交
1363
      console.info("File truncated");
A
annie_wangli 已提交
1364 1365 1366 1367
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1368 1369


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

A
annie_wangli 已提交
1372
truncate(path: string, len: number, callback:AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
1373

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

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

A
Annie_wang 已提交
1378
**Parameters**
A
Annie_wang 已提交
1379

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

A
Annie_wang 已提交
1386
**Example**
A
Annie_wang 已提交
1387

A
annie_wangli 已提交
1388 1389 1390 1391 1392
  ```js
  fileio.truncate(path, len, function(err){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1393 1394


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

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

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

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

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

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

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

A
annie_wangli 已提交
1412
  ```js
P
Peter_1988 已提交
1413
  fileio.truncateSync(path, len);
A
annie_wangli 已提交
1414
  ```
Z
zengyawen 已提交
1415 1416


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

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

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

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

A
Annie_wang 已提交
1429
**Parameters**
A
Annie_wang 已提交
1430

A
Annie_wang 已提交
1431 1432 1433 1434
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filePath | string | Yes  | Application sandbox path of the file to read.                                  |
| options  | Object | No  | The options are as follows:<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**encoding** (string): format of the data (string) to be encoded. The default value is **utf-8**, which is the only value supported.|
Z
zengyawen 已提交
1435

A
Annie_wang 已提交
1436
**Return value**
A
Annie_wang 已提交
1437

A
annie_wangli 已提交
1438 1439
  | Type                   | Description        |
  | --------------------- | ---------- |
A
Annie_wang 已提交
1440
  | Promise&lt;string&gt; | Promise used to return the content read.|
Z
zengyawen 已提交
1441

A
Annie_wang 已提交
1442
**Example**
A
Annie_wang 已提交
1443

A
annie_wangli 已提交
1444 1445
  ```js
  fileio.readText(path).then(function(str) {
A
Annie_wang 已提交
1446
      console.info("Read file text:"+ str);
A
annie_wangli 已提交
1447
  }).catch(function(err){
A
Annie_wang 已提交
1448
      console.info("Failed to read text. Error:"+ err);
A
annie_wangli 已提交
1449 1450
  });
  ```
Z
zengyawen 已提交
1451 1452


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

A
annie_wangli 已提交
1455 1456 1457 1458 1459
readText(filePath: string, options: {
    position?: number;
    length?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1460

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

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

A
Annie_wang 已提交
1465
**Parameters**
A
Annie_wang 已提交
1466

A
Annie_wang 已提交
1467 1468 1469
| Name  | Type                       | Mandatory| Description                                                        |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| filePath | string                      | Yes  | Application sandbox path of the file to read.                                  |
A
Annie_wang 已提交
1470 1471
| options  | Object                      | No  | The options are as follows:<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- &nbsp;**encoding**: format of the string to be encoded. The default value is &nbsp;**utf-8**, which is the only value supported.|
| callback | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the content read.                        |
Z
zengyawen 已提交
1472

A
Annie_wang 已提交
1473
**Example**
A
Annie_wang 已提交
1474

A
annie_wangli 已提交
1475 1476 1477 1478 1479
  ```js
  fileio.readText(path, function(err, str){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1480 1481


A
annie_wangli 已提交
1482 1483
## fileio.readTextSync<sup>7+</sup>

A
annie_wangli 已提交
1484 1485 1486 1487 1488
readTextSync(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): string
A
annie_wangli 已提交
1489 1490 1491

Synchronously reads the text of a file. 

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

A
Annie_wang 已提交
1494
**Parameters**
A
Annie_wang 已提交
1495

A
Annie_wang 已提交
1496 1497
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1498
| filePath | string | Yes  | Application sandbox path of the file to read.                                  |
A
Annie_wang 已提交
1499
| options  | Object | No  | The options are as follows:<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**encoding** (string): format of the data (string) to be encoded. The default value is **utf-8**, which is the only value supported.|
A
annie_wangli 已提交
1500

A
Annie_wang 已提交
1501
**Return value**
A
Annie_wang 已提交
1502

A
annie_wangli 已提交
1503 1504
  | Type  | Description                |
  | ------ | -------------------- |
A
Annie_wang 已提交
1505
  | string | Promise used to return the content of the file read.|
A
annie_wangli 已提交
1506

A
Annie_wang 已提交
1507
**Example**
A
Annie_wang 已提交
1508

A
annie_wangli 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517
  ```js
  let str = fileio.readTextSync(path, {position: 1, length: 3});
  ```


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

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

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

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

A
Annie_wang 已提交
1522
**Parameters**
A
Annie_wang 已提交
1523

A
Annie_wang 已提交
1524 1525
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1526
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1527

A
Annie_wang 已提交
1528
**Return value**
A
Annie_wang 已提交
1529

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

A
Annie_wang 已提交
1534
**Example**
A
Annie_wang 已提交
1535

A
annie_wangli 已提交
1536 1537
  ```js
  fileio.lstat(path).then(function(stat){
A
Annie_wang 已提交
1538
      console.info("Got link info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
  }).catch(function(err){
      console.info("Failed to obtain the link status. Error:"+ err);
  });
  ```


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

lstat(path:string, callback:AsyncCallback&lt;Stat&gt;): void

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

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

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

A
Annie_wang 已提交
1555 1556
| Name  | Type                              | Mandatory| Description                                  |
| -------- | ---------------------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1557 1558
| 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 已提交
1559

A
Annie_wang 已提交
1560
**Example**
A
Annie_wang 已提交
1561

A
annie_wangli 已提交
1562 1563 1564
  ```js
  fileio.lstat(path, function (err, stat) {
      // Do something.
A
annie_wangli 已提交
1565
  });
A
annie_wangli 已提交
1566 1567 1568 1569 1570 1571 1572
  ```


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

lstatSync(path:string): Stat

A
Annie_wang 已提交
1573
Synchronously obtains the link information.
A
annie_wangli 已提交
1574

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

A
Annie_wang 已提交
1577
**Parameters**
A
Annie_wang 已提交
1578

A
Annie_wang 已提交
1579 1580
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1581
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1582

A
Annie_wang 已提交
1583
**Return value**
A
Annie_wang 已提交
1584

A
annie_wangli 已提交
1585 1586
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1587
  | [Stat](#stat) | Link information obtained.|
A
annie_wangli 已提交
1588

A
Annie_wang 已提交
1589
**Example**
A
Annie_wang 已提交
1590

A
annie_wangli 已提交
1591 1592 1593 1594 1595 1596 1597
  ```js
  let stat = fileio.lstatSync(path);
  ```


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

A
annie_wangli 已提交
1598 1599 1600 1601 1602
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
A
annie_wangli 已提交
1603

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

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

A
Annie_wang 已提交
1608
**Parameters**
A
Annie_wang 已提交
1609

A
Annie_wang 已提交
1610 1611 1612
  | Name | Type       | Mandatory| Description                                                        |
  | ------- | ----------- | ---- | ------------------------------------------------------------ |
  | buffer  | ArrayBuffer | Yes  | Buffer used to store the file data read.                          |
A
Annie_wang 已提交
1613
  | options | Object      | No  | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>Constraints: offset + length <= Buffer size|
A
annie_wangli 已提交
1614

A
Annie_wang 已提交
1615
**Return value**
A
Annie_wang 已提交
1616

A
annie_wangli 已提交
1617 1618
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
1619
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
Z
zengyawen 已提交
1620

A
Annie_wang 已提交
1621
**Example**
A
Annie_wang 已提交
1622

A
annie_wangli 已提交
1623 1624
  ```js
  fileio.read(new ArrayBuffer(4096)).then(function(readout){
A
Annie_wang 已提交
1625
      console.info("Read file data");
A
Annie_wang 已提交
1626
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
1627 1628 1629 1630
  }).catch(function(err){
      console.info("Failed to read file data. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1631 1632


A
annie_wangli 已提交
1633 1634
## fileio.read<sup>7+</sup>

A
annie_wangli 已提交
1635 1636 1637 1638 1639
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
A
annie_wangli 已提交
1640

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

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

A
Annie_wang 已提交
1645
**Parameters**
A
Annie_wang 已提交
1646

A
annie_wangli 已提交
1647 1648 1649
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file data read.                       |
A
Annie_wang 已提交
1650
  | options  | Object                                   | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>Constraints: offset + length <= Buffer size|
A
annie_wangli 已提交
1651
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously from the file.                         |
A
annie_wangli 已提交
1652

A
Annie_wang 已提交
1653
**Example**
A
Annie_wang 已提交
1654

A
annie_wangli 已提交
1655 1656 1657
  ```js
  let buf = new ArrayBuffer(4096);
  fileio.read(buf, function (err, readOut) {
A
Annie_wang 已提交
1658
      if (readOut) {
A
Annie_wang 已提交
1659
          console.info("Read file data");
A
Annie_wang 已提交
1660
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
1661 1662 1663 1664 1665 1666 1667 1668 1669
      }
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1674
**Parameters**
A
Annie_wang 已提交
1675

A
Annie_wang 已提交
1676 1677 1678 1679
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes  | Application sandbox path of the file to rename.|
| newPath | String | Yes  | Application sandbox path of the file renamed.  |
A
annie_wangli 已提交
1680

A
Annie_wang 已提交
1681
**Return value**
A
Annie_wang 已提交
1682

A
annie_wangli 已提交
1683 1684
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1685
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1686

A
Annie_wang 已提交
1687
**Example**
A
Annie_wang 已提交
1688

A
annie_wangli 已提交
1689
  ```js
A
annie_wangli 已提交
1690
  fileio.rename(oldPath, newPath).then(function() {
A
Annie_wang 已提交
1691
      console.info("File renamed");
A
annie_wangli 已提交
1692
  }).catch(function(err){
A
Annie_wang 已提交
1693
      console.info("Failed to rename the file. Error:"+ err);
A
annie_wangli 已提交
1694 1695 1696 1697 1698 1699 1700 1701
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1706
**Parameters**
A
Annie_wang 已提交
1707

A
Annie_wang 已提交
1708 1709 1710
| Name  | Type                     | Mandatory| Description                        |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | Yes  | Application sandbox path of the file to rename.|
A
Annie_wang 已提交
1711
| newPath  | String                    | Yes  | Application sandbox path of the file renamed.  |
A
Annie_wang 已提交
1712
| Callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously renamed.  |
A
annie_wangli 已提交
1713

A
Annie_wang 已提交
1714
**Example**
A
Annie_wang 已提交
1715

A
annie_wangli 已提交
1716
  ```js
A
annie_wangli 已提交
1717
  fileio.rename(oldPath, newPath, function(err){
A
annie_wangli 已提交
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
  });
  ```


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

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

Synchronously renames a file.

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

A
Annie_wang 已提交
1730
**Parameters**
A
Annie_wang 已提交
1731

A
Annie_wang 已提交
1732 1733 1734 1735
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes  | Application sandbox path of the file to rename.|
| newPath | String | Yes  | Application sandbox path of the file renamed.  |
A
annie_wangli 已提交
1736

A
Annie_wang 已提交
1737
**Example**
A
Annie_wang 已提交
1738

A
annie_wangli 已提交
1739
  ```js
A
annie_wangli 已提交
1740
  fileio.renameSync(oldPath, newPath);
A
annie_wangli 已提交
1741 1742 1743 1744 1745 1746 1747
  ```


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

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

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

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

A
Annie_wang 已提交
1752
**Parameters**
A
Annie_wang 已提交
1753

A
annie_wangli 已提交
1754 1755
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1756
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1757

A
Annie_wang 已提交
1758
**Return value**
A
Annie_wang 已提交
1759

A
annie_wangli 已提交
1760 1761
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1762
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1763

A
Annie_wang 已提交
1764
**Example**
A
Annie_wang 已提交
1765

A
annie_wangli 已提交
1766 1767
  ```js
  fileio.fsync(fd).then(function(){
A
Annie_wang 已提交
1768
      console.info("Data flushed");
A
annie_wangli 已提交
1769
  }).catch(function(err){
A
Annie_wang 已提交
1770
      console.info("Failed to flush data. Error:"+ err);
A
annie_wangli 已提交
1771 1772 1773 1774 1775 1776 1777 1778
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1783
**Parameters**
A
Annie_wang 已提交
1784

A
annie_wangli 已提交
1785 1786
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
A
Annie_wang 已提交
1787
  | fd       | number                    | Yes   | File descriptor of the file to flush.   |
A
annie_wangli 已提交
1788
  | Callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1789

A
Annie_wang 已提交
1790
**Example**
A
Annie_wang 已提交
1791

A
annie_wangli 已提交
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
  ```js
  fileio.fsync(fd, function(err){
      // Do something.
  });
  ```


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

fsyncSync(fd: number): void

A
Annie_wang 已提交
1803
Flushes data of a file to disk in synchronous mode.
A
annie_wangli 已提交
1804 1805

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

A
Annie_wang 已提交
1807
**Parameters**
A
Annie_wang 已提交
1808

A
annie_wangli 已提交
1809 1810
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1811
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1812

A
Annie_wang 已提交
1813
**Example**
A
Annie_wang 已提交
1814

A
annie_wangli 已提交
1815
  ```js
A
Annie_wang 已提交
1816
  fileio.fsyncSync(fd);
A
annie_wangli 已提交
1817 1818 1819 1820 1821 1822 1823
  ```


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

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

A
Annie_wang 已提交
1824
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 已提交
1825

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

A
Annie_wang 已提交
1828
**Parameters**
A
Annie_wang 已提交
1829

A
annie_wangli 已提交
1830 1831
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1832
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1833

A
Annie_wang 已提交
1834
**Return value**
A
Annie_wang 已提交
1835

A
annie_wangli 已提交
1836 1837
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1838
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1839

A
Annie_wang 已提交
1840
**Example**
A
Annie_wang 已提交
1841

A
annie_wangli 已提交
1842 1843
  ```js
  fileio.fdatasync(fd).then(function(err) {
A
Annie_wang 已提交
1844
      console.info("Data flushed");
A
annie_wangli 已提交
1845
  }).catch(function(err){
A
Annie_wang 已提交
1846
      console.info("Failed to flush data. Error:"+ err);
A
annie_wangli 已提交
1847 1848 1849 1850 1851 1852 1853 1854
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1859
**Parameters**
A
Annie_wang 已提交
1860

A
annie_wangli 已提交
1861 1862 1863 1864
  | Name     | Type                             | Mandatory  | Description               |
  | -------- | ------------------------------- | ---- | ----------------- |
  | fd       | number                          | Yes   | File descriptor of the file to synchronize.     |
  | callback | AsyncCallback&nbsp;&lt;void&gt; | Yes   | Callback invoked when the file data is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1865

A
Annie_wang 已提交
1866
**Example**
A
Annie_wang 已提交
1867

A
annie_wangli 已提交
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
  ```js
  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 已提交
1881 1882
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1883
**Parameters**
A
Annie_wang 已提交
1884

A
annie_wangli 已提交
1885 1886
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1887
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1888

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

A
annie_wangli 已提交
1891 1892 1893 1894 1895 1896 1897 1898 1899
  ```js
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

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

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

A
Annie_wang 已提交
1904
**Parameters**
A
Annie_wang 已提交
1905

A
Annie_wang 已提交
1906 1907
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1908 1909
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1910

A
Annie_wang 已提交
1911
**Return value**
A
Annie_wang 已提交
1912

A
annie_wangli 已提交
1913 1914
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1915
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1916

A
Annie_wang 已提交
1917
**Example**
A
Annie_wang 已提交
1918

A
annie_wangli 已提交
1919 1920
  ```js
  fileio.symlink(target, srcPath).then(function() {
A
Annie_wang 已提交
1921
      console.info("Symbolic link created");
A
annie_wangli 已提交
1922
  }).catch(function(err){
A
Annie_wang 已提交
1923
      console.info("Failed to create the symbolic link. Error:"+ err);
A
annie_wangli 已提交
1924 1925 1926 1927 1928 1929 1930 1931
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1936
**Parameters**
A
Annie_wang 已提交
1937

A
Annie_wang 已提交
1938 1939
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1940 1941
| target   | string                    | Yes  | Application sandbox path of the target file.        |
| srcPath  | string                    | Yes  | Application sandbox path of the symbolic link file.    |
A
Annie_wang 已提交
1942
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the symbolic link is created asynchronously.|
A
annie_wangli 已提交
1943

A
Annie_wang 已提交
1944
**Example**
A
Annie_wang 已提交
1945

A
annie_wangli 已提交
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
  ```js
  fileio.symlink(target, srcPath, function (err) {
      // 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 已提交
1959 1960
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1961
**Parameters**
A
Annie_wang 已提交
1962

A
Annie_wang 已提交
1963 1964
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1965 1966
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1967

A
Annie_wang 已提交
1968
**Example**
A
Annie_wang 已提交
1969

A
annie_wangli 已提交
1970 1971 1972 1973 1974 1975 1976 1977 1978
  ```js
  fileio.symlinkSync(target, srcPath);
  ```


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

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

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

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

A
Annie_wang 已提交
1983
**Parameters**
A
Annie_wang 已提交
1984

A
Annie_wang 已提交
1985 1986
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1987
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
1988 1989
| uid    | number | Yes  | New user ID (UID).       |
| gid    | number | Yes  | New group ID (GID).      |
A
annie_wangli 已提交
1990

A
Annie_wang 已提交
1991
**Return value**
A
Annie_wang 已提交
1992

A
annie_wangli 已提交
1993 1994
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1995
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1996

A
Annie_wang 已提交
1997
**Example**
A
Annie_wang 已提交
1998

A
annie_wangli 已提交
1999 2000
  ```js
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
2001
  fileio.chown(path, stat.uid, stat.gid).then(function(){
A
Annie_wang 已提交
2002
      console.info("File owner changed");
A
annie_wangli 已提交
2003
  }).catch(function(err){
A
Annie_wang 已提交
2004
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2005 2006 2007 2008 2009 2010 2011 2012
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2017
**Parameters**
A
Annie_wang 已提交
2018

A
Annie_wang 已提交
2019 2020
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
2021
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
2022 2023 2024
| 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 已提交
2025

A
Annie_wang 已提交
2026
**Example**
A
Annie_wang 已提交
2027

A
annie_wangli 已提交
2028
  ```js
A
Annie_wang 已提交
2029
  let stat = fileio.statSync(path)
A
annie_wangli 已提交
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
  fileio.chown(path, stat.uid, stat.gid, function (err){
      // 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 已提交
2042 2043
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2044
**Parameters**
A
Annie_wang 已提交
2045

A
Annie_wang 已提交
2046 2047
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2048
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2049 2050
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2051

A
Annie_wang 已提交
2052
**Example**
A
Annie_wang 已提交
2053

A
annie_wangli 已提交
2054
  ```js
A
Annie_wang 已提交
2055
  let stat = fileio.statSync(path)
A
annie_wangli 已提交
2056 2057 2058 2059 2060 2061 2062 2063
  fileio.chownSync(path, stat.uid, stat.gid);
  ```


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

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

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

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

A
Annie_wang 已提交
2068
**Parameters**
A
Annie_wang 已提交
2069

A
annie_wangli 已提交
2070 2071 2072
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
2073

A
Annie_wang 已提交
2074
**Return value**
A
Annie_wang 已提交
2075

A
Annie_wang 已提交
2076
  | Type                  | Description        |
A
annie_wangli 已提交
2077
  | --------------------- | ---------- |
A
Annie_wang 已提交
2078
  | Promise&lt;string&gt; | Promise used to return the unique directory generated.|
A
annie_wangli 已提交
2079

A
Annie_wang 已提交
2080
**Example**
A
Annie_wang 已提交
2081

A
annie_wangli 已提交
2082 2083
  ```js
  fileio.mkdtemp(path + "XXXX").then(function(path){
A
Annie_wang 已提交
2084
      console.info("Temporary directory created:"+ path);
A
annie_wangli 已提交
2085
  }).catch(function(err){
A
Annie_wang 已提交
2086
      console.info("Failed to create a temporary directory. Error:"+ err);
A
annie_wangli 已提交
2087 2088 2089 2090 2091 2092 2093 2094
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2099
**Parameters**
A
Annie_wang 已提交
2100

A
annie_wangli 已提交
2101 2102 2103 2104
  | 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 已提交
2105

A
Annie_wang 已提交
2106
**Example**
A
Annie_wang 已提交
2107

A
annie_wangli 已提交
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
  ```js
  fileio.mkdtemp(path + "XXXX", function (err, res) {
      // Do something.
  });
  ```


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

mkdtempSync(prefix: string): string

Synchronously creates a temporary directory.

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

A
Annie_wang 已提交
2123
**Parameters**
A
Annie_wang 已提交
2124

A
annie_wangli 已提交
2125 2126 2127
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
2128

A
Annie_wang 已提交
2129
**Return value**
A
Annie_wang 已提交
2130

A
Annie_wang 已提交
2131
  | Type   | Description        |
A
annie_wangli 已提交
2132
  | ------ | ---------- |
A
annie_wangli 已提交
2133 2134
  | string | Unique path generated.|

A
Annie_wang 已提交
2135
**Example**
A
Annie_wang 已提交
2136

A
annie_wangli 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145
  ```js
  let res = fileio.mkdtempSync(path + "XXXX");
  ```


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

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

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

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

A
Annie_wang 已提交
2150
**Parameters**
A
Annie_wang 已提交
2151

A
annie_wangli 已提交
2152 2153 2154 2155
  | 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>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
A
annie_wangli 已提交
2156

A
Annie_wang 已提交
2157
**Return value**
A
Annie_wang 已提交
2158

A
Annie_wang 已提交
2159
  | Type                | Description                          |
A
annie_wangli 已提交
2160
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2161
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2162

A
Annie_wang 已提交
2163
**Example**
A
Annie_wang 已提交
2164

A
annie_wangli 已提交
2165 2166
  ```js
  fileio.fchmod(fd, mode).then(function() {
A
Annie_wang 已提交
2167
      console.info("File permissions changed");
A
annie_wangli 已提交
2168
  }).catch(function(err){
A
Annie_wang 已提交
2169
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
2170 2171 2172 2173 2174 2175 2176 2177
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2182
**Parameters**
A
Annie_wang 已提交
2183

A
annie_wangli 已提交
2184 2185 2186 2187 2188
  | 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>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
  | callback | AsyncCallback&nbsp;&lt;void&gt; | Yes   | Callback invoked when the file permissions are changed asynchronously.                          |
A
annie_wangli 已提交
2189

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

A
annie_wangli 已提交
2192 2193 2194 2195 2196 2197 2198 2199 2200
  ```js
  fileio.fchmod(fd, mode, function (err) {
      // Do something.
  });
  ```


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

A
annie_wangli 已提交
2201
fchmodSync(fd: number, mode: number): void
A
annie_wangli 已提交
2202 2203 2204

Synchronously changes the file permissions based on the file descriptor.

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

A
Annie_wang 已提交
2207
**Parameters**
A
Annie_wang 已提交
2208

A
annie_wangli 已提交
2209 2210 2211 2212
  | 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>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
A
annie_wangli 已提交
2213

A
Annie_wang 已提交
2214
**Example**
A
Annie_wang 已提交
2215

A
annie_wangli 已提交
2216 2217 2218 2219 2220 2221 2222 2223 2224
  ```js
   fileio.fchmodSync(fd, mode);
  ```


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

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

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

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

A
Annie_wang 已提交
2229
**Parameters**
A
Annie_wang 已提交
2230

A
Annie_wang 已提交
2231 2232
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2233
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2234
| mode   | string | Yes  | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
A
annie_wangli 已提交
2235

A
Annie_wang 已提交
2236
**Return value**
A
Annie_wang 已提交
2237

A
annie_wangli 已提交
2238 2239
  | Type                               | Description       |
  | --------------------------------- | --------- |
A
annie_wangli 已提交
2240 2241
  | Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.|

A
Annie_wang 已提交
2242
**Example**
A
Annie_wang 已提交
2243

A
annie_wangli 已提交
2244 2245
  ```js
  fileio.createStream(path, "r+").then(function(stream){
A
Annie_wang 已提交
2246
      console.info("Stream opened");
A
annie_wangli 已提交
2247
  }).catch(function(err){
A
Annie_wang 已提交
2248
      console.info("Failed to create the stream. Error:"+ err);
A
annie_wangli 已提交
2249 2250 2251 2252 2253 2254 2255 2256
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2261
**Parameters**
A
Annie_wang 已提交
2262

A
Annie_wang 已提交
2263 2264
| Name  | Type                                   | Mandatory| Description                                                        |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2265
| path     | string                                  | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2266 2267
| mode     | string                                  | Yes  | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
| callback | AsyncCallback&lt;[Stream](#stream7)&gt; | Yes  | Callback invoked when the stream is open asynchronously.                                  |
A
annie_wangli 已提交
2268

A
Annie_wang 已提交
2269
**Example**
A
Annie_wang 已提交
2270

A
annie_wangli 已提交
2271
  ```js
A
Annie_wang 已提交
2272
  fileio.createStream(path, "r+", function(err, stream){
A
annie_wangli 已提交
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
      // 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 已提交
2284 2285
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2286
**Parameters**
A
Annie_wang 已提交
2287

A
Annie_wang 已提交
2288 2289
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2290
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2291
| mode   | string | Yes  | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
A
annie_wangli 已提交
2292

A
Annie_wang 已提交
2293
**Return value**
A
Annie_wang 已提交
2294

A
Annie_wang 已提交
2295
  | Type               | Description       |
A
annie_wangli 已提交
2296
  | ------------------ | --------- |
A
annie_wangli 已提交
2297
  | [Stream](#stream7) | Stream opened.|
A
annie_wangli 已提交
2298

A
Annie_wang 已提交
2299
**Example**
A
Annie_wang 已提交
2300

A
annie_wangli 已提交
2301 2302 2303 2304 2305 2306 2307 2308 2309
  ```js
  let ss = fileio.createStreamSync(path, "r+");
  ```


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

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

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

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

A
Annie_wang 已提交
2314
**Parameters**
A
Annie_wang 已提交
2315

A
annie_wangli 已提交
2316 2317 2318 2319
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | Yes   | File descriptor of the target file.                            |
  | mode | string | Yes   | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
A
annie_wangli 已提交
2320

A
Annie_wang 已提交
2321
**Return value**
A
Annie_wang 已提交
2322

A
Annie_wang 已提交
2323
  | Type                              | Description       |
A
annie_wangli 已提交
2324
  | --------------------------------- | --------- |
A
annie_wangli 已提交
2325 2326
  | Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.|

A
Annie_wang 已提交
2327
**Example**
A
Annie_wang 已提交
2328

A
annie_wangli 已提交
2329
  ```js
A
Annie_wang 已提交
2330 2331 2332
  let fd = fileio.openSync(path);
  fileio.fdopenStream(fd, "r+").then(function(stream){
      console.info("Stream opened");
A
annie_wangli 已提交
2333
  }).catch(function(err){
A
Annie_wang 已提交
2334
      console.info("Failed to open the stream. Error:"+ err);
A
annie_wangli 已提交
2335 2336 2337 2338 2339 2340 2341 2342
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2347
**Parameters**
A
Annie_wang 已提交
2348

A
annie_wangli 已提交
2349 2350 2351 2352 2353
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                                   | Yes   | File descriptor of the target file.                            |
  | mode     | string                                   | Yes   | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
  | callback | AsyncCallback&nbsp;&lt;[Stream](#stream7)&gt; | Yes   | Callback invoked when the stream is open asynchronously.                           |
A
annie_wangli 已提交
2354

A
Annie_wang 已提交
2355
**Example**
A
Annie_wang 已提交
2356

A
annie_wangli 已提交
2357
  ```js
A
Annie_wang 已提交
2358 2359
  let fd = fileio.openSync(path);
  fileio.fdopenStream(fd, "r+", function (err, stream) {
A
annie_wangli 已提交
2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
      // 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 已提交
2371 2372
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2373
**Parameters**
A
Annie_wang 已提交
2374

A
annie_wangli 已提交
2375 2376 2377 2378
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | Yes   | File descriptor of the target file.                            |
  | mode | string | Yes   | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
A
annie_wangli 已提交
2379

A
Annie_wang 已提交
2380
**Return value**
A
Annie_wang 已提交
2381

A
Annie_wang 已提交
2382
  | Type               | Description       |
A
annie_wangli 已提交
2383
  | ------------------ | --------- |
A
annie_wangli 已提交
2384
  | [Stream](#stream7) | Stream opened.|
A
annie_wangli 已提交
2385

A
Annie_wang 已提交
2386
**Example**
A
Annie_wang 已提交
2387

A
annie_wangli 已提交
2388
  ```js
A
Annie_wang 已提交
2389
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
2390 2391 2392 2393 2394 2395 2396 2397
  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 已提交
2398
Changes the file owner based on the file descriptor. This API uses a promise to return the result.
A
annie_wangli 已提交
2399

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

A
Annie_wang 已提交
2402
**Parameters**
A
Annie_wang 已提交
2403

A
annie_wangli 已提交
2404 2405 2406 2407 2408
  | 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 已提交
2409

A
Annie_wang 已提交
2410
**Return value**
A
Annie_wang 已提交
2411

A
annie_wangli 已提交
2412 2413
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2414
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2415

A
Annie_wang 已提交
2416
**Example**
A
Annie_wang 已提交
2417

A
annie_wangli 已提交
2418 2419 2420
  ```js
  let stat = fileio.statSync(path);
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2421
      console.info("File owner changed");
A
annie_wangli 已提交
2422
  }).catch(function(err){
A
Annie_wang 已提交
2423
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2424 2425 2426 2427 2428 2429 2430 2431
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2436
**Parameters**
A
Annie_wang 已提交
2437

A
annie_wangli 已提交
2438 2439 2440 2441 2442 2443
  | 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 已提交
2444

A
Annie_wang 已提交
2445
**Example**
A
Annie_wang 已提交
2446

A
annie_wangli 已提交
2447
  ```js
A
Annie_wang 已提交
2448
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
  fileio.fchown(fd, stat.uid, stat.gid, function (err){
      // 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 已提交
2461 2462
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2463
**Parameters**
A
Annie_wang 已提交
2464

A
annie_wangli 已提交
2465 2466 2467 2468 2469
  | 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 已提交
2470

A
Annie_wang 已提交
2471
**Example**
A
Annie_wang 已提交
2472

A
annie_wangli 已提交
2473
  ```js
A
Annie_wang 已提交
2474
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
2475 2476 2477 2478 2479 2480 2481 2482
  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 已提交
2483
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 已提交
2484

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

A
Annie_wang 已提交
2487
**Parameters**
A
Annie_wang 已提交
2488

A
Annie_wang 已提交
2489 2490
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2491
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2492 2493
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2494

A
Annie_wang 已提交
2495
**Return value**
A
Annie_wang 已提交
2496

A
annie_wangli 已提交
2497 2498
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2499
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2500

A
Annie_wang 已提交
2501
**Example**
A
Annie_wang 已提交
2502

A
annie_wangli 已提交
2503 2504 2505
  ```js
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2506
      console.info("File owner changed");
A
annie_wangli 已提交
2507
  }).catch(function(err){
A
Annie_wang 已提交
2508
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2509 2510 2511 2512 2513 2514 2515 2516
  });
  ```


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

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

A
Annie_wang 已提交
2517
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 已提交
2518

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

A
Annie_wang 已提交
2521
**Parameters**
A
Annie_wang 已提交
2522

A
Annie_wang 已提交
2523 2524
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
2525
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
2526 2527 2528
| 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 已提交
2529

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

A
annie_wangli 已提交
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
  ```js
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid, function (err){
      // 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 已提交
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
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2553 2554
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2555

A
Annie_wang 已提交
2556
**Example**
A
Annie_wang 已提交
2557

A
annie_wangli 已提交
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567
  ```js
  let stat = fileio.statSync(path);
  fileio.lchownSync(path, stat.uid, stat.gid);
  ```


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

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

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

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

A
Annie_wang 已提交
2572
**Parameters**
A
Annie_wang 已提交
2573

A
Annie_wang 已提交
2574 2575
| Name  | Type                             | Mandatory| Description                                                        |
| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2576
| filename | string                            | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2577 2578
| events   | Number                            | Yes  | -&nbsp;**1**: The file or directory is renamed.<br>-&nbsp;**2**: The file or directory is modified.<br>-&nbsp;**3**: The file or directory is modified and renamed.|
| callback | AsyncCallback&lt;number&nbsp;&gt; | Yes  | Called each time a change is detected.                            |
A
annie_wangli 已提交
2579

A
Annie_wang 已提交
2580
**Return value**
A
Annie_wang 已提交
2581

A
Annie_wang 已提交
2582
  | Type                 | Description        |
A
annie_wangli 已提交
2583
  | -------------------- | ---------- |
A
Annie_wang 已提交
2584
  | [Watcher](#watcher7) | Promise used to return the **Watcher** instance.|
A
annie_wangli 已提交
2585

A
Annie_wang 已提交
2586
**Example**
A
Annie_wang 已提交
2587

A
annie_wangli 已提交
2588
  ```js
A
Annie_wang 已提交
2589 2590 2591
  let filename = path +"/test.txt";
  fileio.createWatcher(filename, 1, function(number){
     console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2592
  });
A
Annie_wang 已提交
2593
  
A
annie_wangli 已提交
2594 2595 2596 2597 2598 2599 2600
  ```


## Readout

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

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

A
annie_wangli 已提交
2603 2604 2605 2606 2607
| 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.|
| buffer    | ArrayBufer | Yes   | Yes   | Buffer for storing the data read.      |
A
annie_wangli 已提交
2608 2609 2610 2611 2612 2613


## 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 已提交
2614
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
2615 2616 2617

### Attributes

A
annie_wangli 已提交
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
| 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.                |
| mode   | number | Yes   | No   | File type and permissions. The first four bits indicate the file type, and the last 12 bits indicate the permissions. The bit fields are described as follows:<br>-&nbsp;**0o170000**: mask used to obtain the file type.<br>-&nbsp;**0o140000**: The file is a socket.<br>-&nbsp;**0o120000**: The file is a symbolic link.<br>-&nbsp;**0o100000**: The file is a regular file.<br>-&nbsp;**0o060000**: The file is a block device.<br>-&nbsp;**0o040000**: The file is a directory.<br>-&nbsp;**0o020000**: The file is a character device.<br>-&nbsp;**0o010000**: The file is a named pipe (FIFO).<br>-&nbsp;**0o0700**: mask used to obtain the owner permissions.<br>-&nbsp;**0o0400**: The owner has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0100**: The owner has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0070**: mask used to obtain the user group permissions.<br>-&nbsp;**0o0040**: The user group has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0020**: The user group has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0010**: The user group has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0007**: mask used to obtain the permissions of other users.<br>-&nbsp;**0o0004**: Other users have the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0002**: Other users have the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0001**: Other users have the permission to execute a regular file or search for the specified path in a directory.|
| 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 已提交
2632 2633 2634 2635 2636 2637


### isBlockDevice

isBlockDevice(): boolean

A
Annie_wang 已提交
2638
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 已提交
2639

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

A
Annie_wang 已提交
2642
**Return value**
A
Annie_wang 已提交
2643

A
annie_wangli 已提交
2644 2645
  | Type     | Description              |
  | ------- | ---------------- |
A
Annie_wang 已提交
2646
  | boolean | Whether the file is a block special file.|
A
annie_wangli 已提交
2647

A
Annie_wang 已提交
2648
**Example**
A
Annie_wang 已提交
2649

A
annie_wangli 已提交
2650 2651 2652 2653 2654 2655 2656 2657 2658
  ```js
  let isBLockDevice = fileio.statSync(path).isBlockDevice();
  ```


### isCharacterDevice

isCharacterDevice(): boolean

A
Annie_wang 已提交
2659
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 已提交
2660

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

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

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

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

A
annie_wangli 已提交
2671 2672 2673 2674 2675 2676 2677 2678 2679
  ```js
  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
  ```


### isDirectory

isDirectory(): boolean

A
Annie_wang 已提交
2680
Checks whether this file is a directory.
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_wangli 已提交
2686 2687
  | Type     | Description           |
  | ------- | ------------- |
A
Annie_wang 已提交
2688
  | boolean | Whether the file is a directory.|
A
annie_wangli 已提交
2689

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

A
annie_wangli 已提交
2692 2693 2694 2695 2696 2697 2698 2699 2700
  ```js
  let isDirectory = fileio.statSync(path).isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

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

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

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

A
annie_wangli 已提交
2707 2708
  | Type     | Description                   |
  | ------- | --------------------- |
A
Annie_wang 已提交
2709
  | boolean | Whether the file is an FIFO.|
A
annie_wangli 已提交
2710

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

A
annie_wangli 已提交
2713 2714 2715 2716 2717 2718 2719 2720 2721
  ```js
  let isFIFO = fileio.statSync(path).isFIFO(); 
  ```


### isFile

isFile(): boolean

A
Annie_wang 已提交
2722
Checks whether this file is a regular file.
A
annie_wangli 已提交
2723

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

A
Annie_wang 已提交
2726
**Return value**
A
Annie_wang 已提交
2727

A
annie_wangli 已提交
2728 2729
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2730
  | boolean | Whether the file is a regular file.|
A
annie_wangli 已提交
2731

A
Annie_wang 已提交
2732
**Example**
A
Annie_wang 已提交
2733

A
annie_wangli 已提交
2734
  ```js
A
Annie_wang 已提交
2735
  let isFile = fileio.statSync(path).isFile();
A
annie_wangli 已提交
2736 2737 2738 2739 2740 2741 2742
  ```


### isSocket

isSocket(): boolean

A
Annie_wang 已提交
2743
Checks whether this file is a socket.
A
annie_wangli 已提交
2744

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

A
Annie_wang 已提交
2747
**Return value**
A
Annie_wang 已提交
2748

A
annie_wangli 已提交
2749 2750
  | Type     | Description            |
  | ------- | -------------- |
A
Annie_wang 已提交
2751
  | boolean | Whether the file is a socket.|
A
annie_wangli 已提交
2752

A
Annie_wang 已提交
2753
**Example**
A
Annie_wang 已提交
2754

A
annie_wangli 已提交
2755 2756 2757 2758 2759 2760 2761 2762 2763
  ```js
  let isSocket = fileio.statSync(path).isSocket(); 
  ```


### isSymbolicLink

isSymbolicLink(): boolean

A
Annie_wang 已提交
2764
Checks whether this file is a symbolic link.
A
annie_wangli 已提交
2765

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

A
Annie_wang 已提交
2768
**Return value**
A
Annie_wang 已提交
2769

A
annie_wangli 已提交
2770 2771
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2772
  | boolean | Whether the file is a symbolic link.|
A
annie_wangli 已提交
2773

A
Annie_wang 已提交
2774
**Example**
A
Annie_wang 已提交
2775

A
annie_wangli 已提交
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787
  ```js
  let isSymbolicLink = fileio.statSync(path).isSymbolicLink(); 
  ```


## 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 已提交
2788
stop(): Promise&lt;void&gt;
A
annie_wangli 已提交
2789

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

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

A
Annie_wang 已提交
2794
**Example**
A
Annie_wang 已提交
2795

A
annie_wangli 已提交
2796
  ```js
A
Annie_wang 已提交
2797 2798 2799 2800 2801 2802 2803
  let filename = path +"/test.txt";
  let watcher = await fileio.createWatcher(filename, 1, function(number){
      console.info("Monitoring times: "+number);
  });
  watcher.stop().then(function(){
       console.info("Watcher stopped");
  });
A
annie_wangli 已提交
2804 2805 2806 2807 2808
  ```


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

A
annie_wangli 已提交
2809
stop(callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2810

A
Annie_wang 已提交
2811
Stops the **watcher** instance. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2812

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

A
Annie_wang 已提交
2815
**Parameters**
A
Annie_wang 已提交
2816

A
annie_wangli 已提交
2817 2818 2819
  | Name     | Type                       | Mandatory  | Description                    |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when **watcher** is stopped asynchronously.|
A
annie_wangli 已提交
2820

A
Annie_wang 已提交
2821
**Example**
A
Annie_wang 已提交
2822

A
annie_wangli 已提交
2823
  ```js
A
Annie_wang 已提交
2824 2825 2826
  let filename = path +"/test.txt";
  let watcher = await fileio.createWatcher(filename, 1, function(number){
      console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2827
  });
A
Annie_wang 已提交
2828 2829 2830
  watcher.stop(function(){
      console.info("Watcher stopped");
  })
A
annie_wangli 已提交
2831
  ```
A
annie_wangli 已提交
2832

A
Annie_wang 已提交
2833
## Stream
A
annie_wangli 已提交
2834

A
Annie_wang 已提交
2835
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 已提交
2836 2837 2838 2839 2840 2841


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

close(): Promise&lt;void&gt;

A
Annie_wang 已提交
2842
Closes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2843 2844 2845

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2846
**Return value**
A
Annie_wang 已提交
2847

A
annie_wangli 已提交
2848 2849 2850 2851
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream close result.|

A
Annie_wang 已提交
2852
**Example**
A
Annie_wang 已提交
2853

A
annie_wangli 已提交
2854
  ```js
A
Annie_wang 已提交
2855
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2856
  ss.close().then(function(){
A
Annie_wang 已提交
2857
      console.info("Stream closed");
A
annie_wangli 已提交
2858
  }).catch(function(err){
A
Annie_wang 已提交
2859
      console.info("Failed to close the file stream. Error:"+ err);
A
annie_wangli 已提交
2860 2861 2862 2863 2864 2865 2866 2867
  });
  ```


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

close(callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
2868
Closes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2869 2870 2871

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2872
**Parameters**
A
Annie_wang 已提交
2873

A
annie_wangli 已提交
2874 2875 2876 2877
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|

A
Annie_wang 已提交
2878
**Example**
A
Annie_wang 已提交
2879

A
annie_wangli 已提交
2880
  ```js
A
Annie_wang 已提交
2881
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2882
  ss.close(function (err) {
A
Annie_wang 已提交
2883
      // Do something
A
annie_wangli 已提交
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
  });
  ```


### closeSync

closeSync(): void

Synchronously closes the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2896
**Example**
A
Annie_wang 已提交
2897

A
annie_wangli 已提交
2898
  ```js
A
Annie_wang 已提交
2899
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2900 2901 2902 2903 2904 2905 2906 2907
  ss.closeSync();
  ```


### flush<sup>7+</sup>

flush(): Promise&lt;void&gt;

A
Annie_wang 已提交
2908
Flushes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2909 2910 2911

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2912
**Return value**
A
Annie_wang 已提交
2913

A
annie_wangli 已提交
2914 2915 2916 2917
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream flushing result.|

A
Annie_wang 已提交
2918
**Example**
A
Annie_wang 已提交
2919

A
annie_wangli 已提交
2920
  ```js
A
Annie_wang 已提交
2921
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2922
  ss.flush().then(function (){
A
Annie_wang 已提交
2923
      console.info("Stream flushed");
A
annie_wangli 已提交
2924
  }).catch(function(err){
A
Annie_wang 已提交
2925
      console.info("Failed to flush the stream. Error:"+ err);
A
annie_wangli 已提交
2926 2927 2928 2929 2930 2931 2932 2933
  });
  ```


### flush<sup>7+</sup>

flush(callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
2934
Flushes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2935 2936 2937

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2938
**Parameters**
A
Annie_wang 已提交
2939

A
annie_wangli 已提交
2940 2941 2942 2943
  | Name     | Type                       | Mandatory  | Description            |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is asynchronously flushed.|

A
Annie_wang 已提交
2944
**Example**
A
Annie_wang 已提交
2945

A
annie_wangli 已提交
2946
  ```js
A
Annie_wang 已提交
2947
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2948
  ss.flush(function (err) {
A
Annie_wang 已提交
2949
      // Do something
A
annie_wangli 已提交
2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
  });
  ```


### flushSync<sup>7+</sup>

flushSync(): void

Synchronously flushes the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2962
**Example**
A
Annie_wang 已提交
2963

A
annie_wangli 已提交
2964
  ```js
A
Annie_wang 已提交
2965
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978
  ss.flushSync();
  ```


### write<sup>7+</sup>

write(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;

A
Annie_wang 已提交
2979
Writes data into the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2980 2981 2982

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2983
**Parameters**
A
Annie_wang 已提交
2984

A
annie_wangli 已提交
2985 2986 2987
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
2988
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
A
annie_wangli 已提交
2989

A
Annie_wang 已提交
2990
**Return value**
A
Annie_wang 已提交
2991

A
annie_wangli 已提交
2992 2993
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
2994
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
A
annie_wangli 已提交
2995

A
Annie_wang 已提交
2996
**Example**
A
Annie_wang 已提交
2997

A
annie_wangli 已提交
2998
  ```js
A
Annie_wang 已提交
2999
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
3000
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
A
Annie_wang 已提交
3001
      console.info("Data written to the stream. Size is:"+ number);
A
annie_wangli 已提交
3002
  }).catch(function(err){
A
Annie_wang 已提交
3003
      console.info("Failed to write data to the stream. Error:"+ err);
A
annie_wangli 已提交
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
  });
  ```


### write<sup>7+</sup>

write(buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void

A
Annie_wang 已提交
3017
Writes data into the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3018 3019 3020

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3021
**Parameters**
A
Annie_wang 已提交
3022

A
Annie_wang 已提交
3023 3024 3025
  | Name  | Type                           | Mandatory| Description                                                        |
  | -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | Yes  | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
3026
  | options  | Object                          | No  | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
A
Annie_wang 已提交
3027
  | callback | AsyncCallback&lt;number&gt;     | Yes  | Callback invoked when the data is written asynchronously.                              |
A
annie_wangli 已提交
3028

A
Annie_wang 已提交
3029
**Example**
A
Annie_wang 已提交
3030

A
annie_wangli 已提交
3031
  ```js
A
Annie_wang 已提交
3032
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
3033
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
A
Annie_wang 已提交
3034
      if (bytesWritten) {
A
Annie_wang 已提交
3035
         // Do something
A
Annie_wang 已提交
3036
         console.info("Data written to the stream. Size is:"+ bytesWritten);
A
annie_wangli 已提交
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054
      }
  });
  ```


### writeSync<sup>7+</sup>

writeSync(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number

Synchronously writes data into the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3055
**Parameters**
A
Annie_wang 已提交
3056

A
annie_wangli 已提交
3057 3058 3059
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
3060
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
A
annie_wangli 已提交
3061

A
Annie_wang 已提交
3062
**Return value**
A
Annie_wang 已提交
3063

A
annie_wangli 已提交
3064 3065 3066 3067
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data written in the file.|

A
Annie_wang 已提交
3068
**Example**
A
Annie_wang 已提交
3069

A
annie_wangli 已提交
3070
  ```js
A
Annie_wang 已提交
3071
  let ss= fileio.createStreamSync(path,"r+");
A
annie_wangli 已提交
3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


### read<sup>7+</sup>

read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;

A
Annie_wang 已提交
3084
Reads data from the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
3085 3086 3087

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3088
**Parameters**
A
Annie_wang 已提交
3089

A
annie_wangli 已提交
3090 3091 3092
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3093
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
A
annie_wangli 已提交
3094

A
Annie_wang 已提交
3095
**Return value**
A
Annie_wang 已提交
3096

A
annie_wangli 已提交
3097 3098
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
3099
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
3100

A
Annie_wang 已提交
3101
**Example**
A
Annie_wang 已提交
3102

A
annie_wangli 已提交
3103
  ```js
A
Annie_wang 已提交
3104
  let ss = fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
3105
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readout){
A
Annie_wang 已提交
3106
      console.info("Read data successfully");
A
Annie_wang 已提交
3107
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
3108
  }).catch(function(err){
A
Annie_wang 已提交
3109
      console.info("Failed to read data. Error:"+ err);
A
annie_wangli 已提交
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
  });
  ```


### read<sup>7+</sup>

read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void

A
Annie_wang 已提交
3122
Reads data from the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3123 3124 3125

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3126
**Parameters**
A
Annie_wang 已提交
3127

A
annie_wangli 已提交
3128 3129 3130
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3131
  | options  | Object                                   | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
A
annie_wangli 已提交
3132 3133
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when data is read asynchronously from the stream.                        |

A
Annie_wang 已提交
3134
**Example**
A
Annie_wang 已提交
3135

A
annie_wangli 已提交
3136
  ```js
A
Annie_wang 已提交
3137
  let ss = fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
3138
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
A
Annie_wang 已提交
3139
      if (readOut) {
A
Annie_wang 已提交
3140
          console.info("Read data successfully");
A
Annie_wang 已提交
3141
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158
      }
  });
  ```


### readSync<sup>7+</sup>

readSync(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): number

Synchronously reads data from the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3159 3160
**Parameters**

A
annie_wangli 已提交
3161 3162 3163
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3164 3165 3166
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |

**Return value**
A
annie_wangli 已提交
3167 3168 3169 3170 3171

  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data read.|

A
Annie_wang 已提交
3172
**Example**
A
Annie_wang 已提交
3173

A
annie_wangli 已提交
3174
  ```js
A
Annie_wang 已提交
3175
  let ss = fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
  ```


## Dir

Manages directories. Before calling a method of the **Dir** class, use the [opendir()](#fileioopendir) method synchronously or asynchronously to create a **Dir** instance.


### read

read(): Promise&lt;Dirent&gt;

A
Annie_wang 已提交
3189
Reads the next directory entry. This API uses a promise to return the result.
A
annie_wangli 已提交
3190 3191 3192

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3193
**Return value**
A
Annie_wang 已提交
3194

A
annie_wangli 已提交
3195 3196
  | Type                              | Description           |
  | -------------------------------- | ------------- |
A
Annie_wang 已提交
3197
  | Promise&lt;[Dirent](#dirent)&gt; | Promise used to return the directory entry read.|
A
annie_wangli 已提交
3198

A
Annie_wang 已提交
3199
**Example**
A
Annie_wang 已提交
3200

A
annie_wangli 已提交
3201 3202 3203
  ```js
  let dir = fileio.opendirSync(path);
  dir.read().then(function (dirent){
A
Annie_wang 已提交
3204
      console.log("Read the next directory entry:"+JSON.stringify(dirent));
A
annie_wangli 已提交
3205
  }).catch(function(err){
A
Annie_wang 已提交
3206
      console.info("Failed to read the next directory entry. Error:"+ err);
A
annie_wangli 已提交
3207 3208 3209 3210 3211 3212 3213 3214
  });
  ```


### read

read(callback: AsyncCallback&lt;Dirent&gt;): void

A
Annie_wang 已提交
3215
Reads the next directory entry. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3216 3217 3218

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3219
**Parameters**
A
Annie_wang 已提交
3220

A
annie_wangli 已提交
3221 3222 3223 3224
  | Name     | Type                                    | Mandatory  | Description              |
  | -------- | -------------------------------------- | ---- | ---------------- |
  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | Yes   | Callback invoked when the next directory entry is asynchronously read.|

A
Annie_wang 已提交
3225
**Example**
A
Annie_wang 已提交
3226

A
annie_wangli 已提交
3227 3228 3229
  ```js
  let dir = fileio.opendirSync(path);
  dir.read(function (err, dirent) {
A
Annie_wang 已提交
3230
      if (dirent) {
A
Annie_wang 已提交
3231 3232
          // Do something
          console.log("Read the next directory entry:"+JSON.stringify(dirent));
A
annie_wangli 已提交
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245
      }
  });
  ```


### readSync

readSync(): Dirent

Synchronously reads the next directory entry.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3246
**Return value**
A
Annie_wang 已提交
3247

A
annie_wangli 已提交
3248 3249 3250 3251
  | Type               | Description      |
  | ----------------- | -------- |
  | [Dirent](#dirent) | Directory entry read.|

A
Annie_wang 已提交
3252
**Example**
A
Annie_wang 已提交
3253

A
annie_wangli 已提交
3254 3255 3256 3257 3258 3259
  ```js
  let dir = fileio.opendirSync(path);
  let dirent = dir.readSync();
  ```


A
Annie_wang 已提交
3260 3261 3262 3263 3264 3265 3266 3267 3268
### 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 已提交
3269

A
Annie_wang 已提交
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286
  ```js
  let dir = fileio.opendirSync(path);
  dir.close().then(function(err){
      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 已提交
3287

A
Annie_wang 已提交
3288 3289 3290 3291 3292 3293 3294 3295
  ```js
  let dir = fileio.opendirSync(path);
  dir.close(function(err){
      console.info("close dir successfully");
  });
  ```


A
annie_wangli 已提交
3296 3297 3298 3299 3300 3301 3302 3303
### 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 已提交
3304
**Example**
A
Annie_wang 已提交
3305

A
annie_wangli 已提交
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328
  ```js
  let dir = fileio.opendirSync(path);
  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 已提交
3329
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 已提交
3330 3331 3332

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3333
**Return value**
A
Annie_wang 已提交
3334

A
annie_wangli 已提交
3335 3336 3337 3338
  | Type     | Description              |
  | ------- | ---------------- |
  | boolean | Whether the directory entry is a block special file.|

A
Annie_wang 已提交
3339
**Example**
A
Annie_wang 已提交
3340

A
annie_wangli 已提交
3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
  ```js
  let dir = fileio.opendirSync(path);
  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 已提交
3355
**Return value**
A
Annie_wang 已提交
3356

A
annie_wangli 已提交
3357 3358 3359 3360
  | Type     | Description               |
  | ------- | ----------------- |
  | boolean | Whether the directory entry is a character special file.|

A
Annie_wang 已提交
3361
**Example**
A
Annie_wang 已提交
3362

A
annie_wangli 已提交
3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376
  ```js
  let dir = fileio.opendirSync(path);
  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 已提交
3377
**Return value**
A
Annie_wang 已提交
3378

A
annie_wangli 已提交
3379 3380 3381 3382
  | Type     | Description           |
  | ------- | ------------- |
  | boolean | Whether the directory entry is a directory.|

A
Annie_wang 已提交
3383
**Example**
A
Annie_wang 已提交
3384

A
annie_wangli 已提交
3385 3386 3387 3388 3389 3390 3391 3392 3393 3394
  ```js
  let dir = fileio.opendirSync(path);
  let isDirectory = dir.readSync().isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

A
Annie_wang 已提交
3395
Checks whether this directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication.
A
annie_wangli 已提交
3396 3397 3398

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3399
**Return value**
A
Annie_wang 已提交
3400

A
annie_wangli 已提交
3401 3402 3403 3404
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a FIFO.|

A
Annie_wang 已提交
3405
**Example**
A
Annie_wang 已提交
3406

A
annie_wangli 已提交
3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
  ```js
  let dir = fileio.opendirSync(path);
  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 已提交
3421
**Return value**
A
Annie_wang 已提交
3422

A
annie_wangli 已提交
3423 3424 3425 3426
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a regular file.|

A
Annie_wang 已提交
3427
**Example**
A
Annie_wang 已提交
3428

A
annie_wangli 已提交
3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442
  ```js
  let dir = fileio.opendirSync(path);
  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 已提交
3443
**Return value**
A
Annie_wang 已提交
3444

A
annie_wangli 已提交
3445 3446 3447 3448
  | Type     | Description            |
  | ------- | -------------- |
  | boolean | Whether the directory entry is a socket.|

A
Annie_wang 已提交
3449
**Example**
A
Annie_wang 已提交
3450

A
annie_wangli 已提交
3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
  ```js
  let dir = fileio.opendirSync(dpath);
  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 已提交
3465
**Return value**
A
Annie_wang 已提交
3466

A
annie_wangli 已提交
3467 3468 3469 3470
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a symbolic link.|

A
Annie_wang 已提交
3471
**Example**
A
Annie_wang 已提交
3472

A
annie_wangli 已提交
3473 3474 3475 3476
  ```js
  let dir = fileio.opendirSync(path);
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```