js-apis-fileio.md 131.4 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

- Example
  ```js
  fileio.mkdir(path, function(err) {
A
Annie_wang 已提交
506
    console.info("mkdir successfully");
A
annie_wangli 已提交
507 508 509 510 511 512
  });
  ```


## fileio.mkdirSync

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

Synchronously creates a directory.

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

A
annie_wangli 已提交
519
- Parameters
A
annie_wangli 已提交
520 521 522 523
  | 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 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536

- 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 已提交
537 538
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
539
- Parameters
A
annie_wangli 已提交
540 541 542 543 544
  | 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 已提交
545 546

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

- 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 已提交
567 568
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
569
- Parameters
A
annie_wangli 已提交
570 571 572 573 574 575
  | 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 已提交
576 577 578 579 580 581 582 583 584 585 586 587

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


## fileio.openSync

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

Synchronously opens a file.

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

A
annie_wangli 已提交
593
- Parameters
A
annie_wangli 已提交
594 595 596 597 598
  | 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 已提交
599 600

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

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


## fileio.read

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

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

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

A
annie_wangli 已提交
623
- Parameters
A
annie_wangli 已提交
624 625 626 627
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | fd      | number      | Yes   | File descriptor of the file to read.                            |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file data read.                       |
A
Annie_wang 已提交
628
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size. |
A
annie_wangli 已提交
629 630

