js-apis-fileio.md 133.2 KB
Newer Older
A
Annie_wang 已提交
1
File Management
Z
zengyawen 已提交
2

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

A
Annie_wang 已提交
6
Provides file storage and management capabilities, including basic file management, file directory management, file information statistics, and stream read and write.
A
Annie_wang 已提交
7

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

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

A
annie_wangli 已提交
14

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

A
Annie_wang 已提交
17 18 19 20 21 22 23 24 25
Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows:
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
 let context = featureAbility.getContext();
 let path = '';
 context.getFilesDir().then((data) => {
      path = data;
 })
 ```
Z
zengyawen 已提交
26 27


A
annie_wangli 已提交
28 29 30 31
## fileio.stat

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

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

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

A
Annie_wang 已提交
36 37
**Parameters**

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

A
Annie_wang 已提交
42 43
**Return value**

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

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


## fileio.stat

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

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

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

A
Annie_wang 已提交
66
**Parameters**
A
Annie_wang 已提交
67 68
| Name  | Type                              | Mandatory| Description                          |
| -------- | ---------------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
69
| path     | string                             | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
70
| callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes  | Callback invoked to return the file information obtained.|
A
annie_wangli 已提交
71

A
Annie_wang 已提交
72
**Example**
A
annie_wangli 已提交
73 74 75 76 77 78 79 80 81 82
  ```js
  fileio.stat(path, function (err, stat) {
      // Example code in Stat
  });
  ```


## fileio.statSync

statSync(path:string): Stat
Z
zengyawen 已提交
83 84 85

Synchronously obtains file information.

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

A
Annie_wang 已提交
88
**Parameters**
A
Annie_wang 已提交
89 90
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
91
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
92 93


A
Annie_wang 已提交
94
**Return value**
A
annie_wangli 已提交
95 96
  | Type           | Description        |
  | ------------- | ---------- |
A
annie_wangli 已提交
97 98
  | [Stat](#stat) | File information obtained.|

A
Annie_wang 已提交
99
**Example**
A
annie_wangli 已提交
100 101 102 103 104 105 106 107 108 109
  ```js
  let stat = fileio.statSync(path);
  // Example code in Stat
  ```


## fileio.opendir

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

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

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

A
Annie_wang 已提交
114
**Parameters**
A
Annie_wang 已提交
115 116 117
| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
118

A
Annie_wang 已提交
119
**Return value**
A
annie_wangli 已提交
120 121
  | Type                        | Description      |
  | -------------------------- | -------- |
A
Annie_wang 已提交
122
  | Promise&lt;[Dir](#dir)&gt; | Promise used to return the **Dir** object.|
A
annie_wangli 已提交
123

A
Annie_wang 已提交
124
**Example**
A
annie_wangli 已提交
125 126
  ```js
  fileio.opendir(path).then(function(dir){
A
Annie_wang 已提交
127
      console.info("Directory opened:"+ JSON.stringify(dir));
A
annie_wangli 已提交
128
  }).catch(function(err){
A
Annie_wang 已提交
129
      console.info("Failed to open the directory. Error:"+ err);
A
annie_wangli 已提交
130 131 132 133 134 135 136 137
  });
  ```


## fileio.opendir

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

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

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

A
Annie_wang 已提交
142 143
**Parameters**

A
Annie_wang 已提交
144 145 146 147
| 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 已提交
148

A
Annie_wang 已提交
149
**Example**
A
annie_wangli 已提交
150 151 152
  ```js
  fileio.opendir(path, function (err, dir) { 
      // Example code in Dir struct
A
annie_wangli 已提交
153
      // Use read/readSync/close.
A
annie_wangli 已提交
154 155 156 157 158 159 160
  });
  ```


## fileio.opendirSync

opendirSync(path: string): Dir
Z
zengyawen 已提交
161 162 163

Synchronously opens a directory.

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

A
annie_wangli 已提交
166

A
Annie_wang 已提交
167 168
**Parameters**

A
Annie_wang 已提交
169 170 171
| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
172

A
Annie_wang 已提交
173
**Return value**
A
annie_wangli 已提交
174 175
  | Type         | Description      |
  | ----------- | -------- |
A
annie_wangli 已提交
176 177
  | [Dir](#dir) | A **Dir** instance corresponding to the directory.|

A
Annie_wang 已提交
178
**Example**
A
annie_wangli 已提交
179 180 181 182 183 184 185 186 187 188 189
  ```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 已提交
190
Checks whether the current process can access a file. This API uses a promise to return the result.
A
annie_wangli 已提交
191

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

A
Annie_wang 已提交
194 195
**Parameters**

A
Annie_wang 已提交
196 197
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
198
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
199
| 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 已提交
200

A
Annie_wang 已提交
201
**Return value**
A
annie_wangli 已提交
202 203
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
204
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
205

A
Annie_wang 已提交
206
**Example**
A
annie_wangli 已提交
207 208
  ```js
  fileio.access(path).then(function() {
A
Annie_wang 已提交
209
      console.info("Access successful");
A
annie_wangli 已提交
210
  }).catch(function(err){
A
Annie_wang 已提交
211
      console.info("Access failed. Error:"+ err);
A
annie_wangli 已提交
212 213 214 215 216 217
  });
  ```


## fileio.access

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

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

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

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

