js-apis-fileio.md 129.5 KB
Newer Older
Z
zengyawen 已提交
1 2
# File Management

A
annie_wangli 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
A
annie_wangli 已提交
4
> 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 已提交
5 6 7

## Modules to Import

A
annie_wangli 已提交
8
```js
Z
zengyawen 已提交
9 10 11
import fileio from '@ohos.fileio';
```

A
annie_wangli 已提交
12

Z
zengyawen 已提交
13 14 15 16 17
## Required Permissions

None


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

A
annie_wangli 已提交
20
Before using this module to perform operations on a file or directory, obtain the absolute path of the file or directory. For details, see [getOrCreateLocalDir of the Context module](js-apis-Context.md).
Z
zengyawen 已提交
21

A
annie_wangli 已提交
22
Absolute file or directory path = Application directory + File name or directory name
Z
zengyawen 已提交
23

A
annie_wangli 已提交
24
For example, if the application directory obtained by using **getOrCreateLocalDir** is **dir** and the file name is **xxx.txt**, the absolute path of the file is as follows:
Z
zengyawen 已提交
25

A
annie_wangli 已提交
26 27
```js
let path = dir + "/xxx.txt";
Z
zengyawen 已提交
28
```
A
annie_wangli 已提交
29

Z
zengyawen 已提交
30 31 32

The file descriptor is as follows:

A
annie_wangli 已提交
33 34

```js
Z
zengyawen 已提交
35 36 37 38
let fd = fileio.openSync(path);
```


A
annie_wangli 已提交
39 40 41 42 43 44
## fileio.stat

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

Asynchronously obtains file information. This method uses a promise to return the result.

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

A
annie_wangli 已提交
47
- Parameters
A
annie_wangli 已提交
48 49 50
  | Name | Type    | Mandatory  | Description         |
  | ---- | ------ | ---- | ----------- |
  | path | string | Yes   | Absolute path of the target file.|
A
annie_wangli 已提交
51 52

- Return value
A
annie_wangli 已提交
53 54
  | Type                          | Description        |
  | ---------------------------- | ---------- |
A
annie_wangli 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information obtained.|

- Example
  ```js
  fileio.stat(path).then(function(stat){
      console.info("getFileInfo successfully:"+ JSON.stringify(stat));
  }).catch(function(err){
      console.info("getFileInfo failed with error:"+ err);
  });
  ```


## fileio.stat

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

Asynchronously obtains file information. This method uses a callback to return the result.

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

A
annie_wangli 已提交
75
- Parameters
A
annie_wangli 已提交
76 77 78 79
  | Name     | Type                                | Mandatory  | Description             |
  | -------- | ---------------------------------- | ---- | --------------- |
  | path     | string                             | Yes   | Absolute path of the target file.    |
  | callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes   | Callback invoked to return the file information obtained.|
A
annie_wangli 已提交
80 81 82 83 84 85 86 87 88 89 90 91

- Example
  ```js
  fileio.stat(path, function (err, stat) {
      // Example code in Stat
  });
  ```


## fileio.statSync

statSync(path:string): Stat
Z
zengyawen 已提交
92 93 94

Synchronously obtains file information.

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

A
annie_wangli 已提交
97
- Parameters
A
annie_wangli 已提交
98 99 100
  | Name | Type    | Mandatory  | Description         |
  | ---- | ------ | ---- | ----------- |
  | path | string | Yes   | Absolute path of the target file.|
A
annie_wangli 已提交
101 102 103


- Return value
A
annie_wangli 已提交
104 105
  | Type           | Description        |
  | ------------- | ---------- |
