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

A
annie_wangli 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> 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
## Note
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
## fileio.stat

stat(path: string): Promise<Stat>

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise<[Stat](#stat)> | 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<Stat>): void

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
  | callback | AsyncCallback<[Stat](#stat)> | Yes| Callback invoked to return the file information obtained.|

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


## fileio.statSync

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

Synchronously obtains file information.

A
annie_wangli 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|


- Return value
  | Type| Description|
  | -------- | -------- |
  | [Stat](#stat) | File information obtained.|

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


## fileio.opendir

opendir(path: string): Promise<Dir>

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the directory to open.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise<[Dir](#dir)> | 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<Dir>): void

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the directory to open.|
  | callback | AsyncCallback<[Dir](#dir)> | Yes| Callback invoked when the directory is open asynchronously.|

- Example
  ```js
  fileio.opendir(path, function (err, dir) { 
      // Example code in Dir struct
      // Use read/readSync/close
  });
  ```


## fileio.opendirSync

opendirSync(path: string): Dir
Z
zengyawen 已提交
159 160 161

Synchronously opens a directory.

A
annie_wangli 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the directory to open.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [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<void>

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
A
annie_wangli 已提交
191
  | 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 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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

access(path: String, mode?: number, callback: AsyncCallback&lt;void&gt;): void

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
A
annie_wangli 已提交
218
  | 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 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked when the file is asynchronously checked.|

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


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
A
annie_wangli 已提交
240
  | 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 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to close.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | 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.|

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


## fileio.closeSync

closeSync(fd: number): void
Z
zengyawen 已提交
303 304 305

Synchronously closes a file.

A
annie_wangli 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to close.|

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

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

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

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

- Parameters
  | 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.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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

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

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

- Parameters
  | 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.|

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


## fileio.copyFileSync

fileio.copyFileSync(src:string | number, dest:string | number, mode?:number): void
Z
zengyawen 已提交
410 411 412

Synchronously copies a file.

A
annie_wangli 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
- Parameters
  | 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.|

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


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the directory to create.|
A
annie_wangli 已提交
437
  | 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 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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

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

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the directory to create.|
A
annie_wangli 已提交
464
  | 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 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked when the directory is created asynchronously.|

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


## fileio.mkdirSync

fileio.mkdirSync(path: string, mode?: number): void
Z
zengyawen 已提交
480 481 482

Synchronously creates a directory.

A
annie_wangli 已提交
483 484 485 486
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the directory to create.|
A
annie_wangli 已提交
487
  | 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 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
A
annie_wangli 已提交
505 506
  | flags | number | No| Option for opening a file. You must specify one of the following options. By default, the file is opened 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/>You can also specify the following options, separated using a bitwise OR operator (&#124;). By default, no extra 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/>-&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 已提交
507 508 509 510

- Return value
  | Type| Description|
  | -------- | -------- |
A
annie_wangli 已提交
511
  | Promise&lt;number&gt; | Promise used to return the file descriptor of the file opened.|
A
annie_wangli 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
A
annie_wangli 已提交
533 534
  | flags | number | Yes| Option for opening a file. You must specify one of the following options. By default, the file is opened 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/>You can also specify the following options, separated using a bitwise OR operator (&#124;). By default, no extra 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/>-&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.|
A
annie_wangli 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547
  | callback | AsyncCallback&nbsp;&lt;void&gt; | Yes| Callback invoked when the file is open asynchronously.|

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


## fileio.openSync

openSync(path:string, flags?:number, mode?:number): number
Z
zengyawen 已提交
548 549 550

Synchronously opens a file.

A
annie_wangli 已提交
551 552 553 554
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
A
annie_wangli 已提交
555 556
  | flags | number | No| Option for opening a file. You must specify one of the following options. By default, the file is opened 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/>You can also specify the following options, separated using a bitwise OR operator (&#124;). By default, no extra 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/>-&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 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

- Return value
  | Type| Description|
  | -------- | -------- |
  | number | File descriptor of the file opened.|

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


## fileio.read

read(fd: number, buffer: ArrayBuffer, options?: Object): Promise&lt;Readout&gt;

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to read.|
  | buffer | ArrayBuffer | Yes| Buffer used to store the file data read.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to be read. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): position of the data to be read in the file. By default, data is read from the current position.|

- Return value
  | Type| Description|
  | -------- | -------- |
A
annie_wangli 已提交
585
  | Promise&lt;[Readout](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

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


## fileio.read

read(fd: number, buffer: ArrayBuffer, options?: Object, callback: AsyncCallback&lt;Readout&gt;): void

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to read.|
  | buffer | ArrayBuffer | Yes| Buffer used to store the file data read.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to be read. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): position of the data to be read in the file. By default, data is read from the current position.|
  | callback | AsyncCallback&lt;[Readout](#readout)&gt; | Yes| Callback invoked when the data is read asynchronously.|

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


## fileio.readSync

readSync(fd: number, buffer: ArrayBuffer, options?: Object): number
Z
zengyawen 已提交
628 629 630

Synchronously reads data from a file.

A
annie_wangli 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to read.|
  | buffer | ArrayBuffer | Yes| Buffer used to store the file data read.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to be read. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): position of the data to be read in the file. By default, data is read from the current position.|

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

- Example
  ```js
  let 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.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the directory to delete.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | 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.|

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


## fileio.rmdirSync

rmdirSync(path:string)

Synchronously deletes a directory.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the directory to delete.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the file to delete.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | 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.|

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


## fileio.unlinkSync

unlinkSync(path: string): void
Z
zengyawen 已提交
765 766 767

Synchronously deletes a file.

A
annie_wangli 已提交
768 769 770 771
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the file to delete.|
Z
zengyawen 已提交
772

A
annie_wangli 已提交
773 774 775 776
- Example
  ```js
  fileio.unlinkSync(path);
  ```
Z
zengyawen 已提交
777 778


A
annie_wangli 已提交
779
## fileio.write
Z
zengyawen 已提交
780

A
annie_wangli 已提交
781
write(fd: number, buffer: ArrayBuffer | string, options?: Object): Promise&lt;number&gt;
Z
zengyawen 已提交
782

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

A
annie_wangli 已提交
785 786 787 788 789 790
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to write.|
  | buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes| Data to write. It can be a string or data from a buffer.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position. <br/>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.|
Z
zengyawen 已提交
791

A
annie_wangli 已提交
792 793 794
- Return value
  | Type| Description|
  | -------- | -------- |
A
annie_wangli 已提交
795
  | Promise&lt;number&gt; | Promise used to return the length of the data written in the file.|
Z
zengyawen 已提交
796

A
annie_wangli 已提交
797 798 799 800 801 802 803 804 805
- Example
  ```js
  let fd = fileio.openSync(fpath, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world").then(function(number){
       console.info("write data to file successfully:"+ number);
  }).catch(function(err){
      console.info("write data to file failed with error:"+ err);
  });
  ```
Z
zengyawen 已提交
806 807


A
annie_wangli 已提交
808
## fileio.write
Z
zengyawen 已提交
809

A
annie_wangli 已提交
810
write(fd:number, buffer:ArrayBuffer | string,options?:Object, callback:AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
811

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

A
annie_wangli 已提交
814 815 816 817 818 819 820
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to write.|
  | buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes| Data to write. It can be a string or data from a buffer.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position. <br/>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.|
  | callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked when the data is written asynchronously.|
Z
zengyawen 已提交
821

A
annie_wangli 已提交
822 823 824 825 826 827 828 829 830
- Example
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
      if (!err) {
         console.log(bytesWritten)
      }
  });
  ```
Z
zengyawen 已提交
831 832


A
annie_wangli 已提交
833
## fileio.writeSync
Z
zengyawen 已提交
834

A
annie_wangli 已提交
835
writeSync(fd: number, buffer: ArrayBuffer | string, options?:Object): number
Z
zengyawen 已提交
836

A
annie_wangli 已提交
837
Synchronously writes data into a file.
Z
zengyawen 已提交
838

A
annie_wangli 已提交
839 840 841 842 843 844
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to write.|
  | buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes| Data to write. It can be a string or data from a buffer.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position. <br/>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.|
Z
zengyawen 已提交
845

A
annie_wangli 已提交
846 847 848 849
- Return value
  | Type| Description|
  | -------- | -------- |
  | number | Length of the data written in the file.|
Z
zengyawen 已提交
850

A
annie_wangli 已提交
851 852 853 854 855
- Example
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  let num = fileio.writeSync(fd, "hello, world");
  ```
Z
zengyawen 已提交
856 857


A
annie_wangli 已提交
858
## fileio.hash
Z
zengyawen 已提交
859

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

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

A
annie_wangli 已提交
864 865 866 867 868
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
  | algorithm | string | Yes| Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**.**sha256** is recommended for security purposes.|
Z
zengyawen 已提交
869

A
annie_wangli 已提交
870 871 872 873
- Return value
  | Type| Description|
  | -------- | -------- |
  | 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 已提交
874

A
annie_wangli 已提交
875 876 877 878 879 880 881 882
- 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 已提交
883 884


A
annie_wangli 已提交
885
## fileio.hash
Z
zengyawen 已提交
886

A
annie_wangli 已提交
887
hash(psth:string, algorithm:string, callback:AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
888

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

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

A
annie_wangli 已提交
898 899 900 901 902 903 904 905
- Example
  ```js
  fileio.hash(fpath, "sha256", function(err, hashStr) {
      if (!err) {
          console.log(hashStr)
      }
  });
  ```
Z
zengyawen 已提交
906 907


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

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

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

A
annie_wangli 已提交
914 915 916 917
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
A
annie_wangli 已提交
918
  | 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 已提交
919

A
annie_wangli 已提交
920 921 922 923
- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|
Z
zengyawen 已提交
924

A
annie_wangli 已提交
925 926 927 928 929 930 931 932
- Example
  ```js
  fileio.chmod(path, mode).then(function() {
      console.info("chmod successfully");
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
  });
  ```
Z
zengyawen 已提交
933 934


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

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

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

A
annie_wangli 已提交
941 942 943 944
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
A
annie_wangli 已提交
945
  | 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 已提交
946
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked when the file permissions are changed asynchronously.|
Z
zengyawen 已提交
947

A
annie_wangli 已提交
948 949 950 951 952 953
- Example
  ```js
  fileio.chmod(path, mode, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
954 955


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

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

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

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

A
annie_wangli 已提交
968 969 970 971
- Example
  ```js
  fileio.chmodSync(fpath, mode);
  ```
Z
zengyawen 已提交
972 973


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

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

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

A
annie_wangli 已提交
980 981 982 983
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the target file.|
Z
zengyawen 已提交
984

A
annie_wangli 已提交
985 986 987 988
- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file status information.|
Z
zengyawen 已提交
989

A
annie_wangli 已提交
990 991 992 993 994 995 996 997
- 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 已提交
998 999


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

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

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

A
annie_wangli 已提交
1006 1007 1008 1009 1010
- Parameters
  | 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 已提交
1011

A
annie_wangli 已提交
1012 1013 1014 1015 1016 1017 1018
- Example
  ```js
  let fd = fileio.openSync(path);
  fileio.fstat(fd, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
1019 1020


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

A
annie_wangli 已提交
1023
fstatSync(fd: number): Stat
Z
zengyawen 已提交
1024

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

A
annie_wangli 已提交
1027 1028 1029 1030
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the target file.|
Z
zengyawen 已提交
1031

A
annie_wangli 已提交
1032 1033 1034 1035
- Return value
  | Type| Description|
  | -------- | -------- |
  | [Stat](#stat) | File status information obtained.|
Z
zengyawen 已提交
1036

A
annie_wangli 已提交
1037 1038 1039 1040 1041
- Example
  ```js
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```
Z
zengyawen 已提交
1042 1043


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

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

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

A
annie_wangli 已提交
1050 1051 1052 1053 1054
- Parameters
  | 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 已提交
1055

A
annie_wangli 已提交
1056 1057 1058 1059
- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|
Z
zengyawen 已提交
1060

A
annie_wangli 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069
- Example
  ```js
  let fd = fileio.openSync(path);
  fileio.ftruncate(fd, 5).then(function(err) {    
      console.info("File truncated successfully");
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1070 1071


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

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

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

A
annie_wangli 已提交
1078 1079 1080 1081 1082 1083
- Parameters
  | 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 已提交
1084

A
annie_wangli 已提交
1085 1086 1087 1088 1089 1090
- Example
  ```js
  fileio.ftruncate(fd, len, function(err){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1091 1092


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

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

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

A
annie_wangli 已提交
1099 1100 1101 1102 1103
- Parameters
  | 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 已提交
1104

A
annie_wangli 已提交
1105 1106 1107 1108
- Example
  ```js
  fileio.ftruncate(fd, len);
  ```
Z
zengyawen 已提交
1109 1110


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

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

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

A
annie_wangli 已提交
1117 1118 1119 1120 1121
- Parameters
  | 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 已提交
1122

A
annie_wangli 已提交
1123 1124 1125 1126
- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|
Z
zengyawen 已提交
1127

A
annie_wangli 已提交
1128 1129 1130 1131 1132 1133 1134 1135
- Example
  ```js
  fileio.truncate(path, len).then(function(){
      console.info("File truncated successfully");
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1136 1137


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

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

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

A
annie_wangli 已提交
1144 1145 1146 1147 1148 1149
- Parameters
  | 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 已提交
1150

A
annie_wangli 已提交
1151 1152 1153 1154 1155 1156
- Example
  ```js
  fileio.truncate(path, len, function(err){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1157 1158


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

A
annie_wangli 已提交
1161
truncateSync(fpath: string, len?: number): void
Z
zengyawen 已提交
1162

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

A
annie_wangli 已提交
1165 1166 1167 1168 1169
- Parameters
  | 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 已提交
1170

A
annie_wangli 已提交
1171 1172 1173 1174
- Example
  ```js
  fileio.ftruncate(path, len);
  ```
Z
zengyawen 已提交
1175 1176


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

A
annie_wangli 已提交
1179
readText(filePath: string, options?:Object): Promise&lt;string&gt;
Z
zengyawen 已提交
1180

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

A
annie_wangli 已提交
1183 1184 1185 1186 1187
- Parameters
  | 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 be 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 已提交
1188

A
annie_wangli 已提交
1189 1190 1191 1192
- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;string&gt; | Promise used to return the content of the file read.|
Z
zengyawen 已提交
1193

A
annie_wangli 已提交
1194 1195 1196 1197 1198 1199 1200 1201
- 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 已提交
1202 1203


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

A
annie_wangli 已提交
1206
readText(filePath: string, options?:Object, callback:AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1207

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

A
annie_wangli 已提交
1210 1211 1212 1213 1214 1215
- Parameters
  | 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 be 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 已提交
1216

A
annie_wangli 已提交
1217 1218 1219 1220 1221 1222
- Example
  ```js
  fileio.readText(path, function(err, str){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1223 1224


A
annie_wangli 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
## fileio.readTextSync<sup>7+</sup>

readTextSync(filePath: string, options?:Object): string

Synchronously reads the text of a file. 

- Parameters
  | 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 be 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.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | string| Content of the file read.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file, pointing to the link status.|

- Return value
  | Type| Description|
  | -------- | -------- |
A
annie_wangli 已提交
1262
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the link status.|
A
annie_wangli 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330

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

- Parameters
  | 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.|

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


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

lstatSync(path:string): Stat

Synchronously obtains link status information.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file, pointing to the link status.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [Stat](#stat) | Link status information.|

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


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

read(buffer: ArrayBuffer, options?: Object): Promise&lt;Readout&gt;

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

- Parameters
  | 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 be read. It is optional. The default value is the buffer length minus the offset.|

- Return value
  | Type| Description|
  | -------- | -------- |
A
annie_wangli 已提交
1331
  | Promise&lt;[Readout](#readout)&gt; | Promise used to return the data read.|
Z
zengyawen 已提交
1332

A
annie_wangli 已提交
1333 1334 1335 1336 1337 1338 1339 1340
- Example
  ```js
  fileio.read(new ArrayBuffer(4096)).then(function(readout){
      console.info("File data read successfully:"+ String.fromCharCode.apply(null, new Uint8Array(readout.buffer)));
  }).catch(function(err){
      console.info("Failed to read file data. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1341 1342


A
annie_wangli 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
## fileio.read<sup>7+</sup>

read(buffer: ArrayBuffer, options?: Object, callback: AsyncCallback&lt;Readout&gt;): void

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

- Parameters
  | 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 be read. It is optional. The default value is the buffer length minus the offset.|
  | callback | AsyncCallback&lt;[Readout](#readout)&gt; | Yes| Callback invoked when the data is read asynchronously from the file.|

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


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

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

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | oldPath | string | Yes| Absolute path of the file to rename.|
  | Newpath | String | Yes| Absolute path of the file renamed.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result. An empty value is returned.|

- Example
  ```js
  fileio.rename(oldPath, Newpath).then(function() {
      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.

- Parameters
  | 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.|

- Example
  ```js
  fileio.rename(oldpath, Newpath, function(err){
  });
  ```


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

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

Synchronously renames a file.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | oldPath | string | Yes| Absolute path of the file to rename.|
  | Newpath | String | Yes| Absolute path of the file renamed.|

- Example
  ```js
  fileio.renameSync(oldpath, newpath);
  ```


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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to synchronize.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | 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.|

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


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

fsyncSync(fd: number): void

Synchronizes a file in synchronous mode.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to synchronize.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to synchronize.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | 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.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the file to synchronize.|

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

- Parameters
  | 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.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | 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.|

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

- Parameters
  | 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.|

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

- Parameters
  | 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).|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result asynchronously. An empty value is returned.|

- Example
  ```js
  let stat = fileio.statSync(path);
  fileio.chown(path, stat.uid, stat.gid)).then(function(){
      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.

- Parameters
  | 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.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
  | uid | number | Yes| New UID.|
  | gid | number | Yes| New GID.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | prefix | string | Yes| A randomly generated string used to replace "XXXXXX" in a directory.|

- Return value
  | Name| Description|
  | -------- | -------- |
A
annie_wangli 已提交
1710
  | Promise&lt;string&gt; | Promise used to return the unique path generated.|
A
annie_wangli 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773

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

- Parameters
  | 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.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | prefix | string | Yes| A randomly generated string used to replace "XXXXXX" in a directory.|

- Return value
  | Name| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the target file.|
A
annie_wangli 已提交
1774
  | 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 已提交
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800

- Return value
  | Name| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the target file.|
A
annie_wangli 已提交
1801
  | 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 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
  | callback | AsyncCallback&nbsp;&lt;void&gt; | Yes| Callback invoked when the file permissions are changed asynchronously.|

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


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

fchmodSync(existingPath: string, newPath: string): void

Synchronously changes the file permissions based on the file descriptor.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the target file.|
A
annie_wangli 已提交
1822
  | 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 已提交
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458

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

- Parameters
  | 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).|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | 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.|

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

- Parameters
  | 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).|

- Return value
  | Name| Description|
  | -------- | -------- |
  | [Stream](#stream7) | Stream obtained.|

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

- Parameters
  | 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).|

- Return value
  | Name| Description|
  | -------- | -------- |
  | Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.|

- Example
  ```js
  fileio.fdopenStream(fd, mode).then(function(stream){
      console.info("openStream successfully"+);
  }).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.

- Parameters
  | 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.|

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

- Parameters
  | 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).|

- Return value
  | Name| Description|
  | -------- | -------- |
  | [Stream](#stream7) | Stream operation result.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the target file.|
  | uid | number | Yes| New UID.|
  | gid | number | Yes| New GID.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | 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.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fd | number | Yes| File descriptor of the target file.|
  | uid | number | Yes| New UID.|
  | gid | number | Yes| New GID.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
  | uid | number | Yes| New UID.|
  | gid | number | Yes| New GID.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | 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.

- Parameters
  | 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.|

- 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).

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Absolute path of the target file.|
  | uid | number | Yes| New UID.|
  | gid | number | Yes| New GID.|

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | filename | string | Yes| Absolute path of the target file.|
  | events | Number | Yes| -&nbsp;**1**:&nbsp;: 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.|

- Return value
  | Name| Description|
  | -------- | -------- |
  | [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.

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


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


### Attributes

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


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

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

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


### isCharacterDevice

isCharacterDevice(): boolean

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

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

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


### isDirectory

isDirectory(): boolean

Checks whether a directory entry is a directory.

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

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


### isFIFO

isFIFO(): boolean

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

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

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


### isFile

isFile(): boolean

Checks whether a directory entry is a regular file.

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

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


### isSocket

isSocket(): boolean

Checks whether a directory entry is a socket.

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

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


### isSymbolicLink

isSymbolicLink(): boolean

Checks whether a directory entry is a symbolic link.

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

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


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

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


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

stop(): void

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

- Example
  ```js
  fileio.stop();
  ```


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

stop(callback: AsyncCallback): void

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked when **watcher** is stopped asynchronously.|

- Example
  ```js
  fileio.stop(function(err){
      // Do something.
  });
  ```
## 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.

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

- 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<sup>7+</sup>

closeSync(): void

Synchronously closes the stream.

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

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

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

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


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

write(buffer: ArrayBuffer | string, options?: Object): Promise&lt;number&gt;

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes| Data to write. It can be a string or data from a buffer.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position. <br/>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.|

- Return value
  | Type| Description|
  | -------- | -------- |
A
annie_wangli 已提交
2459
  | Promise&lt;number&gt; | Promise used to return the length of the data written in the file.|
A
annie_wangli 已提交
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535

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


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

write(buffer:ArrayBuffer | string,options?:Object, callback:AsyncCallback&lt;number&gt;): void

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes| Data to write. It can be a string or data from a buffer.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position. <br/>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.|
  | callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked when the data is written asynchronously.|

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


### writeSync<sup>7+</sup>

writeSync(buffer: ArrayBuffer | string, options?:Object): number

Synchronously writes data into the stream.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes| Data to write. It can be a string or data from a buffer.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position. <br/>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.|

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

- Example
  ```js
  let ss= fileio.createStreamSync(fpath,"r+");
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


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

read(buffer: ArrayBuffer, options?: Object): Promise&lt;Readout&gt;

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | buffer | ArrayBuffer | Yes| Buffer used to store the file read.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): position of the data to be read in the file. By default, data is read from the current position.|

- Return value
  | Type| Description|
  | -------- | -------- |
A
annie_wangli 已提交
2536
  | Promise&lt;[Readout](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802

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


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

read(buffer: ArrayBuffer, options?: Object, callback: AsyncCallback&lt;Readout&gt;): void

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | buffer | ArrayBuffer | Yes| Buffer used to store the file read.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): position of the data to be read in the file. By default, data is read from the current position.|
  | callback | AsyncCallback&lt;[Readout](#readout)&gt; | Yes| Callback invoked when data is read asynchronously from the stream.|

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


### readSync<sup>7+</sup>

readSync(buffer: ArrayBuffer, options?: Object): number

Synchronously reads data from the stream.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | buffer | ArrayBuffer | Yes| Buffer used to store the file read.|
  | options | Object | No| The options are as follows: <br/>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**. <br/>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset. <br/>-&nbsp;**position** (number): position of the data to be read in the file. By default, data is read from the current position.|

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

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


## Dir

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


### read

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

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

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

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


### read

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

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

- 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(dpath);
  dir.read(function (err, dirent) {
      if (!err) {
          // do something
          console.log(dirent.name)
      }
  });
  ```


### readSync

readSync(): Dirent

Synchronously reads the next directory entry.

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

- Example
  ```js
  let dir = fileio.opendirSync(dpath);
  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.

- Example
  ```js
  let dir = fileio.opendirSync(dpath);
  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.


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

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

- Example
  ```js
  let dir = fileio.opendirSync(dpath);
  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.

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

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


### isDirectory

isDirectory(): boolean

Checks whether a directory entry is a directory.

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

- Example
  ```js
  let dir = fileio.opendirSync(dpath);
  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.

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

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


### isFile

isFile(): boolean

Checks whether a directory entry is a regular file.

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

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


### isSocket

isSocket(): boolean

Checks whether a directory entry is a socket.

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

A
annie_wangli 已提交
2804 2805 2806 2807
- Return value
  | Type| Description|
  | -------- | -------- |
  | boolean | Whether the directory entry is a symbolic link.|
Z
zengyawen 已提交
2808

A
annie_wangli 已提交
2809 2810 2811 2812 2813
- Example
  ```js
  let dir = fileio.opendirSync(dpath);
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```