A
Annie_wang 已提交
231
**Example**
A
annie_wangli 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244
  ```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 已提交
245
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
246

A
Annie_wang 已提交
247
**Parameters**
A
Annie_wang 已提交
248 249
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
250
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
251
| 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 已提交
252

A
Annie_wang 已提交
253
**Example**
A
annie_wangli 已提交
254 255 256 257
  ```js
  try {
      fileio.accessSync(path);
  } catch(err) {
A
Annie_wang 已提交
258
      console.info("accessSync failed. Error:"+ err);
A
annie_wangli 已提交
259 260 261 262 263 264 265 266
  }
  ```


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

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

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

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

A
Annie_wang 已提交
271
**Parameters**
A
annie_wangli 已提交
272 273 274
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
275

A
Annie_wang 已提交
276
**Return value**
A
annie_wangli 已提交
277 278
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
279
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
280

A
Annie_wang 已提交
281
**Example**
A
annie_wangli 已提交
282 283 284
  ```js
  let fd = fileio.openSync(path);
  fileio.close(fd).then(function(){
A
Annie_wang 已提交
285
      console.info("File closed");
A
annie_wangli 已提交
286
  }).catch(function(err){
A
Annie_wang 已提交
287
      console.info("Failed to close the file. Error:"+ err);
A
annie_wangli 已提交
288 289 290 291 292 293 294 295
  });
  ```


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

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

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

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

A
Annie_wang 已提交
300
**Parameters**
A
annie_wangli 已提交
301 302 303 304
  | 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 已提交
305

A
Annie_wang 已提交
306
**Example**
A
annie_wangli 已提交
307 308 309 310 311 312 313 314 315 316 317
  ```js
  let fd = fileio.openSync(path);
  fileio.close(fd, function (err) {
      // Do something.
  });
  ```


## fileio.closeSync

closeSync(fd: number): void
Z
zengyawen 已提交
318 319 320

Synchronously closes a file.

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

A
Annie_wang 已提交
323
**Parameters**
A
annie_wangli 已提交
324 325 326
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
327

A
Annie_wang 已提交
328
**Example**
A
annie_wangli 已提交
329 330 331 332 333 334 335 336 337
  ```js
  fileio.closeSync(fd);
  ```


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

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

A
Annie_wang 已提交
338
Closes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
339

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

A
Annie_wang 已提交
342
**Return value**
A
annie_wangli 已提交
343 344
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
345
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
346

A
Annie_wang 已提交
347
**Example**
A
annie_wangli 已提交
348 349
  ```js
  fileio.close().then(function(){
A
Annie_wang 已提交
350
      console.info("File stream closed");
A
annie_wangli 已提交
351
  }).catch(function(err){
A
Annie_wang 已提交
352
      console.info("Failed to close the file stream. Error:"+ err);
A
annie_wangli 已提交
353 354 355 356 357 358 359 360
  });
  ```


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

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

A
Annie_wang 已提交
361
Closes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
362

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

A
Annie_wang 已提交
365
**Parameters**
A
annie_wangli 已提交
366 367 368
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|
A
annie_wangli 已提交
369

A
Annie_wang 已提交
370
**Example**
A
annie_wangli 已提交
371 372 373 374 375 376 377 378 379 380 381
  ```js
  fileio.close(function(err){
      // Do something.
  });
  ```


## fileio.copyFile

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

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

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

A
Annie_wang 已提交
386
**Parameters**
A
annie_wangli 已提交
387 388 389 390 391
  | 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 已提交
392

A
Annie_wang 已提交
393
**Return value**
A
annie_wangli 已提交
394 395
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
396
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
397

A
Annie_wang 已提交
398
**Example**
A
annie_wangli 已提交
399 400
  ```js
  fileio.copyFile(src, dest).then(function(){
A
Annie_wang 已提交
401
      console.info("File copied");
A
annie_wangli 已提交
402
  }).catch(function(err){
A
Annie_wang 已提交
403
      console.info("Failed to copy the file. Error:"+ err);
A
annie_wangli 已提交
404 405 406 407 408 409
  });
  ```


## fileio.copyFile

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

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

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

A
Annie_wang 已提交
416
**Parameters**
A
annie_wangli 已提交
417 418 419 420 421 422
  | 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 已提交
423

A
Annie_wang 已提交
424
**Example**
A
annie_wangli 已提交
425 426 427 428 429 430 431 432 433
  ```js
  fileio.copyFile(src, dest, function (err) {
      // Do something.
  });
  ```


## fileio.copyFileSync

A
annie_wangli 已提交
434
copyFileSync(src: string | number, dest: string | number, mode?: number): void
Z
zengyawen 已提交
435 436 437

Synchronously copies a file.

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

A
Annie_wang 已提交
440
**Parameters**
A
annie_wangli 已提交
441 442 443 444 445
  | 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 已提交
446

A
Annie_wang 已提交
447
**Example**
A
annie_wangli 已提交
448 449 450 451 452 453 454 455 456
  ```js
  fileio.copyFileSync(src, dest);
  ```


## fileio.mkdir

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

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

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

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

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

A
Annie_wang 已提交
472
**Example**
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 492
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
493
| path     | string                    | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
494 495
| 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 已提交
496

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


## fileio.mkdirSync

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

Synchronously creates a directory.

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

A
Annie_wang 已提交
513
**Parameters**
A
Annie_wang 已提交
514 515
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
516
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
517
| 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 已提交
518

A
Annie_wang 已提交
519
**Example**
A
annie_wangli 已提交
520 521 522 523 524 525 526 527 528
  ```js
  fileio.mkdirSync(path);
  ```


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

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

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

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

A
Annie_wang 已提交
533
**Parameters**
A
Annie_wang 已提交
534 535
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
536 537
| 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 已提交
538
| 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 已提交
539

A
Annie_wang 已提交
540
**Return value**
A
annie_wangli 已提交
541 542
  | Type                   | Description         |
  | --------------------- | ----------- |
A
Annie_wang 已提交
543
  | Promise&lt;number&gt; | Promise used to return the file descriptor of the file opened.|
A
annie_wangli 已提交
544

A
Annie_wang 已提交
545
**Example**
A
annie_wangli 已提交
546 547
  ```js
  fileio.open(path, 0o1, 0o0200).then(function(number){
A
Annie_wang 已提交
548 549 550
      console.info("File opened");
  }).catch(function(err){
      console.info("Failed to open the file. Error:"+ err);
A
annie_wangli 已提交
551 552 553 554 555 556 557 558
  });
  ```


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

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

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

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

A
Annie_wang 已提交
563
**Parameters**
A
Annie_wang 已提交
564 565
| Name  | Type                           | Mandatory| Description                                                        |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
566 567
| 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 已提交
568 569
| 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 已提交
570

A
Annie_wang 已提交
571
**Example**
A
annie_wangli 已提交
572 573 574 575 576 577 578 579 580 581
  ```js
  fileio.open(path, 0, function(err, fd) {
      // Do something.
  });
  ```


## fileio.openSync

openSync(path:string, flags?:number, mode?:number): number
Z
zengyawen 已提交
582 583 584

Synchronously opens a file.

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

A
Annie_wang 已提交
587
**Parameters**
A
Annie_wang 已提交
588 589
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
590 591 592
| 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 已提交
593

A
Annie_wang 已提交
594
**Return value**
A
annie_wangli 已提交
595 596
  | Type    | Description         |
  | ------ | ----------- |
A
annie_wangli 已提交
597 598
  | number | File descriptor of the file opened.|

A
Annie_wang 已提交
599
**Example**
A
annie_wangli 已提交
600
  ```js
A
Annie_wang 已提交
601 602 603 604 605 606 607 608 609
  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 已提交
610 611 612 613 614
  ```


## fileio.read

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

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

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

A
Annie_wang 已提交
625 626 627 628 629 630 631 632
**Parameters**
| Name | Type       | Mandatory| Description                                                        |
| ------- | ----------- | ---- | ------------------------------------------------------------ |
| fd      | number      | Yes  | File descriptor of the file to read.                                    |
| buffer  | ArrayBuffer | Yes  | Buffer used to store the file data read.                          |
| options | Object      | No  | The options are as follows:<br>-&nbsp;**offset** (number): position to store the data read in the buffer in reference to the start address of the buffer. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to 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 已提交
633

A
annie_wangli 已提交
634 635
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
636
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
637

A
Annie_wang 已提交
638
**Example**
A
annie_wangli 已提交
639 640 641
  ```js
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
A
Annie_wang 已提交
642 643
  fileio.read(fd, buf).then(function(readOut){
      console.info("Read file data");
A
Annie_wang 已提交
644
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
Annie_wang 已提交
645 646
  }).catch(function(err){
      console.info("Failed to read file data. Error:"+ err);
A
annie_wangli 已提交
647 648 649 650 651 652
  });
  ```


## fileio.read

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

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

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

A
Annie_wang 已提交
663
**Parameters**
A
annie_wangli 已提交
664 665 666 667
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                                   | Yes   | File descriptor of the file to read.                            |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file data read.                       |
A
Annie_wang 已提交
668
  | 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 已提交
669
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously.                            |
A
annie_wangli 已提交
670

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


## fileio.readSync

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

Synchronously reads data from a file.

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

A
Annie_wang 已提交
696
**Parameters**
A
annie_wangli 已提交
697 698 699 700
  | 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 已提交
701
  | 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 已提交
702

A
Annie_wang 已提交
703
**Return value**
A
annie_wangli 已提交
704 705
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
706 707
  | number | Length of the data read.|

A
Annie_wang 已提交
708
**Example**
A
annie_wangli 已提交
709 710 711 712 713 714 715 716 717 718 719
  ```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 已提交
720
Deletes a directory. This API uses a promise to return the result.
A
annie_wangli 已提交
721

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

A
Annie_wang 已提交
724
**Parameters**
A
Annie_wang 已提交
725 726
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
727
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
728

A
Annie_wang 已提交
729
**Return value**
A
annie_wangli 已提交
730 731
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
732
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
733

A
Annie_wang 已提交
734
**Example**
A
annie_wangli 已提交
735 736
  ```js
  fileio.rmdir(path).then(function() {
A
Annie_wang 已提交
737
      console.info("Directory deleted");
A
annie_wangli 已提交
738
  }).catch(function(err){
A
Annie_wang 已提交
739
      console.info("Failed to delete the directory. Error:"+ err);
A
annie_wangli 已提交
740 741 742 743 744 745 746 747
  });
  ```


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

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

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

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

A
Annie_wang 已提交
752
**Parameters**
A
Annie_wang 已提交
753 754
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
755
| path     | string                    | Yes  | Application sandbox path of the directory.|
A
Annie_wang 已提交
756
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is deleted asynchronously.  |
A
annie_wangli 已提交
757

A
Annie_wang 已提交
758
**Example**
A
annie_wangli 已提交
759 760 761
  ```js
  fileio.rmdir(path, function(err){
      // Do something.
A
Annie_wang 已提交
762
      console.info("Directory deleted");
A
annie_wangli 已提交
763 764 765 766
  });
  ```


A
annie_wangli 已提交
767
## fileio.rmdirSync<sup>7+</sup>
A
annie_wangli 已提交
768

A
annie_wangli 已提交
769
rmdirSync(path: string): void
A
annie_wangli 已提交
770 771 772

Synchronously deletes a directory.

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

A
Annie_wang 已提交
775
**Parameters**
A
Annie_wang 已提交
776 777
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
778
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
779

A
Annie_wang 已提交
780
**Example**
A
annie_wangli 已提交
781 782 783 784 785 786 787 788 789
  ```js
  fileio.rmdirSync(path);
  ```


## fileio.unlink

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

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

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

A
Annie_wang 已提交
794
**Parameters**
A
Annie_wang 已提交
795 796
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
797
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
798

A
Annie_wang 已提交
799
**Return value**
A
annie_wangli 已提交
800 801
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
802
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
803

A
Annie_wang 已提交
804
**Example**
A
annie_wangli 已提交
805 806
  ```js
  fileio.unlink(path).then(function(){
A
Annie_wang 已提交
807
      console.info("File deleted");
A
annie_wangli 已提交
808
  }).catch(function(error){
A
Annie_wang 已提交
809
      console.info("Failed to delete the file. Error:"+ error);
A
annie_wangli 已提交
810 811 812 813 814 815 816 817
  });
  ```


## fileio.unlink

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

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

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

A
Annie_wang 已提交
822
**Parameters**
A
Annie_wang 已提交
823 824
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
825
| path     | string                    | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
826
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is deleted asynchronously.  |
A
annie_wangli 已提交
827