- Return value
A
annie_wangli 已提交
631 632
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
annie_wangli 已提交
633
  | Promise&lt;[ReadOut](#readout)&gt; | Data read.|
A
annie_wangli 已提交
634 635 636 637 638 639

- Example
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
  fileio.read(fd, buf).then(function(readout){
A
Annie_wang 已提交
640 641
      console.info("read file data successfully");
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
642 643 644 645 646 647 648 649
  }).catch(function(error){
      console.info("read file data failed with error:"+ error);
  });
  ```


## fileio.read

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

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

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

A
annie_wangli 已提交
660
- Parameters
A
annie_wangli 已提交
661 662 663 664
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                                   | Yes   | File descriptor of the file to read.                            |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file data read.                       |
A
Annie_wang 已提交
665
  | options  | Object                                   | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size. |
A
annie_wangli 已提交
666
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously.                            |
A
annie_wangli 已提交
667 668 669 670 671 672

- Example
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
  fileio.read(fd, buf, function (err, readOut) {
A
Annie_wang 已提交
673 674 675
      if (readOut) {
          console.info("read file data successfully");
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
676 677 678 679 680 681 682
      }
  });
  ```


## 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
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | fd      | number      | Yes   | File descriptor of the file to read.                            |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file data read.                       |
A
Annie_wang 已提交
698
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size. |
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

- Example
  ```js
  fileio.rmdir(path, function(err){
      // Do something.
A
Annie_wang 已提交
759
      console.info("rmdir successfully");
A
annie_wangli 已提交
760 761 762 763
  });
  ```


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

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

Synchronously deletes a directory.

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

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

- 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 已提交
789 790
**System capability**: SystemCapability.FileManagement.File.FileIO

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

- Return value
A
annie_wangli 已提交
797 798
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
  | 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 已提交
817 818
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
819
- Parameters
A
annie_wangli 已提交
820 821 822 823
  | 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 已提交
824 825 826 827

- Example
  ```js
  fileio.unlink(path, function(err) {
A
Annie_wang 已提交
828
      console.info("remove file successfully");
A
annie_wangli 已提交
829 830 831 832 833 834 835
  });
  ```


## fileio.unlinkSync

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

Synchronously deletes a file.

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

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

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


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

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

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

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

A
annie_wangli 已提交
865
- Parameters
A
annie_wangli 已提交
866 867 868 869
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
870
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size.|
Z
zengyawen 已提交
871

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

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


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

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

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

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

A
annie_wangli 已提交
901
- Parameters
A
annie_wangli 已提交
902 903 904 905
  | Name     | Type                             | Mandatory  | Description                                      |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
906
  | options  | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size.|
A
annie_wangli 已提交
907
  | callback | AsyncCallback&lt;number&gt;     | Yes   | Callback invoked when the data is written asynchronously.                      |
Z
zengyawen 已提交
908

A
annie_wangli 已提交
909 910 911 912
- Example
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
A
Annie_wang 已提交
913 914
      if (bytesWritten) {
         console.info("write data to file successfully and size is:"+ bytesWritten);
A
annie_wangli 已提交
915 916 917
      }
  });
  ```
Z
zengyawen 已提交
918 919


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

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

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

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

A
annie_wangli 已提交
933
- Parameters
A
annie_wangli 已提交
934 935 936 937
  | Name    | Type                             | Mandatory  | Description                                      |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
938
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size.|
Z
zengyawen 已提交
939

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

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


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

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

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

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

A
annie_wangli 已提交
960
- Parameters
A
annie_wangli 已提交
961 962 963
  | Name      | Type    | Mandatory  | Description                                      |
  | --------- | ------ | ---- | ---------------------------------------- |
  | path      | string | Yes   | Absolute path of the target file.                          |
A
Annie_wang 已提交
964
  | algorithm | string | Yes   | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**.<br/>**sha256** is recommended for security purposes.|
Z
zengyawen 已提交
965

A
annie_wangli 已提交
966
- Return value
A
annie_wangli 已提交
967 968
  | Type                   | Description                        |
  | --------------------- | -------------------------- |
A
annie_wangli 已提交
969
  | 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 已提交
970

A
annie_wangli 已提交
971 972 973 974 975 976 977 978
- 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 已提交
979 980


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

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

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

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

A
annie_wangli 已提交
989
- Parameters
A
annie_wangli 已提交
990 991 992
  | Name      | Type                         | Mandatory  | Description                                      |
  | --------- | --------------------------- | ---- | ---------------------------------------- |
  | path      | string                      | Yes   | Absolute path of the target file.                          |
A
Annie_wang 已提交
993
  | algorithm | string                      | Yes   | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**.<br/>**sha256** is recommended for security purposes.|
A
annie_wangli 已提交
994
  | 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 已提交
995

A
annie_wangli 已提交
996 997 998
- Example
  ```js
  fileio.hash(fpath, "sha256", function(err, hashStr) {
A
Annie_wang 已提交
999 1000
      if (hashStr) {
          console.info("calculate file hash successfully:"+ hashStr);
A
annie_wangli 已提交
1001 1002 1003
      }
  });
  ```
Z
zengyawen 已提交
1004 1005


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

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

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

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

A
annie_wangli 已提交
1014
- Parameters
A
annie_wangli 已提交
1015 1016 1017 1018
  | 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 已提交
1019

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

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


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

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

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

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

A
annie_wangli 已提交
1043
- Parameters
A
annie_wangli 已提交
1044 1045 1046 1047 1048
  | 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 已提交
1049

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


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

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

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

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

A
annie_wangli 已提交
1066
- Parameters
A
annie_wangli 已提交
1067 1068 1069 1070
  | 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 已提交
1071

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


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

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

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

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

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

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

A
annie_wangli 已提交
1096 1097 1098 1099 1100 1101 1102 1103
- 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 已提交
1104 1105


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

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

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

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

A
annie_wangli 已提交
1114
- Parameters
A
annie_wangli 已提交
1115 1116 1117 1118
  | 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 已提交
1119

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


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

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

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

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

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

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

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


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

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

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

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

A
annie_wangli 已提交
1162
- Parameters
A
annie_wangli 已提交
1163 1164 1165 1166
  | 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 已提交
1167

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

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


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

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

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

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

A
annie_wangli 已提交
1192
- Parameters
A
annie_wangli 已提交
1193 1194 1195 1196 1197
  | 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 已提交
1198

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


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

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

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

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

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

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


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

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

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

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

A
annie_wangli 已提交
1235
- Parameters
A
annie_wangli 已提交
1236 1237 1238 1239
  | 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 已提交
1240

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

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


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

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

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

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

A
annie_wangli 已提交
1264
- Parameters
A
annie_wangli 已提交
1265 1266 1267 1268 1269
  | 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 已提交
1270

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


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

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

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

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

A
annie_wangli 已提交
1287
- Parameters
A
annie_wangli 已提交
1288 1289 1290 1291
  | 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 已提交
1292

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


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

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

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

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

A
annie_wangli 已提交
1311
- Parameters
A
annie_wangli 已提交
1312 1313 1314 1315
  | 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 已提交
1316

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

A
annie_wangli 已提交
1322 1323 1324 1325 1326 1327 1328 1329
- 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 已提交
1330 1331


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

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

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

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

A
annie_wangli 已提交
1344
- Parameters
A
annie_wangli 已提交
1345 1346 1347 1348 1349
  | 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 已提交
1350

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


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

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

Synchronously reads the text of a file. 

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

A
annie_wangli 已提交
1371
- Parameters
A
annie_wangli 已提交
1372 1373 1374 1375
  | 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 已提交
1376 1377

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

- 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 已提交
1394 1395
**System capability**: SystemCapability.FileManagement.File.FileIO

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

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

- 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 已提交
1422 1423
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1424
- Parameters
A
annie_wangli 已提交
1425 1426 1427 1428
  | 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 已提交
1429 1430 1431 1432 1433

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


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

lstatSync(path:string): Stat

Synchronously obtains link status information.

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

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

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

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


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

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

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

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

A
annie_wangli 已提交
1474
- Parameters
A
Annie_wang 已提交
1475 1476 1477 1478
  | 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.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size.|
A
annie_wangli 已提交
1479 1480

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

A
annie_wangli 已提交
1485 1486 1487
- Example
  ```js
  fileio.read(new ArrayBuffer(4096)).then(function(readout){
A
Annie_wang 已提交
1488 1489
      console.info("read file data successfully");
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
1490 1491 1492 1493
  }).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
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file data read.                       |
A
Annie_wang 已提交
1512
  | 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;The sum of **offset** and **length** must be less than or equal to the buffer size.|
