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

The fileio module provides APIs for file storage and management, including basic file management, directory management, file information statistics, and stream read and write.
Z
zengyawen 已提交
4

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

A
Annie_wang 已提交
8

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

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

A
annie_wangli 已提交
15

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

A
Annie_wang 已提交
18
Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows:
A
Annie_wang 已提交
19

A
Annie_wang 已提交
20 21 22 23 24 25 26 27
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
 let context = featureAbility.getContext();
 let path = '';
 context.getFilesDir().then((data) => {
      path = data;
 })
 ```
Z
zengyawen 已提交
28 29


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

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

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

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

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

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

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

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

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

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


## fileio.stat

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

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

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

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

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

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

A
annie_wangli 已提交
78 79 80 81 82 83 84 85 86 87
  ```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
**System capability**: SystemCapability.FileManagement.File.FileIO

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

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


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

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

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

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


## fileio.opendir

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

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

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

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

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

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

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

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

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


## fileio.opendir

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

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

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

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

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

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

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


## fileio.opendirSync

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

Synchronously opens a directory.

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

A
annie_wangli 已提交
178

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

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

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

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

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

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


## fileio.access

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

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

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

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

A
Annie_wang 已提交
210 211
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
212
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
213
| 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 已提交
214

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

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

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

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


## fileio.access

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

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

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

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

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

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

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


## fileio.accessSync

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

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

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

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

A
Annie_wang 已提交
267 268
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
269
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
270
| 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 已提交
271

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

A
annie_wangli 已提交
325 326 327 328
  | Name     | Type                       | Mandatory  | Description          |
  | -------- | ------------------------- | ---- | ------------ |
  | fd       | number                    | Yes   | File descriptor of the file to close.|
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is closed asynchronously.|
A
annie_wangli 已提交
329

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

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


## fileio.closeSync

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

Synchronously closes a file.

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

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

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

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

A
annie_wangli 已提交
356
  ```js
A
Annie_wang 已提交
357
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
358 359 360 361 362 363 364 365
  fileio.closeSync(fd);
  ```


## fileio.copyFile

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

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

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

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

A
annie_wangli 已提交
372 373 374 375 376
  | Name | Type                        | Mandatory  | Description                                      |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
  | src  | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the file to copy.                     |
  | dest | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the new file.                         |
  | mode | number                     | No   | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
A
annie_wangli 已提交
377

A
Annie_wang 已提交
378
**Return value**
A
Annie_wang 已提交
379

A
annie_wangli 已提交
380 381
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
382
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
383

A
Annie_wang 已提交
384
**Example**
A
Annie_wang 已提交
385

A
annie_wangli 已提交
386
  ```js
A
Annie_wang 已提交
387 388
  let src = path;
  let dest = src + 'tgt';
A
annie_wangli 已提交
389
  fileio.copyFile(src, dest).then(function(){
A
Annie_wang 已提交
390
      console.info("File copied");
A
annie_wangli 已提交
391
  }).catch(function(err){
A
Annie_wang 已提交
392
      console.info("Failed to copy the file. Error:"+ err);
A
annie_wangli 已提交
393 394 395 396 397 398
  });
  ```


## fileio.copyFile

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

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

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

A
Annie_wang 已提交
405
**Parameters**
A
Annie_wang 已提交
406

A
annie_wangli 已提交
407 408 409 410 411 412
  | Name     | Type                        | Mandatory  | Description                                      |
  | -------- | -------------------------- | ---- | ---------------------------------------- |
  | src      | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the file to copy.                     |
  | dest     | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the new file.                         |
  | mode     | number                     | No   | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
  | callback | AsyncCallback&lt;void&gt;  | Yes   | Callback invoked when the file is copied asynchronously.                            |
A
annie_wangli 已提交
413

A
Annie_wang 已提交
414
**Example**
A
Annie_wang 已提交
415

A
annie_wangli 已提交
416
  ```js
A
Annie_wang 已提交
417 418
  let src = path;
  let dest = src + 'tgt';
A
annie_wangli 已提交
419 420 421 422 423 424 425 426
  fileio.copyFile(src, dest, function (err) {
      // Do something.
  });
  ```


## fileio.copyFileSync

A
annie_wangli 已提交
427
copyFileSync(src: string | number, dest: string | number, mode?: number): void
Z
zengyawen 已提交
428 429 430

Synchronously copies a file.

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

A
Annie_wang 已提交
433
**Parameters**
A
Annie_wang 已提交
434

A
annie_wangli 已提交
435 436 437 438 439
  | Name | Type                        | Mandatory  | Description                                      |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
  | src  | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the file to copy.                     |
  | dest | string&nbsp;\|&nbsp;number | Yes   | Path or file descriptor of the new file.                         |
  | mode | number                     | No   | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
A
annie_wangli 已提交
440

A
Annie_wang 已提交
441
**Example**
A
Annie_wang 已提交
442

A
annie_wangli 已提交
443
  ```js
A
Annie_wang 已提交
444 445
  let src = path;
  let dest = src + 'tgt';
A
annie_wangli 已提交
446 447 448 449 450 451 452 453
  fileio.copyFileSync(src, dest);
  ```


## fileio.mkdir

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

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

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

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

A
Annie_wang 已提交
460 461
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
462
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
463
| 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 已提交
464

A
Annie_wang 已提交
465
**Return value**
A
Annie_wang 已提交
466

A
annie_wangli 已提交
467 468
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
469
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
470

A
Annie_wang 已提交
471
**Example**
A
Annie_wang 已提交
472

A
annie_wangli 已提交
473 474
  ```js
  fileio.mkdir(path).then(function() {
A
Annie_wang 已提交
475
      console.info("Directory created");
A
annie_wangli 已提交
476
  }).catch(function (error){
A
Annie_wang 已提交
477
      console.info("Failed to create the directory. Error:"+ error);
A
annie_wangli 已提交
478 479 480 481 482 483
  });
  ```


## fileio.mkdir

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

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

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

A
Annie_wang 已提交
490
**Parameters**
A
Annie_wang 已提交
491

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

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

A
annie_wangli 已提交
500 501
  ```js
  fileio.mkdir(path, function(err) {
A
Annie_wang 已提交
502
    console.info("Directory created");
A
annie_wangli 已提交
503 504 505 506 507 508
  });
  ```


## fileio.mkdirSync

A
annie_wangli 已提交
509
mkdirSync(path: string, mode?: number): void
Z
zengyawen 已提交
510 511 512

Synchronously creates a directory.

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

A
Annie_wang 已提交
515
**Parameters**
A
Annie_wang 已提交
516

A
Annie_wang 已提交
517 518
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
519
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
520
| 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 已提交
521

A
Annie_wang 已提交
522
**Example**
A
Annie_wang 已提交
523

A
annie_wangli 已提交
524 525 526 527 528 529 530 531 532
  ```js
  fileio.mkdirSync(path);
  ```


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

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

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

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

A
Annie_wang 已提交
537
**Parameters**
A
Annie_wang 已提交
538

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

A
Annie_wang 已提交
545
**Return value**
A
Annie_wang 已提交
546

A
annie_wangli 已提交
547 548
  | Type                   | Description         |
  | --------------------- | ----------- |
A
Annie_wang 已提交
549
  | Promise&lt;number&gt; | Promise used to return the file descriptor of the file opened.|
A
annie_wangli 已提交
550

A
Annie_wang 已提交
551
**Example**
A
Annie_wang 已提交
552

A
annie_wangli 已提交
553 554
  ```js
  fileio.open(path, 0o1, 0o0200).then(function(number){
A
Annie_wang 已提交
555 556 557
      console.info("File opened");
  }).catch(function(err){
      console.info("Failed to open the file. Error:"+ err);
A
annie_wangli 已提交
558 559 560 561 562 563 564 565
  });
  ```


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

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

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

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

A
Annie_wang 已提交
570
**Parameters**
A
Annie_wang 已提交
571

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

A
Annie_wang 已提交
579
**Example**
A
Annie_wang 已提交
580

A
annie_wangli 已提交
581 582 583 584 585 586 587 588 589 590
  ```js
  fileio.open(path, 0, function(err, fd) {
      // Do something.
  });
  ```


## fileio.openSync

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

Synchronously opens a file.

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

A
Annie_wang 已提交
596
**Parameters**
A
Annie_wang 已提交
597

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

A
Annie_wang 已提交
604
**Return value**
A
Annie_wang 已提交
605

A
annie_wangli 已提交
606 607
  | Type    | Description         |
  | ------ | ----------- |
A
annie_wangli 已提交
608 609
  | number | File descriptor of the file opened.|

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

A
annie_wangli 已提交
612
  ```js
A
Annie_wang 已提交
613 614 615 616 617 618 619 620 621
  let fd = fileio.openSync(path, 0o102, 0o640);
  ```
  ```js
  let fd = fileio.openSync(path, 0o102, 0o666);
  fileio.writeSync(fd, 'hello world');
  let fd1 = fileio.openSync(path, 0o2002);
  fileio.writeSync(fd1, 'hello world');
  let num = fileio.readSync(fd1, new ArrayBuffer(4096), {position: 0});
  console.info("num == " + num);
A
annie_wangli 已提交
622 623 624 625 626
  ```


## fileio.read

A
annie_wangli 已提交
627 628 629 630 631
read(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): Promise&lt;ReadOut&gt;
A
annie_wangli 已提交
632

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

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

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

A
Annie_wang 已提交
639 640 641 642 643 644 645
| Name | Type       | Mandatory| Description                                                        |
| ------- | ----------- | ---- | ------------------------------------------------------------ |
| fd      | number      | Yes  | File descriptor of the file to read.                                    |
| buffer  | ArrayBuffer | Yes  | Buffer used to store the file data read.                          |
| options | Object      | No  | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size|

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

A
annie_wangli 已提交
647 648
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
649
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
650

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

A
annie_wangli 已提交
653 654 655
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
A
Annie_wang 已提交
656 657
  fileio.read(fd, buf).then(function(readOut){
      console.info("Read file data");
A
Annie_wang 已提交
658
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
Annie_wang 已提交
659 660
  }).catch(function(err){
      console.info("Failed to read file data. Error:"+ err);
A
annie_wangli 已提交
661 662 663 664 665 666
  });
  ```


## fileio.read

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

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

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

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

A
annie_wangli 已提交
679 680 681 682
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                                   | Yes   | File descriptor of the file to read.                            |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file data read.                       |
A
Annie_wang 已提交
683
  | options  | Object                                   | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
A
annie_wangli 已提交
684
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously.                            |
A
annie_wangli 已提交
685

A
Annie_wang 已提交
686
**Example**
A
Annie_wang 已提交
687

A
annie_wangli 已提交
688 689 690 691
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
  fileio.read(fd, buf, function (err, readOut) {
A
Annie_wang 已提交
692
      if (readOut) {
A
Annie_wang 已提交
693
          console.info("Read file data");
A
Annie_wang 已提交
694
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
695 696 697 698 699 700 701
      }
  });
  ```