A
Annie_wang 已提交
828
**Example**
A
annie_wangli 已提交
829 830
  ```js
  fileio.unlink(path, function(err) {
A
Annie_wang 已提交
831
      console.info("File deleted");
A
annie_wangli 已提交
832 833 834 835 836 837 838
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void
Z
zengyawen 已提交
839 840 841

Synchronously deletes a file.

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

A
Annie_wang 已提交
844
**Parameters**
A
Annie_wang 已提交
845 846
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
847
| path   | string | Yes  | Application sandbox path of the file.|
Z
zengyawen 已提交
848

A
Annie_wang 已提交
849
**Example**
A
annie_wangli 已提交
850 851 852
  ```js
  fileio.unlinkSync(path);
  ```
Z
zengyawen 已提交
853 854


A
annie_wangli 已提交
855
## fileio.write
Z
zengyawen 已提交
856

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

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

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

A
Annie_wang 已提交
868
**Parameters**
A
annie_wangli 已提交
869 870 871 872
  | 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 已提交
873
  | 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 已提交
874

A
Annie_wang 已提交
875
**Return value**
A
annie_wangli 已提交
876 877
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
878
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
Z
zengyawen 已提交
879

A
Annie_wang 已提交
880
**Example**
A
annie_wangli 已提交
881
  ```js
A
Annie_wang 已提交
882
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
A
annie_wangli 已提交
883
  fileio.write(fd, "hello, world").then(function(number){
A
Annie_wang 已提交
884
       console.info("Data written to file and size is:"+ number);
A
annie_wangli 已提交
885
  }).catch(function(err){
A
Annie_wang 已提交
886
      console.info("Failed to write data to the file. Error:"+ err);
A
annie_wangli 已提交
887 888
  });
  ```
Z
zengyawen 已提交
889 890


A
annie_wangli 已提交
891
## fileio.write
Z
zengyawen 已提交
892

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

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

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

A
Annie_wang 已提交
904
**Parameters**
A
annie_wangli 已提交
905 906 907 908
  | Name     | Type                             | Mandatory  | Description                                      |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | Yes   | File descriptor of the file to write.                            |
  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | Yes   | Data to write. It can be a string or data from a buffer.                    |
A
Annie_wang 已提交
909
  | 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 已提交
910
  | callback | AsyncCallback&lt;number&gt;     | Yes   | Callback invoked when the data is written asynchronously.                      |
Z
zengyawen 已提交
911

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


A
annie_wangli 已提交
923
## fileio.writeSync
Z
zengyawen 已提交
924

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

A
annie_wangli 已提交
932
Synchronously writes data into a file.
Z
zengyawen 已提交
933

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

A
Annie_wang 已提交
936
**Parameters**
A
annie_wangli 已提交
937 938 939 940
  | 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 已提交
941
  | 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 已提交
942

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

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


A
annie_wangli 已提交
955
## fileio.hash
Z
zengyawen 已提交
956

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

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

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

A
Annie_wang 已提交
963
**Parameters**
A
Annie_wang 已提交
964 965
| Name   | Type  | Mandatory| Description                                                        |
| --------- | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
966
| path      | string | Yes  | Application sandbox path of the file.                            |
A
Annie_wang 已提交
967
| 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 已提交
968

A
Annie_wang 已提交
969
**Return value**
A
annie_wangli 已提交
970 971
  | Type                   | Description                        |
  | --------------------- | -------------------------- |
A
Annie_wang 已提交
972
  | 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 已提交
973

A
Annie_wang 已提交
974
**Example**
A
annie_wangli 已提交
975 976
  ```js
  fileio.hash(path, "sha256").then(function(str){
A
Annie_wang 已提交
977
      console.info("Calculated file hash:"+ str);
A
annie_wangli 已提交
978
  }).catch(function(error){
A
Annie_wang 已提交
979
      console.info("Failed to calculate the file hash. Error:"+ err);
A
annie_wangli 已提交
980 981
  });
  ```
Z
zengyawen 已提交
982 983


A
annie_wangli 已提交
984
## fileio.hash
Z
zengyawen 已提交
985

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

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

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

A
Annie_wang 已提交
992
**Parameters**
A
Annie_wang 已提交
993 994
| Name   | Type                       | Mandatory| Description                                                        |
| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
995
| path      | string                      | Yes  | Application sandbox path of the file.                            |
A
Annie_wang 已提交
996
| 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 已提交
997
| 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 已提交
998

A
Annie_wang 已提交
999
**Example**
A
annie_wangli 已提交
1000
  ```js
A
Annie_wang 已提交
1001
  fileio.hash(path, "sha256", function(err, hashStr) {
A
Annie_wang 已提交
1002
      if (hashStr) {
A
Annie_wang 已提交
1003
          console.info("Calculated file hash:"+ hashStr);
A
annie_wangli 已提交
1004 1005 1006
      }
  });
  ```
Z
zengyawen 已提交
1007 1008


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

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

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

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

A
Annie_wang 已提交
1017
**Parameters**
A
Annie_wang 已提交
1018 1019
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1020
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1021
| 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 已提交
1022

A
Annie_wang 已提交
1023
**Return value**
A
annie_wangli 已提交
1024 1025
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1026
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1027

A
Annie_wang 已提交
1028
**Example**
A
annie_wangli 已提交
1029 1030
  ```js
  fileio.chmod(path, mode).then(function() {
A
Annie_wang 已提交
1031
      console.info("File permissions changed");
A
annie_wangli 已提交
1032
  }).catch(function(err){
A
Annie_wang 已提交
1033
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
1034 1035
  });
  ```
Z
zengyawen 已提交
1036 1037


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

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

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

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

A
Annie_wang 已提交
1046
**Parameters**
A
Annie_wang 已提交
1047 1048
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1049
| path     | string                    | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1050 1051
| 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 已提交
1052

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


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

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

A
Annie_wang 已提交
1065
Synchronously changes file permissions.
Z
zengyawen 已提交
1066

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

A
Annie_wang 已提交
1069
**Parameters**
A
Annie_wang 已提交
1070 1071
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1072
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1073
| 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 已提交
1074

A
Annie_wang 已提交
1075
**Example**
A
annie_wangli 已提交
1076
  ```js
A
Annie_wang 已提交
1077
  fileio.chmodSync(path, mode);