A
annie_wangli 已提交
1513
  | 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

- Example
  ```js
  let buf = new ArrayBuffer(4096);
  fileio.read(buf, function (err, readOut) {
A
Annie_wang 已提交
1519 1520 1521
      if (readOut) {
          console.info("read file data successfully");
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
      }
  });
  ```


## 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 已提交
1533 1534
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1535
- Parameters
A
annie_wangli 已提交
1536 1537 1538 1539
  | 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 已提交
1540 1541

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

- Example
  ```js
A
annie_wangli 已提交
1548
  fileio.rename(oldPath, newPath).then(function() {
A
annie_wangli 已提交
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
      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 已提交
1562 1563
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1564
- Parameters
A
annie_wangli 已提交
1565 1566 1567 1568 1569
  | 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 已提交
1570 1571 1572

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


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

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

Synchronously renames a file.

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

A
annie_wangli 已提交
1586
- Parameters
A
annie_wangli 已提交
1587 1588 1589 1590
  | 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 已提交
1591 1592 1593

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


## 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 已提交
1604 1605
**System capability**: SystemCapability.FileManagement.File.FileIO

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

- Return value
A
annie_wangli 已提交
1612 1613
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
  | 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 已提交
1632 1633
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1634
- Parameters
A
annie_wangli 已提交
1635 1636 1637 1638
  | 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 已提交
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651

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


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

fsyncSync(fd: number): void

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

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

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

- 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 已提交
1673 1674
**System capability**: SystemCapability.FileManagement.File.FileIO

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

- Return value
A
annie_wangli 已提交
1681 1682
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
  | 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 已提交
1701 1702
**System capability**: SystemCapability.FileManagement.File.FileIO

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

- 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 已提交
1723 1724
**System capability**: SystemCapability.FileManagement.File.FileIO

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

- 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 已提交
1742 1743
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1744
- Parameters
A
annie_wangli 已提交
1745 1746 1747 1748
  | 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 已提交
1749 1750

- Return value
A
annie_wangli 已提交
1751 1752
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
  | 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 已提交
1771 1772
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1773
- Parameters
A
annie_wangli 已提交
1774 1775 1776 1777 1778
  | 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 已提交
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793

- 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 已提交
1794 1795
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1796
- Parameters
A
annie_wangli 已提交
1797 1798 1799 1800
  | 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 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813

- 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 已提交
1814 1815
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1816
- Parameters
A
annie_wangli 已提交
1817 1818 1819 1820 1821
  | 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 已提交
1822 1823

- Return value
A
annie_wangli 已提交
1824 1825
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1826 1827 1828 1829 1830
  | 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 已提交
1831
  fileio.chown(path, stat.uid, stat.gid).then(function(){
A
annie_wangli 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
      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 已提交
1845 1846
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1847
- Parameters
A
annie_wangli 已提交
1848 1849 1850 1851 1852 1853
  | 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 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869

- 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 已提交
1870 1871
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
1872
- Parameters
A
annie_wangli 已提交
1873 1874 1875 1876 1877
  | 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 已提交
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891

- 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 已提交
1892 1893
**System capability**: SystemCapability.FileManagement.File.FileIO

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

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

- 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 已提交
1920 1921
**System capability**: SystemCapability.FileManagement.File.FileIO

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

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

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

- Return value
A
annie_wangli 已提交
1950 1951
  | Name   | Description        |
  | ------ | ---------- |
A
annie_wangli 已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
  | 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 已提交
1966 1967
**System capability**: SystemCapability.FileManagement.File.FileIO

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

- Return value
A
annie_wangli 已提交
1975 1976
  | Name                | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
  | 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 已提交
1995 1996
**System capability**: SystemCapability.FileManagement.File.FileIO

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

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


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

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

Synchronously changes the file permissions based on the file descriptor.

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

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

- 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 已提交
2038 2039
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2040
- Parameters
A
annie_wangli 已提交
2041 2042 2043 2044
  | 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 已提交
2045 2046

- Return value
A
annie_wangli 已提交
2047 2048
  | Type                               | Description       |
  | --------------------------------- | --------- |
A
annie_wangli 已提交
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
  | 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 已提交
2067 2068
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2069
- Parameters
A
annie_wangli 已提交
2070 2071 2072 2073 2074
  | 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 已提交
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089

- 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 已提交
2090 2091
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2092
- Parameters
A
annie_wangli 已提交
2093 2094 2095 2096
  | 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 已提交
2097 2098

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

- 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 已提交
2115 2116
**System capability**: SystemCapability.FileManagement.File.FileIO

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

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

- Example
  ```js
  fileio.fdopenStream(fd, mode).then(function(stream){
A
annie_wangli 已提交
2131
      console.info("openStream successfully");
A
annie_wangli 已提交
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
  }).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 已提交