## fileio.readSync

A
annie_wangli 已提交
702 703 704 705 706
readSync(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): number
Z
zengyawen 已提交
707 708 709

Synchronously reads data from a file.

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

A
Annie_wang 已提交
712
**Parameters**
A
Annie_wang 已提交
713

A
annie_wangli 已提交
714 715 716 717
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | fd      | number      | Yes   | File descriptor of the file to read.                            |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file data read.                       |
A
Annie_wang 已提交
718
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |
A
annie_wangli 已提交
719

A
Annie_wang 已提交
720
**Return value**
A
Annie_wang 已提交
721

A
annie_wangli 已提交
722 723
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
724 725
  | number | Length of the data read.|

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

A
annie_wangli 已提交
728 729 730 731 732 733 734 735 736 737 738
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
  let num = fileio.readSync(fd, buf);
  ```


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

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

A
Annie_wang 已提交
739
Deletes a directory. This API uses a promise to return the result.
A
annie_wangli 已提交
740

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

A
Annie_wang 已提交
743
**Parameters**
A
Annie_wang 已提交
744

A
Annie_wang 已提交
745 746
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
747
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
748

A
Annie_wang 已提交
749
**Return value**
A
Annie_wang 已提交
750

A
annie_wangli 已提交
751 752
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
753
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
754

A
Annie_wang 已提交
755
**Example**
A
Annie_wang 已提交
756

A
annie_wangli 已提交
757 758
  ```js
  fileio.rmdir(path).then(function() {
A
Annie_wang 已提交
759
      console.info("Directory deleted");
A
annie_wangli 已提交
760
  }).catch(function(err){
A
Annie_wang 已提交
761
      console.info("Failed to delete the directory. Error:"+ err);
A
annie_wangli 已提交
762 763 764 765 766 767 768 769
  });
  ```


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

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

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

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

A
Annie_wang 已提交
774
**Parameters**
A
Annie_wang 已提交
775

A
Annie_wang 已提交
776 777
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
778
| path     | string                    | Yes  | Application sandbox path of the directory.|
A
Annie_wang 已提交
779
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is deleted asynchronously.  |
A
annie_wangli 已提交
780

A
Annie_wang 已提交
781
**Example**
A
Annie_wang 已提交
782

A
annie_wangli 已提交
783 784 785
  ```js
  fileio.rmdir(path, function(err){
      // Do something.
A
Annie_wang 已提交
786
      console.info("Directory deleted");
A
annie_wangli 已提交
787 788 789 790
  });
  ```


A
annie_wangli 已提交
791
## fileio.rmdirSync<sup>7+</sup>
A
annie_wangli 已提交
792

A
annie_wangli 已提交
793
rmdirSync(path: string): void
A
annie_wangli 已提交
794 795 796

Synchronously deletes a directory.

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

A
Annie_wang 已提交
799
**Parameters**
A
Annie_wang 已提交
800

A
Annie_wang 已提交
801 802
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
803
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
804

A
Annie_wang 已提交
805
**Example**
A
Annie_wang 已提交
806

A
annie_wangli 已提交
807 808 809 810 811 812 813 814 815
  ```js
  fileio.rmdirSync(path);
  ```


## fileio.unlink

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

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

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

A
Annie_wang 已提交
820
**Parameters**
A
Annie_wang 已提交
821

A
Annie_wang 已提交
822 823
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
824
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
825

A
Annie_wang 已提交
826
**Return value**
A
Annie_wang 已提交
827

A
annie_wangli 已提交
828 829
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
830
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
831

A
Annie_wang 已提交
832
**Example**
A
Annie_wang 已提交
833

A
annie_wangli 已提交
834 835
  ```js
  fileio.unlink(path).then(function(){
A
Annie_wang 已提交
836
      console.info("File deleted");
A
annie_wangli 已提交
837
  }).catch(function(error){
A
Annie_wang 已提交
838
      console.info("Failed to delete the file. Error:"+ error);
A
annie_wangli 已提交
839 840 841 842 843 844 845 846
  });
  ```


## fileio.unlink

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

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

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

A
Annie_wang 已提交
851
**Parameters**
A
Annie_wang 已提交
852

A
Annie_wang 已提交
853 854
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
855
| path     | string                    | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
856
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is deleted asynchronously.  |
A
annie_wangli 已提交
857

A
Annie_wang 已提交
858
**Example**
A
Annie_wang 已提交
859

A
annie_wangli 已提交
860 861
  ```js
  fileio.unlink(path, function(err) {
A
Annie_wang 已提交
862
      console.info("File deleted");
A
annie_wangli 已提交
863 864 865 866 867 868 869
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void
Z
zengyawen 已提交
870 871 872

Synchronously deletes a file.

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

A
Annie_wang 已提交
875
**Parameters**
A
Annie_wang 已提交
876

A
Annie_wang 已提交
877 878
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
879
| path   | string | Yes  | Application sandbox path of the file.|
Z
zengyawen 已提交
880

A
Annie_wang 已提交
881
**Example**
A
Annie_wang 已提交
882

A
annie_wangli 已提交
883 884 885
  ```js
  fileio.unlinkSync(path);
  ```
Z
zengyawen 已提交
886 887


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

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

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

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

A
Annie_wang 已提交
901
**Parameters**
A
Annie_wang 已提交
902

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

A
Annie_wang 已提交
909
**Return value**
A
Annie_wang 已提交
910

A
annie_wangli 已提交
911 912
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
913
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
Z
zengyawen 已提交
914

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

A
annie_wangli 已提交
917
  ```js
A
Annie_wang 已提交
918
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
A
annie_wangli 已提交
919
  fileio.write(fd, "hello, world").then(function(number){
A
Annie_wang 已提交
920
       console.info("Data written to the file. Size is:"+ number);
A
annie_wangli 已提交
921
  }).catch(function(err){
A
Annie_wang 已提交
922
      console.info("Failed to write data to the file. Error:"+ err);
A
annie_wangli 已提交
923 924
  });
  ```
Z
zengyawen 已提交
925 926


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

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

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

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

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

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

A
Annie_wang 已提交
949
**Example**
A
Annie_wang 已提交
950

A
annie_wangli 已提交
951 952 953
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
A
Annie_wang 已提交
954
      if (bytesWritten) {
A
Annie_wang 已提交
955
         console.info("Data written to the file. Size is:"+ bytesWritten);
A
annie_wangli 已提交
956 957 958
      }
  });
  ```
Z
zengyawen 已提交
959 960


A
annie_wangli 已提交
961
## fileio.writeSync
Z
zengyawen 已提交
962

A
annie_wangli 已提交
963 964 965 966 967 968
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
969

A
annie_wangli 已提交
970
Synchronously writes data into a file.
Z
zengyawen 已提交
971

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

A
Annie_wang 已提交
974
**Parameters**
A
Annie_wang 已提交
975

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

A
Annie_wang 已提交
982
**Return value**
A
Annie_wang 已提交
983

A
annie_wangli 已提交
984 985
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
986
  | number | Length of the data written in the file.|
Z
zengyawen 已提交
987

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

A
annie_wangli 已提交
990 991 992 993
  ```js
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  let num = fileio.writeSync(fd, "hello, world");
  ```
Z
zengyawen 已提交
994 995


A
annie_wangli 已提交
996
## fileio.hash
Z
zengyawen 已提交
997

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

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

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

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

A
Annie_wang 已提交
1006 1007
| Name   | Type  | Mandatory| Description                                                        |
| --------- | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1008
| path      | string | Yes  | Application sandbox path of the file.                            |
A
Annie_wang 已提交
1009
| 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 已提交
1010

A
Annie_wang 已提交
1011
**Return value**
A
Annie_wang 已提交
1012

A
annie_wangli 已提交
1013 1014
  | Type                   | Description                        |
  | --------------------- | -------------------------- |
A
Annie_wang 已提交
1015
  | Promise&lt;string&gt; | Promise used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
Z
zengyawen 已提交
1016

A
Annie_wang 已提交
1017
**Example**
A
Annie_wang 已提交
1018

A
annie_wangli 已提交
1019 1020
  ```js
  fileio.hash(path, "sha256").then(function(str){
A
Annie_wang 已提交
1021
      console.info("Calculated file hash:"+ str);
A
Annie_wang 已提交
1022
  }).catch(function(err){
A
Annie_wang 已提交
1023
      console.info("Failed to calculate the file hash. Error:"+ err);
A
annie_wangli 已提交
1024 1025
  });
  ```
Z
zengyawen 已提交
1026 1027


A
annie_wangli 已提交
1028
## fileio.hash
Z
zengyawen 已提交
1029

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

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

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

A
Annie_wang 已提交
1036
**Parameters**
A
Annie_wang 已提交
1037

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

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

A
annie_wangli 已提交
1046
  ```js
A
Annie_wang 已提交
1047
  fileio.hash(path, "sha256", function(err, hashStr) {
A
Annie_wang 已提交
1048
      if (hashStr) {
A
Annie_wang 已提交
1049
          console.info("Calculated file hash:"+ hashStr);
A
annie_wangli 已提交
1050 1051 1052
      }
  });
  ```
Z
zengyawen 已提交
1053 1054


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

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

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

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

A
Annie_wang 已提交
1063
**Parameters**
A
Annie_wang 已提交
1064

A
Annie_wang 已提交
1065 1066
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1067
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1068
| 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 已提交
1069

A
Annie_wang 已提交
1070
**Return value**
A
Annie_wang 已提交
1071

A
annie_wangli 已提交
1072 1073
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1074
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1075

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

A
annie_wangli 已提交
1078
  ```js
A
Annie_wang 已提交
1079
  fileio.chmod(path, 0o700).then(function() {
A
Annie_wang 已提交
1080
      console.info("File permissions changed");
A
annie_wangli 已提交
1081
  }).catch(function(err){
A
Annie_wang 已提交
1082
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
1083 1084
  });
  ```
Z
zengyawen 已提交
1085 1086


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

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

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

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

A
Annie_wang 已提交
1095
**Parameters**
A
Annie_wang 已提交
1096

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

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

A
annie_wangli 已提交
1105
  ```js
A
Annie_wang 已提交
1106
  fileio.chmod(path, 0o700, function (err) {
A
annie_wangli 已提交
1107 1108 1109
      // Do something.
  });
  ```
Z
zengyawen 已提交
1110 1111


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

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

A
Annie_wang 已提交
1116
Synchronously changes file permissions.
Z
zengyawen 已提交
1117

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

A
Annie_wang 已提交
1120
**Parameters**
A
Annie_wang 已提交
1121

A
Annie_wang 已提交
1122 1123
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1124
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1125
| 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 已提交
1126

A
Annie_wang 已提交
1127
**Example**
A
Annie_wang 已提交
1128

A
annie_wangli 已提交
1129
  ```js