A
annie_wangli 已提交
1078
  ```
Z
zengyawen 已提交
1079 1080


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

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

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

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

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

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

A
Annie_wang 已提交
1099
**Example**
A
annie_wangli 已提交
1100 1101
  ```js
  fileio.fstat(fd).then(function(stat){
A
Annie_wang 已提交
1102
      console.info("File information obtained:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1103
  }).catch(function(err){
A
Annie_wang 已提交
1104
      console.info("Failed to obtain file information. Error:"+ err);
A
annie_wangli 已提交
1105 1106
  });
  ```
Z
zengyawen 已提交
1107 1108


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

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

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

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

A
Annie_wang 已提交
1117
**Parameters**
A
annie_wangli 已提交
1118 1119 1120
  | Name     | Type                                | Mandatory  | Description              |
  | -------- | ---------------------------------- | ---- | ---------------- |
  | fd       | number                             | Yes   | File descriptor of the target file.    |
A
Annie_wang 已提交
1121
  | callback | AsyncCallback&lt;[Stat](#stat)&gt; | Yes   | Callback invoked to return the file information obtained.|
Z
zengyawen 已提交
1122

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


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

A
annie_wangli 已提交
1134
fstatSync(fd: number): Stat
Z
zengyawen 已提交
1135

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

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

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

A
Annie_wang 已提交
1145
**Return value**
A
annie_wangli 已提交
1146 1147
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1148
  | [Stat](#stat) | File information obtained.|
Z
zengyawen 已提交
1149

A
Annie_wang 已提交
1150
**Example**
A
annie_wangli 已提交
1151 1152 1153 1154
  ```js
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```
Z
zengyawen 已提交
1155 1156


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

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

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

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

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

A
Annie_wang 已提交
1171
**Return value**
A
annie_wangli 已提交
1172 1173
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1174
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1175

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


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

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

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

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

A
Annie_wang 已提交
1195
**Parameters**
A
annie_wangli 已提交
1196 1197 1198 1199
  | 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 已提交
1200
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback that returns no value. |
Z
zengyawen 已提交
1201

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


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

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

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

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

A
Annie_wang 已提交
1218
**Parameters**
A
annie_wangli 已提交
1219 1220 1221 1222
  | 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 已提交
1223

A
Annie_wang 已提交
1224
**Example**
A
annie_wangli 已提交
1225
  ```js
P
Peter_1988 已提交
1226
  fileio.ftruncateSync(fd, len);
A
annie_wangli 已提交
1227
  ```
Z
zengyawen 已提交
1228 1229


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

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

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

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

A
Annie_wang 已提交
1238
**Parameters**
A
Annie_wang 已提交
1239 1240 1241
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
| path   | string | Yes  | Application sandbox path of the file to truncate.      |
A
Annie_wang 已提交
1242
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1243

A
Annie_wang 已提交
1244
**Return value**
A
annie_wangli 已提交
1245 1246
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1247
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1248

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


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

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

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

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

A
Annie_wang 已提交
1267
**Parameters**
A
Annie_wang 已提交
1268 1269
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1270
| path     | string                    | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1271
| len      | number                    | Yes  | File length, in bytes, after truncation.|
A
Annie_wang 已提交
1272
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback that returns no value.  |
Z
zengyawen 已提交
1273

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


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

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

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

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

A
Annie_wang 已提交
1290
**Parameters**
A
Annie_wang 已提交
1291 1292
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
A
Annie_wang 已提交
1293
| path   | string | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1294
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1295

A
Annie_wang 已提交
1296
**Example**
A
annie_wangli 已提交
1297
  ```js
P
Peter_1988 已提交
1298
  fileio.truncateSync(path, len);
A
annie_wangli 已提交
1299
  ```
Z
zengyawen 已提交
1300 1301


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

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

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

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

A
Annie_wang 已提交
1314
**Parameters**
A
Annie_wang 已提交
1315 1316 1317 1318
| 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 已提交
1319

A
Annie_wang 已提交
1320
**Return value**
A
annie_wangli 已提交
1321 1322
  | Type                   | Description        |
  | --------------------- | ---------- |
A
Annie_wang 已提交
1323
  | Promise&lt;string&gt; | Promise used to return the content read.|
Z
zengyawen 已提交
1324

A
Annie_wang 已提交
1325
**Example**
A
annie_wangli 已提交
1326 1327
  ```js
  fileio.readText(path).then(function(str) {
A
Annie_wang 已提交
1328
      console.info("Read file text:"+ str);
A
annie_wangli 已提交
1329
  }).catch(function(err){
A
Annie_wang 已提交
1330
      console.info("Failed to read text. Error:"+ err);
A
annie_wangli 已提交
1331 1332
  });
  ```
Z
zengyawen 已提交
1333 1334


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

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

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

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

A
Annie_wang 已提交
1347
**Parameters**
A
Annie_wang 已提交
1348 1349 1350
| Name  | Type                       | Mandatory| Description                                                        |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| filePath | string                      | Yes  | Application sandbox path of the file to read.                                  |
A
Annie_wang 已提交
1351 1352
| 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**: format of the string to be encoded. The default value is &nbsp;**utf-8**, which is the only value supported.|
| callback | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the content read.                        |
Z
zengyawen 已提交
1353

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


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

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

Synchronously reads the text of a file. 

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

A
Annie_wang 已提交
1374
**Parameters**
A
Annie_wang 已提交
1375 1376
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1377
| filePath | string | Yes  | Application sandbox path of the file to read.                                  |
A
Annie_wang 已提交
1378
| 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 已提交
1379

A
Annie_wang 已提交
1380
**Return value**
A
annie_wangli 已提交
1381 1382
  | Type  | Description                |
  | ------ | -------------------- |
A
Annie_wang 已提交
1383
  | string | Promise used to return the content of the file read.|
A
annie_wangli 已提交
1384

A
Annie_wang 已提交
1385
**Example**
A
annie_wangli 已提交
1386 1387 1388 1389 1390 1391 1392 1393 1394
  ```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 已提交
1395
Obtains link information. This API uses a promise to return the result.
A
annie_wangli 已提交
1396

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

A
Annie_wang 已提交
1399
**Parameters**
A
Annie_wang 已提交
1400 1401
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1402
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1403

A
Annie_wang 已提交
1404
**Return value**
A
annie_wangli 已提交
1405 1406
  | Type                          | Description        |
  | ---------------------------- | ---------- |
A
Annie_wang 已提交
1407
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the link information obtained. For details, see [Stat](#stat).|
A
annie_wangli 已提交
1408

A
Annie_wang 已提交
1409
**Example**
A
annie_wangli 已提交
1410 1411
  ```js
  fileio.lstat(path).then(function(stat){
A
Annie_wang 已提交
1412
      console.info("Got link info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
  }).catch(function(err){
      console.info("Failed to obtain the link status. Error:"+ err);
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1427
**Parameters**
A
Annie_wang 已提交
1428 1429
| Name  | Type                              | Mandatory| Description                                  |
| -------- | ---------------------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1430 1431
| 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 已提交
1432

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


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

lstatSync(path:string): Stat

A
Annie_wang 已提交
1445
Synchronously obtains the link information.
A
annie_wangli 已提交
1446

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

A
Annie_wang 已提交
1449
**Parameters**
A
Annie_wang 已提交
1450 1451
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1452
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1453

A
Annie_wang 已提交
1454
**Return value**
A
annie_wangli 已提交
1455 1456
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1457
  | [Stat](#stat) | Link information obtained.|
A
annie_wangli 已提交
1458

A
Annie_wang 已提交
1459
**Example**
A
annie_wangli 已提交
1460 1461 1462 1463 1464 1465 1466
  ```js
  let stat = fileio.lstatSync(path);
  ```


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

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

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

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

A
Annie_wang 已提交
1477
**Parameters**
A
Annie_wang 已提交
1478 1479 1480
  | Name | Type       | Mandatory| Description                                                        |
  | ------- | ----------- | ---- | ------------------------------------------------------------ |
  | buffer  | ArrayBuffer | Yes  | Buffer used to store the file data read.                          |
A
Annie_wang 已提交
1481
  | 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>Constraints: offset + length <= Buffer size|
A
annie_wangli 已提交
1482

A
Annie_wang 已提交
1483
**Return value**
A
annie_wangli 已提交
1484 1485
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
1486
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
Z
zengyawen 已提交
1487

A
Annie_wang 已提交
1488
**Example**
A
annie_wangli 已提交
1489 1490
  ```js
  fileio.read(new ArrayBuffer(4096)).then(function(readout){
A
Annie_wang 已提交
1491
      console.info("Read file data");
A
Annie_wang 已提交
1492
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
1493 1494 1495 1496
  }).catch(function(err){
      console.info("Failed to read file data. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1497 1498


A
annie_wangli 已提交
1499 1500
## fileio.read<sup>7+</sup>

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

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

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

A
Annie_wang 已提交
1511
**Parameters**
A
annie_wangli 已提交
1512 1513 1514
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file data read.                       |
A
Annie_wang 已提交
1515
  | 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>Constraints: offset + length <= Buffer size|
A
annie_wangli 已提交
1516
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously from the file.                         |
A
annie_wangli 已提交
1517

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


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

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

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

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

A
Annie_wang 已提交
1538
**Parameters**
A
Annie_wang 已提交
1539 1540 1541 1542
| 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 已提交
1543

A
Annie_wang 已提交
1544
**Return value**
A
annie_wangli 已提交
1545 1546
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1547
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1548

A
Annie_wang 已提交
1549
**Example**
A
annie_wangli 已提交
1550
  ```js
A
annie_wangli 已提交
1551
  fileio.rename(oldPath, newPath).then(function() {
A
Annie_wang 已提交
1552
      console.info("File renamed");
A
annie_wangli 已提交
1553
  }).catch(function(err){
A
Annie_wang 已提交
1554
      console.info("Failed to rename the file. Error:"+ err);
A
annie_wangli 已提交
1555 1556 1557 1558 1559 1560 1561 1562
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1567
**Parameters**
A
Annie_wang 已提交
1568 1569 1570
| Name  | Type                     | Mandatory| Description                        |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | Yes  | Application sandbox path of the file to rename.|
A
Annie_wang 已提交
1571
| newPath  | String                    | Yes  | Application sandbox path of the file renamed.  |
A
Annie_wang 已提交
1572
| Callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously renamed.  |
A
annie_wangli 已提交
1573

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


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

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

Synchronously renames a file.

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

A
Annie_wang 已提交
1589
**Parameters**
A
Annie_wang 已提交
1590 1591 1592 1593
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes  | Application sandbox path of the file to rename.|
| newPath | String | Yes  | Application sandbox path of the file renamed.  |
A
annie_wangli 已提交
1594

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


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

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

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

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

A
Annie_wang 已提交
1609
**Parameters**
A
annie_wangli 已提交
1610 1611
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1612
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1613

A
Annie_wang 已提交
1614
**Return value**
A
annie_wangli 已提交
1615 1616
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1617
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1618

A
Annie_wang 已提交
1619
**Example**
A
annie_wangli 已提交
1620 1621
  ```js
  fileio.fsync(fd).then(function(){
A
Annie_wang 已提交
1622
      console.info("Data flushed");
A
annie_wangli 已提交
1623
  }).catch(function(err){
A
Annie_wang 已提交
1624
      console.info("Failed to flush data. Error:"+ err);
A
annie_wangli 已提交
1625 1626 1627 1628 1629 1630 1631 1632
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1637
**Parameters**
A
annie_wangli 已提交
1638 1639
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
A
Annie_wang 已提交
1640
  | fd       | number                    | Yes   | File descriptor of the file to flush.   |
A
annie_wangli 已提交
1641
  | Callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1642

A
Annie_wang 已提交
1643
**Example**
A
annie_wangli 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
  ```js
  fileio.fsync(fd, function(err){
      // Do something.
  });
  ```


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

fsyncSync(fd: number): void

A
Annie_wang 已提交
1655
Flushes data of a file to disk in synchronous mode.
A
annie_wangli 已提交
1656 1657

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

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

A
Annie_wang 已提交
1664
**Example**
A
annie_wangli 已提交
1665
  ```js
A
Annie_wang 已提交
1666
  fileio.fsyncSync(fd);
A
annie_wangli 已提交
1667 1668 1669 1670 1671 1672 1673
  ```


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

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

A
Annie_wang 已提交
1674
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 已提交
1675

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

A
Annie_wang 已提交
1678
**Parameters**
A
annie_wangli 已提交
1679 1680
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1681
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1682

A
Annie_wang 已提交
1683
**Return value**
A
annie_wangli 已提交
1684 1685
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1686
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1687

A
Annie_wang 已提交
1688
**Example**
A
annie_wangli 已提交
1689 1690
  ```js
  fileio.fdatasync(fd).then(function(err) {
A
Annie_wang 已提交
1691
      console.info("Data flushed");
A
annie_wangli 已提交
1692
  }).catch(function(err){
A
Annie_wang 已提交
1693
      console.info("Failed to flush data. Error:"+ err);
A
annie_wangli 已提交
1694 1695 1696 1697 1698 1699 1700 1701
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1706
**Parameters**
A
annie_wangli 已提交
1707 1708 1709 1710
  | 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 已提交
1711

A
Annie_wang 已提交
1712
**Example**
A
annie_wangli 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
  ```js
  fileio.fdatasync (fd, function (err) {
      // Do something.
  });
  ```


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

fdatasyncSync(fd: number): void

Synchronizes data in a file in synchronous mode.

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

A
Annie_wang 已提交
1728
**Parameters**
A
annie_wangli 已提交
1729 1730
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1731
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1732

A
Annie_wang 已提交
1733
**Example**
A
annie_wangli 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742
  ```js
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

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

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

A
Annie_wang 已提交
1747
**Parameters**
A
Annie_wang 已提交
1748 1749
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1750 1751
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1752

A
Annie_wang 已提交
1753
**Return value**
A
annie_wangli 已提交
1754 1755
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1756
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1757

A
Annie_wang 已提交
1758
**Example**
A
annie_wangli 已提交
1759 1760
  ```js
  fileio.symlink(target, srcPath).then(function() {
A
Annie_wang 已提交
1761
      console.info("Symbolic link created");
A
annie_wangli 已提交
1762
  }).catch(function(err){
A
Annie_wang 已提交
1763
      console.info("Failed to create the symbolic link. Error:"+ err);
A
annie_wangli 已提交
1764 1765 1766 1767 1768 1769 1770 1771
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1776
**Parameters**
A
Annie_wang 已提交
1777 1778
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1779 1780
| target   | string                    | Yes  | Application sandbox path of the target file.        |
| srcPath  | string                    | Yes  | Application sandbox path of the symbolic link file.    |
A
Annie_wang 已提交
1781
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the symbolic link is created asynchronously.|
A
annie_wangli 已提交
1782

A
Annie_wang 已提交
1783
**Example**
A
annie_wangli 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
  ```js
  fileio.symlink(target, srcPath, function (err) {
      // Do something.
  });
  ```


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

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

Synchronously creates a symbolic link based on a specified path.

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

A
Annie_wang 已提交
1799
**Parameters**
A
Annie_wang 已提交
1800 1801
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1802 1803
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1804

A
Annie_wang 已提交
1805
**Example**
A
annie_wangli 已提交
1806 1807 1808 1809 1810 1811 1812 1813 1814
  ```js
  fileio.symlinkSync(target, srcPath);
  ```


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

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

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

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

A
Annie_wang 已提交
1819
**Parameters**
A
Annie_wang 已提交
1820 1821
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1822
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
1823 1824
| uid    | number | Yes  | New user ID (UID).       |
| gid    | number | Yes  | New group ID (GID).      |
A
annie_wangli 已提交
1825

A
Annie_wang 已提交
1826
**Return value**
A
annie_wangli 已提交
1827 1828
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1829
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1830

A
Annie_wang 已提交
1831
**Example**
A
annie_wangli 已提交
1832 1833
  ```js
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
1834
  fileio.chown(path, stat.uid, stat.gid).then(function(){
A
Annie_wang 已提交
1835
      console.info("File owner changed");
A
annie_wangli 已提交
1836
  }).catch(function(err){
A
Annie_wang 已提交
1837
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
1838 1839 1840 1841 1842 1843 1844 1845
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1850
**Parameters**
A
Annie_wang 已提交
1851 1852
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
1853
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
1854 1855 1856
| 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 已提交
1857

A
Annie_wang 已提交
1858
**Example**
A
annie_wangli 已提交
1859
  ```js
A
Annie_wang 已提交
1860
  let stat = fileio.statSync(path)
A
annie_wangli 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
  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 已提交
1873 1874
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1875
**Parameters**
A
Annie_wang 已提交
1876 1877
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1878
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
1879 1880
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
1881

A
Annie_wang 已提交
1882
**Example**
A
annie_wangli 已提交
1883
  ```js
A
Annie_wang 已提交
1884
  let stat = fileio.statSync(path)
A
annie_wangli 已提交
1885 1886 1887 1888 1889 1890 1891 1892
  fileio.chownSync(path, stat.uid, stat.gid);
  ```


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

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

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

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

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

A
Annie_wang 已提交
1902
**Return value**
A
Annie_wang 已提交
1903
  | Type                  | Description        |
A
annie_wangli 已提交
1904
  | --------------------- | ---------- |
A
Annie_wang 已提交
1905
  | Promise&lt;string&gt; | Promise used to return the unique directory generated.|
A
annie_wangli 已提交
1906

A
Annie_wang 已提交
1907
**Example**
A
annie_wangli 已提交
1908 1909
  ```js
  fileio.mkdtemp(path + "XXXX").then(function(path){
A
Annie_wang 已提交
1910
      console.info("Temporary directory created:"+ path);
A
annie_wangli 已提交
1911
  }).catch(function(err){
A
Annie_wang 已提交
1912
      console.info("Failed to create a temporary directory. Error:"+ err);
A
annie_wangli 已提交
1913 1914 1915 1916 1917 1918 1919 1920
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1925
**Parameters**
A
annie_wangli 已提交
1926 1927 1928 1929
  | 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 已提交
1930

A
Annie_wang 已提交
1931
**Example**
A
annie_wangli 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
  ```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 已提交
1945 1946
**System capability**: SystemCapability.FileManagement.File.FileIO

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

A
Annie_wang 已提交
1952
**Return value**
A
Annie_wang 已提交
1953
  | Type   | Description        |
A
annie_wangli 已提交
1954
  | ------ | ---------- |
A
annie_wangli 已提交
1955 1956
  | string | Unique path generated.|

A
Annie_wang 已提交
1957
**Example**
A
annie_wangli 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966
  ```js
  let res = fileio.mkdtempSync(path + "XXXX");
  ```


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

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

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

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

A
Annie_wang 已提交
1971
**Parameters**
A
annie_wangli 已提交
1972 1973 1974 1975
  | 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 已提交
1976

A
Annie_wang 已提交
1977
**Return value**
A
Annie_wang 已提交
1978
  | Type                | Description                          |
A
annie_wangli 已提交
1979
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1980
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1981

A
Annie_wang 已提交
1982
**Example**
A
annie_wangli 已提交
1983 1984
  ```js
  fileio.fchmod(fd, mode).then(function() {
A
Annie_wang 已提交
1985
      console.info("File permissions changed");
A
annie_wangli 已提交
1986
  }).catch(function(err){
A
Annie_wang 已提交
1987
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
1988 1989 1990 1991 1992 1993 1994 1995
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2000
**Parameters**
A
annie_wangli 已提交
2001 2002 2003 2004 2005
  | 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 已提交
2006

A
Annie_wang 已提交
2007
**Example**
A
annie_wangli 已提交
2008 2009 2010 2011 2012 2013 2014 2015 2016
  ```js
  fileio.fchmod(fd, mode, function (err) {
      // Do something.
  });
  ```


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

A
annie_wangli 已提交
2017
fchmodSync(fd: number, mode: number): void
A
annie_wangli 已提交
2018 2019 2020

Synchronously changes the file permissions based on the file descriptor.

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

A
Annie_wang 已提交
2023
**Parameters**
A
annie_wangli 已提交
2024 2025 2026 2027
  | 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 已提交
2028

A
Annie_wang 已提交
2029
**Example**
A
annie_wangli 已提交
2030 2031 2032 2033 2034 2035 2036 2037 2038
  ```js
   fileio.fchmodSync(fd, mode);
  ```


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

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

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

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

A
Annie_wang 已提交
2043
**Parameters**
A
Annie_wang 已提交
2044 2045
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2046
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2047
| 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 已提交
2048

A
Annie_wang 已提交
2049
**Return value**
A
annie_wangli 已提交
2050 2051
  | Type                               | Description       |
  | --------------------------------- | --------- |
A
annie_wangli 已提交
2052 2053
  | Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.|

A
Annie_wang 已提交
2054
**Example**
A
annie_wangli 已提交
2055 2056
  ```js
  fileio.createStream(path, "r+").then(function(stream){
A
Annie_wang 已提交
2057
      console.info("Stream opened");
A
annie_wangli 已提交
2058
  }).catch(function(err){
A
Annie_wang 已提交
2059
      console.info("Failed to create the stream. Error:"+ err);
A
annie_wangli 已提交
2060 2061 2062 2063 2064 2065 2066 2067
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2072
**Parameters**
A
Annie_wang 已提交
2073 2074
| Name  | Type                                   | Mandatory| Description                                                        |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2075
| path     | string                                  | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2076 2077
| 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 已提交
2078

A
Annie_wang 已提交
2079
**Example**
A
annie_wangli 已提交
2080
  ```js
A
Annie_wang 已提交
2081
  fileio.createStream(path, "r+", function(err, stream){
A
annie_wangli 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
      // 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 已提交
2093 2094
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2095
**Parameters**
A
Annie_wang 已提交
2096 2097
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2098
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2099
| 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 已提交
2100

A
Annie_wang 已提交
2101
**Return value**
A
Annie_wang 已提交
2102
  | Type               | Description       |
A
annie_wangli 已提交
2103
  | ------------------ | --------- |
A
annie_wangli 已提交
2104
  | [Stream](#stream7) | Stream opened.|
A
annie_wangli 已提交
2105

A
Annie_wang 已提交
2106
**Example**
A
annie_wangli 已提交
2107 2108 2109 2110 2111 2112 2113 2114 2115
  ```js
  let ss = fileio.createStreamSync(path, "r+");
  ```


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

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

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

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

A
Annie_wang 已提交
2120
**Parameters**
A
annie_wangli 已提交
2121 2122 2123 2124
  | 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 已提交
2125

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

A
Annie_wang 已提交
2131
**Example**
A
annie_wangli 已提交
2132
  ```js
A
Annie_wang 已提交
2133 2134 2135
  let fd = fileio.openSync(path);
  fileio.fdopenStream(fd, "r+").then(function(stream){
      console.info("Stream opened");
A
annie_wangli 已提交
2136
  }).catch(function(err){
A
Annie_wang 已提交
2137
      console.info("Failed to open the stream. Error:"+ err);
A
annie_wangli 已提交
2138 2139 2140 2141 2142 2143 2144 2145
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2150
**Parameters**
A
annie_wangli 已提交
2151 2152 2153 2154 2155
  | 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 已提交
2156

A
Annie_wang 已提交
2157
**Example**
A
annie_wangli 已提交
2158
  ```js
A
Annie_wang 已提交
2159 2160
  let fd = fileio.openSync(path);
  fileio.fdopenStream(fd, "r+", function (err, stream) {
A
annie_wangli 已提交
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
      // 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 已提交
2172 2173
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2174
**Parameters**
A
annie_wangli 已提交
2175 2176 2177 2178
  | 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 已提交
2179

A
Annie_wang 已提交
2180 2181
**Return value**
  | Type               | Description       |
A
annie_wangli 已提交
2182
  | ------------------ | --------- |
A
annie_wangli 已提交
2183
  | [Stream](#stream7) | Stream opened.|
A
annie_wangli 已提交
2184

A
Annie_wang 已提交
2185
**Example**
A
annie_wangli 已提交
2186
  ```js
A
Annie_wang 已提交
2187
  let fd = fileio.openSync(path);
A
annie_wangli 已提交
2188 2189 2190 2191 2192 2193 2194 2195
  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 已提交
2196
Changes the file owner based on the file descriptor. This API uses a promise to return the result.
A
annie_wangli 已提交
2197

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

A
Annie_wang 已提交
2200
**Parameters**
A
annie_wangli 已提交
2201 2202 2203 2204 2205
  | 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 已提交
2206

A
Annie_wang 已提交
2207
**Return value**
A
annie_wangli 已提交
2208 2209
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2210
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2211

A
Annie_wang 已提交
2212
**Example**
A
annie_wangli 已提交
2213 2214 2215
  ```js
  let stat = fileio.statSync(path);
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2216
      console.info("File owner changed");
A
annie_wangli 已提交
2217
  }).catch(function(err){
A
Annie_wang 已提交
2218
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2219 2220 2221 2222 2223 2224 2225 2226
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2231
**Parameters**
A
annie_wangli 已提交
2232 2233 2234 2235 2236 2237
  | 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 已提交
2238

A
Annie_wang 已提交
2239
**Example**
A
annie_wangli 已提交
2240
  ```js
A
Annie_wang 已提交
2241
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
  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 已提交
2254 2255
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2256
**Parameters**
A
annie_wangli 已提交
2257 2258 2259 2260 2261
  | 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 已提交
2262

A
Annie_wang 已提交
2263
**Example**
A
annie_wangli 已提交
2264
  ```js
A
Annie_wang 已提交
2265
  let stat = fileio.statSync(path);
A
annie_wangli 已提交
2266 2267 2268 2269 2270 2271 2272 2273
  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 已提交
2274
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 已提交
2275

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

A
Annie_wang 已提交
2278
**Parameters**
A
Annie_wang 已提交
2279 2280
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2281
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2282 2283
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2284

A
Annie_wang 已提交
2285
**Return value**
A
annie_wangli 已提交
2286 2287
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2288
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2289

A
Annie_wang 已提交
2290
**Example**
A
annie_wangli 已提交
2291 2292 2293
  ```js
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2294
      console.info("File owner changed");
A
annie_wangli 已提交
2295
  }).catch(function(err){
A
Annie_wang 已提交
2296
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2297 2298 2299 2300 2301 2302 2303 2304
  });
  ```


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

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

A
Annie_wang 已提交
2305
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 已提交
2306

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

A
Annie_wang 已提交
2309
**Parameters**
A
Annie_wang 已提交
2310 2311
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
2312
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
2313 2314 2315
| 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 已提交
2316

A
Annie_wang 已提交
2317
**Example**
A
annie_wangli 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
  ```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 已提交
2332 2333
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2334
**Parameters**
A
Annie_wang 已提交
2335 2336
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2337
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2338 2339
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2340

A
Annie_wang 已提交
2341
**Example**
A
annie_wangli 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
  ```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 已提交
2352
Listens for file or directory changes. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2353

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

A
Annie_wang 已提交
2356
**Parameters**
A
Annie_wang 已提交
2357 2358
| Name  | Type                             | Mandatory| Description                                                        |
| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2359
| filename | string                            | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2360 2361
| 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 已提交
2362

A
Annie_wang 已提交
2363
**Return value**
A
Annie_wang 已提交
2364
  | Type                 | Description        |
A
annie_wangli 已提交
2365
  | -------------------- | ---------- |
A
Annie_wang 已提交
2366
  | [Watcher](#watcher7) | Promise used to return the **Watcher** instance.|
A
annie_wangli 已提交
2367

A
Annie_wang 已提交
2368
**Example**
A
annie_wangli 已提交
2369
  ```js
A
Annie_wang 已提交
2370 2371 2372
  let filename = path +"/test.txt";
  fileio.createWatcher(filename, 1, function(number){
     console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2373
  });
A
Annie_wang 已提交
2374
  
A
annie_wangli 已提交
2375 2376 2377 2378 2379 2380 2381
  ```


## Readout

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

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

A
annie_wangli 已提交
2384 2385 2386 2387 2388
| 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 已提交
2389 2390 2391 2392 2393 2394


## 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 已提交
2395
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
2396 2397 2398

### Attributes

A
annie_wangli 已提交
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
| 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 已提交
2413 2414 2415 2416 2417 2418


### isBlockDevice

isBlockDevice(): boolean

A
Annie_wang 已提交
2419
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 已提交
2420

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

A
Annie_wang 已提交
2423
**Return value**
A
annie_wangli 已提交
2424 2425
  | Type     | Description              |
  | ------- | ---------------- |
A
Annie_wang 已提交
2426
  | boolean | Whether the file is a block special file.|
A
annie_wangli 已提交
2427

A
Annie_wang 已提交
2428
**Example**
A
annie_wangli 已提交
2429 2430 2431 2432 2433 2434 2435 2436 2437
  ```js
  let isBLockDevice = fileio.statSync(path).isBlockDevice();
  ```


### isCharacterDevice

isCharacterDevice(): boolean

A
Annie_wang 已提交
2438
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 已提交
2439

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

A
Annie_wang 已提交
2442
**Return value**
A
annie_wangli 已提交
2443 2444
  | Type     | Description               |
  | ------- | ----------------- |
A
Annie_wang 已提交
2445
  | boolean | Whether the file is a character special file.|
A
annie_wangli 已提交
2446

A
Annie_wang 已提交
2447
**Example**
A
annie_wangli 已提交
2448 2449 2450 2451 2452 2453 2454 2455 2456
  ```js
  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
  ```


### isDirectory

isDirectory(): boolean

A
Annie_wang 已提交
2457
Checks whether this file is a directory.
A
annie_wangli 已提交
2458

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

A
Annie_wang 已提交
2461
**Return value**
A
annie_wangli 已提交
2462 2463
  | Type     | Description           |
  | ------- | ------------- |
A
Annie_wang 已提交
2464
  | boolean | Whether the file is a directory.|
A
annie_wangli 已提交
2465

A
Annie_wang 已提交
2466
**Example**
A
annie_wangli 已提交
2467 2468 2469 2470 2471 2472 2473 2474 2475
  ```js
  let isDirectory = fileio.statSync(path).isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

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

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

A
Annie_wang 已提交
2480
**Return value**
A
annie_wangli 已提交
2481 2482
  | Type     | Description                   |
  | ------- | --------------------- |
A
Annie_wang 已提交
2483
  | boolean | Whether the file is an FIFO.|
A
annie_wangli 已提交
2484

A
Annie_wang 已提交
2485
**Example**
A
annie_wangli 已提交
2486 2487 2488 2489 2490 2491 2492 2493 2494
  ```js
  let isFIFO = fileio.statSync(path).isFIFO(); 
  ```


### isFile

isFile(): boolean

A
Annie_wang 已提交
2495
Checks whether this file is a regular file.
A
annie_wangli 已提交
2496

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

A
Annie_wang 已提交
2499
**Return value**
A
annie_wangli 已提交
2500 2501
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2502
  | boolean | Whether the file is a regular file.|
A
annie_wangli 已提交
2503

A
Annie_wang 已提交
2504
**Example**
A
annie_wangli 已提交
2505
  ```js
A
Annie_wang 已提交
2506
  let isFile = fileio.statSync(path).isFile();
A
annie_wangli 已提交
2507 2508 2509 2510 2511 2512 2513
  ```


### isSocket

isSocket(): boolean

A
Annie_wang 已提交
2514
Checks whether this file is a socket.
A
annie_wangli 已提交
2515

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

A
Annie_wang 已提交
2518
**Return value**
A
annie_wangli 已提交
2519 2520
  | Type     | Description            |
  | ------- | -------------- |
A
Annie_wang 已提交
2521
  | boolean | Whether the file is a socket.|
A
annie_wangli 已提交
2522

A
Annie_wang 已提交
2523
**Example**
A
annie_wangli 已提交
2524 2525 2526 2527 2528 2529 2530 2531 2532
  ```js
  let isSocket = fileio.statSync(path).isSocket(); 
  ```


### isSymbolicLink

isSymbolicLink(): boolean

A
Annie_wang 已提交
2533
Checks whether this file is a symbolic link.
A
annie_wangli 已提交
2534

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

A
Annie_wang 已提交
2537
**Return value**
A
annie_wangli 已提交
2538 2539
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2540
  | boolean | Whether the file is a symbolic link.|
A
annie_wangli 已提交
2541

A
Annie_wang 已提交
2542
**Example**
A
annie_wangli 已提交
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
  ```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 已提交
2555
stop(): Promise&lt;void&gt;
A
annie_wangli 已提交
2556

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

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

A
Annie_wang 已提交
2561
**Example**
A
annie_wangli 已提交
2562
  ```js
A
Annie_wang 已提交
2563 2564 2565 2566 2567 2568 2569
  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 已提交
2570 2571 2572 2573 2574
  ```


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

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

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

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

A
Annie_wang 已提交
2581
**Parameters**
A
annie_wangli 已提交
2582 2583 2584
  | Name     | Type                       | Mandatory  | Description                    |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when **watcher** is stopped asynchronously.|
A
annie_wangli 已提交
2585

A
Annie_wang 已提交
2586
**Example**
A
annie_wangli 已提交
2587
  ```js
A
Annie_wang 已提交
2588 2589 2590
  let filename = path +"/test.txt";
  let watcher = await fileio.createWatcher(filename, 1, function(number){
      console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2591
  });
A
Annie_wang 已提交
2592 2593 2594
  watcher.stop(function(){
      console.info("Watcher stopped");
  })
A
annie_wangli 已提交
2595
  ```
A
annie_wangli 已提交
2596

A
Annie_wang 已提交
2597
## Stream
A
annie_wangli 已提交
2598

A
Annie_wang 已提交
2599
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 已提交
2600 2601 2602 2603 2604 2605


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

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

A
Annie_wang 已提交
2606
Closes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2607 2608 2609

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

A
Annie_wang 已提交
2610
**Return value**
A
annie_wangli 已提交
2611 2612 2613 2614
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream close result.|

A
Annie_wang 已提交
2615
**Example**
A
annie_wangli 已提交
2616
  ```js
A
Annie_wang 已提交
2617
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2618
  ss.close().then(function(){
A
Annie_wang 已提交
2619
      console.info("Stream closed");
A
annie_wangli 已提交
2620
  }).catch(function(err){
A
Annie_wang 已提交
2621
      console.info("Failed to close the file stream. Error:"+ err);
A
annie_wangli 已提交
2622 2623 2624 2625 2626 2627 2628 2629
  });
  ```


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

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

A
Annie_wang 已提交
2630
Closes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2631 2632 2633

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

A
Annie_wang 已提交
2634
**Parameters**
A
annie_wangli 已提交
2635 2636 2637 2638
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|

A
Annie_wang 已提交
2639
**Example**
A
annie_wangli 已提交
2640
  ```js
A
Annie_wang 已提交
2641
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2642
  ss.close(function (err) {
A
Annie_wang 已提交
2643
      // Do something
A
annie_wangli 已提交
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
  });
  ```


### closeSync

closeSync(): void

Synchronously closes the stream.

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

A
Annie_wang 已提交
2656
**Example**
A
annie_wangli 已提交
2657
  ```js
A
Annie_wang 已提交
2658
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2659 2660 2661 2662 2663 2664 2665 2666
  ss.closeSync();
  ```


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

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

A
Annie_wang 已提交
2667
Flushes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2668 2669 2670

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

A
Annie_wang 已提交
2671
**Return value**
A
annie_wangli 已提交
2672 2673 2674 2675
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream flushing result.|

A
Annie_wang 已提交
2676
**Example**
A
annie_wangli 已提交
2677
  ```js
A
Annie_wang 已提交
2678
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2679
  ss.flush().then(function (){
A
Annie_wang 已提交
2680
      console.info("Stream flushed");
A
annie_wangli 已提交
2681
  }).catch(function(err){
A
Annie_wang 已提交
2682
      console.info("Failed to flush the stream. Error:"+ err);
A
annie_wangli 已提交
2683 2684 2685 2686 2687 2688 2689 2690
  });
  ```


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

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

A
Annie_wang 已提交
2691
Flushes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2692 2693 2694

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

A
Annie_wang 已提交
2695
**Parameters**
A
annie_wangli 已提交
2696 2697 2698 2699
  | Name     | Type                       | Mandatory  | Description            |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is asynchronously flushed.|

A
Annie_wang 已提交
2700
**Example**
A
annie_wangli 已提交
2701
  ```js
A
Annie_wang 已提交
2702
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2703
  ss.flush(function (err) {
A
Annie_wang 已提交
2704
      // Do something
A
annie_wangli 已提交
2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
  });
  ```


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

flushSync(): void

Synchronously flushes the stream.

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

A
Annie_wang 已提交
2717
**Example**
A
annie_wangli 已提交
2718
  ```js
A
Annie_wang 已提交
2719
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
  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 已提交
2733
Writes data into the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2734 2735 2736

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

A
Annie_wang 已提交
2737
**Parameters**
A
annie_wangli 已提交
2738 2739 2740
  | 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 已提交
2741
  | 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 已提交
2742

A
Annie_wang 已提交
2743
**Return value**
A
annie_wangli 已提交
2744 2745
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
2746
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
A
annie_wangli 已提交
2747

A
Annie_wang 已提交
2748
**Example**
A
annie_wangli 已提交
2749
  ```js
A
Annie_wang 已提交
2750
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2751
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
A
Annie_wang 已提交
2752
      console.info("Data written to the stream and size is:"+ number);
A
annie_wangli 已提交
2753
  }).catch(function(err){
A
Annie_wang 已提交
2754
      console.info("Failed to write data into the stream. Error:"+ err);
A
annie_wangli 已提交
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
  });
  ```


### 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 已提交
2768
Writes data into the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2769 2770 2771

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

A
Annie_wang 已提交
2772
**Parameters**
A
Annie_wang 已提交
2773 2774 2775
  | 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 已提交
2776
  | 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 已提交
2777
  | callback | AsyncCallback&lt;number&gt;     | Yes  | Callback invoked when the data is written asynchronously.                              |
A
annie_wangli 已提交
2778

A
Annie_wang 已提交
2779
**Example**
A
annie_wangli 已提交
2780
  ```js
A
Annie_wang 已提交
2781
  let ss= fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2782
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
A
Annie_wang 已提交
2783
      if (bytesWritten) {
A
Annie_wang 已提交
2784 2785
         // Do something
         console.info("Data written to the stream and size is:"+ bytesWritten);
A
annie_wangli 已提交
2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803
      }
  });
  ```


### 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 已提交
2804
**Parameters**
A
annie_wangli 已提交
2805 2806 2807
  | 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 已提交
2808
  | 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 已提交
2809

A
Annie_wang 已提交
2810
**Return value**
A
annie_wangli 已提交
2811 2812 2813 2814
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data written in the file.|

A
Annie_wang 已提交
2815
**Example**
A
annie_wangli 已提交
2816
  ```js
A
Annie_wang 已提交
2817
  let ss= fileio.createStreamSync(path,"r+");
A
annie_wangli 已提交
2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
  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 已提交
2830
Reads data from the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2831 2832 2833

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

A
Annie_wang 已提交
2834
**Parameters**
A
annie_wangli 已提交
2835 2836 2837
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
2838
  | 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 已提交
2839

A
Annie_wang 已提交
2840
**Return value**
A
annie_wangli 已提交
2841 2842
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
2843
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
2844

A
Annie_wang 已提交
2845
**Example**
A
annie_wangli 已提交
2846
  ```js
A
Annie_wang 已提交
2847
  let ss = fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2848
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readout){
A
Annie_wang 已提交
2849
      console.info("Read data successfully");
A
Annie_wang 已提交
2850
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
2851
  }).catch(function(err){
A
Annie_wang 已提交
2852
      console.info("Failed to read data. Error:"+ err);
A
annie_wangli 已提交
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
  });
  ```


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

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

A
Annie_wang 已提交
2865
Reads data from the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2866 2867 2868

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

A
Annie_wang 已提交
2869
**Parameters**
A
annie_wangli 已提交
2870 2871 2872
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
2873
  | 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 已提交
2874 2875
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when data is read asynchronously from the stream.                        |

A
Annie_wang 已提交
2876
**Example**
A
annie_wangli 已提交
2877
  ```js
A
Annie_wang 已提交
2878
  let ss = fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2879
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
A
Annie_wang 已提交
2880
      if (readOut) {
A
Annie_wang 已提交
2881
          console.info("Read data successfully");
A
Annie_wang 已提交
2882
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
      }
  });
  ```


### 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 已提交
2900 2901
**Parameters**

A
annie_wangli 已提交
2902 2903 2904
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
2905 2906 2907
  | 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 已提交
2908 2909 2910 2911 2912

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

A
Annie_wang 已提交
2913
**Example**
A
annie_wangli 已提交
2914
  ```js
A
Annie_wang 已提交
2915
  let ss = fileio.createStreamSync(path, "r+");
A
annie_wangli 已提交
2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
  ```


## Dir

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


### read

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

A
Annie_wang 已提交
2929
Reads the next directory entry. This API uses a promise to return the result.
A
annie_wangli 已提交
2930 2931 2932

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

A
Annie_wang 已提交
2933
**Return value**
A
annie_wangli 已提交
2934 2935
  | Type                              | Description           |
  | -------------------------------- | ------------- |
A
Annie_wang 已提交
2936
  | Promise&lt;[Dirent](#dirent)&gt; | Promise used to return the directory entry read.|
A
annie_wangli 已提交
2937

A
Annie_wang 已提交
2938
**Example**
A
annie_wangli 已提交
2939 2940 2941
  ```js
  let dir = fileio.opendirSync(path);
  dir.read().then(function (dirent){
A
Annie_wang 已提交
2942
      console.log("Read the next directory entry:"+JSON.stringify(dirent));
A
annie_wangli 已提交
2943
  }).catch(function(err){
A
Annie_wang 已提交
2944
      console.info("Failed to read data. Error:"+ err);
A
annie_wangli 已提交
2945 2946 2947 2948 2949 2950 2951 2952
  });
  ```


### read

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

A
Annie_wang 已提交
2953
Reads the next directory entry. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2954 2955 2956

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

A
Annie_wang 已提交
2957
**Parameters**
A
annie_wangli 已提交
2958 2959 2960 2961
  | Name     | Type                                    | Mandatory  | Description              |
  | -------- | -------------------------------------- | ---- | ---------------- |
  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | Yes   | Callback invoked when the next directory entry is asynchronously read.|

A
Annie_wang 已提交
2962
**Example**
A
annie_wangli 已提交
2963 2964 2965
  ```js
  let dir = fileio.opendirSync(path);
  dir.read(function (err, dirent) {
A
Annie_wang 已提交
2966
      if (dirent) {
A
Annie_wang 已提交
2967 2968
          // Do something
          console.log("Read the next directory entry:"+JSON.stringify(dirent));
A
annie_wangli 已提交
2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
      }
  });
  ```


### readSync

readSync(): Dirent

Synchronously reads the next directory entry.

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

A
Annie_wang 已提交
2982
**Return value**
A
annie_wangli 已提交
2983 2984 2985 2986
  | Type               | Description      |
  | ----------------- | -------- |
  | [Dirent](#dirent) | Directory entry read.|

A
Annie_wang 已提交
2987
**Example**
A
annie_wangli 已提交
2988 2989 2990 2991 2992 2993
  ```js
  let dir = fileio.opendirSync(path);
  let dirent = dir.readSync();
  ```


A
Annie_wang 已提交
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
### 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**
  ```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**
  ```js
  let dir = fileio.opendirSync(path);
  dir.close(function(err){
      console.info("close dir successfully");
  });
  ```


A
annie_wangli 已提交
3028 3029 3030 3031 3032 3033 3034 3035
### 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 已提交
3036
**Example**
A
annie_wangli 已提交
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
  ```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 已提交
3060
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 已提交
3061 3062 3063

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

A
Annie_wang 已提交
3064
**Return value**
A
annie_wangli 已提交
3065 3066 3067 3068
  | Type     | Description              |
  | ------- | ---------------- |
  | boolean | Whether the directory entry is a block special file.|

A
Annie_wang 已提交
3069
**Example**
A
annie_wangli 已提交
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
  ```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 已提交
3084
**Return value**
A
annie_wangli 已提交
3085 3086 3087 3088
  | Type     | Description               |
  | ------- | ----------------- |
  | boolean | Whether the directory entry is a character special file.|

A
Annie_wang 已提交
3089
**Example**
A
annie_wangli 已提交
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
  ```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 已提交
3104
**Return value**
A
annie_wangli 已提交
3105 3106 3107 3108
  | Type     | Description           |
  | ------- | ------------- |
  | boolean | Whether the directory entry is a directory.|

A
Annie_wang 已提交
3109
**Example**
A
annie_wangli 已提交
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119
  ```js
  let dir = fileio.opendirSync(path);
  let isDirectory = dir.readSync().isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

A
Annie_wang 已提交
3120
Checks whether this directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication.
A
annie_wangli 已提交
3121 3122 3123

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

A
Annie_wang 已提交
3124
**Return value**
A
annie_wangli 已提交
3125 3126 3127 3128
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a FIFO.|

A
Annie_wang 已提交
3129
**Example**
A
annie_wangli 已提交
3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
  ```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 已提交
3144
**Return value**
A
annie_wangli 已提交
3145 3146 3147 3148
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a regular file.|

A
Annie_wang 已提交
3149
**Example**
A
annie_wangli 已提交
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
  ```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 已提交
3164
**Return value**
A
annie_wangli 已提交
3165 3166 3167 3168
  | Type     | Description            |
  | ------- | -------------- |
  | boolean | Whether the directory entry is a socket.|

A
Annie_wang 已提交
3169
**Example**
A
annie_wangli 已提交
3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
  ```js
  let dir = fileio.opendirSync(dpath);
  let isSocket = dir.readSync().isSocket(); 
  ```


### isSymbolicLink

isSymbolicLink(): boolean

Checks whether a directory entry is a symbolic link.

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

A
Annie_wang 已提交
3184
**Return value**
A
annie_wangli 已提交
3185 3186 3187 3188
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a symbolic link.|

A
Annie_wang 已提交
3189
**Example**
A
annie_wangli 已提交
3190 3191 3192 3193
  ```js
  let dir = fileio.opendirSync(path);
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```