2144 2145
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2146
- Parameters
A
annie_wangli 已提交
2147 2148 2149 2150 2151
  | 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 已提交
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166

- 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 已提交
2167 2168
**System capability**: SystemCapability.FileManagement.File.FileIO

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

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

- 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 已提交
2192 2193
**System capability**: SystemCapability.FileManagement.File.FileIO

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

- Return value
A
annie_wangli 已提交
2202 2203
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
  | 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 已提交
2223 2224
**System capability**: SystemCapability.FileManagement.File.FileIO

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

- 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 已提交
2248 2249
**System capability**: SystemCapability.FileManagement.File.FileIO

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

- 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 已提交
2270 2271
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2272
- Parameters
A
annie_wangli 已提交
2273 2274 2275 2276 2277
  | 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 已提交
2278 2279

- Return value
A
annie_wangli 已提交
2280 2281
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
annie_wangli 已提交
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
  | 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 已提交
2301 2302
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2303
- Parameters
A
annie_wangli 已提交
2304 2305 2306 2307 2308 2309
  | 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 已提交
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325

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

A
annie_wangli 已提交
2328
- Parameters
A
annie_wangli 已提交
2329 2330 2331 2332 2333
  | 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 已提交
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347

- 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 已提交
2348 2349
**System capability**: SystemCapability.FileManagement.File.FileIO