A
annie_wangli 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  | [Stat](#stat) | File information obtained.|

- Example
  ```js
  let stat = fileio.statSync(path);
  // Example code in Stat
  ```


## fileio.opendir

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

Asynchronously opens a directory. This method uses a promise to return the result.

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

A
annie_wangli 已提交
123
- Parameters
A
annie_wangli 已提交
124 125 126
  | Name | Type    | Mandatory  | Description           |
  | ---- | ------ | ---- | ------------- |
  | path | string | Yes   | Absolute path of the directory to open.|
A
annie_wangli 已提交
127 128

- Return value
A
annie_wangli 已提交
129 130
  | Type                        | Description      |
  | -------------------------- | -------- |
A
annie_wangli 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  | Promise&lt;[Dir](#dir)&gt; | A **Dir** instance corresponding to the directory.|

- Example
  ```js
  fileio.opendir(path).then(function(dir){
      console.info("opendir successfully:"+ JSON.stringify(dir));
  }).catch(function(err){
      console.info("opendir failed with error:"+ err);
  });
  ```


## fileio.opendir

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

Asynchronously opens a directory. This method uses a callback to return the result.

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

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

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


## fileio.opendirSync

opendirSync(path: string): Dir
Z
zengyawen 已提交
169 170 171

Synchronously opens a directory.

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

A
annie_wangli 已提交
174 175

- Parameters
A
annie_wangli 已提交
176 177 178
  | Name | Type    | Mandatory  | Description           |
  | ---- | ------ | ---- | ------------- |
  | path | string | Yes   | Absolute path of the directory to open.|
A
annie_wangli 已提交
179 180

- Return value
A
annie_wangli 已提交
181 182
  | Type         | Description      |
  | ----------- | -------- |
A
annie_wangli 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  | [Dir](#dir) | A **Dir** instance corresponding to the directory.|

- Example
  ```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;

Asynchronously checks whether the current process can access a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
201
- Parameters
A
annie_wangli 已提交
202 203 204 205
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | path | string | Yes   | Absolute path of the target file.                             |
  | 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 已提交
206 207

- Return value
A
annie_wangli 已提交
208 209
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  fileio.access(path).then(function() {
      console.info("access successfully");
  }).catch(function(err){
      console.info("access failed with error:"+ err);
  });
  ```


## fileio.access

A
annie_wangli 已提交
224
access(path: string, mode: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
225 226 227

Asynchronously checks whether the current process can access a file. This method uses a callback to return the result.

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

A
annie_wangli 已提交
230
- Parameters
A
annie_wangli 已提交
231 232 233 234 235
  | Name     | Type                       | Mandatory  | Description                                      |
  | -------- | ------------------------- | ---- | ---------------------------------------- |
  | path     | string                    | Yes   | Absolute path of the target file.                             |
  | 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 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

- Example
  ```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 已提交
251
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
252 253

- Parameters
A
annie_wangli 已提交
254 255 256 257
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | path | string | Yes   | Absolute path of the target file.                             |
  | 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 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

- Example
  ```js
  try {
      fileio.accessSync(path);
  } catch(err) {
      console.info("accessSync failed with error:"+ err);
  }
  ```


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

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

Asynchronously closes a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
277
- Parameters
A
annie_wangli 已提交
278 279 280
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
281 282

- Return value
A
annie_wangli 已提交
283 284
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  let fd = fileio.openSync(path);
  fileio.close(fd).then(function(){
      console.info("close file successfully");
  }).catch(function(err){
      console.info("close file failed with error:"+ err);
  });
  ```


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

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

Asynchronously closes a file. This method uses a callback to return the result.

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

A
annie_wangli 已提交
306
- Parameters
A
annie_wangli 已提交
307 308 309 310
  | 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 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323

- Example
  ```js
  let fd = fileio.openSync(path);
  fileio.close(fd, function (err) {
      // Do something.
  });
  ```


## fileio.closeSync

closeSync(fd: number): void
Z
zengyawen 已提交
324 325 326

Synchronously closes a file.

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

A
annie_wangli 已提交
329
- Parameters
A
annie_wangli 已提交
330 331 332
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345

- Example
  ```js
  fileio.closeSync(fd);
  ```


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

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

Asynchronously closes the stream. This method uses a promise to return the result.

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

A
annie_wangli 已提交
348
- Return value
A
annie_wangli 已提交
349 350
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  fileio.close().then(function(){
      console.info("close file stream successfully");
  }).catch(function(err){
      console.info("close file stream failed with error:"+ err);
  });
  ```


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

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

Asynchronously closes the stream. This method uses a callback to return the result.

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

A
annie_wangli 已提交
371
- Parameters
A
annie_wangli 已提交
372 373 374
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|
A
annie_wangli 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

- Example
  ```js
  fileio.close(function(err){
      // Do something.
  });
  ```


## fileio.copyFile

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

Asynchronously copies a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
392
- Parameters
A
annie_wangli 已提交
393 394 395 396 397
  | 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 已提交
398 399

- Return value
A
annie_wangli 已提交
400 401
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  fileio.copyFile(src, dest).then(function(){
      console.info("copyFile successfully");
  }).catch(function(err){
      console.info("copyFile failed with error:"+ err);
  });
  ```


## fileio.copyFile

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

Asynchronously copies a file. This method uses a callback to return the result.

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

A
annie_wangli 已提交
422
- Parameters
A
annie_wangli 已提交
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 430 431 432 433 434 435 436 437 438 439

- Example
  ```js
  fileio.copyFile(src, dest, function (err) {
      // Do something.
  });
  ```


## fileio.copyFileSync

A
annie_wangli 已提交
440
copyFileSync(src: string | number, dest: string | number, mode?: number): void
Z
zengyawen 已提交
441 442 443

Synchronously copies a file.

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

A
annie_wangli 已提交
446
- Parameters
A
annie_wangli 已提交
447 448 449 450 451
  | 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 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464

- Example
  ```js
  fileio.copyFileSync(src, dest);
  ```


## fileio.mkdir

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

Asynchronously creates a directory. This method uses a promise to return the result.

A
annie_wangli 已提交
465
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
466 467

- Parameters
A
annie_wangli 已提交
468 469 470 471
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | path | string | Yes   | Absolute path of the directory to create.                             |
  | 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 已提交
472 473

- Return value
A
annie_wangli 已提交
474 475
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  fileio.mkdir(path).then(function() {
      console.info("mkdir successfully");
  }).catch(function (error){
      console.info("mkdir failed with error:"+ error);
  });
  ```


## fileio.mkdir

A
annie_wangli 已提交
490
mkdir(path: string, mode: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
491 492 493

Asynchronously creates a directory. This method uses a callback to return the result.

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

A
annie_wangli 已提交
496
- Parameters
A
annie_wangli 已提交
497 498 499 500 501
  | Name     | Type                       | Mandatory  | Description                                      |
  | -------- | ------------------------- | ---- | ---------------------------------------- |
  | path     | string                    | Yes   | Absolute path of the directory to create.                             |
  | 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 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514

- Example
  ```js
  fileio.mkdir(path, function(err) {
      if (!err) {
          // Do something.
      }
  });
  ```


## fileio.mkdirSync

A
annie_wangli 已提交
515
mkdirSync(path: string, mode?: number): void
Z
zengyawen 已提交
516 517 518

Synchronously creates a directory.

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

A
annie_wangli 已提交
521
- Parameters
A
annie_wangli 已提交
522 523 524 525
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | path | string | Yes   | Absolute path of the directory to create.                             |
  | 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 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538

- Example
  ```js
  fileio.mkdirSync(path);
  ```


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

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

Asynchronously opens a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
541
- Parameters
A
annie_wangli 已提交
542 543 544 545 546
  | Name  | Type    | Mandatory  | Description                                      |
  | ----- | ------ | ---- | ---------------------------------------- |
  | path  | string | Yes   | Absolute path of the target file.                             |
  | flags | number | No   | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>-&nbsp;**0o200000**: If **path** points to a directory, throw an exception.<br><br/>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
  | mode  | number | No   | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>-&nbsp;**0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>-&nbsp;**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 已提交
547 548

- Return value
A
annie_wangli 已提交
549 550
  | Type                   | Description         |
  | --------------------- | ----------- |
A
annie_wangli 已提交
551
  | Promise&lt;number&gt; | File descriptor of the file opened.|
A
annie_wangli 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

- Example
  ```js
  fileio.open(path, 0o1, 0o0200).then(function(number){
      console.info("open file successfully");
  }).catch(function(error){
      console.info("open file failed with error:"+ err);
  });
  ```


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

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

Asynchronously opens a file. This method uses a callback to return the result.

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

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

- Example
  ```js
  fileio.open(path, 0, function(err, fd) {
      // Do something.
  });
  ```


## fileio.openSync

openSync(path:string, flags?:number, mode?:number): number
Z
zengyawen 已提交
590 591 592

Synchronously opens a file.

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

A
annie_wangli 已提交
595
- Parameters
A
annie_wangli 已提交
596 597 598 599 600
  | Name  | Type    | Mandatory  | Description                                      |
  | ----- | ------ | ---- | ---------------------------------------- |
  | path  | string | Yes   | Absolute path of the target file.                             |
  | flags | number | No   | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>-&nbsp;**0o200000**: If **path** points to a directory, throw an exception.<br><br/>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
  | mode  | number | No   | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>-&nbsp;**0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>-&nbsp;**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 已提交
601 602

- Return value
A
annie_wangli 已提交
603 604
  | Type    | Description         |
  | ------ | ----------- |
A
annie_wangli 已提交
605 606 607 608 609 610 611 612 613 614
  | number | File descriptor of the file opened.|

- Example
  ```js
  let fd = fileio.openSync(path);
  ```


## fileio.read

A
annie_wangli 已提交
615 616 617 618 619
read(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): Promise&lt;ReadOut&gt;
A
annie_wangli 已提交
620 621 622

Asynchronously reads data from a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
625
- Parameters
A
annie_wangli 已提交
626 627 628 629 630
  | 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.|
A
annie_wangli 已提交
631 632

- Return value
A
annie_wangli 已提交
633 634
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
annie_wangli 已提交
635
  | Promise&lt;[ReadOut](#readout)&gt; | Data read.|
A
annie_wangli 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

- Example
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
  fileio.read(fd, buf).then(function(readout){
      console.info("read file data successfully:"+ JSON.stringify(readout));
  }).catch(function(error){
      console.info("read file data failed with error:"+ error);
  });
  ```


## fileio.read

A
annie_wangli 已提交
651 652 653 654 655
read(fd: number, buffer: ArrayBuffer, options: {
    offset?: number;
    length?: number;
    position?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
A
annie_wangli 已提交
656 657 658

Asynchronously reads data from a file. This method uses a callback to return the result.

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

A
annie_wangli 已提交
661
- Parameters
A
annie_wangli 已提交
662 663 664 665 666 667
  | 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.|
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously.                            |
A
annie_wangli 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

- Example
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
  fileio.read(fd, buf, function (err, readOut) {
      if (!err) {
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)))
      }
  });
  ```


## fileio.readSync

A
annie_wangli 已提交
683 684 685 686 687
readSync(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): number
Z
zengyawen 已提交
688 689 690

Synchronously reads data from a file.

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

A
annie_wangli 已提交
693
- Parameters
A
annie_wangli 已提交
694 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.                       |
  | 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.|
A
annie_wangli 已提交
699 700

- Return value
A
annie_wangli 已提交
701 702
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
  | number | Length of the data read.|

- Example
  ```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;

Asynchronously deletes a directory. This method uses a promise to return the result.

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

A
annie_wangli 已提交
721
- Parameters
A
annie_wangli 已提交
722 723 724
  | Name | Type    | Mandatory  | Description         |
  | ---- | ------ | ---- | ----------- |
  | path | string | Yes   | Absolute path of the directory to delete.|
A
annie_wangli 已提交
725 726

- Return value
A
annie_wangli 已提交
727 728
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  fileio.rmdir(path).then(function() {
      console.info("rmdir successfully");
  }).catch(function(err){
      console.info("rmdir failed with error:"+ err);
  });
  ```


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

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

Asynchronously deletes a directory. This method uses a callback to return the result.

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

A
annie_wangli 已提交
749
- Parameters
A
annie_wangli 已提交
750 751 752 753
  | Name     | Type                       | Mandatory  | Description          |
  | -------- | ------------------------- | ---- | ------------ |
  | path     | string                    | Yes   | Absolute path of the directory to delete. |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the directory is deleted asynchronously.|
A
annie_wangli 已提交
754 755 756 757 758 759 760 761 762

- Example
  ```js
  fileio.rmdir(path, function(err){
      // Do something.
  });
  ```


A
annie_wangli 已提交
763
## fileio.rmdirSync<sup>7+</sup>
A
annie_wangli 已提交
764

A
annie_wangli 已提交
765
rmdirSync(path: string): void
A
annie_wangli 已提交
766 767 768

Synchronously deletes a directory.

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

A
annie_wangli 已提交
771
- Parameters
A
annie_wangli 已提交
772 773 774
  | Name | Type    | Mandatory  | Description         |
  | ---- | ------ | ---- | ----------- |
  | path | string | Yes   | Absolute path of the directory to delete.|
A
annie_wangli 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787

- Example
  ```js
  fileio.rmdirSync(path);
  ```


## fileio.unlink

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

Asynchronously deletes a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
790
- Parameters
A
annie_wangli 已提交
791 792 793
  | Name | Type    | Mandatory  | Description         |
  | ---- | ------ | ---- | ----------- |
  | path | string | Yes   | Absolute path of the file to delete.|
A
annie_wangli 已提交
794 795

- Return value
A
annie_wangli 已提交
796 797
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  fileio.unlink(path).then(function(){
      console.info("remove file successfully");
  }).catch(function(error){
      console.info("remove file failed with error:"+ error);
  });
  ```


## fileio.unlink

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

Asynchronously deletes a file. This method uses a callback to return the result.

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

A
annie_wangli 已提交
818
- Parameters
A
annie_wangli 已提交
819 820 821 822
  | Name     | Type                       | Mandatory  | Description          |
  | -------- | ------------------------- | ---- | ------------ |
  | path     | string                    | Yes   | Absolute path of the file to delete. |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is deleted asynchronously.|
A
annie_wangli 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836

- Example
  ```js
  fileio.unlink(path, function(err) {
      if (!err) {
          // Do something.
      }
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void
Z
zengyawen 已提交
837 838 839

Synchronously deletes a file.

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

A
annie_wangli 已提交
842
- Parameters
A
annie_wangli 已提交
843 844 845
  | Name | Type    | Mandatory  | Description         |
  | ---- | ------ | ---- | ----------- |
  | path | string | Yes   | Absolute path of the file to delete.|
Z
zengyawen 已提交
846

A
annie_wangli 已提交
847 848 849 850
- Example
  ```js
  fileio.unlinkSync(path);
  ```
Z
zengyawen 已提交
851 852


A
annie_wangli 已提交
853
## fileio.write
Z
zengyawen 已提交
854

A
annie_wangli 已提交
855 856 857 858 859 860
write(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
861

A
annie_wangli 已提交
862
Asynchronously writes data into a file. This method uses a promise to return the result.
Z
zengyawen 已提交
863

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

A
annie_wangli 已提交
866
- Parameters
A
annie_wangli 已提交
867 868 869 870 871
  | 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.                    |
  | 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.|
Z
zengyawen 已提交
872

A
annie_wangli 已提交
873
- Return value
A
annie_wangli 已提交
874 875
  | Type                   | Description      |
  | --------------------- | -------- |
A
annie_wangli 已提交
876
  | Promise&lt;number&gt; | Length of the data written in the file.|
Z
zengyawen 已提交
877

A
annie_wangli 已提交
878 879 880 881 882 883 884 885 886
- Example
  ```js
  let fd = fileio.openSync(fpath, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world").then(function(number){
       console.info("write data to file successfully:"+ number);
  }).catch(function(err){
      console.info("write data to file failed with error:"+ err);
  });
  ```
Z
zengyawen 已提交
887 888


A
annie_wangli 已提交
889
## fileio.write
Z
zengyawen 已提交
890

A
annie_wangli 已提交
891 892 893 894 895 896
write(fd: number, buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
897

A
annie_wangli 已提交
898
Asynchronously writes data into a file. This method uses a callback to return the result.
Z
zengyawen 已提交
899

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

A
annie_wangli 已提交
902
- Parameters
A
annie_wangli 已提交
903 904 905 906 907 908
  | 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.                    |
  | 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.|
  | callback | AsyncCallback&lt;number&gt;     | Yes   | Callback invoked when the data is written asynchronously.                      |
Z
zengyawen 已提交
909

A
annie_wangli 已提交
910 911 912 913 914 915 916 917 918
- Example
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
      if (!err) {
         console.log(bytesWritten)
      }
  });
  ```
Z
zengyawen 已提交
919 920


A
annie_wangli 已提交
921
## fileio.writeSync
Z
zengyawen 已提交
922

A
annie_wangli 已提交
923 924 925 926 927 928
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
929

A
annie_wangli 已提交
930
Synchronously writes data into a file.
Z
zengyawen 已提交
931

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

A
annie_wangli 已提交
934
- Parameters
A
annie_wangli 已提交
935 936 937 938 939
  | 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.                    |
  | 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.|
Z
zengyawen 已提交
940

A
annie_wangli 已提交
941
- Return value
A
annie_wangli 已提交
942 943
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
944
  | number | Length of the data written in the file.|
Z
zengyawen 已提交
945

A
annie_wangli 已提交
946 947 948 949 950
- Example
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  let num = fileio.writeSync(fd, "hello, world");
  ```
Z
zengyawen 已提交
951 952


A
annie_wangli 已提交
953
## fileio.hash
Z
zengyawen 已提交
954

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

A
annie_wangli 已提交
957
Asynchronously calculates the hash value of a file. This method uses a promise to return the result.
Z
zengyawen 已提交
958

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

A
annie_wangli 已提交
961
- Parameters
A
annie_wangli 已提交
962 963 964 965
  | Name      | Type    | Mandatory  | Description                                      |
  | --------- | ------ | ---- | ---------------------------------------- |
  | path      | string | Yes   | Absolute path of the target file.                          |
  | 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 已提交
966

A
annie_wangli 已提交
967
- Return value
A
annie_wangli 已提交
968 969
  | Type                   | Description                        |
  | --------------------- | -------------------------- |
A
annie_wangli 已提交
970
  | Promise&lt;string&gt; | Promise used to return the hash value of the file. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
Z
zengyawen 已提交
971

A
annie_wangli 已提交
972 973 974 975 976 977 978 979
- Example
  ```js
  fileio.hash(path, "sha256").then(function(str){
      console.info("calculate file hash successfully:"+ str);
  }).catch(function(error){
      console.info("calculate file hash failed with error:"+ err);
  });
  ```
Z
zengyawen 已提交
980 981


A
annie_wangli 已提交
982
## fileio.hash
Z
zengyawen 已提交
983

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

A
annie_wangli 已提交
986
Asynchronously calculates the hash value of a file. This method uses a callback to return the result.
Z
zengyawen 已提交
987

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

A
annie_wangli 已提交
990
- Parameters
A
annie_wangli 已提交
991 992 993 994 995
  | Name      | Type                         | Mandatory  | Description                                      |
  | --------- | --------------------------- | ---- | ---------------------------------------- |
  | path      | string                      | Yes   | Absolute path of the target file.                          |
  | algorithm | string                      | Yes   | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**.**sha256** is recommended for security purposes.|
  | callback  | AsyncCallback&lt;string&gt; | Yes   | Callback used to return the hash value. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
Z
zengyawen 已提交
996

A
annie_wangli 已提交
997 998 999 1000 1001 1002 1003 1004
- Example
  ```js
  fileio.hash(fpath, "sha256", function(err, hashStr) {
      if (!err) {
          console.log(hashStr)
      }
  });
  ```
Z
zengyawen 已提交
1005 1006


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

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

A
annie_wangli 已提交
1011
Asynchronously changes the file permissions based on its path. This method uses a promise to return the result.
Z
zengyawen 已提交
1012

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

A
annie_wangli 已提交
1015
- Parameters
A
annie_wangli 已提交
1016 1017 1018 1019
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | path | string | Yes   | Absolute path 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.|
Z
zengyawen 已提交
1020

A
annie_wangli 已提交
1021
- Return value
A
annie_wangli 已提交
1022 1023
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1024
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|
Z
zengyawen 已提交
1025

A
annie_wangli 已提交
1026 1027 1028 1029 1030 1031 1032 1033
- Example
  ```js
  fileio.chmod(path, mode).then(function() {
      console.info("chmod successfully");
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
  });
  ```
Z
zengyawen 已提交
1034 1035


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

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

A
annie_wangli 已提交
1040
Asynchronously changes the file permissions based on its path. This method uses a callback to return the result.
Z
zengyawen 已提交
1041

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

A
annie_wangli 已提交
1044
- Parameters
A
annie_wangli 已提交
1045 1046 1047 1048 1049
  | Name     | Type                       | Mandatory  | Description                                      |
  | -------- | ------------------------- | ---- | ---------------------------------------- |
  | path     | string                    | Yes   | Absolute path 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&lt;void&gt; | Yes   | Callback invoked when the file permissions are changed asynchronously.                          |
Z
zengyawen 已提交
1050

A
annie_wangli 已提交
1051 1052 1053 1054 1055 1056
- Example
  ```js
  fileio.chmod(path, mode, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
1057 1058


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

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

A
annie_wangli 已提交
1063
Synchronously changes the file permissions based on its path.
Z
zengyawen 已提交
1064

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

A
annie_wangli 已提交
1067
- Parameters
A
annie_wangli 已提交
1068 1069 1070 1071
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | path | string | Yes   | Absolute path 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.|
Z
zengyawen 已提交
1072

A
annie_wangli 已提交
1073 1074 1075 1076
- Example
  ```js
  fileio.chmodSync(fpath, mode);
  ```
Z
zengyawen 已提交
1077 1078


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

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

A
annie_wangli 已提交
1083
Asynchronously obtains file status information based on the file descriptor. This method uses a promise to return the result.
Z
zengyawen 已提交
1084

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

A
annie_wangli 已提交
1087
- Parameters
A
annie_wangli 已提交
1088 1089 1090
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1091

A
annie_wangli 已提交
1092
- Return value
A
annie_wangli 已提交
1093
  | Type                          | Description        |
A
annie_wangli 已提交
1094
  | -------- | -------- |
A
annie_wangli 已提交
1095
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file status information obtained.|
Z
zengyawen 已提交
1096

A
annie_wangli 已提交
1097 1098 1099 1100 1101 1102 1103 1104
- Example
  ```js
  fileio.fstat(fd).then(function(stat){
      console.info("fstat successfully:"+ JSON.stringify(stat));
  }).catch(function(err){
      console.info("fstat failed with error:"+ err);
  });
  ```
Z
zengyawen 已提交
1105 1106


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

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

A
annie_wangli 已提交
1111
Asynchronously obtains file status information based on the file descriptor. This method uses a callback to return the result.
Z
zengyawen 已提交
1112

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

A
annie_wangli 已提交
1115
- Parameters
A
annie_wangli 已提交
1116 1117 1118 1119
  | Name     | Type                                | Mandatory  | Description              |
  | -------- | ---------------------------------- | ---- | ---------------- |
  | fd       | number                             | Yes   | File descriptor of the target file.    |
  | callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes   | Callback invoked when the file status information is obtained asynchronously.|
Z
zengyawen 已提交
1120

A
annie_wangli 已提交
1121 1122 1123 1124 1125 1126 1127
- Example
  ```js
  let fd = fileio.openSync(path);
  fileio.fstat(fd, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
1128 1129


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

A
annie_wangli 已提交
1132
fstatSync(fd: number): Stat
Z
zengyawen 已提交
1133

A
annie_wangli 已提交
1134
Synchronously obtains file status information based on the file descriptor.
Z
zengyawen 已提交
1135

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

A
annie_wangli 已提交
1138
- Parameters
A
annie_wangli 已提交
1139 1140 1141
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1142

A
annie_wangli 已提交
1143
- Return value
A
annie_wangli 已提交
1144 1145
  | Type           | Description        |
  | ------------- | ---------- |
A
annie_wangli 已提交
1146
  | [Stat](#stat) | File status information obtained.|
Z
zengyawen 已提交
1147

A
annie_wangli 已提交
1148 1149 1150 1151 1152
- Example
  ```js
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```
Z
zengyawen 已提交
1153 1154


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

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

A
annie_wangli 已提交
1159
Asynchronously truncates a file based on the file descriptor. This method uses a promise to return the result.
Z
zengyawen 已提交
1160

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

A
annie_wangli 已提交
1163
- Parameters
A
annie_wangli 已提交
1164 1165 1166 1167
  | Name | Type    | Mandatory  | Description              |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | Yes   | File descriptor of the file to truncate.    |
  | len  | number | Yes   | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1168

A
annie_wangli 已提交
1169
- Return value
A
annie_wangli 已提交
1170 1171
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1172
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|
Z
zengyawen 已提交
1173

A
annie_wangli 已提交
1174 1175 1176 1177
- Example
  ```js
  let fd = fileio.openSync(path);
  fileio.ftruncate(fd, 5).then(function(err) {    
A
annie_wangli 已提交
1178
      console.info("File truncated successfully.");
A
annie_wangli 已提交
1179 1180 1181 1182
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1183 1184


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

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

A
annie_wangli 已提交
1189
Asynchronously truncates a file based on the file descriptor. This method uses a callback to return the result.
Z
zengyawen 已提交
1190

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

A
annie_wangli 已提交
1193
- Parameters
A
annie_wangli 已提交
1194 1195 1196 1197 1198
  | Name     | Type                       | Mandatory  | Description              |
  | -------- | ------------------------- | ---- | ---------------- |
  | fd       | number                    | Yes   | File descriptor of the file to truncate.    |
  | len      | number                    | Yes   | File length, in bytes, after truncation.|
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is truncated asynchronously. |
Z
zengyawen 已提交
1199

A
annie_wangli 已提交
1200 1201 1202 1203 1204 1205
- Example
  ```js
  fileio.ftruncate(fd, len, function(err){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1206 1207


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

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

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

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

A
annie_wangli 已提交
1216
- Parameters
A
annie_wangli 已提交
1217 1218 1219 1220
  | 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 已提交
1221

A
annie_wangli 已提交
1222 1223 1224 1225
- Example
  ```js
  fileio.ftruncate(fd, len);
  ```
Z
zengyawen 已提交
1226 1227


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

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

A
annie_wangli 已提交
1232
Asynchronously truncates a file based on its path. This method uses a promise to return the result.
Z
zengyawen 已提交
1233

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

A
annie_wangli 已提交
1236
- Parameters
A
annie_wangli 已提交
1237 1238 1239 1240
  | Name | Type    | Mandatory  | Description              |
  | ---- | ------ | ---- | ---------------- |
  | path | string | Yes   | Absolute path of the file to truncate.     |
  | len  | number | Yes   | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1241

A
annie_wangli 已提交
1242
- Return value
A
annie_wangli 已提交
1243 1244
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1245
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|
Z
zengyawen 已提交
1246

A
annie_wangli 已提交
1247 1248 1249
- Example
  ```js
  fileio.truncate(path, len).then(function(){
A
annie_wangli 已提交
1250
      console.info("File truncated successfully.");
A
annie_wangli 已提交
1251 1252 1253 1254
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1255 1256


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

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

A
annie_wangli 已提交
1261
Asynchronously truncates a file based on its path. This method uses a callback to return the result.
Z
zengyawen 已提交
1262

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

A
annie_wangli 已提交
1265
- Parameters
A
annie_wangli 已提交
1266 1267 1268 1269 1270
  | Name     | Type                       | Mandatory  | Description              |
  | -------- | ------------------------- | ---- | ---------------- |
  | path     | string                    | Yes   | Absolute path of the file to truncate.     |
  | len      | number                    | Yes   | File length, in bytes, after truncation.|
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is truncated asynchronously. |
Z
zengyawen 已提交
1271

A
annie_wangli 已提交
1272 1273 1274 1275 1276 1277
- Example
  ```js
  fileio.truncate(path, len, function(err){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1278 1279


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

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

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

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

A
annie_wangli 已提交
1288
- Parameters
A
annie_wangli 已提交
1289 1290 1291 1292
  | Name | Type    | Mandatory  | Description              |
  | ---- | ------ | ---- | ---------------- |
  | path | string | Yes   | Absolute path of the file to truncate.     |
  | len  | number | No   | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1293

A
annie_wangli 已提交
1294 1295 1296 1297
- Example
  ```js
  fileio.ftruncate(path, len);
  ```
Z
zengyawen 已提交
1298 1299


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

A
annie_wangli 已提交
1302 1303 1304 1305 1306
readText(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): Promise&lt;string&gt;
Z
zengyawen 已提交
1307

A
annie_wangli 已提交
1308
Asynchronously reads the text of a file. This method uses a promise to return the result.
Z
zengyawen 已提交
1309

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

A
annie_wangli 已提交
1312
- Parameters
A
annie_wangli 已提交
1313 1314 1315 1316
  | Name     | Type    | Mandatory  | Description                                      |
  | -------- | ------ | ---- | ---------------------------------------- |
  | filePath | string | Yes   | Absolute 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 已提交
1317

A
annie_wangli 已提交
1318
- Return value
A
annie_wangli 已提交
1319 1320
  | Type                   | Description        |
  | --------------------- | ---------- |
A
annie_wangli 已提交
1321
  | Promise&lt;string&gt; | Promise used to return the content of the file read.|
Z
zengyawen 已提交
1322

A
annie_wangli 已提交
1323 1324 1325 1326 1327 1328 1329 1330
- Example
  ```js
  fileio.readText(path).then(function(str) {
      console.info("readText successfully:"+ str);
  }).catch(function(err){
      console.info("readText failed with error:"+ err);
  });
  ```
Z
zengyawen 已提交
1331 1332


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

A
annie_wangli 已提交
1335 1336 1337 1338 1339
readText(filePath: string, options: {
    position?: number;
    length?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1340

A
annie_wangli 已提交
1341
Asynchronously reads the text of a file. This method uses a callback to return the result.
Z
zengyawen 已提交
1342

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

A
annie_wangli 已提交
1345
- Parameters
A
annie_wangli 已提交
1346 1347 1348 1349 1350
  | Name     | Type                         | Mandatory  | Description                                      |
  | -------- | --------------------------- | ---- | ---------------------------------------- |
  | filePath | string                      | Yes   | Absolute 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.|
  | callback | AsyncCallback&lt;string&gt; | Yes   | Callback invoked when the file is read asynchronously.                      |
Z
zengyawen 已提交
1351

A
annie_wangli 已提交
1352 1353 1354 1355 1356 1357
- Example
  ```js
  fileio.readText(path, function(err, str){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1358 1359


A
annie_wangli 已提交
1360 1361
## fileio.readTextSync<sup>7+</sup>

A
annie_wangli 已提交
1362 1363 1364 1365 1366
readTextSync(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): string
A
annie_wangli 已提交
1367 1368 1369

Synchronously reads the text of a file. 

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

A
annie_wangli 已提交
1372
- Parameters
A
annie_wangli 已提交
1373 1374 1375 1376
  | Name     | Type    | Mandatory  | Description                                      |
  | -------- | ------ | ---- | ---------------------------------------- |
  | filePath | string | Yes   | Absolute 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.|
A
annie_wangli 已提交
1377 1378

- Return value
A
annie_wangli 已提交
1379 1380 1381
  | Type  | Description                |
  | ------ | -------------------- |
  | string | Content of the file read.|
A
annie_wangli 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394

- Example
  ```js
  let str = fileio.readTextSync(path, {position: 1, length: 3});
  ```


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

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

Asynchronously obtains link status information. This method uses a promise to return the result.

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

A
annie_wangli 已提交
1397
- Parameters
A
annie_wangli 已提交
1398 1399 1400
  | Name | Type    | Mandatory  | Description               |
  | ---- | ------ | ---- | ----------------- |
  | path | string | Yes   | Absolute path of the target file, pointing to the link status.|
A
annie_wangli 已提交
1401 1402

- Return value
A
annie_wangli 已提交
1403 1404
  | Type                          | Description        |
  | ---------------------------- | ---------- |
A
annie_wangli 已提交
1405
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the link status obtained.|
A
annie_wangli 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422

- Example
  ```js
  fileio.lstat(path).then(function(stat){
      console.info("Link status obtained:"+ number);
  }).catch(function(err){
      console.info("Failed to obtain the link status. Error:"+ err);
  });
  ```


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

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

Asynchronously obtains link status information. This method uses a callback to return the result.

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

A
annie_wangli 已提交
1425
- Parameters
A
annie_wangli 已提交
1426 1427 1428 1429
  | Name     | Type                                | Mandatory  | Description               |
  | -------- | ---------------------------------- | ---- | ----------------- |
  | path     | string                             | Yes   | Absolute path of the target file, pointing to the link status.|
  | callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes   | Callback invoked when the link status information is obtained asynchronously. |
A
annie_wangli 已提交
1430 1431 1432 1433 1434

- Example
  ```js
  fileio.lstat(path, function (err, stat) {
      // Do something.
A
annie_wangli 已提交
1435
  });
A
annie_wangli 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444
  ```


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

lstatSync(path:string): Stat

Synchronously obtains link status information.

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

A
annie_wangli 已提交
1447
- Parameters
A
annie_wangli 已提交
1448 1449 1450
  | Name | Type    | Mandatory  | Description               |
  | ---- | ------ | ---- | ----------------- |
  | path | string | Yes   | Absolute path of the target file, pointing to the link status.|
A
annie_wangli 已提交
1451 1452

- Return value
A
annie_wangli 已提交
1453 1454
  | Type           | Description        |
  | ------------- | ---------- |
A
annie_wangli 已提交
1455
  | [Stat](#stat) | Link status obtained.|
A
annie_wangli 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464

- Example
  ```js
  let stat = fileio.lstatSync(path);
  ```


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

A
annie_wangli 已提交
1465 1466 1467 1468 1469
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
A
annie_wangli 已提交
1470 1471 1472

Asynchronously reads data from a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
1475
- Parameters
A
annie_wangli 已提交
1476 1477 1478 1479
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | 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.|
A
annie_wangli 已提交
1480 1481

- Return value
A
annie_wangli 已提交
1482 1483
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
annie_wangli 已提交
1484
  | Promise&lt;[ReadOut](#readout)&gt; | Data read.|
Z
zengyawen 已提交
1485

A
annie_wangli 已提交
1486 1487 1488 1489 1490 1491 1492 1493
- Example
  ```js
  fileio.read(new ArrayBuffer(4096)).then(function(readout){
      console.info("File data read successfully:"+ String.fromCharCode.apply(null, new Uint8Array(readout.buffer)));
  }).catch(function(err){
      console.info("Failed to read file data. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1494 1495


A
annie_wangli 已提交
1496 1497
## fileio.read<sup>7+</sup>

A
annie_wangli 已提交
1498 1499 1500 1501 1502
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
A
annie_wangli 已提交
1503 1504 1505

Asynchronously reads data from a file. This method uses a callback to return the result.

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

A
annie_wangli 已提交
1508
- Parameters
A
annie_wangli 已提交
1509 1510 1511 1512 1513
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | 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.|
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously from the file.                         |
A
annie_wangli 已提交
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531

- Example
  ```js
  let buf = new ArrayBuffer(4096);
  fileio.read(buf, function (err, readOut) {
      if (!err) {
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)))
      }
  });
  ```


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

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

Asynchronously renames a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
1534
- Parameters
A
annie_wangli 已提交
1535 1536 1537 1538
  | Name | Type  | Mandatory| Description                    |
  | ------- | ------ | ---- | ------------------------ |
  | oldPath | string | Yes  | Absolute path of the file to rename.|
  | newPath | String | Yes  | Absolute path of the file renamed.  |
A
annie_wangli 已提交
1539 1540

- Return value
A
annie_wangli 已提交
1541 1542
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1543 1544 1545 1546
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
A
annie_wangli 已提交
1547
  fileio.rename(oldPath, newPath).then(function() {
A
annie_wangli 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
      console.info("rename successfully");
  }).catch(function(err){
      console.info("rename failed with error:"+ err);
  });
  ```


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

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

Asynchronously renames a file. This method uses a callback to return the result.

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

A
annie_wangli 已提交
1563
- Parameters
A
annie_wangli 已提交
1564 1565 1566 1567 1568
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | oldPath  | string                    | Yes   | Absolute path of the file to rename. |
  | newPath  | String                    | Yes   | Absolute path of the file renamed.  |
  | Callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is asynchronously renamed.|
A
annie_wangli 已提交
1569 1570 1571

- Example
  ```js
A
annie_wangli 已提交
1572
  fileio.rename(oldPath, newPath, function(err){
A
annie_wangli 已提交
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
  });
  ```


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

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

Synchronously renames a file.

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

A
annie_wangli 已提交
1585
- Parameters
A
annie_wangli 已提交
1586 1587 1588 1589
  | Name    | Type    | Mandatory  | Description          |
  | ------- | ------ | ---- | ------------ |
  | oldPath | string | Yes   | Absolute path of the file to rename.|
  | newPath | String | Yes   | Absolute path of the file renamed. |
A
annie_wangli 已提交
1590 1591 1592

- Example
  ```js
A
annie_wangli 已提交
1593
  fileio.renameSync(oldPath, newPath);
A
annie_wangli 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602
  ```


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

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

Asynchronously synchronizes a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
1605
- Parameters
A
annie_wangli 已提交
1606 1607 1608
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to synchronize.|
A
annie_wangli 已提交
1609 1610

- Return value
A
annie_wangli 已提交
1611 1612
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  fileio.fsync(fd).then(function(){
      console.info("sync data successfully");
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
  });
  ```


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

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

Asynchronously synchronizes a file. This method uses a callback to return the result.

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

A
annie_wangli 已提交
1633
- Parameters
A
annie_wangli 已提交
1634 1635 1636 1637
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | Yes   | File descriptor of the file to synchronize.   |
  | Callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650

- Example
  ```js
  fileio.fsync(fd, function(err){
      // Do something.
  });
  ```


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

fsyncSync(fd: number): void

A
annie_wangli 已提交
1651 1652 1653
Synchronizes a file in synchronous mode. 

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

- Parameters
A
annie_wangli 已提交
1656 1657 1658
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to synchronize.|
A
annie_wangli 已提交
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671

- Example
  ```js
  fileio.fyncsSync(fd);
  ```


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

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

Asynchronously synchronizes data in a file. This method uses a promise to return the result.

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

A
annie_wangli 已提交
1674
- Parameters
A
annie_wangli 已提交
1675 1676 1677
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to synchronize.|
A
annie_wangli 已提交
1678 1679

- Return value
A
annie_wangli 已提交
1680 1681
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
  | Promise&lt;void&gt; | Promise used to return the result asynchronously. An empty value is returned.|

- Example
  ```js
  fileio.fdatasync(fd).then(function(err) {
      console.info("sync data successfully");
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
  });
  ```


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

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

Asynchronously synchronizes data in a file. This method uses a callback to return the result.

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

A
annie_wangli 已提交
1702
- Parameters
A
annie_wangli 已提交
1703 1704 1705 1706
  | 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 已提交
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721

- Example
  ```js
  fileio.fdatasync (fd, function (err) {
      // Do something.
  });
  ```


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

fdatasyncSync(fd: number): void

Synchronizes data in a file in synchronous mode.

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

A
annie_wangli 已提交
1724
- Parameters
A
annie_wangli 已提交
1725 1726 1727
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to synchronize.|
A
annie_wangli 已提交
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740

- Example
  ```js
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

Asynchronously creates a symbolic link based on a path. This method uses a promise to return the result.

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

A
annie_wangli 已提交
1743
- Parameters
A
annie_wangli 已提交
1744 1745 1746 1747
  | Name    | Type    | Mandatory  | Description          |
  | ------- | ------ | ---- | ------------ |
  | target  | string | Yes   | Absolute path of the symbolic link.  |
  | srcPath | string | Yes   | Absolute path of the referenced file or directory contained in the symbolic link.|
A
annie_wangli 已提交
1748 1749

- Return value
A
annie_wangli 已提交
1750 1751
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
  | Promise&lt;void&gt; | Promise used to return the result asynchronously. An empty value is returned.|

- Example
  ```js
  fileio.symlink(target, srcPath).then(function() {
      console.info("symlink successfully");
  }).catch(function(err){
      console.info("symlink failed with error:"+ err);
  });
  ```


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

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

Asynchronously creates a symbolic link based on a path. This method uses a callback to return the result.

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

A
annie_wangli 已提交
1772
- Parameters
A
annie_wangli 已提交
1773 1774 1775 1776 1777
  | Name     | Type                       | Mandatory  | Description              |
  | -------- | ------------------------- | ---- | ---------------- |
  | target   | string                    | Yes   | Absolute path of the symbolic link.      |
  | srcPath  | string                    | Yes   | Absolute path of the referenced file or directory contained in the symbolic link.    |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the symbolic link is created asynchronously.|
A
annie_wangli 已提交
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792

- Example
  ```js
  fileio.symlink(target, srcPath, function (err) {
      // Do something.
  });
  ```


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

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

Synchronously creates a symbolic link based on a specified path.

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

A
annie_wangli 已提交
1795
- Parameters
A
annie_wangli 已提交
1796 1797 1798 1799
  | Name    | Type    | Mandatory  | Description          |
  | ------- | ------ | ---- | ------------ |
  | target  | string | Yes   | Absolute path of the symbolic link.  |
  | srcPath | string | Yes   | Absolute path of the referenced file or directory contained in the symbolic link.|
A
annie_wangli 已提交
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812

- Example
  ```js
  fileio.symlinkSync(target, srcPath);
  ```


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

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

Asynchronously changes the file owner based on its path. This method uses a promise to return the result.

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

A
annie_wangli 已提交
1815
- Parameters
A
annie_wangli 已提交
1816 1817 1818 1819 1820
  | Name | Type    | Mandatory  | Description             |
  | ---- | ------ | ---- | --------------- |
  | path | string | Yes   | Absolute path of the target file.    |
  | uid  | number | Yes   | New user ID (UID). |
  | gid  | number | Yes   | New group ID (GID).|
A
annie_wangli 已提交
1821 1822

- Return value
A
annie_wangli 已提交
1823 1824
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1825 1826 1827 1828 1829
  | Promise&lt;void&gt; | Promise used to return the result asynchronously. An empty value is returned.|

- Example
  ```js
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
1830
  fileio.chown(path, stat.uid, stat.gid).then(function(){
A
annie_wangli 已提交
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
      console.info("chown successfully");
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
  ```


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

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

Asynchronously changes the file owner based on its path. This method uses a callback to return the result.

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

A
annie_wangli 已提交
1846
- Parameters
A
annie_wangli 已提交
1847 1848 1849 1850 1851 1852
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
  | path     | string                    | Yes   | Absolute path 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 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868

- Example
  ```js
  let stat = fileio.statSync(fpath)
  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 已提交
1869 1870
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1871
- Parameters
A
annie_wangli 已提交
1872 1873 1874 1875 1876
  | Name | Type    | Mandatory  | Description         |
  | ---- | ------ | ---- | ----------- |
  | path | string | Yes   | Absolute path of the target file.|
  | uid  | number | Yes   | New UID.     |
  | gid  | number | Yes   | New GID.     |
A
annie_wangli 已提交
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890

- Example
  ```js
  let stat = fileio.statSync(fpath)
  fileio.chownSync(path, stat.uid, stat.gid);
  ```


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

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

Asynchronously creates a temporary directory. This method uses a promise to return the result.

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

A
annie_wangli 已提交
1893
- Parameters
A
annie_wangli 已提交
1894 1895 1896
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
1897 1898

- Return value
A
annie_wangli 已提交
1899 1900
  | Name                  | Description        |
  | --------------------- | ---------- |
A
annie_wangli 已提交
1901
  | Promise&lt;string&gt; | Unique path generated.|
A
annie_wangli 已提交
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918

- Example
  ```js
  fileio.mkdtemp(path + "XXXX").then(function(path){
      console.info("mkdtemp successfully:"+ path);
  }).catch(function(err){
      console.info("mkdtemp failed with error:"+ err);
  });
  ```


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

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

Asynchronously creates a temporary directory. This method uses a callback to return the result.

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

A
annie_wangli 已提交
1921
- Parameters
A
annie_wangli 已提交
1922 1923 1924 1925
  | 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 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940

- Example
  ```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 已提交
1941 1942
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1943
- Parameters
A
annie_wangli 已提交
1944 1945 1946
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
1947 1948

- Return value
A
annie_wangli 已提交
1949 1950
  | Name   | Description        |
  | ------ | ---------- |
A
annie_wangli 已提交
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
  | string | Unique path generated.|

- Example
  ```js
  let res = fileio.mkdtempSync(path + "XXXX");
  ```


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

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

Asynchronously changes the file permissions based on the file descriptor. This method uses a promise to return the result.

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

A
annie_wangli 已提交
1967
- Parameters
A
annie_wangli 已提交
1968 1969 1970 1971
  | 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 已提交
1972 1973

- Return value
A
annie_wangli 已提交
1974 1975
  | Name                | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
  | Promise&lt;void&gt; | Promise used to return the result asynchronously. An empty value is returned.|

- Example
  ```js
  fileio.fchmod(fd, mode).then(function() {
      console.info("chmod successfully");
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
  });
  ```


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

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

Asynchronously changes the file permissions based on the file descriptor. This method uses a callback to return the result.

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

A
annie_wangli 已提交
1996
- Parameters
A
annie_wangli 已提交
1997 1998 1999 2000 2001
  | 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 已提交
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012

- Example
  ```js
  fileio.fchmod(fd, mode, function (err) {
      // Do something.
  });
  ```


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

A
annie_wangli 已提交
2013
fchmodSync(fd: number, mode: number): void
A
annie_wangli 已提交
2014 2015 2016

Synchronously changes the file permissions based on the file descriptor.

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

A
annie_wangli 已提交
2019
- Parameters
A
annie_wangli 已提交
2020 2021 2022 2023
  | 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 已提交
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036

- Example
  ```js
   fileio.fchmodSync(fd, mode);
  ```


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

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

Asynchronously opens a stream based on the file path. This method uses a promise to return the result.

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

A
annie_wangli 已提交
2039
- Parameters
A
annie_wangli 已提交
2040 2041 2042 2043
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | path | string | Yes   | Absolute path 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 已提交
2044 2045

- Return value
A
annie_wangli 已提交
2046 2047
  | Type                               | Description       |
  | --------------------------------- | --------- |
A
annie_wangli 已提交
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
  | Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.|

- Example
  ```js
  fileio.createStream(path, "r+").then(function(stream){
      console.info("createStream successfully");
  }).catch(function(err){
      console.info("createStream failed with error:"+ err);
  });
  ```


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

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

Asynchronously opens a stream based on the file path. This method uses a callback to return the result.

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

A
annie_wangli 已提交
2068
- Parameters
A
annie_wangli 已提交
2069 2070 2071 2072 2073
  | Name     | Type                                     | Mandatory  | Description                                      |
  | -------- | --------------------------------------- | ---- | ---------------------------------------- |
  | path     | string                                  | Yes   | Absolute path of the target file.                             |
  | mode     | string                                  | Yes   | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
  | callback | AsyncCallback&lt;[Stream](#stream7)&gt; | Yes   | Callback invoked when the stream is open asynchronously.                           |
A
annie_wangli 已提交
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088

- Example
  ```js
  fileio.createStream(path, mode, function(err, stream){
      // 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 已提交
2089 2090
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2091
- Parameters
A
annie_wangli 已提交
2092 2093 2094 2095
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | path | string | Yes   | Absolute path 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 已提交
2096 2097

- Return value
A
annie_wangli 已提交
2098 2099
  | Name               | Description       |
  | ------------------ | --------- |
A
annie_wangli 已提交
2100
  | [Stream](#stream7) | Stream opened.|
A
annie_wangli 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113

- Example
  ```js
  let ss = fileio.createStreamSync(path, "r+");
  ```


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

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

Asynchronously opens a stream based on the file descriptor. This method uses a promise to return the result.

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

A
annie_wangli 已提交
2116
- Parameters
A
annie_wangli 已提交
2117 2118 2119 2120
  | 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 已提交
2121 2122

- Return value
A
annie_wangli 已提交
2123 2124
  | Name                              | Description       |
  | --------------------------------- | --------- |
A
annie_wangli 已提交
2125 2126 2127 2128 2129
  | Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.|

- Example
  ```js
  fileio.fdopenStream(fd, mode).then(function(stream){
A
annie_wangli 已提交
2130
      console.info("openStream successfully");
A
annie_wangli 已提交
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
  }).catch(function(err){
      console.info("openStream failed with error:"+ err);
  });
  ```


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

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

Asynchronously opens a stream based on the file descriptor. This method uses a callback to return the result.

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

A
annie_wangli 已提交
2145
- Parameters
A
annie_wangli 已提交
2146 2147 2148 2149 2150
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                                   | Yes   | File descriptor of the target file.                            |
  | mode     | string                                   | Yes   | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
  | callback | AsyncCallback&nbsp;&lt;[Stream](#stream7)&gt; | Yes   | Callback invoked when the stream is open asynchronously.                           |
A
annie_wangli 已提交
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165

- Example
  ```js
  fileio.fdopenStream(fd, mode, function (err, stream) {
      // 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 已提交
2166 2167
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2168
- Parameters
A
annie_wangli 已提交
2169 2170 2171 2172
  | 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 已提交
2173 2174

- Return value
A
annie_wangli 已提交
2175 2176
  | Name               | Description       |
  | ------------------ | --------- |
A
annie_wangli 已提交
2177
  | [Stream](#stream7) | Stream opened.|
A
annie_wangli 已提交
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190

- Example
  ```js
  let ss = fileio.fdopenStreamSync(fd, "r+");
  ```


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

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

Asynchronously changes the file owner based on the file descriptor. This method uses a promise to return the result.

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

A
annie_wangli 已提交
2193
- Parameters
A
annie_wangli 已提交
2194 2195 2196 2197 2198
  | 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 已提交
2199 2200

- Return value
A
annie_wangli 已提交
2201 2202
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  let stat = fileio.statSync(path);
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
      console.info("chown successfully");
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
  ```


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

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

Asynchronously changes the file owner based on the file descriptor. This method uses a callback to return the result.

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

A
annie_wangli 已提交
2224
- Parameters
A
annie_wangli 已提交
2225 2226 2227 2228 2229 2230
  | 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 已提交
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246

- Example
  ```js
  let stat = fileio.statSync(fpath);
  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 已提交
2247 2248
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2249
- Parameters
A
annie_wangli 已提交
2250 2251 2252 2253 2254
  | 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 已提交
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268

- Example
  ```js
  let stat = fileio.statSync(fpath);
  fileio.fchownSync(fd, stat.uid, stat.gid);
  ```


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

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

Asynchronously changes the file owner based on the file path, changes the owner of the symbolic link (not the referenced file), and returns the result in a promise.

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

A
annie_wangli 已提交
2271
- Parameters
A
annie_wangli 已提交
2272 2273 2274 2275 2276
  | Name | Type    | Mandatory  | Description         |
  | ---- | ------ | ---- | ----------- |
  | path | string | Yes   | Absolute path of the target file.|
  | uid  | number | Yes   | New UID.     |
  | gid  | number | Yes   | New GID.     |
A
annie_wangli 已提交
2277 2278

- Return value
A
annie_wangli 已提交
2279 2280
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid).then(function() {
      console.info("chown successfully");
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
  ```


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

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

Asynchronously changes the file owner based on the file path, changes the owner of the symbolic link (not the referenced file), and returns the result in a callback.

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

A
annie_wangli 已提交
2302
- Parameters
A
annie_wangli 已提交
2303 2304 2305 2306 2307 2308
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
  | path     | string                    | Yes   | Absolute path 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 已提交
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324

- Example
  ```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 已提交
2325 2326
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2327
- Parameters
A
annie_wangli 已提交
2328 2329 2330 2331 2332
  | Name | Type    | Mandatory  | Description         |
  | ---- | ------ | ---- | ----------- |
  | path | string | Yes   | Absolute path of the target file.|
  | uid  | number | Yes   | New UID.     |
  | gid  | number | Yes   | New GID.     |
A
annie_wangli 已提交
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346

- Example
  ```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

Asynchronously listens for the changes of a file or directory. This method uses a callback to return the result.

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

A
annie_wangli 已提交
2349
- Parameters
A
annie_wangli 已提交
2350 2351 2352 2353 2354
  | Name     | Type                               | Mandatory  | Description                                      |
  | -------- | --------------------------------- | ---- | ---------------------------------------- |
  | filename | string                            | Yes   | Absolute path of the target file.                             |
  | 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 已提交
2355 2356

- Return value
A
annie_wangli 已提交
2357 2358
  | Name                 | Description        |
  | -------------------- | ---------- |
A
annie_wangli 已提交
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
  | [Watcher](#watcher7) | Instance for listening for a file change event.|

- Example
  ```js
  fileio.createWatcher(filename, events, function(watcher){
      // Do something.
  });
  ```


## Readout

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

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

A
annie_wangli 已提交
2375 2376 2377 2378 2379
| 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 已提交
2380 2381 2382 2383 2384 2385


## 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 已提交
2386
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
2387 2388 2389

### Attributes

A
annie_wangli 已提交
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
| 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 已提交
2404 2405 2406 2407 2408 2409 2410 2411


### isBlockDevice

isBlockDevice(): boolean

Checks whether the current directory entry is a block special file. A block special file supports access by block only, and it is cached when accessed.

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

A
annie_wangli 已提交
2414
- Return value
A
annie_wangli 已提交
2415 2416
  | Type     | Description              |
  | ------- | ---------------- |
A
annie_wangli 已提交
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
  | boolean | Whether the directory entry is a block special file.|

- Example
  ```js
  let isBLockDevice = fileio.statSync(path).isBlockDevice();
  ```


### isCharacterDevice

isCharacterDevice(): boolean

Checks whether the current directory entry is a character special file. A character special file supports random access, and it is not cached when accessed.

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

A
annie_wangli 已提交
2433
- Return value
A
annie_wangli 已提交
2434 2435
  | Type     | Description               |
  | ------- | ----------------- |
A
annie_wangli 已提交
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
  | boolean | Whether the directory entry is a character special file.|

- Example
  ```js
  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
  ```


### isDirectory

isDirectory(): boolean

Checks whether a directory entry is a directory.

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

A
annie_wangli 已提交
2452
- Return value
A
annie_wangli 已提交
2453 2454
  | Type     | Description           |
  | ------- | ------------- |
A
annie_wangli 已提交
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
  | boolean | Whether the directory entry is a directory.|

- Example
  ```js
  let isDirectory = fileio.statSync(path).isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

Checks whether the current directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication.

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

A
annie_wangli 已提交
2471
- Return value
A
annie_wangli 已提交
2472 2473
  | Type     | Description                   |
  | ------- | --------------------- |
A
annie_wangli 已提交
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
  | boolean | Whether the directory entry is a FIFO.|

- Example
  ```js
  let isFIFO = fileio.statSync(path).isFIFO(); 
  ```


### isFile

isFile(): boolean

Checks whether a directory entry is a regular file.

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

A
annie_wangli 已提交
2490
- Return value
A
annie_wangli 已提交
2491 2492
  | Type     | Description             |
  | ------- | --------------- |
A
annie_wangli 已提交
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
  | boolean | Whether the directory entry is a regular file.|

- Example
  ```js
  let isFile = fileio.statSync(fpath).isFile();
  ```


### isSocket

isSocket(): boolean

Checks whether a directory entry is a socket.

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

A
annie_wangli 已提交
2509
- Return value
A
annie_wangli 已提交
2510 2511
  | Type     | Description            |
  | ------- | -------------- |
A
annie_wangli 已提交
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525
  | boolean | Whether the directory entry is a socket.|

- Example
  ```js
  let isSocket = fileio.statSync(path).isSocket(); 
  ```


### isSymbolicLink

isSymbolicLink(): boolean

Checks whether a directory entry is a symbolic link.

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

A
annie_wangli 已提交
2528
- Return value
A
annie_wangli 已提交
2529 2530
  | Type     | Description             |
  | ------- | --------------- |
A
annie_wangli 已提交
2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
  | boolean | Whether the directory entry is a symbolic link.|

- Example
  ```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 已提交
2546
stop(): Promise&lt;void&gt;
A
annie_wangli 已提交
2547 2548 2549

Asynchronously stops **watcher**. This method uses a promise to return the result.

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

A
annie_wangli 已提交
2552 2553 2554 2555 2556 2557 2558 2559
- Example
  ```js
  fileio.stop();
  ```


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

A
annie_wangli 已提交
2560
stop(callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2561 2562 2563

Asynchronously stops **watcher**. This method uses a callback to return the result.

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

A
annie_wangli 已提交
2566
- Parameters
A
annie_wangli 已提交
2567 2568 2569
  | Name     | Type                       | Mandatory  | Description                    |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when **watcher** is stopped asynchronously.|
A
annie_wangli 已提交
2570 2571 2572 2573 2574 2575 2576

- Example
  ```js
  fileio.stop(function(err){
      // Do something.
  });
  ```
A
annie_wangli 已提交
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137


## Stream<sup>7+</sup>

File stream. Before calling a method of the **Stream** class, use the **createStream()** method synchronously or asynchronously to create a **Stream** instance.


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

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

Asynchronously closes the stream. This method uses a promise to return the result.

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

- Return value
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream close result.|

- Example
  ```js
  let ss= fileio.createStreamSync(path);
  ss.close().then(function(){
      console.info("close fileStream successfully");
  }).catch(function(err){
      console.info("close fileStream  failed with error:"+ err);
  });
  ```


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

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

Asynchronously closes the stream. This method uses a callback to return the result.

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

- Parameters
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|

- Example
  ```js
  let ss= fileio.createStreamSync(path);
  ss.close(function (err) {
      // do something
  });
  ```


### closeSync

closeSync(): void

Synchronously closes the stream.

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

- Example
  ```js
  let ss= fileio.createStreamSync(path);
  ss.closeSync();
  ```


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

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

Asynchronously flushes the stream. This method uses a promise to return the result.

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

- Return value
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream flushing result.|

- Example
  ```js
  let ss= fileio.createStreamSync(path);
  ss.flush().then(function (){
      console.info("flush successfully");
  }).catch(function(err){
      console.info("flush failed with error:"+ err);
  });
  ```


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

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

Asynchronously flushes the stream. This method uses a callback to return the result.

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

- Parameters
  | Name     | Type                       | Mandatory  | Description            |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is asynchronously flushed.|

- Example
  ```js
  let ss= fileio.createStreamSync(path);
  ss.flush(function (err) {
      // do something
  });
  ```


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

flushSync(): void

Synchronously flushes the stream.

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

- Example
  ```js
  let ss= fileio.createStreamSync(path);
  ss.flushSync();
  ```


### write<sup>7+</sup>

write(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;

Asynchronously writes data into the stream. This method uses a promise to return the result.

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

- Parameters
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
  | 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.|

- Return value
  | Type                   | Description      |
  | --------------------- | -------- |
  | Promise&lt;number&gt; | Length of the data written in the file.|

- Example
  ```js
  let ss= fileio.createStreamSync(fpath, "r+");
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
      console.info("write successfully:"+ number);
  }).catch(function(err){
      console.info("write failed with error:"+ err);
  });
  ```


### write<sup>7+</sup>

write(buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void

Asynchronously writes data into the stream. This method uses a callback to return the result.

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

- Parameters
  | Name     | Type                             | Mandatory  | Description                                      |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
  | 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.|
  | callback | AsyncCallback&lt;number&gt;     | Yes   | Callback invoked when the data is written asynchronously.                         |

- Example
  ```js
  let ss= fileio.createStreamSync(fpath, "r+");
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
      if (!err) {
         // do something
         console.log(bytesWritten);
      }
  });
  ```


### 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

- Parameters
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
  | 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.|

- Return value
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data written in the file.|

- Example
  ```js
  let ss= fileio.createStreamSync(fpath,"r+");
  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;

Asynchronously reads data from the stream. This method uses a promise to return the result.

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

- Parameters
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file 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.|

- Return value
  | Type                                | Description    |
  | ---------------------------------- | ------ |
  | Promise&lt;[ReadOut](#readout)&gt; | Data read.|

- Example
  ```js
  let ss = fileio.createStreamSync(fpath, "r+");
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readout){
      console.info("read data successfully");
  }).catch(function(err){
      console.info("read data failed with error:"+ err);
  });
  ```


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

read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void

Asynchronously reads data from the stream. This method uses a callback to return the result.

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

- Parameters
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file 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.|
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when data is read asynchronously from the stream.                        |

- Example
  ```js
  let ss = fileio.createStreamSync(fpath, "r+");
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
      if (!err) {
          // do something
      }
  });
  ```


### 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

- Parameters
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file 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.|

- Return value
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data read.|

- Example
  ```js
  let ss = fileio.createStreamSync(fpath, "r+");
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
  ```


## Dir

Manages directories. Before calling a method of the **Dir** class, use the [opendir()](#fileioopendir) method synchronously or asynchronously to create a **Dir** instance.


### read

read(): Promise&lt;Dirent&gt;

Asynchronously reads the next directory entry. This method uses a promise to return the result.

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

- Return value
  | Type                              | Description           |
  | -------------------------------- | ------------- |
  | Promise&lt;[Dirent](#dirent)&gt; | Directory entry that is read asynchronously.|

- Example
  ```js
  let dir = fileio.opendirSync(path);
  dir.read().then(function (dirent){
      console.info("read successfully:"+ dirent.name);
  }).catch(function(err){
      console.info("read failed with error:"+ err);
  });
  ```


### read

read(callback: AsyncCallback&lt;Dirent&gt;): void

Asynchronously reads the next directory entry. This method uses a callback to return the result.

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

- Parameters
  | Name     | Type                                    | Mandatory  | Description              |
  | -------- | -------------------------------------- | ---- | ---------------- |
  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | Yes   | Callback invoked when the next directory entry is asynchronously read.|

- Example
  ```js
  let dir = fileio.opendirSync(path);
  dir.read(function (err, dirent) {
      if (!err) {
          // do something
          console.log(dirent.name)
      }
  });
  ```


### readSync

readSync(): Dirent

Synchronously reads the next directory entry.

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

- Return value
  | Type               | Description      |
  | ----------------- | -------- |
  | [Dirent](#dirent) | Directory entry read.|

- Example
  ```js
  let dir = fileio.opendirSync(path);
  let dirent = dir.readSync();
  ```


### 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

- Example
  ```js
  let dir = fileio.opendirSync(path);
  dir.closeSync();
  ```


## Dirent

Provides information about files and directories. Before calling a method of the **Dirent** class, use the [dir.read()](#read) method synchronously or asynchronously to create a **Dirent** instance.

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

### Attributes

| Name  | Type  | Readable  | Writable  | Description     |
| ---- | ------ | ---- | ---- | ------- |
| name | string | Yes   | No   | Directory entry name.|


### isBlockDevice

isBlockDevice(): boolean

Checks whether the current directory entry is a block special file. A block special file supports access by block only, and it is cached when accessed.

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

- Return value
  | Type     | Description              |
  | ------- | ---------------- |
  | boolean | Whether the directory entry is a block special file.|

- Example
  ```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

- Return value
  | Type     | Description               |
  | ------- | ----------------- |
  | boolean | Whether the directory entry is a character special file.|

- Example
  ```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

- Return value
  | Type     | Description           |
  | ------- | ------------- |
  | boolean | Whether the directory entry is a directory.|

- Example
  ```js
  let dir = fileio.opendirSync(path);
  let isDirectory = dir.readSync().isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

Checks whether the current directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication.

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

- Return value
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a FIFO.|

- Example
  ```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

- Return value
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a regular file.|

- Example
  ```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

- Return value
  | Type     | Description            |
  | ------- | -------------- |
  | boolean | Whether the directory entry is a socket.|

- Example
  ```js
  let dir = fileio.opendirSync(dpath);
  let isSocket = dir.readSync().isSocket(); 
  ```


### isSymbolicLink

isSymbolicLink(): boolean

Checks whether a directory entry is a symbolic link.

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

- Return value
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a symbolic link.|

- Example
  ```js
  let dir = fileio.opendirSync(path);
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```