A
Annie_wang 已提交
1130
  fileio.chmodSync(path, 0o700);
A
annie_wangli 已提交
1131
  ```
Z
zengyawen 已提交
1132 1133


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

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

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

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

A
Annie_wang 已提交
1142
**Parameters**
A
Annie_wang 已提交
1143

A
annie_wangli 已提交
1144 1145 1146
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1147

A
Annie_wang 已提交
1148
**Return value**
A
Annie_wang 已提交
1149

A
annie_wangli 已提交
1150
  | Type                          | Description        |
A
Annie_wang 已提交
1151
  | ---------------------------- | ---------- |
A
Annie_wang 已提交
1152
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information.|
Z
zengyawen 已提交
1153

A
Annie_wang 已提交
1154
**Example**
A
Annie_wang 已提交
1155

A
annie_wangli 已提交
1156
  ```js
A
Annie_wang 已提交
1157
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1158
  fileio.fstat(fd).then(function(stat){
A
Annie_wang 已提交
1159
      console.info("Obtained file info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1160
  }).catch(function(err){
A
Annie_wang 已提交
1161
      console.info("Failed to obtain file info. Error:"+ err);
A
annie_wangli 已提交
1162 1163
  });
  ```
Z
zengyawen 已提交
1164 1165


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

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

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

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

A
Annie_wang 已提交
1174
**Parameters**
A
Annie_wang 已提交
1175

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

A
Annie_wang 已提交
1181
**Example**
A
Annie_wang 已提交
1182

A
annie_wangli 已提交
1183 1184 1185 1186 1187 1188
  ```js
  let fd = fileio.openSync(path);
  fileio.fstat(fd, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
1189 1190


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

A
annie_wangli 已提交
1193
fstatSync(fd: number): Stat
Z
zengyawen 已提交
1194

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

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

A
Annie_wang 已提交
1199
**Parameters**
A
Annie_wang 已提交
1200

A
annie_wangli 已提交
1201 1202 1203
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1204

A
Annie_wang 已提交
1205
**Return value**
A
Annie_wang 已提交
1206

A
annie_wangli 已提交
1207 1208
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1209
  | [Stat](#stat) | File information obtained.|
Z
zengyawen 已提交
1210

A
Annie_wang 已提交
1211
**Example**
A
Annie_wang 已提交
1212

A
annie_wangli 已提交
1213 1214 1215 1216
  ```js
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```
Z
zengyawen 已提交
1217 1218


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

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

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

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

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

A
annie_wangli 已提交
1229 1230 1231
  | Name | Type    | Mandatory  | Description              |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | Yes   | File descriptor of the file to truncate.    |
A
Annie_wang 已提交
1232
  | len  | number | No   | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1233

A
Annie_wang 已提交
1234
**Return value**
A
Annie_wang 已提交
1235

A
annie_wangli 已提交
1236 1237
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1238
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1239

A
Annie_wang 已提交
1240
**Example**
A
Annie_wang 已提交
1241

A
annie_wangli 已提交
1242 1243 1244
  ```js
  let fd = fileio.openSync(path);
  fileio.ftruncate(fd, 5).then(function(err) {    
A
Annie_wang 已提交
1245
      console.info("File truncated");
A
annie_wangli 已提交
1246 1247 1248 1249
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1250 1251


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

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

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

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

A
Annie_wang 已提交
1260
**Parameters**
A
Annie_wang 已提交
1261

A
annie_wangli 已提交
1262 1263 1264 1265
  | Name     | Type                       | Mandatory  | Description              |
  | -------- | ------------------------- | ---- | ---------------- |
  | fd       | number                    | Yes   | File descriptor of the file to truncate.    |
  | len      | number                    | Yes   | File length, in bytes, after truncation.|
A
Annie_wang 已提交
1266
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback that returns no value. |
Z
zengyawen 已提交
1267

A
Annie_wang 已提交
1268
**Example**
A
Annie_wang 已提交
1269

A
annie_wangli 已提交
1270
  ```js
A
Annie_wang 已提交
1271 1272 1273
  let fd = fileio.openSync(path);
  let len = 5;
  fileio.ftruncate(fd, 5, function(err){
A
annie_wangli 已提交
1274 1275 1276
      // Do something.
  });
  ```
Z
zengyawen 已提交
1277 1278


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

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

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

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

A
Annie_wang 已提交
1287
**Parameters**
A
Annie_wang 已提交
1288

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

A
Annie_wang 已提交
1294
**Example**
A
Annie_wang 已提交
1295

A
annie_wangli 已提交
1296
  ```js
A
Annie_wang 已提交
1297 1298
  let fd = fileio.openSync(path);
  let len = 5;
P
Peter_1988 已提交
1299
  fileio.ftruncateSync(fd, len);
A
annie_wangli 已提交
1300
  ```
Z
zengyawen 已提交
1301 1302


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

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

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

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

A
Annie_wang 已提交
1311
**Parameters**
A
Annie_wang 已提交
1312

A
Annie_wang 已提交
1313 1314
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
A
Annie_wang 已提交
1315
| path   | string | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1316
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1317

A
Annie_wang 已提交
1318
**Return value**
A
Annie_wang 已提交
1319

A
annie_wangli 已提交
1320 1321
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1322
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1323

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

A
annie_wangli 已提交
1326
  ```js
A
Annie_wang 已提交
1327
  let len = 5;
A
annie_wangli 已提交
1328
  fileio.truncate(path, len).then(function(){
A
Annie_wang 已提交
1329
      console.info("File truncated");
A
annie_wangli 已提交
1330 1331 1332 1333
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1334 1335


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

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

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

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

A
Annie_wang 已提交
1344
**Parameters**
A
Annie_wang 已提交
1345

A
Annie_wang 已提交
1346 1347
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1348
| path     | string                    | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1349
| len      | number                    | Yes  | File length, in bytes, after truncation.|
A
Annie_wang 已提交
1350
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback that returns no value.  |
Z
zengyawen 已提交
1351

A
Annie_wang 已提交
1352
**Example**
A
Annie_wang 已提交
1353

A
annie_wangli 已提交
1354
  ```js
A
Annie_wang 已提交
1355
  let len = 5;
A
annie_wangli 已提交
1356 1357 1358 1359
  fileio.truncate(path, len, function(err){
      // Do something.
  });
  ```
Z
zengyawen 已提交
1360 1361


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

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

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

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

A
Annie_wang 已提交
1370
**Parameters**
A
Annie_wang 已提交
1371

A
Annie_wang 已提交
1372 1373
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
A
Annie_wang 已提交
1374
| path   | string | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1375
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1376

A
Annie_wang 已提交
1377
**Example**
A
Annie_wang 已提交
1378

A
annie_wangli 已提交
1379
  ```js
A
Annie_wang 已提交
1380
  let len = 5;
P
Peter_1988 已提交
1381
  fileio.truncateSync(path, len);
A
annie_wangli 已提交
1382
  ```
Z
zengyawen 已提交
1383 1384


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

A
annie_wangli 已提交
1387 1388 1389 1390 1391
readText(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): Promise&lt;string&gt;
Z
zengyawen 已提交
1392

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

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

A
Annie_wang 已提交
1397
**Parameters**
A
Annie_wang 已提交
1398

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

A
Annie_wang 已提交
1404
**Return value**
A
Annie_wang 已提交
1405

A
annie_wangli 已提交
1406 1407
  | Type                   | Description        |
  | --------------------- | ---------- |
A
Annie_wang 已提交
1408
  | Promise&lt;string&gt; | Promise used to return the content read.|
Z
zengyawen 已提交
1409

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

A
annie_wangli 已提交
1412 1413
  ```js
  fileio.readText(path).then(function(str) {
A
Annie_wang 已提交
1414
      console.info("Read text:"+ str);
A
annie_wangli 已提交
1415
  }).catch(function(err){
A
Annie_wang 已提交
1416
      console.info("Failed to read the text. Error:"+ err);
A
annie_wangli 已提交
1417 1418
  });
  ```
Z
zengyawen 已提交
1419 1420


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

A
annie_wangli 已提交
1423 1424 1425 1426 1427
readText(filePath: string, options: {
    position?: number;
    length?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1428

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

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

A
Annie_wang 已提交
1433
**Parameters**
A
Annie_wang 已提交
1434

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

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

A
annie_wangli 已提交
1443
  ```js
A
Annie_wang 已提交
1444
  fileio.readText(path, { position: 1, encoding: 'UTF-8' }, function(err, str){
A
annie_wangli 已提交
1445 1446 1447
      // Do something.
  });
  ```
Z
zengyawen 已提交
1448 1449


A
annie_wangli 已提交
1450 1451
## fileio.readTextSync<sup>7+</sup>

A
annie_wangli 已提交
1452 1453 1454 1455 1456
readTextSync(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): string
A
annie_wangli 已提交
1457 1458 1459

Synchronously reads the text of a file. 

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

A
Annie_wang 已提交
1462
**Parameters**
A
Annie_wang 已提交
1463

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

A
Annie_wang 已提交
1469
**Return value**
A
Annie_wang 已提交
1470

A
annie_wangli 已提交
1471 1472
  | Type  | Description                |
  | ------ | -------------------- |
A
Annie_wang 已提交
1473
  | string | Promise used to return the content of the file read.|
A
annie_wangli 已提交
1474

A
Annie_wang 已提交
1475
**Example**
A
Annie_wang 已提交
1476

A
annie_wangli 已提交
1477 1478 1479 1480 1481 1482 1483 1484 1485
  ```js
  let str = fileio.readTextSync(path, {position: 1, length: 3});
  ```


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

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

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

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

A
Annie_wang 已提交
1490
**Parameters**
A
Annie_wang 已提交
1491

A
Annie_wang 已提交
1492 1493
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1494
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1495

A
Annie_wang 已提交
1496
**Return value**
A
Annie_wang 已提交
1497

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

A
Annie_wang 已提交
1502
**Example**
A
Annie_wang 已提交
1503

A
annie_wangli 已提交
1504 1505
  ```js
  fileio.lstat(path).then(function(stat){
A
Annie_wang 已提交
1506
      console.info("Got link info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1507
  }).catch(function(err){
A
Annie_wang 已提交
1508
      console.info("Failed to obtain link info. Error:"+ err);
A
annie_wangli 已提交
1509 1510 1511 1512 1513 1514 1515 1516
  });
  ```


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

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

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

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

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

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

A
Annie_wang 已提交
1528
**Example**
A
Annie_wang 已提交
1529

A
annie_wangli 已提交
1530 1531 1532
  ```js
  fileio.lstat(path, function (err, stat) {
      // Do something.
A
annie_wangli 已提交
1533
  });
A
annie_wangli 已提交
1534 1535 1536 1537 1538 1539 1540
  ```


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

lstatSync(path:string): Stat

A
Annie_wang 已提交
1541
Synchronously obtains the link information.
A
annie_wangli 已提交
1542

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

A
Annie_wang 已提交
1545
**Parameters**
A
Annie_wang 已提交
1546

A
Annie_wang 已提交
1547 1548
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1549
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1550

A
Annie_wang 已提交
1551
**Return value**
A
Annie_wang 已提交
1552

A
annie_wangli 已提交
1553 1554
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1555
  | [Stat](#stat) | Link information obtained.|
A
annie_wangli 已提交
1556

A
Annie_wang 已提交
1557
**Example**
A
Annie_wang 已提交
1558

A
annie_wangli 已提交
1559 1560 1561 1562 1563 1564 1565 1566 1567
  ```js
  let stat = fileio.lstatSync(path);
  ```


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

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

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

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

A
Annie_wang 已提交
1572
**Parameters**
A
Annie_wang 已提交
1573

A
Annie_wang 已提交
1574 1575 1576 1577
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes  | Application sandbox path of the file to rename.|
| newPath | String | Yes  | Application sandbox path of the file renamed.  |
A
annie_wangli 已提交
1578

A
Annie_wang 已提交
1579
**Return value**
A
Annie_wang 已提交
1580

A
annie_wangli 已提交
1581 1582
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1583
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1584

A
Annie_wang 已提交
1585
**Example**
A
Annie_wang 已提交
1586

A
annie_wangli 已提交
1587
  ```js
A
Annie_wang 已提交
1588 1589
  let oldPath = path;
  let newPath = oldPath + '123';
A
annie_wangli 已提交
1590
  fileio.rename(oldPath, newPath).then(function() {
A
Annie_wang 已提交
1591
      console.info("File renamed");
A
annie_wangli 已提交
1592
  }).catch(function(err){
A
Annie_wang 已提交
1593
      console.info("Failed to rename the file. Error:"+ err);
A
annie_wangli 已提交
1594 1595 1596 1597 1598 1599 1600 1601
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1606
**Parameters**
A
Annie_wang 已提交
1607

A
Annie_wang 已提交
1608 1609 1610
| Name  | Type                     | Mandatory| Description                        |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | Yes  | Application sandbox path of the file to rename.|
A
Annie_wang 已提交
1611
| newPath  | String                    | Yes  | Application sandbox path of the file renamed.  |
A
Annie_wang 已提交
1612
| Callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously renamed.  |
A
annie_wangli 已提交
1613

A
Annie_wang 已提交
1614
**Example**
A
Annie_wang 已提交
1615

A
annie_wangli 已提交
1616
  ```js
A
Annie_wang 已提交
1617 1618
  let oldPath = path;
  let newPath = oldPath + '123';
A
annie_wangli 已提交
1619
  fileio.rename(oldPath, newPath, function(err){
A
annie_wangli 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
  });
  ```


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

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

Synchronously renames a file.

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

A
Annie_wang 已提交
1632
**Parameters**
A
Annie_wang 已提交
1633

A
Annie_wang 已提交
1634 1635 1636 1637
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes  | Application sandbox path of the file to rename.|
| newPath | String | Yes  | Application sandbox path of the file renamed.  |
A
annie_wangli 已提交
1638

A
Annie_wang 已提交
1639
**Example**
A
Annie_wang 已提交
1640

A
annie_wangli 已提交
1641
  ```js
A
Annie_wang 已提交
1642 1643
  let oldPath = path;
  let newPath = oldPath + '123';
A
annie_wangli 已提交
1644
  fileio.renameSync(oldPath, newPath);
A
annie_wangli 已提交
1645 1646 1647 1648 1649 1650 1651
  ```


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

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

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

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

A
Annie_wang 已提交
1656
**Parameters**
A
Annie_wang 已提交
1657

A
annie_wangli 已提交
1658 1659
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1660
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1661

A
Annie_wang 已提交
1662
**Return value**
A
Annie_wang 已提交
1663

A
annie_wangli 已提交
1664 1665
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1666
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1667

A
Annie_wang 已提交
1668
**Example**
A
Annie_wang 已提交
1669

A
annie_wangli 已提交
1670
  ```js
A
Annie_wang 已提交
1671
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1672
  fileio.fsync(fd).then(function(){
A
Annie_wang 已提交
1673
      console.info("Data flushed");
A
annie_wangli 已提交
1674
  }).catch(function(err){
A
Annie_wang 已提交
1675
      console.info("Failed to flush data. Error:"+ err);
A
annie_wangli 已提交
1676 1677 1678 1679 1680 1681 1682 1683
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1688
**Parameters**
A
Annie_wang 已提交
1689

A
annie_wangli 已提交
1690 1691
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
A
Annie_wang 已提交
1692
  | fd       | number                    | Yes   | File descriptor of the file to flush.   |
A
annie_wangli 已提交
1693
  | Callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1694

A
Annie_wang 已提交
1695
**Example**
A
Annie_wang 已提交
1696

A
annie_wangli 已提交
1697
  ```js
A
Annie_wang 已提交
1698
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
  fileio.fsync(fd, function(err){
      // Do something.
  });
  ```


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

fsyncSync(fd: number): void

A
Annie_wang 已提交
1709
Flushes data of a file to disk in synchronous mode.
A
annie_wangli 已提交
1710 1711

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

A
Annie_wang 已提交
1713
**Parameters**
A
Annie_wang 已提交
1714

A
annie_wangli 已提交
1715 1716
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1717
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1718

A
Annie_wang 已提交
1719
**Example**
A
Annie_wang 已提交
1720

A
annie_wangli 已提交
1721
  ```js
A
Annie_wang 已提交
1722
  let fd = fileio.openSync(path);
A
Annie_wang 已提交
1723
  fileio.fsyncSync(fd);
A
annie_wangli 已提交
1724 1725 1726 1727 1728 1729 1730
  ```


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

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

A
Annie_wang 已提交
1731
Flushes data of a file to disk. This API uses a promise to return the result. **fdatasync()** is similar to **fsync()**, but does not flush modified metadata unless that metadata is needed.
A
annie_wangli 已提交
1732

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

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

A
annie_wangli 已提交
1737 1738
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1739
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1740

A
Annie_wang 已提交
1741
**Return value**
A
Annie_wang 已提交
1742

A
annie_wangli 已提交
1743 1744
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1745
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1746

A
Annie_wang 已提交
1747
**Example**
A
Annie_wang 已提交
1748

A
annie_wangli 已提交
1749
  ```js
A
Annie_wang 已提交
1750
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1751
  fileio.fdatasync(fd).then(function(err) {
A
Annie_wang 已提交
1752
      console.info("Data flushed");
A
annie_wangli 已提交
1753
  }).catch(function(err){
A
Annie_wang 已提交
1754
      console.info("Failed to flush data. Error:"+ err);
A
annie_wangli 已提交
1755 1756 1757 1758 1759 1760 1761 1762
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1767
**Parameters**
A
Annie_wang 已提交
1768

A
annie_wangli 已提交
1769 1770 1771 1772
  | Name     | Type                             | Mandatory  | Description               |
  | -------- | ------------------------------- | ---- | ----------------- |
  | fd       | number                          | Yes   | File descriptor of the file to synchronize.     |
  | callback | AsyncCallback&nbsp;&lt;void&gt; | Yes   | Callback invoked when the file data is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1773

A
Annie_wang 已提交
1774
**Example**
A
Annie_wang 已提交
1775

A
annie_wangli 已提交
1776
  ```js
A
Annie_wang 已提交
1777
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
  fileio.fdatasync (fd, function (err) {
      // Do something.
  });
  ```


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

fdatasyncSync(fd: number): void

Synchronizes data in a file in synchronous mode.

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

A
Annie_wang 已提交
1792
**Parameters**
A
Annie_wang 已提交
1793

A
annie_wangli 已提交
1794 1795
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1796
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1797

A
Annie_wang 已提交
1798
**Example**
A
Annie_wang 已提交
1799

A
annie_wangli 已提交
1800
  ```js
A
Annie_wang 已提交
1801
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
1802 1803 1804 1805 1806 1807 1808 1809
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

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

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

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

A
Annie_wang 已提交
1816 1817
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1818 1819
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1820

A
Annie_wang 已提交
1821
**Return value**
A
Annie_wang 已提交
1822

A
annie_wangli 已提交
1823 1824
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1825
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1826

A
Annie_wang 已提交
1827
**Example**
A
Annie_wang 已提交
1828

A
annie_wangli 已提交
1829
  ```js
A
Annie_wang 已提交
1830 1831
  let target = path;
  let srcPath = target + 'aaa';
A
annie_wangli 已提交
1832
  fileio.symlink(target, srcPath).then(function() {
A
Annie_wang 已提交
1833
      console.info("Symbolic link created");
A
annie_wangli 已提交
1834
  }).catch(function(err){
A
Annie_wang 已提交
1835
      console.info("Failed to create the symbolic link. Error:"+ err);
A
annie_wangli 已提交
1836 1837 1838 1839 1840 1841 1842 1843
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1848
**Parameters**
A
Annie_wang 已提交
1849

A
Annie_wang 已提交
1850 1851
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1852 1853
| target   | string                    | Yes  | Application sandbox path of the target file.        |
| srcPath  | string                    | Yes  | Application sandbox path of the symbolic link file.    |
A
Annie_wang 已提交
1854
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the symbolic link is created asynchronously.|
A
annie_wangli 已提交
1855

A
Annie_wang 已提交
1856
**Example**
A
Annie_wang 已提交
1857

A
annie_wangli 已提交
1858
  ```js
A
Annie_wang 已提交
1859 1860
  let target = path;
  let srcPath = target + 'aaa';
A
annie_wangli 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
  fileio.symlink(target, srcPath, function (err) {
      // Do something.
  });
  ```


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

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

Synchronously creates a symbolic link based on a specified path.

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

A
Annie_wang 已提交
1875
**Parameters**
A
Annie_wang 已提交
1876

A
Annie_wang 已提交
1877 1878
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1879 1880
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1881

A
Annie_wang 已提交
1882
**Example**
A
Annie_wang 已提交
1883

A
annie_wangli 已提交
1884
  ```js
A
Annie_wang 已提交
1885 1886
  let target = path;
  let srcPath = target + 'aaa';
A
annie_wangli 已提交
1887 1888 1889 1890 1891 1892 1893 1894
  fileio.symlinkSync(target, srcPath);
  ```


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

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

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

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

A
Annie_wang 已提交
1899
**Parameters**
A
Annie_wang 已提交
1900

A
Annie_wang 已提交
1901 1902
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1903
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
1904 1905
| uid    | number | Yes  | New user ID (UID).       |
| gid    | number | Yes  | New group ID (GID).      |
A
annie_wangli 已提交
1906

A
Annie_wang 已提交
1907
**Return value**
A
Annie_wang 已提交
1908

A
annie_wangli 已提交
1909 1910
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1911
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1912

A
Annie_wang 已提交
1913
**Example**
A
Annie_wang 已提交
1914

A
annie_wangli 已提交
1915 1916
  ```js
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
1917
  fileio.chown(path, stat.uid, stat.gid).then(function(){
A
Annie_wang 已提交
1918
      console.info("File owner changed");
A
annie_wangli 已提交
1919
  }).catch(function(err){
A
Annie_wang 已提交
1920
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
1921 1922 1923 1924 1925 1926 1927 1928
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1933
**Parameters**
A
Annie_wang 已提交
1934

A
Annie_wang 已提交
1935 1936
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
1937
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
1938 1939 1940
| uid      | number                    | Yes  | New UID.                     |
| gid      | number                    | Yes  | New GID.                     |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file owner is changed asynchronously.|
A
annie_wangli 已提交
1941

A
Annie_wang 已提交
1942
**Example**
A
Annie_wang 已提交
1943

A
annie_wangli 已提交
1944
  ```js
A
Annie_wang 已提交
1945
  let stat = fileio.statSync(path)
A
annie_wangli 已提交
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
  fileio.chown(path, stat.uid, stat.gid, function (err){
      // Do something.
  });
  ```


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

chownSync(path: string, uid: number, gid: number): void

Synchronously changes the file owner based on its path.

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

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

A
Annie_wang 已提交
1962 1963
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1964
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
1965 1966
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
1967

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

A
annie_wangli 已提交
1970
  ```js
A
Annie_wang 已提交
1971
  let stat = fileio.statSync(path)
A
annie_wangli 已提交
1972 1973 1974 1975 1976 1977 1978 1979
  fileio.chownSync(path, stat.uid, stat.gid);
  ```


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

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

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

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

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

A
annie_wangli 已提交
1986 1987 1988
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
1989

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

A
Annie_wang 已提交
1992
  | Type                  | Description        |
A
annie_wangli 已提交
1993
  | --------------------- | ---------- |
A
Annie_wang 已提交
1994
  | Promise&lt;string&gt; | Promise used to return the unique directory generated.|
A
annie_wangli 已提交
1995

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

A
annie_wangli 已提交
1998 1999
  ```js
  fileio.mkdtemp(path + "XXXX").then(function(path){
A
Annie_wang 已提交
2000
      console.info("Temporary directory created:"+ path);
A
annie_wangli 已提交
2001
  }).catch(function(err){
A
Annie_wang 已提交
2002
      console.info("Failed to create the temporary directory. Error:"+ err);
A
annie_wangli 已提交
2003 2004 2005 2006 2007 2008 2009 2010
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2015
**Parameters**
A
Annie_wang 已提交
2016

A
annie_wangli 已提交
2017 2018 2019 2020
  | Name     | Type                         | Mandatory  | Description                         |
  | -------- | --------------------------- | ---- | --------------------------- |
  | prefix   | string                      | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
  | callback | AsyncCallback&lt;string&gt; | Yes   | Callback invoked when a temporary directory is created asynchronously.             |
A
annie_wangli 已提交
2021

A
Annie_wang 已提交
2022
**Example**
A
Annie_wang 已提交
2023

A
annie_wangli 已提交
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
  ```js
  fileio.mkdtemp(path + "XXXX", function (err, res) {
      // Do something.
  });
  ```


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

mkdtempSync(prefix: string): string

Synchronously creates a temporary directory.

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

A
Annie_wang 已提交
2039
**Parameters**
A
Annie_wang 已提交
2040

A
annie_wangli 已提交
2041 2042 2043
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
2044

A
Annie_wang 已提交
2045
**Return value**
A
Annie_wang 已提交
2046

A
Annie_wang 已提交
2047
  | Type   | Description        |
A
annie_wangli 已提交
2048
  | ------ | ---------- |
A
annie_wangli 已提交
2049 2050
  | string | Unique path generated.|

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

A
annie_wangli 已提交
2053 2054 2055 2056 2057 2058 2059 2060 2061
  ```js
  let res = fileio.mkdtempSync(path + "XXXX");
  ```


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

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

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

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

A
Annie_wang 已提交
2066
**Parameters**
A
Annie_wang 已提交
2067

A
annie_wangli 已提交
2068 2069 2070 2071
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | Yes   | File descriptor of the target file.                            |
  | mode | number | Yes   | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
A
annie_wangli 已提交
2072

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

A
Annie_wang 已提交
2075
  | Type                | Description                          |
A
annie_wangli 已提交
2076
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2077
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2078

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

A
annie_wangli 已提交
2081
  ```js
A
Annie_wang 已提交
2082 2083
  let fd = fileio.openSync(path);
  let mode = 0o700;
A
annie_wangli 已提交
2084
  fileio.fchmod(fd, mode).then(function() {
A
Annie_wang 已提交
2085
      console.info("File permissions changed");
A
annie_wangli 已提交
2086
  }).catch(function(err){
A
Annie_wang 已提交
2087
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
2088 2089 2090 2091 2092 2093 2094 2095
  });
  ```


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

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

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

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

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

A
annie_wangli 已提交
2102 2103 2104 2105 2106
  | Name     | Type                             | Mandatory  | Description                                      |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | Yes   | File descriptor of the target file.                            |
  | mode     | number                          | Yes   | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
  | callback | AsyncCallback&nbsp;&lt;void&gt; | Yes   | Callback invoked when the file permissions are changed asynchronously.                          |
A
annie_wangli 已提交
2107

A
Annie_wang 已提交
2108
**Example**
A
Annie_wang 已提交
2109

A
annie_wangli 已提交
2110
  ```js
A
Annie_wang 已提交
2111 2112
  let fd = fileio.openSync(path);
  let mode = 0o700;
A
annie_wangli 已提交
2113 2114 2115 2116 2117 2118 2119 2120
  fileio.fchmod(fd, mode, function (err) {
      // Do something.
  });
  ```


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

A
annie_wangli 已提交
2121
fchmodSync(fd: number, mode: number): void
A
annie_wangli 已提交
2122 2123 2124

Synchronously changes the file permissions based on the file descriptor.

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

A
Annie_wang 已提交
2127
**Parameters**
A
Annie_wang 已提交
2128

A
annie_wangli 已提交
2129 2130 2131 2132
  | Name | Type    | Mandatory  | Description                                      |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | Yes   | File descriptor of the target file.                            |
  | mode | number | Yes   | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
A
annie_wangli 已提交
2133

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

A
annie_wangli 已提交
2136
  ```js
A
Annie_wang 已提交
2137 2138
  let fd = fileio.openSync(path);
  let mode = 0o700;
A
annie_wangli 已提交
2139 2140 2141 2142 2143 2144 2145 2146
   fileio.fchmodSync(fd, mode);
  ```


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

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

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

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

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

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

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

A
annie_wangli 已提交
2160 2161
  | Type                               | Description       |
  | --------------------------------- | --------- |
A
annie_wangli 已提交
2162 2163
  | Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.|

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

A
annie_wangli 已提交
2166 2167
  ```js
  fileio.createStream(path, "r+").then(function(stream){
A
Annie_wang 已提交
2168
      console.info("Stream opened");
A
annie_wangli 已提交
2169
  }).catch(function(err){
A
Annie_wang 已提交
2170
      console.info("Failed to create the stream. Error:"+ err);
A
annie_wangli 已提交
2171 2172 2173 2174 2175 2176 2177 2178
  });
  ```


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

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

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

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

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

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

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

A
annie_wangli 已提交
2193
  ```js
A
Annie_wang 已提交
2194
  fileio.createStream(path, "r+", function(err, stream){
A
annie_wangli 已提交
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
      // Do something.
  });
  ```


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

createStreamSync(path: string, mode: string): Stream

Synchronously opens a stream based on the file path.

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

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

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

A
Annie_wang 已提交
2215
**Return value**
A
Annie_wang 已提交
2216

A
Annie_wang 已提交
2217
  | Type               | Description       |
A
annie_wangli 已提交
2218
  | ------------------ | --------- |
A
annie_wangli 已提交
2219
  | [Stream](#stream7) | Stream opened.|
A
annie_wangli 已提交
2220

A
Annie_wang 已提交
2221
**Example**
A
Annie_wang 已提交
2222

A
annie_wangli 已提交
2223 2224 2225 2226 2227 2228 2229 2230 2231
  ```js
  let ss = fileio.createStreamSync(path, "r+");
  ```


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

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

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

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

A
Annie_wang 已提交
2236
**Parameters**
A
Annie_wang 已提交
2237

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

A
Annie_wang 已提交
2243
**Return value**
A
Annie_wang 已提交
2244

A
Annie_wang 已提交
2245
  | Type                              | Description       |
A
annie_wangli 已提交
2246
  | --------------------------------- | --------- |
A
annie_wangli 已提交
2247 2248
  | Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.|

A
Annie_wang 已提交
2249
**Example**
A
Annie_wang 已提交
2250

A
annie_wangli 已提交
2251
  ```js
A
Annie_wang 已提交
2252 2253 2254
  let fd = fileio.openSync(path);
  fileio.fdopenStream(fd, "r+").then(function(stream){
      console.info("Stream opened");
A
annie_wangli 已提交
2255
  }).catch(function(err){
A
Annie_wang 已提交
2256
      console.info("Failed to open the stream. Error:"+ err);
A
annie_wangli 已提交
2257 2258 2259 2260 2261 2262 2263 2264
  });
  ```


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

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

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

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

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

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

A
Annie_wang 已提交
2277
**Example**
A
Annie_wang 已提交
2278

A
annie_wangli 已提交
2279
  ```js
A
Annie_wang 已提交
2280 2281
  let fd = fileio.openSync(path);
  fileio.fdopenStream(fd, "r+", function (err, stream) {
A
annie_wangli 已提交
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
      // Do something.
  });
  ```


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

fdopenStreamSync(fd: number, mode: string): Stream

Synchronously opens a stream based on the file descriptor.

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

A
Annie_wang 已提交
2295
**Parameters**
A
Annie_wang 已提交
2296

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

A
Annie_wang 已提交
2302
**Return value**
A
Annie_wang 已提交
2303

A
Annie_wang 已提交
2304
  | Type               | Description       |
A
annie_wangli 已提交
2305
  | ------------------ | --------- |
A
annie_wangli 已提交
2306
  | [Stream](#stream7) | Stream opened.|
A
annie_wangli 已提交
2307

A
Annie_wang 已提交
2308
**Example**
A
Annie_wang 已提交
2309

A
annie_wangli 已提交
2310
  ```js
A
Annie_wang 已提交
2311
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
2312 2313 2314 2315 2316 2317 2318 2319
  let ss = fileio.fdopenStreamSync(fd, "r+");
  ```


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

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

A
Annie_wang 已提交
2320
Changes the file owner based on the file descriptor. This API uses a promise to return the result.
A
annie_wangli 已提交
2321

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

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

A
annie_wangli 已提交
2326 2327 2328 2329 2330
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
  | uid  | number | Yes   | New UID.  |
  | gid  | number | Yes   | New GID.  |
A
annie_wangli 已提交
2331

A
Annie_wang 已提交
2332
**Return value**
A
Annie_wang 已提交
2333

A
annie_wangli 已提交
2334 2335
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2336
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2337

A
Annie_wang 已提交
2338
**Example**
A
Annie_wang 已提交
2339

A
annie_wangli 已提交
2340
  ```js
A
Annie_wang 已提交
2341
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
2342 2343
  let stat = fileio.statSync(path);
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2344
      console.info("File owner changed");
A
annie_wangli 已提交
2345
  }).catch(function(err){
A
Annie_wang 已提交
2346
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2347 2348 2349 2350 2351 2352 2353 2354
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2359
**Parameters**
A
Annie_wang 已提交
2360

A
annie_wangli 已提交
2361 2362 2363 2364 2365 2366
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | Yes   | File descriptor of the target file.   |
  | uid      | number                    | Yes   | New UID.     |
  | gid      | number                    | Yes   | New GID.     |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file owner is changed asynchronously.|
A
annie_wangli 已提交
2367

A
Annie_wang 已提交
2368
**Example**
A
Annie_wang 已提交
2369

A
annie_wangli 已提交
2370
  ```js
A
Annie_wang 已提交
2371
  let fd = fileio.openSync(path);
A
Annie_wang 已提交
2372
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
  fileio.fchown(fd, stat.uid, stat.gid, function (err){
      // Do something.
  });
  ```


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

fchownSync(fd: number, uid: number, gid: number): void

Synchronously changes the file owner based on the file descriptor.

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

A
Annie_wang 已提交
2387
**Parameters**
A
Annie_wang 已提交
2388

A
annie_wangli 已提交
2389 2390 2391 2392 2393
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
  | uid  | number | Yes   | New UID.  |
  | gid  | number | Yes   | New GID.  |
A
annie_wangli 已提交
2394

A
Annie_wang 已提交
2395
**Example**
A
Annie_wang 已提交
2396

A
annie_wangli 已提交
2397
  ```js
A
Annie_wang 已提交
2398
  let fd = fileio.openSync(path);
A
Annie_wang 已提交
2399
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
2400 2401 2402 2403 2404 2405 2406 2407
  fileio.fchownSync(fd, stat.uid, stat.gid);
  ```


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

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

A
Annie_wang 已提交
2408
Changes the file owner (owner of the symbolic link, not the file referred to by the symbolic link) based on the file path. This API uses a promise to return the result.
A
annie_wangli 已提交
2409

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

A
Annie_wang 已提交
2412
**Parameters**
A
Annie_wang 已提交
2413

A
Annie_wang 已提交
2414 2415
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2416
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2417 2418
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2419

A
Annie_wang 已提交
2420
**Return value**
A
Annie_wang 已提交
2421

A
annie_wangli 已提交
2422 2423
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2424
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2425

A
Annie_wang 已提交
2426
**Example**
A
Annie_wang 已提交
2427

A
annie_wangli 已提交
2428 2429 2430
  ```js
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2431
      console.info("File owner changed");
A
annie_wangli 已提交
2432
  }).catch(function(err){
A
Annie_wang 已提交
2433
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2434 2435 2436 2437 2438 2439 2440 2441
  });
  ```


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

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

A
Annie_wang 已提交
2442
Changes the file owner (owner of the symbolic link, not the file referred to by the symbolic link) based on the file path. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2443

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

A
Annie_wang 已提交
2446
**Parameters**
A
Annie_wang 已提交
2447

A
Annie_wang 已提交
2448 2449
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
2450
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
2451 2452 2453
| uid      | number                    | Yes  | New UID.                     |
| gid      | number                    | Yes  | New GID.                     |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file owner is changed asynchronously.|
A
annie_wangli 已提交
2454

A
Annie_wang 已提交
2455
**Example**
A
Annie_wang 已提交
2456

A
annie_wangli 已提交
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
  ```js
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid, function (err){
      // Do something.
  });
  ```


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

lchownSync(path: string, uid: number, gid: number): void

Synchronously changes the file owner based on the file path and changes the owner of the symbolic link (not the referenced file).

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

A
Annie_wang 已提交
2473
**Parameters**
A
Annie_wang 已提交
2474

A
Annie_wang 已提交
2475 2476
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2477
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2478 2479
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2480

A
Annie_wang 已提交
2481
**Example**
A
Annie_wang 已提交
2482

A
annie_wangli 已提交
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
  ```js
  let stat = fileio.statSync(path);
  fileio.lchownSync(path, stat.uid, stat.gid);
  ```


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

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

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

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

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

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

A
Annie_wang 已提交
2505
**Return value**
A
Annie_wang 已提交
2506

A
Annie_wang 已提交
2507
  | Type                 | Description        |
A
annie_wangli 已提交
2508
  | -------------------- | ---------- |
A
Annie_wang 已提交
2509
  | [Watcher](#watcher7) | Promise used to return the **Watcher** instance.|
A
annie_wangli 已提交
2510

A
Annie_wang 已提交
2511
**Example**
A
Annie_wang 已提交
2512

A
annie_wangli 已提交
2513
  ```js
A
Annie_wang 已提交
2514 2515 2516
  let filename = path +"/test.txt";
  fileio.createWatcher(filename, 1, function(number){
     console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2517
  });
A
Annie_wang 已提交
2518
  
A
annie_wangli 已提交
2519 2520 2521 2522 2523 2524 2525
  ```


## Readout

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

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

A
annie_wangli 已提交
2528 2529 2530 2531 2532
| Name       | Type      | Readable  | Writable  | Description               |
| --------- | ---------- | ---- | ---- | ----------------- |
| bytesRead | number     | Yes   | Yes   | Length of the data read.          |
| offset    | number     | Yes   | Yes   | Position of the buffer to which the data will be read in reference to the start address of the buffer.|
| buffer    | ArrayBufer | Yes   | Yes   | Buffer for storing the data read.      |
A
annie_wangli 已提交
2533 2534 2535 2536 2537 2538


## Stat

Provides detailed file information. Before calling a method of the **Stat** class, use the [stat()](#fileiostat) method synchronously or asynchronously to create a **Stat** instance.

A
annie_wangli 已提交
2539
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
2540 2541 2542

### Attributes

A
annie_wangli 已提交
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556
| Name    | Type  | Readable  | Writable  | Description                                      |
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| dev    | number | Yes   | No   | Major device number.                           |
| ino    | number | Yes   | No   | File ID. Different files on the same device have different **ino**s.                |
| mode   | number | Yes   | No   | File type and permissions. The first four bits indicate the file type, and the last 12 bits indicate the permissions. The bit fields are described as follows:<br>-&nbsp;**0o170000**: mask used to obtain the file type.<br>-&nbsp;**0o140000**: The file is a socket.<br>-&nbsp;**0o120000**: The file is a symbolic link.<br>-&nbsp;**0o100000**: The file is a regular file.<br>-&nbsp;**0o060000**: The file is a block device.<br>-&nbsp;**0o040000**: The file is a directory.<br>-&nbsp;**0o020000**: The file is a character device.<br>-&nbsp;**0o010000**: The file is a named pipe (FIFO).<br>-&nbsp;**0o0700**: mask used to obtain the owner permissions.<br>-&nbsp;**0o0400**: The owner has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0100**: The owner has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0070**: mask used to obtain the user group permissions.<br>-&nbsp;**0o0040**: The user group has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0020**: The user group has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0010**: The user group has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0007**: mask used to obtain the permissions of other users.<br>-&nbsp;**0o0004**: Other users have the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0002**: Other users have the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0001**: Other users have the permission to execute a regular file or search for the specified path in a directory.|
| nlink  | number | Yes   | No   | Number of hard links in the file.                                |
| uid    | number | Yes   | No   | User ID, that is ID of the file owner.                               |
| gid    | number | Yes   | No   | Group ID, that is, ID of the user group of the file.                               |
| rdev   | number | Yes   | No   | Minor device number.                           |
| size   | number | Yes   | No   | File size, in bytes. This parameter is valid only for regular files.                  |
| blocks | number | Yes   | No   | Number of blocks occupied by a file. Each block is 512 bytes.                  |
| atime  | number | Yes   | No   | Time of the last access to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.       |
| mtime  | number | Yes   | No   | Time of the last modification to the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.       |
| ctime  | number | Yes   | No   | Time of the last status change of the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.      |
A
annie_wangli 已提交
2557 2558 2559 2560 2561 2562


### isBlockDevice

isBlockDevice(): boolean

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

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

A
Annie_wang 已提交
2567
**Return value**
A
Annie_wang 已提交
2568

A
annie_wangli 已提交
2569 2570
  | Type     | Description              |
  | ------- | ---------------- |
A
Annie_wang 已提交
2571
  | boolean | Whether the file is a block special file.|
A
annie_wangli 已提交
2572

A
Annie_wang 已提交
2573
**Example**
A
Annie_wang 已提交
2574

A
annie_wangli 已提交
2575 2576 2577 2578 2579 2580 2581 2582 2583
  ```js
  let isBLockDevice = fileio.statSync(path).isBlockDevice();
  ```


### isCharacterDevice

isCharacterDevice(): boolean

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

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

A
Annie_wang 已提交
2588
**Return value**
A
Annie_wang 已提交
2589

A
annie_wangli 已提交
2590 2591
  | Type     | Description               |
  | ------- | ----------------- |
A
Annie_wang 已提交
2592
  | boolean | Whether the file is a character special file.|
A
annie_wangli 已提交
2593

A
Annie_wang 已提交
2594
**Example**
A
Annie_wang 已提交
2595

A
annie_wangli 已提交
2596 2597 2598 2599 2600 2601 2602 2603 2604
  ```js
  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
  ```


### isDirectory

isDirectory(): boolean

A
Annie_wang 已提交
2605
Checks whether this file is a directory.
A
annie_wangli 已提交
2606

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

A
Annie_wang 已提交
2609
**Return value**
A
Annie_wang 已提交
2610

A
annie_wangli 已提交
2611 2612
  | Type     | Description           |
  | ------- | ------------- |
A
Annie_wang 已提交
2613
  | boolean | Whether the file is a directory.|
A
annie_wangli 已提交
2614

A
Annie_wang 已提交
2615
**Example**
A
Annie_wang 已提交
2616

A
annie_wangli 已提交
2617 2618 2619 2620 2621 2622 2623 2624 2625
  ```js
  let isDirectory = fileio.statSync(path).isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

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

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

A
Annie_wang 已提交
2630
**Return value**
A
Annie_wang 已提交
2631

A
annie_wangli 已提交
2632 2633
  | Type     | Description                   |
  | ------- | --------------------- |
A
Annie_wang 已提交
2634
  | boolean | Whether the file is an FIFO.|
A
annie_wangli 已提交
2635

A
Annie_wang 已提交
2636
**Example**
A
Annie_wang 已提交
2637

A
annie_wangli 已提交
2638 2639 2640 2641 2642 2643 2644 2645 2646
  ```js
  let isFIFO = fileio.statSync(path).isFIFO(); 
  ```


### isFile

isFile(): boolean

A
Annie_wang 已提交
2647
Checks whether this file is a regular file.
A
annie_wangli 已提交
2648

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

A
Annie_wang 已提交
2651
**Return value**
A
Annie_wang 已提交
2652

A
annie_wangli 已提交
2653 2654
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2655
  | boolean | Whether the file is a regular file.|
A
annie_wangli 已提交
2656

A
Annie_wang 已提交
2657
**Example**
A
Annie_wang 已提交
2658

A
annie_wangli 已提交
2659
  ```js
A
Annie_wang 已提交
2660
  let isFile = fileio.statSync(path).isFile();
A
annie_wangli 已提交
2661 2662 2663 2664 2665 2666 2667
  ```


### isSocket

isSocket(): boolean

A
Annie_wang 已提交
2668
Checks whether this file is a socket.
A
annie_wangli 已提交
2669

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

A
Annie_wang 已提交
2672
**Return value**
A
Annie_wang 已提交
2673

A
annie_wangli 已提交
2674 2675
  | Type     | Description            |
  | ------- | -------------- |
A
Annie_wang 已提交
2676
  | boolean | Whether the file is a socket.|
A
annie_wangli 已提交
2677

A
Annie_wang 已提交
2678
**Example**
A
Annie_wang 已提交
2679

A
annie_wangli 已提交
2680 2681 2682 2683 2684 2685 2686 2687 2688
  ```js
  let isSocket = fileio.statSync(path).isSocket(); 
  ```


### isSymbolicLink

isSymbolicLink(): boolean

A
Annie_wang 已提交
2689
Checks whether this file is a symbolic link.
A
annie_wangli 已提交
2690

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

A
Annie_wang 已提交
2693
**Return value**
A
Annie_wang 已提交
2694

A
annie_wangli 已提交
2695 2696
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2697
  | boolean | Whether the file is a symbolic link.|
A
annie_wangli 已提交
2698

A
Annie_wang 已提交
2699
**Example**
A
Annie_wang 已提交
2700

A
annie_wangli 已提交
2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712
  ```js
  let isSymbolicLink = fileio.statSync(path).isSymbolicLink(); 
  ```


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

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


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

A
annie_wangli 已提交
2713
stop(): Promise&lt;void&gt;
A
annie_wangli 已提交
2714

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

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

A
Annie_wang 已提交
2719
**Example**
A
Annie_wang 已提交
2720

A
annie_wangli 已提交
2721
  ```js
A
Annie_wang 已提交
2722 2723 2724 2725 2726 2727 2728
  let filename = path +"/test.txt";
  let watcher = await fileio.createWatcher(filename, 1, function(number){
      console.info("Monitoring times: "+number);
  });
  watcher.stop().then(function(){
       console.info("Watcher stopped");
  });
A
annie_wangli 已提交
2729 2730 2731 2732 2733
  ```


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

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

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

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

A
Annie_wang 已提交
2740
**Parameters**
A
Annie_wang 已提交
2741

A
annie_wangli 已提交
2742 2743 2744
  | Name     | Type                       | Mandatory  | Description                    |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when **watcher** is stopped asynchronously.|
A
annie_wangli 已提交
2745

A
Annie_wang 已提交
2746
**Example**
A
Annie_wang 已提交
2747

A
annie_wangli 已提交
2748
  ```js
A
Annie_wang 已提交
2749 2750 2751
  let filename = path +"/test.txt";
  let watcher = await fileio.createWatcher(filename, 1, function(number){
      console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2752
  });
A
Annie_wang 已提交
2753 2754 2755
  watcher.stop(function(){
      console.info("Watcher stopped");
  })
A
annie_wangli 已提交
2756
  ```
A
annie_wangli 已提交
2757

A
Annie_wang 已提交
2758
## Stream
A
annie_wangli 已提交
2759

A
Annie_wang 已提交
2760
Provides file stream management. Before calling a method of the **Stream** class, use the **createStream()** method synchronously or asynchronously to create a **Stream** instance.
A
annie_wangli 已提交
2761 2762 2763 2764 2765 2766


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

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

A
Annie_wang 已提交
2767
Closes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2768 2769 2770

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

A
Annie_wang 已提交
2771
**Return value**
A
Annie_wang 已提交
2772

A
annie_wangli 已提交
2773 2774 2775 2776
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream close result.|

A
Annie_wang 已提交
2777
**Example**
A
Annie_wang 已提交
2778

A
annie_wangli 已提交
2779
  ```js
A
Annie_wang 已提交
2780
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2781
  ss.close().then(function(){
A
Annie_wang 已提交
2782
      console.info("File stream closed");
A
annie_wangli 已提交
2783
  }).catch(function(err){
A
Annie_wang 已提交
2784
      console.info("Failed to close the file stream. Error:"+ err);
A
annie_wangli 已提交
2785 2786 2787 2788 2789 2790 2791 2792
  });
  ```


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

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

A
Annie_wang 已提交
2793
Closes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2794 2795 2796

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

A
Annie_wang 已提交
2797
**Parameters**
A
Annie_wang 已提交
2798

A
annie_wangli 已提交
2799 2800 2801 2802
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|

A
Annie_wang 已提交
2803
**Example**
A
Annie_wang 已提交
2804

A
annie_wangli 已提交
2805
  ```js
A
Annie_wang 已提交
2806
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2807
  ss.close(function (err) {
A
Annie_wang 已提交
2808
      // Do something
A
annie_wangli 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
  });
  ```


### closeSync

closeSync(): void

Synchronously closes the stream.

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

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

A
annie_wangli 已提交
2823
  ```js
A
Annie_wang 已提交
2824
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2825 2826 2827 2828 2829 2830 2831 2832
  ss.closeSync();
  ```


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

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

A
Annie_wang 已提交
2833
Flushes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2834 2835 2836

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

A
Annie_wang 已提交
2837
**Return value**
A
Annie_wang 已提交
2838

A
annie_wangli 已提交
2839 2840 2841 2842
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream flushing result.|

A
Annie_wang 已提交
2843
**Example**
A
Annie_wang 已提交
2844

A
annie_wangli 已提交
2845
  ```js
A
Annie_wang 已提交
2846
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2847
  ss.flush().then(function (){
A
Annie_wang 已提交
2848
      console.info("Stream flushed");
A
annie_wangli 已提交
2849
  }).catch(function(err){
A
Annie_wang 已提交
2850
      console.info("Failed to flush the stream. Error:"+ err);
A
annie_wangli 已提交
2851 2852 2853 2854 2855 2856 2857 2858
  });
  ```


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

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

A
Annie_wang 已提交
2859
Flushes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2860 2861 2862

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

A
Annie_wang 已提交
2863
**Parameters**
A
Annie_wang 已提交
2864

A
annie_wangli 已提交
2865 2866 2867 2868
  | Name     | Type                       | Mandatory  | Description            |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is asynchronously flushed.|

A
Annie_wang 已提交
2869
**Example**
A
Annie_wang 已提交
2870

A
annie_wangli 已提交
2871
  ```js
A
Annie_wang 已提交
2872
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2873
  ss.flush(function (err) {
A
Annie_wang 已提交
2874
      // Do something
A
annie_wangli 已提交
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
  });
  ```


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

flushSync(): void

Synchronously flushes the stream.

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

A
Annie_wang 已提交
2887
**Example**
A
Annie_wang 已提交
2888

A
annie_wangli 已提交
2889
  ```js
A
Annie_wang 已提交
2890
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
  ss.flushSync();
  ```


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

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

A
Annie_wang 已提交
2904
Writes data into the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2905 2906 2907

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

A
Annie_wang 已提交
2908
**Parameters**
A
Annie_wang 已提交
2909

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

A
Annie_wang 已提交
2915
**Return value**
A
Annie_wang 已提交
2916

A
annie_wangli 已提交
2917 2918
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
2919
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
A
annie_wangli 已提交
2920

A
Annie_wang 已提交
2921
**Example**
A
Annie_wang 已提交
2922

A
annie_wangli 已提交
2923
  ```js
A
Annie_wang 已提交
2924
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2925
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
A
Annie_wang 已提交
2926
      console.info("Data written to the stream. Size is:"+ number);
A
annie_wangli 已提交
2927
  }).catch(function(err){
A
Annie_wang 已提交
2928
      console.info("Failed to write data to the stream. Error:"+ err);
A
annie_wangli 已提交
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941
  });
  ```


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

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

A
Annie_wang 已提交
2942
Writes data into the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2943 2944 2945

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

A
Annie_wang 已提交
2946
**Parameters**
A
Annie_wang 已提交
2947

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

A
Annie_wang 已提交
2954
**Example**
A
Annie_wang 已提交
2955

A
annie_wangli 已提交
2956
  ```js
A
Annie_wang 已提交
2957
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2958
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
A
Annie_wang 已提交
2959
      if (bytesWritten) {
A
Annie_wang 已提交
2960
         // Do something
A
Annie_wang 已提交
2961
         console.info("Data written to the stream. Size is:"+ bytesWritten);
A
annie_wangli 已提交
2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
      }
  });
  ```


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

writeSync(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number

Synchronously writes data into the stream.

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

A
Annie_wang 已提交
2980
**Parameters**
A
Annie_wang 已提交
2981

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

A
Annie_wang 已提交
2987
**Return value**
A
Annie_wang 已提交
2988

A
annie_wangli 已提交
2989 2990 2991 2992
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data written in the file.|

A
Annie_wang 已提交
2993
**Example**
A
Annie_wang 已提交
2994

A
annie_wangli 已提交
2995
  ```js
A
Annie_wang 已提交
2996
  let ss= fileio.createStreamSync(path,"r+");
A
annie_wangli 已提交
2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


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

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

A
Annie_wang 已提交
3009
Reads data from the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
3010 3011 3012

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

A
Annie_wang 已提交
3013
**Parameters**
A
Annie_wang 已提交
3014

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

A
Annie_wang 已提交
3020
**Return value**
A
Annie_wang 已提交
3021

A
annie_wangli 已提交
3022 3023
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
3024
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
3025

A
Annie_wang 已提交
3026
**Example**
A
Annie_wang 已提交
3027

A
annie_wangli 已提交
3028
  ```js
A
Annie_wang 已提交
3029
  let ss = fileio.createStreamSync(path, "r+");
A
Annie_wang 已提交
3030
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut){
A
Annie_wang 已提交
3031
      console.info("Read data successfully");
A
Annie_wang 已提交
3032
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
3033
  }).catch(function(err){
A
Annie_wang 已提交
3034
      console.info("Failed to read data. Error:"+ err);
A
annie_wangli 已提交
3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
  });
  ```


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

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

A
Annie_wang 已提交
3047
Reads data from the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3048 3049 3050

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

A
Annie_wang 已提交
3051
**Parameters**
A
Annie_wang 已提交
3052

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

A
Annie_wang 已提交
3059
**Example**
A
Annie_wang 已提交
3060

A
annie_wangli 已提交
3061
  ```js
A
Annie_wang 已提交
3062
  let ss = fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
3063
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
A
Annie_wang 已提交
3064
      if (readOut) {
A
Annie_wang 已提交
3065
          console.info("Read data successfully");
A
Annie_wang 已提交
3066
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
      }
  });
  ```


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

readSync(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): number

Synchronously reads data from the stream.

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

A
Annie_wang 已提交
3084 3085
**Parameters**

A
annie_wangli 已提交
3086 3087 3088
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3089 3090 3091
  | options | Object      | No   | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): position of the data to read in the file. By default, data is read from the current position.<br>Constraints: offset + length <= Buffer size |

**Return value**
A
annie_wangli 已提交
3092 3093 3094 3095 3096

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

A
Annie_wang 已提交
3097
**Example**
A
Annie_wang 已提交
3098

A
annie_wangli 已提交
3099
  ```js
A
Annie_wang 已提交
3100
  let ss = fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
3101 3102 3103 3104 3105 3106
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
  ```


## Dir

A
Annie_wang 已提交
3107
Manages directories. Before calling a method of the **Dir** class, use the **opendir()** method synchronously or asynchronously to create a **Dir** instance.
A
annie_wangli 已提交
3108 3109 3110 3111 3112 3113


### read

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

A
Annie_wang 已提交
3114
Reads the next directory entry. This API uses a promise to return the result.
A
annie_wangli 已提交
3115 3116 3117

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

A
Annie_wang 已提交
3118
**Return value**
A
Annie_wang 已提交
3119

A
annie_wangli 已提交
3120 3121
  | Type                              | Description           |
  | -------------------------------- | ------------- |
A
Annie_wang 已提交
3122
  | Promise&lt;[Dirent](#dirent)&gt; | Promise used to return the directory entry read.|
A
annie_wangli 已提交
3123

A
Annie_wang 已提交
3124
**Example**
A
Annie_wang 已提交
3125

A
annie_wangli 已提交
3126 3127 3128
  ```js
  let dir = fileio.opendirSync(path);
  dir.read().then(function (dirent){
A
Annie_wang 已提交
3129
      console.log("Read the next directory entry:"+JSON.stringify(dirent));
A
annie_wangli 已提交
3130
  }).catch(function(err){
A
Annie_wang 已提交
3131
      console.info("Failed to read the next directory entry. Error:"+ err);
A
annie_wangli 已提交
3132 3133 3134 3135 3136 3137 3138 3139
  });
  ```


### read

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

A
Annie_wang 已提交
3140
Reads the next directory entry. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3141 3142 3143

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

A
Annie_wang 已提交
3144
**Parameters**
A
Annie_wang 已提交
3145

A
annie_wangli 已提交
3146 3147 3148 3149
  | Name     | Type                                    | Mandatory  | Description              |
  | -------- | -------------------------------------- | ---- | ---------------- |
  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | Yes   | Callback invoked when the next directory entry is asynchronously read.|

A
Annie_wang 已提交
3150
**Example**
A
Annie_wang 已提交
3151

A
annie_wangli 已提交
3152 3153 3154
  ```js
  let dir = fileio.opendirSync(path);
  dir.read(function (err, dirent) {
A
Annie_wang 已提交
3155
      if (dirent) {
A
Annie_wang 已提交
3156 3157
          // Do something
          console.log("Read the next directory entry:"+JSON.stringify(dirent));
A
annie_wangli 已提交
3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
      }
  });
  ```


### readSync

readSync(): Dirent

Synchronously reads the next directory entry.

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

A
Annie_wang 已提交
3171
**Return value**
A
Annie_wang 已提交
3172

A
annie_wangli 已提交
3173 3174 3175 3176
  | Type               | Description      |
  | ----------------- | -------- |
  | [Dirent](#dirent) | Directory entry read.|

A
Annie_wang 已提交
3177
**Example**
A
Annie_wang 已提交
3178

A
annie_wangli 已提交
3179 3180 3181 3182 3183 3184
  ```js
  let dir = fileio.opendirSync(path);
  let dirent = dir.readSync();
  ```


A
Annie_wang 已提交
3185 3186 3187 3188 3189 3190 3191 3192 3193
### close<sup>7+</sup>

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

Closes a directory. This API uses a promise to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir.

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

**Example**
A
Annie_wang 已提交
3194

A
Annie_wang 已提交
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
  ```js
  let dir = fileio.opendirSync(path);
  dir.close().then(function(err){
      console.info("close dir successfully");
  });
  ```


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

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

Closes a directory. This API uses an asynchronous callback to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir.

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

**Example**
A
Annie_wang 已提交
3212

A
Annie_wang 已提交
3213 3214 3215 3216 3217 3218 3219 3220
  ```js
  let dir = fileio.opendirSync(path);
  dir.close(function(err){
      console.info("close dir successfully");
  });
  ```


A
annie_wangli 已提交
3221 3222 3223 3224 3225 3226 3227 3228
### closeSync

closeSync(): void

Closes a directory. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir.

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

A
Annie_wang 已提交
3229
**Example**
A
Annie_wang 已提交
3230

A
annie_wangli 已提交
3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253
  ```js
  let dir = fileio.opendirSync(path);
  dir.closeSync();
  ```


## Dirent

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

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

### Attributes

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


### isBlockDevice

isBlockDevice(): boolean

A
Annie_wang 已提交
3254
Checks whether this directory entry is a block special file. A block special file supports access by block only, and it is cached when accessed.
A
annie_wangli 已提交
3255 3256 3257

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

A
Annie_wang 已提交
3258
**Return value**
A
Annie_wang 已提交
3259

A
annie_wangli 已提交
3260 3261 3262 3263
  | Type     | Description              |
  | ------- | ---------------- |
  | boolean | Whether the directory entry is a block special file.|

A
Annie_wang 已提交
3264
**Example**
A
Annie_wang 已提交
3265

A
annie_wangli 已提交
3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279
  ```js
  let dir = fileio.opendirSync(path);
  let isBLockDevice = dir.readSync().isBlockDevice();
  ```


### isCharacterDevice

isCharacterDevice(): boolean

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

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

A
Annie_wang 已提交
3280
**Return value**
A
Annie_wang 已提交
3281

A
annie_wangli 已提交
3282 3283 3284 3285
  | Type     | Description               |
  | ------- | ----------------- |
  | boolean | Whether the directory entry is a character special file.|

A
Annie_wang 已提交
3286
**Example**
A
Annie_wang 已提交
3287

A
annie_wangli 已提交
3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
  ```js
  let dir = fileio.opendirSync(path);
  let isCharacterDevice = dir.readSync().isCharacterDevice(); 
  ```


### isDirectory

isDirectory(): boolean

Checks whether a directory entry is a directory.

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

A
Annie_wang 已提交
3302
**Return value**
A
Annie_wang 已提交
3303

A
annie_wangli 已提交
3304 3305 3306 3307
  | Type     | Description           |
  | ------- | ------------- |
  | boolean | Whether the directory entry is a directory.|

A
Annie_wang 已提交
3308
**Example**
A
Annie_wang 已提交
3309

A
annie_wangli 已提交
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319
  ```js
  let dir = fileio.opendirSync(path);
  let isDirectory = dir.readSync().isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

A
Annie_wang 已提交
3320
Checks whether this directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication.
A
annie_wangli 已提交
3321 3322 3323

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

A
Annie_wang 已提交
3324
**Return value**
A
Annie_wang 已提交
3325

A
annie_wangli 已提交
3326 3327 3328 3329
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a FIFO.|

A
Annie_wang 已提交
3330
**Example**
A
Annie_wang 已提交
3331

A
annie_wangli 已提交
3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345
  ```js
  let dir = fileio.opendirSync(path);
  let isFIFO = dir.readSync().isFIFO(); 
  ```


### isFile

isFile(): boolean

Checks whether a directory entry is a regular file.

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

A
Annie_wang 已提交
3346
**Return value**
A
Annie_wang 已提交
3347

A
annie_wangli 已提交
3348 3349 3350 3351
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a regular file.|

A
Annie_wang 已提交
3352
**Example**
A
Annie_wang 已提交
3353

A
annie_wangli 已提交
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
  ```js
  let dir = fileio.opendirSync(path);
  let isFile = dir.readSync().isFile(); 
  ```


### isSocket

isSocket(): boolean

Checks whether a directory entry is a socket.

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

A
Annie_wang 已提交
3368
**Return value**
A
Annie_wang 已提交
3369

A
annie_wangli 已提交
3370 3371 3372 3373
  | Type     | Description            |
  | ------- | -------------- |
  | boolean | Whether the directory entry is a socket.|

A
Annie_wang 已提交
3374
**Example**
A
Annie_wang 已提交
3375

A
annie_wangli 已提交
3376
  ```js
A
Annie_wang 已提交
3377
  let dir = fileio.opendirSync(path);
A
annie_wangli 已提交
3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
  let isSocket = dir.readSync().isSocket(); 
  ```


### isSymbolicLink

isSymbolicLink(): boolean

Checks whether a directory entry is a symbolic link.

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

A
Annie_wang 已提交
3390
**Return value**
A
Annie_wang 已提交
3391

A
annie_wangli 已提交
3392 3393 3394 3395
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a symbolic link.|

A
Annie_wang 已提交
3396
**Example**
A
Annie_wang 已提交
3397

A
annie_wangli 已提交
3398 3399 3400 3401
  ```js
  let dir = fileio.opendirSync(path);
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```