A
annie_wangli 已提交
2350
- Parameters
A
annie_wangli 已提交
2351 2352 2353 2354 2355
  | 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 已提交
2356 2357

- Return value
A
annie_wangli 已提交
2358 2359
  | Name                 | Description        |
  | -------------------- | ---------- |
A
annie_wangli 已提交
2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
  | [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 已提交
2374 2375
**System capability**: SystemCapability.FileManagement.File.FileIO

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


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

### Attributes

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


### isBlockDevice

isBlockDevice(): boolean

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

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

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

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


### isCharacterDevice

isCharacterDevice(): boolean

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

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

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

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


### isDirectory

isDirectory(): boolean

A
Annie_wang 已提交
2449
Checks whether this file is a directory.
A
annie_wangli 已提交
2450

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

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

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


### isFIFO

isFIFO(): boolean

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

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

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

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


### isFile

isFile(): boolean

A
Annie_wang 已提交
2487
Checks whether this file is a regular file.
A
annie_wangli 已提交
2488

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

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

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


### isSocket

isSocket(): boolean

A
Annie_wang 已提交
2506
Checks whether this file is a socket.
A
annie_wangli 已提交
2507

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

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

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


### isSymbolicLink

isSymbolicLink(): boolean

A
Annie_wang 已提交
2525
Checks whether this file is a symbolic link.
A
annie_wangli 已提交
2526

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

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

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

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

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

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


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

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

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

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

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

- Example
  ```js
  fileio.stop(function(err){
      // Do something.
  });
  ```
A
annie_wangli 已提交
2578 2579


A
Annie_wang 已提交
2580

A
annie_wangli 已提交
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
## 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.                    |
A
Annie_wang 已提交
2725
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size. |
A
annie_wangli 已提交
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735

- 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){
A
Annie_wang 已提交
2736
      console.info("write successfully and size is:"+ number);
A
annie_wangli 已提交
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
  }).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
A
Annie_wang 已提交
2757 2758 2759 2760 2761
  | 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.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size.|
  | callback | AsyncCallback&lt;number&gt;     | Yes  | Callback invoked when the data is written asynchronously.                              |
A
annie_wangli 已提交
2762 2763 2764 2765 2766

- Example
  ```js
  let ss= fileio.createStreamSync(fpath, "r+");
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
A
Annie_wang 已提交
2767
      if (bytesWritten) {
A
annie_wangli 已提交
2768
         // do something
A
Annie_wang 已提交
2769
         console.info("write successfully and size is:"+ bytesWritten);
A
annie_wangli 已提交
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
      }
  });
  ```


### 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.                    |
A
Annie_wang 已提交
2792
  | options | Object                          | No   | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size. |
A
annie_wangli 已提交
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

- 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.                             |
A
Annie_wang 已提交
2822
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size. |
A
annie_wangli 已提交
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833

- 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");
A
Annie_wang 已提交
2834
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
  }).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.                             |
A
Annie_wang 已提交
2857
  | options  | Object                                   | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size. |
A
annie_wangli 已提交
2858 2859 2860 2861 2862 2863
  | 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) {
A
Annie_wang 已提交
2864 2865 2866
      if (readOut) {
          console.info("read data successfully");
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
      }
  });
  ```


### 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.                             |
A
Annie_wang 已提交
2888
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- &nbsp;The sum of **offset** and **length** must be less than or equal to the buffer size. |
A
annie_wangli 已提交
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

- 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){
A
Annie_wang 已提交
2924
      console.log("read successfully:"+JSON.stringify(dirent));
A
annie_wangli 已提交
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947
  }).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) {
A
Annie_wang 已提交
2948
      if (dirent) {
A
annie_wangli 已提交
2949
          // do something
A
Annie_wang 已提交
2950
          console.log("read successfully:"+JSON.stringify(dirent));
A
annie_wangli 已提交
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 3138 3139 3140 3141
      }
  });
  ```


### 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();
  ```