js-apis-fileio.md 130.9 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
Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows:
A
Annie_wang 已提交
19

A
Annie_wang 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Stage Model

 ```js
import Ability from '@ohos.application.Ability';
class MainAbility extends Ability {
    onWindowStageCreate(windowStage) {
        let context = this.context;
        let path = context.filesDir;
    }
}
 ```

 For details about how to obtain the stage model context, see [Stage Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-ability-context.md#abilitycontext).

FA Model

A
Annie_wang 已提交
36 37 38 39
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
 let context = featureAbility.getContext();
 context.getFilesDir().then((data) => {
A
Annie_wang 已提交
40
      let path = data;
A
Annie_wang 已提交
41
 })
A
Annie_wang 已提交
42
 ```
A
Annie_wang 已提交
43 44
 
 For details about how to obtain the context of the FA model, see [FA Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-Context.md#context).
Z
zengyawen 已提交
45

A
annie_wangli 已提交
46 47 48 49
## fileio.stat

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

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

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

A
Annie_wang 已提交
54 55 56 57
**Parameters**

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

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

Z
zengyawen 已提交
62 63
  | Type                          | Description        |
  | ---------------------------- | ---------- |
A
annie_wangli 已提交
64 65
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information obtained.|

A
Annie_wang 已提交
66
**Example**
A
Annie_wang 已提交
67

A
annie_wangli 已提交
68 69
  ```js
  fileio.stat(path).then(function(stat){
A
Annie_wang 已提交
70
      console.info("Got file info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
71
  }).catch(function(err){
A
Annie_wang 已提交
72
      console.info("Failed to get file info. Error:"+ err);
A
annie_wangli 已提交
73 74 75 76 77 78 79 80
  });
  ```


## fileio.stat

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

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

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

A
Annie_wang 已提交
85
**Parameters**
A
Annie_wang 已提交
86

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

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

A
annie_wangli 已提交
94 95 96 97 98 99 100 101 102 103
  ```js
  fileio.stat(path, function (err, stat) {
      // Example code in Stat
  });
  ```


## fileio.statSync

statSync(path:string): Stat
Z
zengyawen 已提交
104 105 106

Synchronously obtains file information.

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

A
Annie_wang 已提交
109
**Parameters**
A
Annie_wang 已提交
110

A
Annie_wang 已提交
111 112
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
113
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
114 115


A
Annie_wang 已提交
116
**Return value**
A
Annie_wang 已提交
117

Z
zengyawen 已提交
118 119
  | Type           | Description        |
  | ------------- | ---------- |
A
annie_wangli 已提交
120 121
  | [Stat](#stat) | File information obtained.|

A
Annie_wang 已提交
122
**Example**
A
Annie_wang 已提交
123

A
annie_wangli 已提交
124 125 126 127 128 129 130 131 132 133
  ```js
  let stat = fileio.statSync(path);
  // Example code in Stat
  ```


## fileio.opendir

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

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

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

A
Annie_wang 已提交
138
**Parameters**
A
Annie_wang 已提交
139

A
Annie_wang 已提交
140 141 142
| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
143

A
Annie_wang 已提交
144
**Return value**
A
Annie_wang 已提交
145

Z
zengyawen 已提交
146 147
  | Type                        | Description      |
  | -------------------------- | -------- |
A
Annie_wang 已提交
148
  | Promise&lt;[Dir](#dir)&gt; | Promise used to return the **Dir** object.|
A
annie_wangli 已提交
149

A
Annie_wang 已提交
150
**Example**
A
Annie_wang 已提交
151

A
annie_wangli 已提交
152 153
  ```js
  fileio.opendir(path).then(function(dir){
A
Annie_wang 已提交
154
      console.info("Directory opened:"+ JSON.stringify(dir));
A
annie_wangli 已提交
155
  }).catch(function(err){
A
Annie_wang 已提交
156
      console.info("Failed to open the directory. Error:"+ err);
A
annie_wangli 已提交
157 158 159 160 161 162 163 164
  });
  ```


## fileio.opendir

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

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

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

A
Annie_wang 已提交
169
**Parameters**
A
annie_wangli 已提交
170

A
Annie_wang 已提交
171 172 173 174 175 176
| 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.  |

**Example**
A
Annie_wang 已提交
177

A
annie_wangli 已提交
178 179 180
  ```js
  fileio.opendir(path, function (err, dir) { 
      // Example code in Dir struct
A
annie_wangli 已提交
181
      // Use read/readSync/close.
A
annie_wangli 已提交
182 183 184 185 186 187 188
  });
  ```


## fileio.opendirSync

opendirSync(path: string): Dir
Z
zengyawen 已提交
189 190 191

Synchronously opens a directory.

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

A
annie_wangli 已提交
194

A
Annie_wang 已提交
195 196 197 198 199
**Parameters**

| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
200

A
Annie_wang 已提交
201
**Return value**
A
Annie_wang 已提交
202

Z
zengyawen 已提交
203 204
  | Type         | Description      |
  | ----------- | -------- |
A
annie_wangli 已提交
205 206
  | [Dir](#dir) | A **Dir** instance corresponding to the directory.|

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

A
annie_wangli 已提交
209 210 211 212 213 214 215 216 217 218 219
  ```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 已提交
220
Checks whether the current process can access a file. This API uses a promise to return the result.
A
annie_wangli 已提交
221

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

A
Annie_wang 已提交
224
**Parameters**
A
annie_wangli 已提交
225

A
Annie_wang 已提交
226 227
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
228
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
229 230 231
| 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.|

**Return value**
A
Annie_wang 已提交
232

Z
zengyawen 已提交
233 234
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
235
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
236

A
Annie_wang 已提交
237
**Example**
A
Annie_wang 已提交
238

A
annie_wangli 已提交
239 240
  ```js
  fileio.access(path).then(function() {
A
Annie_wang 已提交
241
      console.info("Access successful");
A
annie_wangli 已提交
242
  }).catch(function(err){
A
Annie_wang 已提交
243
      console.info("Access failed. Error:"+ err);
A
annie_wangli 已提交
244 245 246 247 248 249
  });
  ```


## fileio.access

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

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

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

A
Annie_wang 已提交
256
**Parameters**
A
Annie_wang 已提交
257

A
Annie_wang 已提交
258 259
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
260
| path     | string                    | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
261 262
| 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 已提交
263

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

A
annie_wangli 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278
  ```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 已提交
279
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
280

A
Annie_wang 已提交
281
**Parameters**
A
Annie_wang 已提交
282

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

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

A
annie_wangli 已提交
290 291 292 293
  ```js
  try {
      fileio.accessSync(path);
  } catch(err) {
A
Annie_wang 已提交
294
      console.info("accessSync failed. Error:"+ err);
A
annie_wangli 已提交
295 296 297 298 299 300 301 302
  }
  ```


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

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

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

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

A
Annie_wang 已提交
307
**Parameters**
A
Annie_wang 已提交
308

Z
zengyawen 已提交
309 310 311
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
312

A
Annie_wang 已提交
313
**Return value**
A
Annie_wang 已提交
314

Z
zengyawen 已提交
315 316
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
317
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
318

A
Annie_wang 已提交
319
**Example**
A
Annie_wang 已提交
320

A
annie_wangli 已提交
321 322 323
  ```js
  let fd = fileio.openSync(path);
  fileio.close(fd).then(function(){
A
Annie_wang 已提交
324
      console.info("File closed");
A
annie_wangli 已提交
325
  }).catch(function(err){
A
Annie_wang 已提交
326
      console.info("Failed to close the file. Error:"+ err);
A
annie_wangli 已提交
327 328 329 330 331 332 333 334
  });
  ```


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

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

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

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

A
Annie_wang 已提交
339
**Parameters**
A
Annie_wang 已提交
340

Z
zengyawen 已提交
341 342 343 344
  | 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 已提交
345

A
Annie_wang 已提交
346
**Example**
A
Annie_wang 已提交
347

A
annie_wangli 已提交
348 349 350 351 352 353 354 355 356 357 358
  ```js
  let fd = fileio.openSync(path);
  fileio.close(fd, function (err) {
      // Do something.
  });
  ```


## fileio.closeSync

closeSync(fd: number): void
Z
zengyawen 已提交
359 360 361

Synchronously closes a file.

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

A
Annie_wang 已提交
364
**Parameters**
A
Annie_wang 已提交
365

Z
zengyawen 已提交
366 367 368
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
369

A
Annie_wang 已提交
370
**Example**
A
Annie_wang 已提交
371

A
annie_wangli 已提交
372
  ```js
A
Annie_wang 已提交
373
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
374 375 376 377 378 379 380 381
  fileio.closeSync(fd);
  ```


## fileio.copyFile

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

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

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

A
Annie_wang 已提交
386
**Parameters**
A
Annie_wang 已提交
387

Z
zengyawen 已提交
388 389 390 391 392
  | 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 已提交
393

A
Annie_wang 已提交
394
**Return value**
A
Annie_wang 已提交
395

Z
zengyawen 已提交
396 397
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
398
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
399

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

A
annie_wangli 已提交
402
  ```js
A
Annie_wang 已提交
403 404
  let src = path;
  let dest = src + 'tgt';
A
annie_wangli 已提交
405
  fileio.copyFile(src, dest).then(function(){
A
Annie_wang 已提交
406
      console.info("File copied");
A
annie_wangli 已提交
407
  }).catch(function(err){
A
Annie_wang 已提交
408
      console.info("Failed to copy the file. Error:"+ err);
A
annie_wangli 已提交
409 410 411 412 413 414
  });
  ```


## fileio.copyFile

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

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

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

A
Annie_wang 已提交
421
**Parameters**
A
Annie_wang 已提交
422

Z
zengyawen 已提交
423 424 425 426 427 428
  | 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 已提交
429

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

A
annie_wangli 已提交
432
  ```js
A
Annie_wang 已提交
433 434
  let src = path;
  let dest = src + 'tgt';
A
annie_wangli 已提交
435 436 437 438 439 440 441 442
  fileio.copyFile(src, dest, function (err) {
      // Do something.
  });
  ```


## fileio.copyFileSync

A
annie_wangli 已提交
443
copyFileSync(src: string | number, dest: string | number, mode?: number): void
Z
zengyawen 已提交
444 445 446

Synchronously copies a file.

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

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

Z
zengyawen 已提交
451 452 453 454 455
  | 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 已提交
456

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

A
annie_wangli 已提交
459
  ```js
A
Annie_wang 已提交
460 461
  let src = path;
  let dest = src + 'tgt';
A
annie_wangli 已提交
462 463 464 465 466 467 468 469
  fileio.copyFileSync(src, dest);
  ```


## fileio.mkdir

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

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

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

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

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

A
Annie_wang 已提交
481
**Return value**
A
Annie_wang 已提交
482

Z
zengyawen 已提交
483 484
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
485
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
486

A
Annie_wang 已提交
487
**Example**
A
Annie_wang 已提交
488

A
annie_wangli 已提交
489 490
  ```js
  fileio.mkdir(path).then(function() {
A
Annie_wang 已提交
491
      console.info("Directory created");
A
annie_wangli 已提交
492
  }).catch(function (error){
A
Annie_wang 已提交
493
      console.info("Failed to create the directory. Error:"+ error);
A
annie_wangli 已提交
494 495 496 497 498 499
  });
  ```


## fileio.mkdir

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

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

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

A
Annie_wang 已提交
506
**Parameters**
A
Annie_wang 已提交
507

A
Annie_wang 已提交
508 509
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
510
| path     | string                    | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
511 512
| 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 已提交
513

A
Annie_wang 已提交
514
**Example**
A
Annie_wang 已提交
515

A
annie_wangli 已提交
516 517
  ```js
  fileio.mkdir(path, function(err) {
A
Annie_wang 已提交
518
    console.info("Directory created");
A
annie_wangli 已提交
519 520 521 522 523 524
  });
  ```


## fileio.mkdirSync

A
annie_wangli 已提交
525
mkdirSync(path: string, mode?: number): void
Z
zengyawen 已提交
526 527 528

Synchronously creates a directory.

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

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

A
Annie_wang 已提交
533 534
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
535
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
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.|
A
annie_wangli 已提交
537

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

A
annie_wangli 已提交
540 541 542 543 544 545 546 547 548
  ```js
  fileio.mkdirSync(path);
  ```


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

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

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

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

A
Annie_wang 已提交
553
**Parameters**
A
Annie_wang 已提交
554

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

A
Annie_wang 已提交
561
**Return value**
A
Annie_wang 已提交
562

Z
zengyawen 已提交
563 564
  | Type                   | Description         |
  | --------------------- | ----------- |
A
Annie_wang 已提交
565
  | Promise&lt;number&gt; | Promise used to return the file descriptor of the file opened.|
A
annie_wangli 已提交
566

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

A
annie_wangli 已提交
569 570
  ```js
  fileio.open(path, 0o1, 0o0200).then(function(number){
A
Annie_wang 已提交
571
      console.info("File opened");
A
Annie_wang 已提交
572
  }).catch(function(err){
A
Annie_wang 已提交
573
      console.info("Failed to open the file. Error:"+ err);
A
annie_wangli 已提交
574 575 576 577 578 579 580 581
  });
  ```


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

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

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

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

A
Annie_wang 已提交
586
**Parameters**
A
Annie_wang 已提交
587

A
Annie_wang 已提交
588 589
| Name  | Type                           | Mandatory| Description                                                        |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
590
| path     | string                          | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
591
| flags    | number                          | Yes  | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>-&nbsp;**0o200000**: If **path** does not point to a directory, throw an exception.<br><br>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
A
Annie_wang 已提交
592 593
| 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 已提交
594

A
Annie_wang 已提交
595
**Example**
A
Annie_wang 已提交
596

A
annie_wangli 已提交
597 598 599 600 601 602 603 604 605 606
  ```js
  fileio.open(path, 0, function(err, fd) {
      // Do something.
  });
  ```


## fileio.openSync

openSync(path:string, flags?:number, mode?:number): number
Z
zengyawen 已提交
607 608 609

Synchronously opens a file.

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

A
Annie_wang 已提交
612
**Parameters**
A
Annie_wang 已提交
613

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

A
Annie_wang 已提交
620
**Return value**
A
Annie_wang 已提交
621

Z
zengyawen 已提交
622 623
  | Type    | Description         |
  | ------ | ----------- |
A
annie_wangli 已提交
624 625
  | number | File descriptor of the file opened.|

A
Annie_wang 已提交
626
**Example**
A
Annie_wang 已提交
627

A
annie_wangli 已提交
628
  ```js
A
Annie_wang 已提交
629 630 631 632 633 634 635 636 637
  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 已提交
638 639 640 641 642
  ```


## fileio.read

A
annie_wangli 已提交
643 644 645 646 647
read(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): Promise&lt;ReadOut&gt;
A
annie_wangli 已提交
648

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

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

A
Annie_wang 已提交
653
**Parameters**
A
Annie_wang 已提交
654

A
Annie_wang 已提交
655 656 657 658 659 660 661
| 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 已提交
662

Z
zengyawen 已提交
663 664
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
665
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
666

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

A
annie_wangli 已提交
669 670 671
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
A
Annie_wang 已提交
672
  fileio.read(fd, buf).then(function(readOut){
A
Annie_wang 已提交
673
      console.info("Read file data successfully");
A
Annie_wang 已提交
674
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
Annie_wang 已提交
675 676
  }).catch(function(err){
      console.info("Failed to read file data. Error:"+ err);
A
annie_wangli 已提交
677 678 679 680 681 682
  });
  ```


## fileio.read

A
annie_wangli 已提交
683 684 685 686 687
read(fd: number, buffer: ArrayBuffer, options: {
    offset?: number;
    length?: number;
    position?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
A
annie_wangli 已提交
688

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

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

A
Annie_wang 已提交
693
**Parameters**
A
Annie_wang 已提交
694

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

A
Annie_wang 已提交
702
**Example**
A
Annie_wang 已提交
703

A
annie_wangli 已提交
704 705 706 707
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
  fileio.read(fd, buf, function (err, readOut) {
A
Annie_wang 已提交
708
      if (readOut) {
A
Annie_wang 已提交
709
          console.info("Read file data successfully");
A
Annie_wang 已提交
710
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
711 712 713 714 715 716 717
      }
  });
  ```


## fileio.readSync

A
annie_wangli 已提交
718 719 720 721 722
readSync(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): number
Z
zengyawen 已提交
723 724 725

Synchronously reads data from a file.

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

A
Annie_wang 已提交
728
**Parameters**
A
Annie_wang 已提交
729

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

A
Annie_wang 已提交
736
**Return value**
A
Annie_wang 已提交
737

Z
zengyawen 已提交
738 739
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
740 741
  | number | Length of the data read.|

A
Annie_wang 已提交
742
**Example**
A
Annie_wang 已提交
743

A
annie_wangli 已提交
744 745 746 747 748 749 750 751 752 753 754
  ```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 已提交
755
Deletes a directory. This API uses a promise to return the result.
A
annie_wangli 已提交
756

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

A
Annie_wang 已提交
759
**Parameters**
A
Annie_wang 已提交
760

A
Annie_wang 已提交
761 762
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
763
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
764

A
Annie_wang 已提交
765
**Return value**
A
Annie_wang 已提交
766

Z
zengyawen 已提交
767 768
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
769
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
770

A
Annie_wang 已提交
771
**Example**
A
Annie_wang 已提交
772

A
annie_wangli 已提交
773 774
  ```js
  fileio.rmdir(path).then(function() {
A
Annie_wang 已提交
775
      console.info("Directory deleted");
A
annie_wangli 已提交
776
  }).catch(function(err){
A
Annie_wang 已提交
777
      console.info("Failed to delete the directory. Error:"+ err);
A
annie_wangli 已提交
778 779 780 781 782 783 784 785
  });
  ```


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

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

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

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

A
Annie_wang 已提交
790
**Parameters**
A
Annie_wang 已提交
791

A
Annie_wang 已提交
792 793
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
794
| path     | string                    | Yes  | Application sandbox path of the directory.|
A
Annie_wang 已提交
795
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is deleted asynchronously.  |
A
annie_wangli 已提交
796

A
Annie_wang 已提交
797
**Example**
A
Annie_wang 已提交
798

A
annie_wangli 已提交
799 800 801
  ```js
  fileio.rmdir(path, function(err){
      // Do something.
A
Annie_wang 已提交
802
      console.info("Directory deleted");
A
annie_wangli 已提交
803 804 805 806
  });
  ```


A
annie_wangli 已提交
807
## fileio.rmdirSync<sup>7+</sup>
A
annie_wangli 已提交
808

A
annie_wangli 已提交
809
rmdirSync(path: string): void
A
annie_wangli 已提交
810 811 812

Synchronously deletes a directory.

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

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

A
Annie_wang 已提交
817 818
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
819
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
820

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

A
annie_wangli 已提交
823 824 825 826 827 828 829 830 831
  ```js
  fileio.rmdirSync(path);
  ```


## fileio.unlink

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

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

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

A
Annie_wang 已提交
836
**Parameters**
A
Annie_wang 已提交
837

A
Annie_wang 已提交
838 839
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
840
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
841

A
Annie_wang 已提交
842
**Return value**
A
Annie_wang 已提交
843

Z
zengyawen 已提交
844 845
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
846
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
847

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

A
annie_wangli 已提交
850 851
  ```js
  fileio.unlink(path).then(function(){
A
Annie_wang 已提交
852
      console.info("File deleted");
A
annie_wangli 已提交
853
  }).catch(function(error){
A
Annie_wang 已提交
854
      console.info("Failed to delete the file. Error:"+ error);
A
annie_wangli 已提交
855 856 857 858 859 860 861 862
  });
  ```


## fileio.unlink

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

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

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

A
Annie_wang 已提交
867
**Parameters**
A
Annie_wang 已提交
868

A
Annie_wang 已提交
869 870
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
871
| path     | string                    | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
872
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is deleted asynchronously.  |
A
annie_wangli 已提交
873

A
Annie_wang 已提交
874
**Example**
A
Annie_wang 已提交
875

A
annie_wangli 已提交
876 877
  ```js
  fileio.unlink(path, function(err) {
A
Annie_wang 已提交
878
      console.info("File deleted");
A
annie_wangli 已提交
879 880 881 882 883 884 885
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void
Z
zengyawen 已提交
886 887 888

Synchronously deletes a file.

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.|
Z
zengyawen 已提交
896

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

A
annie_wangli 已提交
899 900 901
  ```js
  fileio.unlinkSync(path);
  ```
Z
zengyawen 已提交
902 903


A
annie_wangli 已提交
904
## fileio.write
Z
zengyawen 已提交
905

A
annie_wangli 已提交
906 907 908 909 910 911
write(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
912

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

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

A
Annie_wang 已提交
917
**Parameters**
A
Annie_wang 已提交
918

Z
zengyawen 已提交
919 920 921 922
  | 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 已提交
923
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
Z
zengyawen 已提交
924

A
Annie_wang 已提交
925
**Return value**
A
Annie_wang 已提交
926

Z
zengyawen 已提交
927 928
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
929
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
Z
zengyawen 已提交
930

A
Annie_wang 已提交
931
**Example**
A
Annie_wang 已提交
932

A
annie_wangli 已提交
933
  ```js
A
Annie_wang 已提交
934
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
A
annie_wangli 已提交
935
  fileio.write(fd, "hello, world").then(function(number){
A
Annie_wang 已提交
936
       console.info("Data written to the file. Size is:"+ number);
A
annie_wangli 已提交
937
  }).catch(function(err){
A
Annie_wang 已提交
938
      console.info("Failed to write data to the file. Error:"+ err);
A
annie_wangli 已提交
939 940
  });
  ```
Z
zengyawen 已提交
941 942


A
annie_wangli 已提交
943
## fileio.write
Z
zengyawen 已提交
944

A
annie_wangli 已提交
945 946 947 948 949 950
write(fd: number, buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
951

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

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

A
Annie_wang 已提交
956
**Parameters**
A
Annie_wang 已提交
957

Z
zengyawen 已提交
958 959 960 961
  | 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 已提交
962
  | options  | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
Z
zengyawen 已提交
963
  | callback | AsyncCallback&lt;number&gt;     | Yes   | Callback invoked when the data is written asynchronously.                      |
Z
zengyawen 已提交
964

A
Annie_wang 已提交
965
**Example**
A
Annie_wang 已提交
966

A
annie_wangli 已提交
967 968 969
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
A
Annie_wang 已提交
970
      if (bytesWritten) {
A
Annie_wang 已提交
971
         console.info("Data written to the file. Size is:"+ bytesWritten);
A
annie_wangli 已提交
972 973 974
      }
  });
  ```
Z
zengyawen 已提交
975 976


A
annie_wangli 已提交
977
## fileio.writeSync
Z
zengyawen 已提交
978

A
annie_wangli 已提交
979 980 981 982 983 984
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
985

A
annie_wangli 已提交
986
Synchronously writes data into a file.
Z
zengyawen 已提交
987

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

A
Annie_wang 已提交
990
**Parameters**
A
Annie_wang 已提交
991

Z
zengyawen 已提交
992 993 994 995
  | 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 已提交
996
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
Z
zengyawen 已提交
997

A
Annie_wang 已提交
998
**Return value**
A
Annie_wang 已提交
999

Z
zengyawen 已提交
1000 1001
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
1002
  | number | Length of the data written in the file.|
Z
zengyawen 已提交
1003

A
Annie_wang 已提交
1004
**Example**
A
Annie_wang 已提交
1005

A
annie_wangli 已提交
1006 1007 1008 1009
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  let num = fileio.writeSync(fd, "hello, world");
  ```
Z
zengyawen 已提交
1010 1011


A
annie_wangli 已提交
1012
## fileio.hash
Z
zengyawen 已提交
1013

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

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

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

A
Annie_wang 已提交
1020
**Parameters**
A
Annie_wang 已提交
1021

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

A
Annie_wang 已提交
1027
**Return value**
A
Annie_wang 已提交
1028

Z
zengyawen 已提交
1029 1030
  | Type                   | Description                        |
  | --------------------- | -------------------------- |
A
Annie_wang 已提交
1031
  | Promise&lt;string&gt; | Promise used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
Z
zengyawen 已提交
1032

A
Annie_wang 已提交
1033
**Example**
A
Annie_wang 已提交
1034

A
annie_wangli 已提交
1035 1036
  ```js
  fileio.hash(path, "sha256").then(function(str){
A
Annie_wang 已提交
1037
      console.info("Calculated file hash:"+ str);
A
Annie_wang 已提交
1038
  }).catch(function(err){
A
Annie_wang 已提交
1039
      console.info("Failed to calculate the file hash. Error:"+ err);
A
annie_wangli 已提交
1040 1041
  });
  ```
Z
zengyawen 已提交
1042 1043


A
annie_wangli 已提交
1044
## fileio.hash
Z
zengyawen 已提交
1045

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

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

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

A
Annie_wang 已提交
1052
**Parameters**
A
Annie_wang 已提交
1053

A
Annie_wang 已提交
1054 1055
| Name   | Type                       | Mandatory| Description                                                        |
| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1056
| path      | string                      | Yes  | Application sandbox path of the file.                            |
A
Annie_wang 已提交
1057
| algorithm | string                      | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
A
Annie_wang 已提交
1058
| callback  | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
Z
zengyawen 已提交
1059

A
Annie_wang 已提交
1060
**Example**
A
Annie_wang 已提交
1061

A
annie_wangli 已提交
1062
  ```js
A
Annie_wang 已提交
1063
  fileio.hash(path, "sha256", function(err, hashStr) {
A
Annie_wang 已提交
1064
      if (hashStr) {
A
Annie_wang 已提交
1065
          console.info("Calculated file hash:"+ hashStr);
A
annie_wangli 已提交
1066 1067 1068
      }
  });
  ```
Z
zengyawen 已提交
1069 1070


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

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

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

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

A
Annie_wang 已提交
1079
**Parameters**
A
Annie_wang 已提交
1080

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

A
Annie_wang 已提交
1086
**Return value**
A
Annie_wang 已提交
1087

Z
zengyawen 已提交
1088 1089
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1090
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1091

A
Annie_wang 已提交
1092
**Example**
A
Annie_wang 已提交
1093

A
annie_wangli 已提交
1094
  ```js
A
Annie_wang 已提交
1095
  fileio.chmod(path, 0o700).then(function() {
A
Annie_wang 已提交
1096
      console.info("File permissions changed");
A
annie_wangli 已提交
1097
  }).catch(function(err){
A
Annie_wang 已提交
1098
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
1099 1100
  });
  ```
Z
zengyawen 已提交
1101 1102


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

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

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

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

A
Annie_wang 已提交
1111
**Parameters**
A
Annie_wang 已提交
1112

A
Annie_wang 已提交
1113 1114
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1115
| path     | string                    | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1116 1117
| 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 已提交
1118

A
Annie_wang 已提交
1119
**Example**
A
Annie_wang 已提交
1120

A
annie_wangli 已提交
1121
  ```js
A
Annie_wang 已提交
1122
  fileio.chmod(path, 0o700, function (err) {
A
annie_wangli 已提交
1123 1124 1125
      // Do something.
  });
  ```
Z
zengyawen 已提交
1126 1127


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

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

A
Annie_wang 已提交
1132
Synchronously changes file permissions.
Z
zengyawen 已提交
1133

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

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

A
Annie_wang 已提交
1138 1139
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1140
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
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.|
Z
zengyawen 已提交
1142

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

A
annie_wangli 已提交
1145
  ```js
A
Annie_wang 已提交
1146
  fileio.chmodSync(path, 0o700);
A
annie_wangli 已提交
1147
  ```
Z
zengyawen 已提交
1148 1149


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

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

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

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

A
Annie_wang 已提交
1158
**Parameters**
A
Annie_wang 已提交
1159

Z
zengyawen 已提交
1160 1161 1162
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1163

A
Annie_wang 已提交
1164
**Return value**
A
Annie_wang 已提交
1165

Z
zengyawen 已提交
1166
  | Type                          | Description        |
A
Annie_wang 已提交
1167
  | ---------------------------- | ---------- |
A
Annie_wang 已提交
1168
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information.|
Z
zengyawen 已提交
1169

A
Annie_wang 已提交
1170
**Example**
A
Annie_wang 已提交
1171

A
annie_wangli 已提交
1172
  ```js
A
Annie_wang 已提交
1173
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1174
  fileio.fstat(fd).then(function(stat){
A
Annie_wang 已提交
1175
      console.info("Obtained file info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1176
  }).catch(function(err){
A
Annie_wang 已提交
1177
      console.info("Failed to obtain file info. Error:"+ err);
A
annie_wangli 已提交
1178 1179
  });
  ```
Z
zengyawen 已提交
1180 1181


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

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

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

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

A
Annie_wang 已提交
1190
**Parameters**
A
Annie_wang 已提交
1191

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

A
Annie_wang 已提交
1197
**Example**
A
Annie_wang 已提交
1198

A
annie_wangli 已提交
1199 1200 1201 1202 1203 1204
  ```js
  let fd = fileio.openSync(path);
  fileio.fstat(fd, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
1205 1206


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

A
annie_wangli 已提交
1209
fstatSync(fd: number): Stat
Z
zengyawen 已提交
1210

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

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

A
Annie_wang 已提交
1215
**Parameters**
A
Annie_wang 已提交
1216

Z
zengyawen 已提交
1217 1218 1219
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1220

A
Annie_wang 已提交
1221
**Return value**
A
Annie_wang 已提交
1222

Z
zengyawen 已提交
1223 1224
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1225
  | [Stat](#stat) | File information obtained.|
Z
zengyawen 已提交
1226

A
Annie_wang 已提交
1227
**Example**
A
Annie_wang 已提交
1228

A
annie_wangli 已提交
1229 1230 1231 1232
  ```js
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```
Z
zengyawen 已提交
1233 1234


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

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

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

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

A
Annie_wang 已提交
1243
**Parameters**
A
Annie_wang 已提交
1244

Z
zengyawen 已提交
1245 1246 1247
  | Name | Type    | Mandatory  | Description              |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | Yes   | File descriptor of the file to truncate.    |
A
Annie_wang 已提交
1248
  | len  | number | No   | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1249

A
Annie_wang 已提交
1250
**Return value**
A
Annie_wang 已提交
1251

Z
zengyawen 已提交
1252 1253
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1254
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1255

A
Annie_wang 已提交
1256
**Example**
A
Annie_wang 已提交
1257

A
annie_wangli 已提交
1258 1259 1260
  ```js
  let fd = fileio.openSync(path);
  fileio.ftruncate(fd, 5).then(function(err) {    
A
Annie_wang 已提交
1261
      console.info("File truncated");
A
annie_wangli 已提交
1262 1263 1264 1265
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1266 1267


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

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

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

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

A
Annie_wang 已提交
1276
**Parameters**
A
Annie_wang 已提交
1277

Z
zengyawen 已提交
1278 1279 1280 1281
  | 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 已提交
1282
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback that returns no value.|
Z
zengyawen 已提交
1283

A
Annie_wang 已提交
1284
**Example**
A
Annie_wang 已提交
1285

A
annie_wangli 已提交
1286
  ```js
A
Annie_wang 已提交
1287 1288 1289
  let fd = fileio.openSync(path);
  let len = 5;
  fileio.ftruncate(fd, 5, function(err){
A
annie_wangli 已提交
1290 1291 1292
      // Do something.
  });
  ```
Z
zengyawen 已提交
1293 1294


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

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

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

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

A
Annie_wang 已提交
1303
**Parameters**
A
Annie_wang 已提交
1304

Z
zengyawen 已提交
1305 1306 1307 1308
  | 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 已提交
1309

A
Annie_wang 已提交
1310
**Example**
A
Annie_wang 已提交
1311

A
annie_wangli 已提交
1312
  ```js
A
Annie_wang 已提交
1313 1314
  let fd = fileio.openSync(path);
  let len = 5;
A
Annie_wang 已提交
1315
  fileio.ftruncateSync(fd, len);
A
annie_wangli 已提交
1316
  ```
Z
zengyawen 已提交
1317 1318


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

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

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

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

A
Annie_wang 已提交
1327
**Parameters**
A
Annie_wang 已提交
1328

A
Annie_wang 已提交
1329 1330
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
A
Annie_wang 已提交
1331
| path   | string | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1332
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1333

A
Annie_wang 已提交
1334
**Return value**
A
Annie_wang 已提交
1335

Z
zengyawen 已提交
1336 1337
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1338
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1339

A
Annie_wang 已提交
1340
**Example**
A
Annie_wang 已提交
1341

A
annie_wangli 已提交
1342
  ```js
A
Annie_wang 已提交
1343
  let len = 5;
A
annie_wangli 已提交
1344
  fileio.truncate(path, len).then(function(){
A
Annie_wang 已提交
1345
      console.info("File truncated");
A
annie_wangli 已提交
1346 1347 1348 1349
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1350 1351


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

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

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

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

A
Annie_wang 已提交
1360
**Parameters**
A
Annie_wang 已提交
1361

A
Annie_wang 已提交
1362 1363
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1364
| path     | string                    | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1365
| len      | number                    | Yes  | File length, in bytes, after truncation.|
A
Annie_wang 已提交
1366
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback that returns no value.|
Z
zengyawen 已提交
1367

A
Annie_wang 已提交
1368
**Example**
A
Annie_wang 已提交
1369

A
annie_wangli 已提交
1370
  ```js
A
Annie_wang 已提交
1371
  let len = 5;
A
annie_wangli 已提交
1372 1373 1374 1375
  fileio.truncate(path, len, function(err){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1376 1377


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

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

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

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

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

A
Annie_wang 已提交
1388 1389
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
A
Annie_wang 已提交
1390
| path   | string | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1391
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1392

A
Annie_wang 已提交
1393
**Example**
A
Annie_wang 已提交
1394

A
annie_wangli 已提交
1395
  ```js
A
Annie_wang 已提交
1396
  let len = 5;
A
Annie_wang 已提交
1397
  fileio.truncateSync(path, len);
A
annie_wangli 已提交
1398
  ```
Z
zengyawen 已提交
1399 1400


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

A
annie_wangli 已提交
1403 1404 1405 1406 1407
readText(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): Promise&lt;string&gt;
Z
zengyawen 已提交
1408

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

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

A
Annie_wang 已提交
1413
**Parameters**
A
Annie_wang 已提交
1414

A
Annie_wang 已提交
1415 1416 1417 1418
| 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 已提交
1419

A
Annie_wang 已提交
1420
**Return value**
A
Annie_wang 已提交
1421

Z
zengyawen 已提交
1422 1423
  | Type                   | Description        |
  | --------------------- | ---------- |
A
Annie_wang 已提交
1424
  | Promise&lt;string&gt; | Promise used to return the content read.|
Z
zengyawen 已提交
1425

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

A
annie_wangli 已提交
1428 1429
  ```js
  fileio.readText(path).then(function(str) {
A
Annie_wang 已提交
1430
      console.info("Read text successfully:"+ str);
A
annie_wangli 已提交
1431
  }).catch(function(err){
A
Annie_wang 已提交
1432
      console.info("Failed to read the text. Error:"+ err);
A
annie_wangli 已提交
1433 1434
  });
  ```
Z
zengyawen 已提交
1435 1436


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

A
annie_wangli 已提交
1439 1440 1441 1442 1443
readText(filePath: string, options: {
    position?: number;
    length?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1444

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

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

A
Annie_wang 已提交
1449
**Parameters**
A
Annie_wang 已提交
1450

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

A
Annie_wang 已提交
1457
**Example**
A
Annie_wang 已提交
1458

A
annie_wangli 已提交
1459
  ```js
A
Annie_wang 已提交
1460
  fileio.readText(path, { position: 1, encoding: 'UTF-8' }, function(err, str){
A
annie_wangli 已提交
1461 1462 1463
      // Do something.
  });
  ```
Z
zengyawen 已提交
1464 1465


A
annie_wangli 已提交
1466 1467
## fileio.readTextSync<sup>7+</sup>

A
annie_wangli 已提交
1468 1469 1470 1471 1472
readTextSync(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): string
A
annie_wangli 已提交
1473 1474 1475

Synchronously reads the text of a file. 

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

A
Annie_wang 已提交
1478
**Parameters**
A
Annie_wang 已提交
1479

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

A
Annie_wang 已提交
1485
**Return value**
A
Annie_wang 已提交
1486

Z
zengyawen 已提交
1487 1488
  | Type  | Description                |
  | ------ | -------------------- |
A
Annie_wang 已提交
1489
  | string | Promise used to return the content of the file read.|
A
annie_wangli 已提交
1490

A
Annie_wang 已提交
1491
**Example**
A
Annie_wang 已提交
1492

A
annie_wangli 已提交
1493 1494 1495 1496 1497 1498 1499 1500 1501
  ```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 已提交
1502
Obtains link information. This API uses a promise to return the result.
A
annie_wangli 已提交
1503

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

A
Annie_wang 已提交
1506
**Parameters**
A
Annie_wang 已提交
1507

A
Annie_wang 已提交
1508 1509
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1510
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1511

A
Annie_wang 已提交
1512
**Return value**
A
Annie_wang 已提交
1513

Z
zengyawen 已提交
1514 1515
  | Type                          | Description        |
  | ---------------------------- | ---------- |
A
Annie_wang 已提交
1516
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the link information obtained. For details, see [Stat](#stat).|
A
annie_wangli 已提交
1517

A
Annie_wang 已提交
1518
**Example**
A
Annie_wang 已提交
1519

A
annie_wangli 已提交
1520 1521
  ```js
  fileio.lstat(path).then(function(stat){
A
Annie_wang 已提交
1522
      console.info("Got link info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1523
  }).catch(function(err){
A
Annie_wang 已提交
1524
      console.info("Failed to obtain link info. Error:"+ err);
A
annie_wangli 已提交
1525 1526 1527 1528 1529 1530 1531 1532
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1537
**Parameters**
A
Annie_wang 已提交
1538

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

A
Annie_wang 已提交
1544
**Example**
A
Annie_wang 已提交
1545

A
annie_wangli 已提交
1546 1547 1548
  ```js
  fileio.lstat(path, function (err, stat) {
      // Do something.
A
annie_wangli 已提交
1549
  });
A
annie_wangli 已提交
1550 1551 1552 1553 1554 1555 1556
  ```


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

lstatSync(path:string): Stat

A
Annie_wang 已提交
1557
Synchronously obtains the link information.
A
annie_wangli 已提交
1558

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

A
Annie_wang 已提交
1561
**Parameters**
A
Annie_wang 已提交
1562

A
Annie_wang 已提交
1563 1564
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1565
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1566

A
Annie_wang 已提交
1567
**Return value**
A
Annie_wang 已提交
1568

Z
zengyawen 已提交
1569 1570
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1571
  | [Stat](#stat) | Link information obtained.|
A
annie_wangli 已提交
1572

A
Annie_wang 已提交
1573
**Example**
A
Annie_wang 已提交
1574

A
annie_wangli 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583
  ```js
  let stat = fileio.lstatSync(path);
  ```


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

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

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

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

A
Annie_wang 已提交
1588
**Parameters**
A
Annie_wang 已提交
1589

A
Annie_wang 已提交
1590 1591 1592 1593
| 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 已提交
1594

A
Annie_wang 已提交
1595
**Return value**
A
Annie_wang 已提交
1596

Z
zengyawen 已提交
1597 1598
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1599
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1600

A
Annie_wang 已提交
1601
**Example**
A
Annie_wang 已提交
1602

A
annie_wangli 已提交
1603
  ```js
A
Annie_wang 已提交
1604 1605
  let oldPath = path;
  let newPath = oldPath + '123';
Z
zengyawen 已提交
1606
  fileio.rename(oldPath, newPath).then(function() {
A
Annie_wang 已提交
1607
      console.info("File renamed");
A
annie_wangli 已提交
1608
  }).catch(function(err){
A
Annie_wang 已提交
1609
      console.info("Failed to rename the file. Error:"+ err);
A
annie_wangli 已提交
1610 1611 1612 1613 1614 1615 1616 1617
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1622
**Parameters**
A
Annie_wang 已提交
1623

A
Annie_wang 已提交
1624 1625 1626
| Name  | Type                     | Mandatory| Description                        |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | Yes  | Application sandbox path of the file to rename.|
A
Annie_wang 已提交
1627
| newPath  | String                    | Yes  | Application sandbox path of the file renamed.  |
A
Annie_wang 已提交
1628
| Callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously renamed.  |
A
annie_wangli 已提交
1629

A
Annie_wang 已提交
1630
**Example**
A
Annie_wang 已提交
1631

A
annie_wangli 已提交
1632
  ```js
A
Annie_wang 已提交
1633 1634
  let oldPath = path;
  let newPath = oldPath + '123';
Z
zengyawen 已提交
1635
  fileio.rename(oldPath, newPath, function(err){
A
annie_wangli 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
  });
  ```


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

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

Synchronously renames a file.

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

A
Annie_wang 已提交
1648
**Parameters**
A
Annie_wang 已提交
1649

A
Annie_wang 已提交
1650 1651 1652 1653
| 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 已提交
1654

A
Annie_wang 已提交
1655
**Example**
A
Annie_wang 已提交
1656

A
annie_wangli 已提交
1657
  ```js
A
Annie_wang 已提交
1658 1659
  let oldPath = path;
  let newPath = oldPath + '123';
Z
zengyawen 已提交
1660
  fileio.renameSync(oldPath, newPath);
A
annie_wangli 已提交
1661 1662 1663 1664 1665 1666 1667
  ```


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

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

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

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

A
Annie_wang 已提交
1672
**Parameters**
A
Annie_wang 已提交
1673

Z
zengyawen 已提交
1674 1675
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1676
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1677

A
Annie_wang 已提交
1678
**Return value**
A
Annie_wang 已提交
1679

Z
zengyawen 已提交
1680 1681
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1682
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1683

A
Annie_wang 已提交
1684
**Example**
A
Annie_wang 已提交
1685

A
annie_wangli 已提交
1686
  ```js
A
Annie_wang 已提交
1687
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1688
  fileio.fsync(fd).then(function(){
A
Annie_wang 已提交
1689
      console.info("Data flushed");
A
annie_wangli 已提交
1690
  }).catch(function(err){
A
Annie_wang 已提交
1691
      console.info("Failed to flush data. Error:"+ err);
A
annie_wangli 已提交
1692 1693 1694 1695 1696 1697 1698 1699
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1704
**Parameters**
A
Annie_wang 已提交
1705

Z
zengyawen 已提交
1706 1707
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
A
Annie_wang 已提交
1708
  | fd       | number                    | Yes   | File descriptor of the file to flush.   |
Z
zengyawen 已提交
1709
  | Callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1710

A
Annie_wang 已提交
1711
**Example**
A
Annie_wang 已提交
1712

A
annie_wangli 已提交
1713
  ```js
A
Annie_wang 已提交
1714
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
  fileio.fsync(fd, function(err){
      // Do something.
  });
  ```


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

fsyncSync(fd: number): void

A
Annie_wang 已提交
1725
Flushes data of a file to disk in synchronous mode.
A
annie_wangli 已提交
1726 1727

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

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

Z
zengyawen 已提交
1731 1732
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1733
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1734

A
Annie_wang 已提交
1735
**Example**
A
Annie_wang 已提交
1736

A
annie_wangli 已提交
1737
  ```js
A
Annie_wang 已提交
1738
  let fd = fileio.openSync(path);
A
Annie_wang 已提交
1739
  fileio.fsyncSync(fd);
A
annie_wangli 已提交
1740 1741 1742 1743 1744 1745 1746
  ```


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

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

A
Annie_wang 已提交
1747
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 已提交
1748

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

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

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

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

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

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

A
annie_wangli 已提交
1765
  ```js
A
Annie_wang 已提交
1766
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1767
  fileio.fdatasync(fd).then(function(err) {
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.fdatasync<sup>7+</sup>

fdatasync(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

Z
zengyawen 已提交
1785 1786 1787 1788
  | 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 已提交
1789

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

A
annie_wangli 已提交
1792
  ```js
A
Annie_wang 已提交
1793
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
  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 已提交
1806 1807
**System capability**: SystemCapability.FileManagement.File.FileIO

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

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

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

A
annie_wangli 已提交
1816
  ```js
A
Annie_wang 已提交
1817
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1818 1819 1820 1821 1822 1823 1824 1825
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

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

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

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

A
Annie_wang 已提交
1832 1833
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1834 1835
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1836

A
Annie_wang 已提交
1837
**Return value**
A
Annie_wang 已提交
1838

Z
zengyawen 已提交
1839 1840
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1841
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1842

A
Annie_wang 已提交
1843
**Example**
A
Annie_wang 已提交
1844

A
annie_wangli 已提交
1845
  ```js
A
Annie_wang 已提交
1846 1847
  let target = path;
  let srcPath = target + 'aaa';
A
annie_wangli 已提交
1848
  fileio.symlink(target, srcPath).then(function() {
A
Annie_wang 已提交
1849
      console.info("Symbolic link created");
A
annie_wangli 已提交
1850
  }).catch(function(err){
A
Annie_wang 已提交
1851
      console.info("Failed to create the symbolic link. Error:"+ err);
A
annie_wangli 已提交
1852 1853 1854 1855 1856 1857 1858 1859
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1864
**Parameters**
A
Annie_wang 已提交
1865

A
Annie_wang 已提交
1866 1867
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1868 1869
| target   | string                    | Yes  | Application sandbox path of the target file.        |
| srcPath  | string                    | Yes  | Application sandbox path of the symbolic link file.    |
A
Annie_wang 已提交
1870
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the symbolic link is created asynchronously.|
A
annie_wangli 已提交
1871

A
Annie_wang 已提交
1872
**Example**
A
Annie_wang 已提交
1873

A
annie_wangli 已提交
1874
  ```js
A
Annie_wang 已提交
1875 1876
  let target = path;
  let srcPath = target + 'aaa';
A
annie_wangli 已提交
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
  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 已提交
1889 1890
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1891
**Parameters**
A
Annie_wang 已提交
1892

A
Annie_wang 已提交
1893 1894
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1895 1896
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1897

A
Annie_wang 已提交
1898
**Example**
A
Annie_wang 已提交
1899

A
annie_wangli 已提交
1900
  ```js
A
Annie_wang 已提交
1901 1902
  let target = path;
  let srcPath = target + 'aaa';
A
annie_wangli 已提交
1903 1904 1905 1906 1907 1908 1909 1910
  fileio.symlinkSync(target, srcPath);
  ```


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

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

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

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

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

A
Annie_wang 已提交
1917 1918
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1919
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
1920 1921
| uid    | number | Yes  | New user ID (UID).       |
| gid    | number | Yes  | New group ID (GID).      |
A
annie_wangli 已提交
1922

A
Annie_wang 已提交
1923
**Return value**
A
Annie_wang 已提交
1924

Z
zengyawen 已提交
1925 1926
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1927
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1928

A
Annie_wang 已提交
1929
**Example**
A
Annie_wang 已提交
1930

A
annie_wangli 已提交
1931 1932
  ```js
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
1933
  fileio.chown(path, stat.uid, stat.gid).then(function(){
A
Annie_wang 已提交
1934
      console.info("File owner changed");
A
annie_wangli 已提交
1935
  }).catch(function(err){
A
Annie_wang 已提交
1936
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
1937 1938 1939 1940 1941 1942 1943 1944
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1949
**Parameters**
A
Annie_wang 已提交
1950

A
Annie_wang 已提交
1951 1952
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
1953
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
1954 1955 1956
| 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 已提交
1957

A
Annie_wang 已提交
1958
**Example**
A
Annie_wang 已提交
1959

A
annie_wangli 已提交
1960
  ```js
A
Annie_wang 已提交
1961
  let stat = fileio.statSync(path)
A
annie_wangli 已提交
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
  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 已提交
1974 1975
**System capability**: SystemCapability.FileManagement.File.FileIO

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

A
Annie_wang 已提交
1978 1979
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1980
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
1981 1982
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
1983

A
Annie_wang 已提交
1984
**Example**
A
Annie_wang 已提交
1985

A
annie_wangli 已提交
1986
  ```js
A
Annie_wang 已提交
1987
  let stat = fileio.statSync(path)
A
annie_wangli 已提交
1988 1989 1990 1991 1992 1993 1994 1995
  fileio.chownSync(path, stat.uid, stat.gid);
  ```


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

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

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

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

A
Annie_wang 已提交
2000
**Parameters**
A
Annie_wang 已提交
2001

Z
zengyawen 已提交
2002 2003 2004
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
2005

A
Annie_wang 已提交
2006
**Return value**
A
Annie_wang 已提交
2007

A
Annie_wang 已提交
2008
  | Type                  | Description        |
Z
zengyawen 已提交
2009
  | --------------------- | ---------- |
A
Annie_wang 已提交
2010
  | Promise&lt;string&gt; | Promise used to return the unique directory generated.|
A
annie_wangli 已提交
2011

A
Annie_wang 已提交
2012
**Example**
A
Annie_wang 已提交
2013

A
annie_wangli 已提交
2014 2015
  ```js
  fileio.mkdtemp(path + "XXXX").then(function(path){
A
Annie_wang 已提交
2016
      console.info("Temporary directory created:"+ path);
A
annie_wangli 已提交
2017
  }).catch(function(err){
A
Annie_wang 已提交
2018
      console.info("Failed to create the temporary directory. Error:"+ err);
A
annie_wangli 已提交
2019 2020 2021 2022 2023 2024 2025 2026
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2031
**Parameters**
A
Annie_wang 已提交
2032

Z
zengyawen 已提交
2033 2034 2035 2036
  | 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 已提交
2037

A
Annie_wang 已提交
2038
**Example**
A
Annie_wang 已提交
2039

A
annie_wangli 已提交
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
  ```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 已提交
2053 2054
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2055
**Parameters**
A
Annie_wang 已提交
2056

Z
zengyawen 已提交
2057 2058 2059
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
2060

A
Annie_wang 已提交
2061
**Return value**
A
Annie_wang 已提交
2062

A
Annie_wang 已提交
2063
  | Type   | Description        |
Z
zengyawen 已提交
2064
  | ------ | ---------- |
A
annie_wangli 已提交
2065 2066
  | string | Unique path generated.|

A
Annie_wang 已提交
2067
**Example**
A
Annie_wang 已提交
2068

A
annie_wangli 已提交
2069 2070 2071 2072 2073 2074 2075 2076 2077
  ```js
  let res = fileio.mkdtempSync(path + "XXXX");
  ```


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

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

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

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

A
Annie_wang 已提交
2082
**Parameters**
A
Annie_wang 已提交
2083

Z
zengyawen 已提交
2084 2085 2086 2087
  | 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 已提交
2088

A
Annie_wang 已提交
2089
**Return value**
A
Annie_wang 已提交
2090

A
Annie_wang 已提交
2091
  | Type                | Description                          |
Z
zengyawen 已提交
2092
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2093
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2094

A
Annie_wang 已提交
2095
**Example**
A
Annie_wang 已提交
2096

A
annie_wangli 已提交
2097
  ```js
A
Annie_wang 已提交
2098 2099
  let fd = fileio.openSync(path);
  let mode = 0o700;
A
annie_wangli 已提交
2100
  fileio.fchmod(fd, mode).then(function() {
A
Annie_wang 已提交
2101
      console.info("File permissions changed");
A
annie_wangli 已提交
2102
  }).catch(function(err){
A
Annie_wang 已提交
2103
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
2104 2105 2106 2107 2108 2109 2110 2111
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2116
**Parameters**
A
Annie_wang 已提交
2117

Z
zengyawen 已提交
2118 2119 2120 2121 2122
  | 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 已提交
2123

A
Annie_wang 已提交
2124
**Example**
A
Annie_wang 已提交
2125

A
annie_wangli 已提交
2126
  ```js
A
Annie_wang 已提交
2127 2128
  let fd = fileio.openSync(path);
  let mode = 0o700;
A
annie_wangli 已提交
2129 2130 2131 2132 2133 2134 2135 2136
  fileio.fchmod(fd, mode, function (err) {
      // Do something.
  });
  ```


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

A
annie_wangli 已提交
2137
fchmodSync(fd: number, mode: number): void
A
annie_wangli 已提交
2138 2139 2140

Synchronously changes the file permissions based on the file descriptor.

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

A
Annie_wang 已提交
2143
**Parameters**
A
Annie_wang 已提交
2144

Z
zengyawen 已提交
2145 2146 2147 2148
  | 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 已提交
2149

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

A
annie_wangli 已提交
2152
  ```js
A
Annie_wang 已提交
2153 2154
  let fd = fileio.openSync(path);
  let mode = 0o700;
A
annie_wangli 已提交
2155 2156 2157 2158 2159 2160 2161 2162
   fileio.fchmodSync(fd, mode);
  ```


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

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

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

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

A
Annie_wang 已提交
2167
**Parameters**
A
Annie_wang 已提交
2168

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

A
Annie_wang 已提交
2174
**Return value**
A
Annie_wang 已提交
2175

Z
zengyawen 已提交
2176 2177
  | Type                               | Description       |
  | --------------------------------- | --------- |
A
Annie_wang 已提交
2178
  | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
A
annie_wangli 已提交
2179

A
Annie_wang 已提交
2180
**Example**
A
Annie_wang 已提交
2181

A
annie_wangli 已提交
2182 2183
  ```js
  fileio.createStream(path, "r+").then(function(stream){
A
Annie_wang 已提交
2184
      console.info("Stream opened");
A
annie_wangli 已提交
2185
  }).catch(function(err){
A
Annie_wang 已提交
2186
      console.info("Failed to create the stream. Error:"+ err);
A
annie_wangli 已提交
2187 2188 2189 2190 2191 2192 2193 2194
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2199
**Parameters**
A
Annie_wang 已提交
2200

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

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

A
annie_wangli 已提交
2209
  ```js
A
Annie_wang 已提交
2210
  fileio.createStream(path, "r+", function(err, stream){
A
annie_wangli 已提交
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
      // 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 已提交
2222 2223
**System capability**: SystemCapability.FileManagement.File.FileIO

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

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

A
Annie_wang 已提交
2231
**Return value**
A
Annie_wang 已提交
2232

A
Annie_wang 已提交
2233
  | Type               | Description       |
Z
zengyawen 已提交
2234
  | ------------------ | --------- |
A
Annie_wang 已提交
2235
  | [Stream](#stream) | Stream opened.|
A
annie_wangli 已提交
2236

A
Annie_wang 已提交
2237
**Example**
A
Annie_wang 已提交
2238

A
annie_wangli 已提交
2239 2240 2241 2242 2243 2244 2245 2246 2247
  ```js
  let ss = fileio.createStreamSync(path, "r+");
  ```


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

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

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

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

A
Annie_wang 已提交
2252
**Parameters**
A
Annie_wang 已提交
2253

Z
zengyawen 已提交
2254 2255 2256 2257
  | 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 已提交
2258

A
Annie_wang 已提交
2259
**Return value**
A
Annie_wang 已提交
2260

A
Annie_wang 已提交
2261
  | Type                              | Description       |
Z
zengyawen 已提交
2262
  | --------------------------------- | --------- |
A
Annie_wang 已提交
2263
  | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
A
annie_wangli 已提交
2264

A
Annie_wang 已提交
2265
**Example**
A
Annie_wang 已提交
2266

A
annie_wangli 已提交
2267
  ```js
A
Annie_wang 已提交
2268 2269
  let fd = fileio.openSync(path);
  fileio.fdopenStream(fd, "r+").then(function(stream){
A
Annie_wang 已提交
2270
      console.info("Stream opened");
A
annie_wangli 已提交
2271
  }).catch(function(err){
A
Annie_wang 已提交
2272
      console.info("Failed to open the stream. Error:"+ err);
A
annie_wangli 已提交
2273 2274 2275 2276 2277 2278 2279 2280
  });
  ```


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

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

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

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

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

Z
zengyawen 已提交
2287 2288 2289 2290
  | 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_wang 已提交
2291
  | callback | AsyncCallback&nbsp;&lt;[Stream](#stream)&gt; | Yes   | Callback invoked when the stream is open asynchronously.                           |
A
annie_wangli 已提交
2292

A
Annie_wang 已提交
2293
**Example**
A
Annie_wang 已提交
2294

A
annie_wangli 已提交
2295
  ```js
A
Annie_wang 已提交
2296 2297
  let fd = fileio.openSync(path);
  fileio.fdopenStream(fd, "r+", function (err, stream) {
A
annie_wangli 已提交
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
      // 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 已提交
2309 2310
**System capability**: SystemCapability.FileManagement.File.FileIO

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

Z
zengyawen 已提交
2313 2314 2315 2316
  | 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 已提交
2317

A
Annie_wang 已提交
2318
**Return value**
A
Annie_wang 已提交
2319

A
Annie_wang 已提交
2320
  | Type               | Description       |
Z
zengyawen 已提交
2321
  | ------------------ | --------- |
A
Annie_wang 已提交
2322
  | [Stream](#stream) | Stream opened.|
A
annie_wangli 已提交
2323

A
Annie_wang 已提交
2324
**Example**
A
Annie_wang 已提交
2325

A
annie_wangli 已提交
2326
  ```js
A
Annie_wang 已提交
2327
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
2328 2329 2330 2331 2332 2333 2334 2335
  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 已提交
2336
Changes the file owner based on the file descriptor. This API uses a promise to return the result.
A
annie_wangli 已提交
2337

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

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

Z
zengyawen 已提交
2342 2343 2344 2345 2346
  | 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 已提交
2347

A
Annie_wang 已提交
2348
**Return value**
A
Annie_wang 已提交
2349

Z
zengyawen 已提交
2350 2351
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2352
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2353

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

A
annie_wangli 已提交
2356
  ```js
A
Annie_wang 已提交
2357
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
2358 2359
  let stat = fileio.statSync(path);
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2360
      console.info("File owner changed");
A
annie_wangli 已提交
2361
  }).catch(function(err){
A
Annie_wang 已提交
2362
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2363 2364 2365 2366 2367 2368 2369 2370
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2375
**Parameters**
A
Annie_wang 已提交
2376

Z
zengyawen 已提交
2377 2378 2379 2380 2381 2382
  | 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 已提交
2383

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

A
annie_wangli 已提交
2386
  ```js
A
Annie_wang 已提交
2387
  let fd = fileio.openSync(path);
A
Annie_wang 已提交
2388
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
  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 已提交
2401 2402
**System capability**: SystemCapability.FileManagement.File.FileIO

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

Z
zengyawen 已提交
2405 2406 2407 2408 2409
  | 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 已提交
2410

A
Annie_wang 已提交
2411
**Example**
A
Annie_wang 已提交
2412

A
annie_wangli 已提交
2413
  ```js
A
Annie_wang 已提交
2414
  let fd = fileio.openSync(path);
A
Annie_wang 已提交
2415
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
2416 2417 2418 2419 2420 2421 2422 2423
  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 已提交
2424
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 已提交
2425

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

A
Annie_wang 已提交
2428
**Parameters**
A
Annie_wang 已提交
2429

A
Annie_wang 已提交
2430 2431
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2432
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2433 2434
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2435

A
Annie_wang 已提交
2436
**Return value**
A
Annie_wang 已提交
2437

Z
zengyawen 已提交
2438 2439
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2440
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2441

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

A
annie_wangli 已提交
2444 2445 2446
  ```js
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2447
      console.info("File owner changed");
A
annie_wangli 已提交
2448
  }).catch(function(err){
A
Annie_wang 已提交
2449
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2450 2451 2452 2453 2454 2455 2456 2457
  });
  ```


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

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

A
Annie_wang 已提交
2458
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 已提交
2459

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

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

A
Annie_wang 已提交
2464 2465
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
2466
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
2467 2468 2469
| 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 已提交
2470

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

A
annie_wangli 已提交
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
  ```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 已提交
2487 2488
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2489
**Parameters**
A
Annie_wang 已提交
2490

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

A
Annie_wang 已提交
2497
**Example**
A
Annie_wang 已提交
2498

A
annie_wangli 已提交
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
  ```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 已提交
2509
Listens for file or directory changes. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2510

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

A
Annie_wang 已提交
2513
**Parameters**
A
Annie_wang 已提交
2514

A
Annie_wang 已提交
2515 2516
| Name  | Type                             | Mandatory| Description                                                        |
| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2517
| filename | string                            | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2518 2519
| 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 已提交
2520

A
Annie_wang 已提交
2521
**Return value**
A
Annie_wang 已提交
2522

A
Annie_wang 已提交
2523
  | Type                 | Description        |
Z
zengyawen 已提交
2524
  | -------------------- | ---------- |
A
Annie_wang 已提交
2525
  | [Watcher](#watcher7) | Promise used to return the **Watcher** instance.|
A
annie_wangli 已提交
2526

A
Annie_wang 已提交
2527
**Example**
A
Annie_wang 已提交
2528

A
annie_wangli 已提交
2529
  ```js
A
Annie_wang 已提交
2530 2531 2532
  let filename = path +"/test.txt";
  fileio.createWatcher(filename, 1, function(number){
     console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2533
  });
A
Annie_wang 已提交
2534
  
A
annie_wangli 已提交
2535 2536 2537 2538 2539 2540 2541
  ```


## Readout

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

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

Z
zengyawen 已提交
2544 2545 2546 2547 2548
| 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 已提交
2549 2550 2551 2552 2553 2554


## 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 已提交
2555
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
2556 2557 2558

### Attributes

Z
zengyawen 已提交
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
| 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 已提交
2573 2574 2575 2576 2577 2578


### isBlockDevice

isBlockDevice(): boolean

A
Annie_wang 已提交
2579
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 已提交
2580

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

A
Annie_wang 已提交
2583
**Return value**
A
Annie_wang 已提交
2584

Z
zengyawen 已提交
2585 2586
  | Type     | Description              |
  | ------- | ---------------- |
A
Annie_wang 已提交
2587
  | boolean | Whether the file is a block special file.|
A
annie_wangli 已提交
2588

A
Annie_wang 已提交
2589
**Example**
A
Annie_wang 已提交
2590

A
annie_wangli 已提交
2591 2592 2593 2594 2595 2596 2597 2598 2599
  ```js
  let isBLockDevice = fileio.statSync(path).isBlockDevice();
  ```


### isCharacterDevice

isCharacterDevice(): boolean

A
Annie_wang 已提交
2600
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 已提交
2601

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

A
Annie_wang 已提交
2604
**Return value**
A
Annie_wang 已提交
2605

Z
zengyawen 已提交
2606 2607
  | Type     | Description               |
  | ------- | ----------------- |
A
Annie_wang 已提交
2608
  | boolean | Whether the file is a character special file.|
A
annie_wangli 已提交
2609

A
Annie_wang 已提交
2610
**Example**
A
Annie_wang 已提交
2611

A
annie_wangli 已提交
2612 2613 2614 2615 2616 2617 2618 2619 2620
  ```js
  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
  ```


### isDirectory

isDirectory(): boolean

A
Annie_wang 已提交
2621
Checks whether this file is a directory.
A
annie_wangli 已提交
2622

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

A
Annie_wang 已提交
2625
**Return value**
A
Annie_wang 已提交
2626

Z
zengyawen 已提交
2627 2628
  | Type     | Description           |
  | ------- | ------------- |
A
Annie_wang 已提交
2629
  | boolean | Whether the file is a directory.|
A
annie_wangli 已提交
2630

A
Annie_wang 已提交
2631
**Example**
A
Annie_wang 已提交
2632

A
annie_wangli 已提交
2633 2634 2635 2636 2637 2638 2639 2640 2641
  ```js
  let isDirectory = fileio.statSync(path).isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

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

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

A
Annie_wang 已提交
2646
**Return value**
A
Annie_wang 已提交
2647

Z
zengyawen 已提交
2648 2649
  | Type     | Description                   |
  | ------- | --------------------- |
A
Annie_wang 已提交
2650
  | boolean | Whether the file is an FIFO.|
A
annie_wangli 已提交
2651

A
Annie_wang 已提交
2652
**Example**
A
Annie_wang 已提交
2653

A
annie_wangli 已提交
2654 2655 2656 2657 2658 2659 2660 2661 2662
  ```js
  let isFIFO = fileio.statSync(path).isFIFO(); 
  ```


### isFile

isFile(): boolean

A
Annie_wang 已提交
2663
Checks whether this file is a regular file.
A
annie_wangli 已提交
2664

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

A
Annie_wang 已提交
2667
**Return value**
A
Annie_wang 已提交
2668

Z
zengyawen 已提交
2669 2670
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2671
  | boolean | Whether the file is a regular file.|
A
annie_wangli 已提交
2672

A
Annie_wang 已提交
2673
**Example**
A
Annie_wang 已提交
2674

A
annie_wangli 已提交
2675
  ```js
A
Annie_wang 已提交
2676
  let isFile = fileio.statSync(path).isFile();
A
annie_wangli 已提交
2677 2678 2679 2680 2681 2682 2683
  ```


### isSocket

isSocket(): boolean

A
Annie_wang 已提交
2684
Checks whether this file is a socket.
A
annie_wangli 已提交
2685

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

A
Annie_wang 已提交
2688
**Return value**
A
Annie_wang 已提交
2689

Z
zengyawen 已提交
2690 2691
  | Type     | Description            |
  | ------- | -------------- |
A
Annie_wang 已提交
2692
  | boolean | Whether the file is a socket.|
A
annie_wangli 已提交
2693

A
Annie_wang 已提交
2694
**Example**
A
Annie_wang 已提交
2695

A
annie_wangli 已提交
2696 2697 2698 2699 2700 2701 2702 2703 2704
  ```js
  let isSocket = fileio.statSync(path).isSocket(); 
  ```


### isSymbolicLink

isSymbolicLink(): boolean

A
Annie_wang 已提交
2705
Checks whether this file is a symbolic link.
A
annie_wangli 已提交
2706

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

A
Annie_wang 已提交
2709
**Return value**
A
Annie_wang 已提交
2710

Z
zengyawen 已提交
2711 2712
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2713
  | boolean | Whether the file is a symbolic link.|
A
annie_wangli 已提交
2714

A
Annie_wang 已提交
2715
**Example**
A
Annie_wang 已提交
2716

A
annie_wangli 已提交
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
  ```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 已提交
2729
stop(): Promise&lt;void&gt;
A
annie_wangli 已提交
2730

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

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

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

A
annie_wangli 已提交
2737
  ```js
A
Annie_wang 已提交
2738
  let filename = path +"/test.txt";
A
Annie_wang 已提交
2739
  let watcher = fileio.createWatcher(filename, 1, function(number){
A
Annie_wang 已提交
2740 2741 2742
      console.info("Monitoring times: "+number);
  });
  watcher.stop().then(function(){
A
Annie_wang 已提交
2743
       console.info("Watcher stopped");
A
Annie_wang 已提交
2744
  });
A
annie_wangli 已提交
2745 2746 2747 2748 2749
  ```


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

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

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

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

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

Z
zengyawen 已提交
2758 2759 2760
  | Name     | Type                       | Mandatory  | Description                    |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when **watcher** is stopped asynchronously.|
A
annie_wangli 已提交
2761

A
Annie_wang 已提交
2762
**Example**
A
Annie_wang 已提交
2763

A
annie_wangli 已提交
2764
  ```js
A
Annie_wang 已提交
2765
  let filename = path +"/test.txt";
A
Annie_wang 已提交
2766
  let watcher = fileio.createWatcher(filename, 1, function(number){
A
Annie_wang 已提交
2767
      console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2768
  });
A
Annie_wang 已提交
2769
  watcher.stop(function(){
A
Annie_wang 已提交
2770
      console.info("Watcher stopped");
A
Annie_wang 已提交
2771
  })
A
annie_wangli 已提交
2772
  ```
Z
zengyawen 已提交
2773

A
Annie_wang 已提交
2774
## Stream
Z
zengyawen 已提交
2775

A
Annie_wang 已提交
2776
Provides file stream management. Before calling a method of the **Stream** class, use the **createStream()** method synchronously or asynchronously to create a **Stream** instance.
Z
zengyawen 已提交
2777 2778 2779 2780 2781 2782


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

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

A
Annie_wang 已提交
2783
Closes the stream. This API uses a promise to return the result.
Z
zengyawen 已提交
2784 2785 2786

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2787
**Return value**
A
Annie_wang 已提交
2788

Z
zengyawen 已提交
2789 2790 2791 2792
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream close result.|

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

Z
zengyawen 已提交
2795
  ```js
A
Annie_wang 已提交
2796
  let ss= fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2797
  ss.close().then(function(){
A
Annie_wang 已提交
2798
      console.info("File stream closed");
Z
zengyawen 已提交
2799
  }).catch(function(err){
A
Annie_wang 已提交
2800
      console.info("Failed to close the file stream. Error:"+ err);
Z
zengyawen 已提交
2801 2802 2803 2804 2805 2806 2807 2808
  });
  ```


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

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

A
Annie_wang 已提交
2809
Closes the stream. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
2810 2811 2812

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2813
**Parameters**
A
Annie_wang 已提交
2814

Z
zengyawen 已提交
2815 2816 2817 2818
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|

A
Annie_wang 已提交
2819
**Example**
A
Annie_wang 已提交
2820

Z
zengyawen 已提交
2821
  ```js
A
Annie_wang 已提交
2822
  let ss= fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2823
  ss.close(function (err) {
A
Annie_wang 已提交
2824
      // Do something
Z
zengyawen 已提交
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836
  });
  ```


### closeSync

closeSync(): void

Synchronously closes the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2837
**Example**
A
Annie_wang 已提交
2838

Z
zengyawen 已提交
2839
  ```js
A
Annie_wang 已提交
2840
  let ss= fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2841 2842 2843 2844 2845 2846 2847 2848
  ss.closeSync();
  ```


### flush<sup>7+</sup>

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

A
Annie_wang 已提交
2849
Flushes the stream. This API uses a promise to return the result.
Z
zengyawen 已提交
2850 2851 2852

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2853
**Return value**
A
Annie_wang 已提交
2854

Z
zengyawen 已提交
2855 2856 2857 2858
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream flushing result.|

A
Annie_wang 已提交
2859
**Example**
A
Annie_wang 已提交
2860

Z
zengyawen 已提交
2861
  ```js
A
Annie_wang 已提交
2862
  let ss= fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2863
  ss.flush().then(function (){
A
Annie_wang 已提交
2864
      console.info("Stream flushed");
Z
zengyawen 已提交
2865
  }).catch(function(err){
A
Annie_wang 已提交
2866
      console.info("Failed to flush the stream. Error:"+ err);
Z
zengyawen 已提交
2867 2868 2869 2870 2871 2872 2873 2874
  });
  ```


### flush<sup>7+</sup>

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

A
Annie_wang 已提交
2875
Flushes the stream. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
2876 2877 2878

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2879
**Parameters**
A
Annie_wang 已提交
2880

Z
zengyawen 已提交
2881 2882 2883 2884
  | Name     | Type                       | Mandatory  | Description            |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is asynchronously flushed.|

A
Annie_wang 已提交
2885
**Example**
A
Annie_wang 已提交
2886

Z
zengyawen 已提交
2887
  ```js
A
Annie_wang 已提交
2888
  let ss= fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2889
  ss.flush(function (err) {
A
Annie_wang 已提交
2890
      // Do something
Z
zengyawen 已提交
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902
  });
  ```


### flushSync<sup>7+</sup>

flushSync(): void

Synchronously flushes the stream.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2903
**Example**
A
Annie_wang 已提交
2904

Z
zengyawen 已提交
2905
  ```js
A
Annie_wang 已提交
2906
  let ss= fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
  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 已提交
2920
Writes data into the stream. This API uses a promise to return the result.
Z
zengyawen 已提交
2921 2922 2923

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2924
**Parameters**
A
Annie_wang 已提交
2925

Z
zengyawen 已提交
2926 2927 2928
  | 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 已提交
2929
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
Z
zengyawen 已提交
2930

A
Annie_wang 已提交
2931
**Return value**
A
Annie_wang 已提交
2932

Z
zengyawen 已提交
2933 2934
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
2935
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
Z
zengyawen 已提交
2936

A
Annie_wang 已提交
2937
**Example**
A
Annie_wang 已提交
2938

Z
zengyawen 已提交
2939
  ```js
A
Annie_wang 已提交
2940
  let ss= fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2941
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
A
Annie_wang 已提交
2942
      console.info("Data written to the stream. Size is:"+ number);
Z
zengyawen 已提交
2943
  }).catch(function(err){
A
Annie_wang 已提交
2944
      console.info("Failed to write data to the stream. Error:"+ err);
Z
zengyawen 已提交
2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957
  });
  ```


### 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 已提交
2958
Writes data into the stream. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
2959 2960 2961

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2962
**Parameters**
A
Annie_wang 已提交
2963

A
Annie_wang 已提交
2964 2965 2966
  | 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 已提交
2967
  | options  | Object                          | No  | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
A
Annie_wang 已提交
2968
  | callback | AsyncCallback&lt;number&gt;     | Yes  | Callback invoked when the data is written asynchronously.                              |
Z
zengyawen 已提交
2969

A
Annie_wang 已提交
2970
**Example**
A
Annie_wang 已提交
2971

Z
zengyawen 已提交
2972
  ```js
A
Annie_wang 已提交
2973
  let ss= fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2974
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
A
Annie_wang 已提交
2975
      if (bytesWritten) {
A
Annie_wang 已提交
2976
         // Do something
A
Annie_wang 已提交
2977
         console.info("Data written to the stream. Size is:"+ bytesWritten);
Z
zengyawen 已提交
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
      }
  });
  ```


### 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 已提交
2996
**Parameters**
A
Annie_wang 已提交
2997

Z
zengyawen 已提交
2998 2999 3000
  | 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 已提交
3001
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
Z
zengyawen 已提交
3002

A
Annie_wang 已提交
3003
**Return value**
A
Annie_wang 已提交
3004

Z
zengyawen 已提交
3005 3006 3007 3008
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data written in the file.|

A
Annie_wang 已提交
3009
**Example**
A
Annie_wang 已提交
3010

Z
zengyawen 已提交
3011
  ```js
A
Annie_wang 已提交
3012
  let ss= fileio.createStreamSync(path,"r+");
Z
zengyawen 已提交
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024
  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 已提交
3025
Reads data from the stream. This API uses a promise to return the result.
Z
zengyawen 已提交
3026 3027 3028

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3029
**Parameters**
A
Annie_wang 已提交
3030

Z
zengyawen 已提交
3031 3032 3033
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3034
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
Z
zengyawen 已提交
3035

A
Annie_wang 已提交
3036
**Return value**
A
Annie_wang 已提交
3037

Z
zengyawen 已提交
3038 3039
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
3040
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
Z
zengyawen 已提交
3041

A
Annie_wang 已提交
3042
**Example**
A
Annie_wang 已提交
3043

Z
zengyawen 已提交
3044
  ```js
A
Annie_wang 已提交
3045
  let ss = fileio.createStreamSync(path, "r+");
A
Annie_wang 已提交
3046
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut){
A
Annie_wang 已提交
3047
      console.info("Read data successfully");
A
Annie_wang 已提交
3048
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
3049
  }).catch(function(err){
A
Annie_wang 已提交
3050
      console.info("Failed to read data. Error:"+ err);
Z
zengyawen 已提交
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
  });
  ```


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

read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void

A
Annie_wang 已提交
3063
Reads data from the stream. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
3064 3065 3066

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3067
**Parameters**
A
Annie_wang 已提交
3068

Z
zengyawen 已提交
3069 3070 3071
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3072
  | options  | Object                                   | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
Z
zengyawen 已提交
3073 3074
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when data is read asynchronously from the stream.                        |

A
Annie_wang 已提交
3075
**Example**
A
Annie_wang 已提交
3076

Z
zengyawen 已提交
3077
  ```js
A
Annie_wang 已提交
3078
  let ss = fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
3079
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
A
Annie_wang 已提交
3080
      if (readOut) {
A
Annie_wang 已提交
3081
          console.info("Read data successfully");
A
Annie_wang 已提交
3082
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
      }
  });
  ```


### 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 已提交
3100 3101
**Parameters**

Z
zengyawen 已提交
3102 3103 3104
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3105 3106 3107
  | 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**
Z
zengyawen 已提交
3108 3109 3110 3111 3112

  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data read.|

A
Annie_wang 已提交
3113
**Example**
A
Annie_wang 已提交
3114

Z
zengyawen 已提交
3115
  ```js
A
Annie_wang 已提交
3116
  let ss = fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
3117 3118 3119 3120 3121 3122
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
  ```


## Dir

A
Annie_wang 已提交
3123
Manages directories. Before calling a method of the **Dir** class, use the **opendir()** method synchronously or asynchronously to create a **Dir** instance.
Z
zengyawen 已提交
3124 3125 3126 3127 3128 3129


### read

read(): Promise&lt;Dirent&gt;

A
Annie_wang 已提交
3130
Reads the next directory entry. This API uses a promise to return the result.
Z
zengyawen 已提交
3131 3132 3133

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3134
**Return value**
A
Annie_wang 已提交
3135

Z
zengyawen 已提交
3136 3137
  | Type                              | Description           |
  | -------------------------------- | ------------- |
A
Annie_wang 已提交
3138
  | Promise&lt;[Dirent](#dirent)&gt; | Promise used to return the directory entry read.|
Z
zengyawen 已提交
3139

A
Annie_wang 已提交
3140
**Example**
A
Annie_wang 已提交
3141

Z
zengyawen 已提交
3142 3143
  ```js
  dir.read().then(function (dirent){
A
Annie_wang 已提交
3144
      console.log("Read the next directory entry:"+JSON.stringify(dirent));
Z
zengyawen 已提交
3145
  }).catch(function(err){
A
Annie_wang 已提交
3146
      console.info("Failed to read the next directory entry. Error:"+ err);
Z
zengyawen 已提交
3147 3148 3149 3150 3151 3152 3153 3154
  });
  ```


### read

read(callback: AsyncCallback&lt;Dirent&gt;): void

A
Annie_wang 已提交
3155
Reads the next directory entry. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
3156 3157 3158

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3159
**Parameters**
A
Annie_wang 已提交
3160

Z
zengyawen 已提交
3161 3162 3163 3164
  | Name     | Type                                    | Mandatory  | Description              |
  | -------- | -------------------------------------- | ---- | ---------------- |
  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | Yes   | Callback invoked when the next directory entry is asynchronously read.|

A
Annie_wang 已提交
3165
**Example**
A
Annie_wang 已提交
3166

Z
zengyawen 已提交
3167 3168
  ```js
  dir.read(function (err, dirent) {
A
Annie_wang 已提交
3169
      if (dirent) {
A
Annie_wang 已提交
3170
          // Do something
A
Annie_wang 已提交
3171
          console.log("Read the next directory entry:"+JSON.stringify(dirent));
Z
zengyawen 已提交
3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
      }
  });
  ```


### readSync

readSync(): Dirent

Synchronously reads the next directory entry.

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3185
**Return value**
A
Annie_wang 已提交
3186

Z
zengyawen 已提交
3187 3188 3189 3190
  | Type               | Description      |
  | ----------------- | -------- |
  | [Dirent](#dirent) | Directory entry read.|

A
Annie_wang 已提交
3191
**Example**
A
Annie_wang 已提交
3192

Z
zengyawen 已提交
3193 3194 3195 3196 3197
  ```js
  let dirent = dir.readSync();
  ```


A
Annie_wang 已提交
3198
### close<sup>7+</sup>
A
Annie_wang 已提交
3199 3200 3201

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

A
Annie_wang 已提交
3202
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.
A
Annie_wang 已提交
3203 3204 3205 3206

**System capability**: SystemCapability.FileManagement.File.FileIO

**Example**
A
Annie_wang 已提交
3207

A
Annie_wang 已提交
3208 3209 3210 3211 3212 3213 3214
  ```js
  dir.close().then(function(err){
      console.info("close dir successfully");
  });
  ```


A
Annie_wang 已提交
3215
  ### close<sup>7+</sup>
A
Annie_wang 已提交
3216 3217 3218 3219 3220 3221 3222 3223

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 已提交
3224

A
Annie_wang 已提交
3225 3226 3227 3228 3229 3230 3231
  ```js
  dir.close(function(err){
      console.info("close dir successfully");
  });
  ```


Z
zengyawen 已提交
3232 3233 3234 3235 3236 3237 3238 3239
### 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 已提交
3240
**Example**
A
Annie_wang 已提交
3241

Z
zengyawen 已提交
3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263
  ```js
  dir.closeSync();
  ```


## Dirent

Provides information about files and directories. Before calling a method of the **Dirent** class, use the [dir.read()](#read) method synchronously or asynchronously to create a **Dirent** instance.

**System capability**: SystemCapability.FileManagement.File.FileIO

### Attributes

| Name  | Type  | Readable  | Writable  | Description     |
| ---- | ------ | ---- | ---- | ------- |
| name | string | Yes   | No   | Directory entry name.|


### isBlockDevice

isBlockDevice(): boolean

A
Annie_wang 已提交
3264
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.
Z
zengyawen 已提交
3265 3266 3267

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3268
**Return value**
A
Annie_wang 已提交
3269

Z
zengyawen 已提交
3270 3271 3272 3273
  | Type     | Description              |
  | ------- | ---------------- |
  | boolean | Whether the directory entry is a block special file.|

A
Annie_wang 已提交
3274
**Example**
A
Annie_wang 已提交
3275

Z
zengyawen 已提交
3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289
  ```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 已提交
3290
**Return value**
A
Annie_wang 已提交
3291

Z
zengyawen 已提交
3292 3293 3294 3295
  | Type     | Description               |
  | ------- | ----------------- |
  | boolean | Whether the directory entry is a character special file.|

A
Annie_wang 已提交
3296
**Example**
A
Annie_wang 已提交
3297

Z
zengyawen 已提交
3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311
  ```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 已提交
3312
**Return value**
A
Annie_wang 已提交
3313

Z
zengyawen 已提交
3314 3315 3316 3317
  | Type     | Description           |
  | ------- | ------------- |
  | boolean | Whether the directory entry is a directory.|

A
Annie_wang 已提交
3318
**Example**
A
Annie_wang 已提交
3319

Z
zengyawen 已提交
3320 3321 3322 3323 3324 3325 3326 3327 3328 3329
  ```js
  let dir = fileio.opendirSync(path);
  let isDirectory = dir.readSync().isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

A
Annie_wang 已提交
3330
Checks whether this directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication.
Z
zengyawen 已提交
3331 3332 3333

**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
3334
**Return value**
A
Annie_wang 已提交
3335

Z
zengyawen 已提交
3336 3337 3338 3339
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a FIFO.|

A
Annie_wang 已提交
3340
**Example**
A
Annie_wang 已提交
3341

Z
zengyawen 已提交
3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
  ```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 已提交
3356
**Return value**
A
Annie_wang 已提交
3357

Z
zengyawen 已提交
3358 3359 3360 3361
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a regular file.|

A
Annie_wang 已提交
3362
**Example**
A
Annie_wang 已提交
3363

Z
zengyawen 已提交
3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
  ```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 已提交
3378
**Return value**
A
Annie_wang 已提交
3379

Z
zengyawen 已提交
3380 3381 3382 3383
  | Type     | Description            |
  | ------- | -------------- |
  | boolean | Whether the directory entry is a socket.|

A
Annie_wang 已提交
3384
**Example**
A
Annie_wang 已提交
3385

Z
zengyawen 已提交
3386
  ```js
A
Annie_wang 已提交
3387
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399
  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 已提交
3400
**Return value**
A
Annie_wang 已提交
3401

Z
zengyawen 已提交
3402 3403 3404 3405
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a symbolic link.|

A
Annie_wang 已提交
3406
**Example**
A
Annie_wang 已提交
3407

Z
zengyawen 已提交
3408 3409 3410 3411
  ```js
  let dir = fileio.opendirSync(path);
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```