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

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

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

A
Annie_wang 已提交
8

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

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

A
annie_wangli 已提交
15

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

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

A
Annie_wang 已提交
20 21 22 23 24 25 26
Stage Model

 ```js
import Ability from '@ohos.application.Ability';
class MainAbility extends Ability {
    onWindowStageCreate(windowStage) {
        let context = this.context;
A
Annie_wang 已提交
27
        let pathDir = context.filesDir;
A
Annie_wang 已提交
28 29 30 31 32 33 34 35
    }
}
 ```

 For details about how to obtain the stage model context, see [Stage Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-ability-context.md#abilitycontext).

FA Model

A
Annie_wang 已提交
36 37 38 39
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
 let context = featureAbility.getContext();
 context.getFilesDir().then((data) => {
A
Annie_wang 已提交
40
      let pathDir = data;
A
Annie_wang 已提交
41 42
 })
 ```
A
Annie_wang 已提交
43 44
 
 For details about how to obtain the context of the FA model, see [FA Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-Context.md#context).
Z
zengyawen 已提交
45

A
annie_wangli 已提交
46 47 48 49
## fileio.stat

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

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

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

A
Annie_wang 已提交
54 55
**Parameters**

A
Annie_wang 已提交
56 57
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
58
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
59

A
Annie_wang 已提交
60 61
**Return value**

A
annie_wangli 已提交
62 63
  | Type                          | Description        |
  | ---------------------------- | ---------- |
A
annie_wangli 已提交
64 65
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information obtained.|

A
Annie_wang 已提交
66
**Example**
A
Annie_wang 已提交
67

A
annie_wangli 已提交
68
  ```js
A
Annie_wang 已提交
69 70
  let filePath = pathDir + "test.txt";
  fileio.stat(filePath).then(function(stat){
A
Annie_wang 已提交
71
      console.info("Got file info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
72
  }).catch(function(err){
A
Annie_wang 已提交
73
      console.info("Failed to get file info. Error:"+ err);
A
annie_wangli 已提交
74 75 76 77 78 79
  });
  ```


## fileio.stat

A
Annie_wang 已提交
80
stat(path: string, callback: AsyncCallback&lt;Stat&gt;): void
A
annie_wangli 已提交
81

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

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

A
Annie_wang 已提交
86
**Parameters**
A
Annie_wang 已提交
87

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

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

A
annie_wangli 已提交
95
  ```js
A
Annie_wang 已提交
96
  fileio.stat(pathDir, function (err, stat) {
A
annie_wangli 已提交
97 98 99 100 101 102 103
      // Example code in Stat
  });
  ```


## fileio.statSync

A
Annie_wang 已提交
104
statSync(path: string): Stat
Z
zengyawen 已提交
105 106 107

Synchronously obtains file information.

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

A
Annie_wang 已提交
110
**Parameters**
A
Annie_wang 已提交
111

A
Annie_wang 已提交
112 113
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
114
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
115 116


A
Annie_wang 已提交
117
**Return value**
A
Annie_wang 已提交
118

A
annie_wangli 已提交
119 120
  | Type           | Description        |
  | ------------- | ---------- |
A
annie_wangli 已提交
121 122
  | [Stat](#stat) | File information obtained.|

A
Annie_wang 已提交
123
**Example**
A
Annie_wang 已提交
124

A
annie_wangli 已提交
125
  ```js
A
Annie_wang 已提交
126
  let stat = fileio.statSync(pathDir);
A
annie_wangli 已提交
127 128 129 130 131 132 133 134
  // Example code in Stat
  ```


## fileio.opendir

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

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

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

A
Annie_wang 已提交
139
**Parameters**
A
Annie_wang 已提交
140

A
Annie_wang 已提交
141 142 143
| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
144

A
Annie_wang 已提交
145
**Return value**
A
Annie_wang 已提交
146

A
annie_wangli 已提交
147 148
  | Type                        | Description      |
  | -------------------------- | -------- |
A
Annie_wang 已提交
149
  | Promise&lt;[Dir](#dir)&gt; | Promise used to return the **Dir** object.|
A
annie_wangli 已提交
150

A
Annie_wang 已提交
151
**Example**
A
Annie_wang 已提交
152

A
annie_wangli 已提交
153
  ```js
A
Annie_wang 已提交
154 155
  let dirPath = pathDir + "/testDir";
  fileio.opendir(dirPath).then(function(dir){
A
Annie_wang 已提交
156
      console.info("Directory opened:"+ JSON.stringify(dir));
A
annie_wangli 已提交
157
  }).catch(function(err){
A
Annie_wang 已提交
158
      console.info("Failed to open the directory. Error:"+ err);
A
annie_wangli 已提交
159 160 161 162 163 164 165 166
  });
  ```


## fileio.opendir

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

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

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

A
Annie_wang 已提交
171 172
**Parameters**

A
Annie_wang 已提交
173 174 175 176
| 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 已提交
177

A
Annie_wang 已提交
178
**Example**
A
Annie_wang 已提交
179

A
annie_wangli 已提交
180
  ```js
A
Annie_wang 已提交
181
  fileio.opendir(pathDir, function (err, dir) { 
A
annie_wangli 已提交
182
      // Example code in Dir struct
A
annie_wangli 已提交
183
      // Use read/readSync/close.
A
annie_wangli 已提交
184 185 186 187 188 189 190
  });
  ```


## fileio.opendirSync

opendirSync(path: string): Dir
Z
zengyawen 已提交
191 192 193

Synchronously opens a directory.

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

A
annie_wangli 已提交
196

A
Annie_wang 已提交
197 198
**Parameters**

A
Annie_wang 已提交
199 200 201
| Name| Type  | Mandatory| Description                          |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | Yes  | Application sandbox path of the directory to open.|
A
annie_wangli 已提交
202

A
Annie_wang 已提交
203
**Return value**
A
Annie_wang 已提交
204

A
annie_wangli 已提交
205 206
  | Type         | Description      |
  | ----------- | -------- |
A
annie_wangli 已提交
207 208
  | [Dir](#dir) | A **Dir** instance corresponding to the directory.|

A
Annie_wang 已提交
209
**Example**
A
Annie_wang 已提交
210

A
annie_wangli 已提交
211
  ```js
A
Annie_wang 已提交
212
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
213 214 215 216 217 218 219 220 221
  // Example code in Dir struct
  // Use read/readSync/close.
  ```


## fileio.access

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

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

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

A
Annie_wang 已提交
226 227
**Parameters**

A
Annie_wang 已提交
228 229
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
230
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
231
| 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 已提交
232

A
Annie_wang 已提交
233
**Return value**
A
Annie_wang 已提交
234

A
annie_wangli 已提交
235 236
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
237
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
238

A
Annie_wang 已提交
239
**Example**
A
Annie_wang 已提交
240

A
annie_wangli 已提交
241
  ```js
A
Annie_wang 已提交
242 243
  let filePath = pathDir + "/test.txt";
  fileio.access(filePath).then(function() {
A
Annie_wang 已提交
244
      console.info("Access successful");
A
annie_wangli 已提交
245
  }).catch(function(err){
A
Annie_wang 已提交
246
      console.info("Access failed. Error:"+ err);
A
annie_wangli 已提交
247 248 249 250 251 252
  });
  ```


## fileio.access

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

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

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

A
Annie_wang 已提交
259
**Parameters**
A
Annie_wang 已提交
260

A
Annie_wang 已提交
261 262
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
263
| path     | string                    | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
264 265
| 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 已提交
266

A
Annie_wang 已提交
267
**Example**
A
Annie_wang 已提交
268

A
annie_wangli 已提交
269
  ```js
A
Annie_wang 已提交
270 271
  let filePath = pathDir + "/test.txt";
  fileio.access(filePath, function (err) {
A
annie_wangli 已提交
272 273 274 275 276 277 278 279 280 281 282
      // Do something.
  });
  ```


## fileio.accessSync

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

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

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

A
Annie_wang 已提交
285
**Parameters**
A
Annie_wang 已提交
286

A
Annie_wang 已提交
287 288
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
289
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
290
| 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 已提交
291

A
Annie_wang 已提交
292
**Example**
A
Annie_wang 已提交
293

A
annie_wangli 已提交
294
  ```js
A
Annie_wang 已提交
295
  let filePath = pathDir + "/test.txt";
A
annie_wangli 已提交
296
  try {
A
Annie_wang 已提交
297
      fileio.accessSync(filePath);
A
annie_wangli 已提交
298
  } catch(err) {
A
Annie_wang 已提交
299
      console.info("accessSync failed with error:"+ err);
A
annie_wangli 已提交
300 301 302 303 304 305
  }
  ```


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

A
Annie_wang 已提交
306
close(fd: number): Promise&lt;void&gt;
A
annie_wangli 已提交
307

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

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

A
Annie_wang 已提交
312
**Parameters**
A
Annie_wang 已提交
313

A
annie_wangli 已提交
314 315 316
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
317

A
Annie_wang 已提交
318
**Return value**
A
Annie_wang 已提交
319

A
annie_wangli 已提交
320 321
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
322
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
323

A
Annie_wang 已提交
324
**Example**
A
Annie_wang 已提交
325

A
annie_wangli 已提交
326
  ```js
A
Annie_wang 已提交
327 328
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
329
  fileio.close(fd).then(function(){
A
Annie_wang 已提交
330
      console.info("File closed");
A
annie_wangli 已提交
331
  }).catch(function(err){
A
Annie_wang 已提交
332
      console.info("Failed to close the file. Error:"+ err);
A
annie_wangli 已提交
333 334 335 336 337 338
  });
  ```


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

A
Annie_wang 已提交
339
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
340

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

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

A
Annie_wang 已提交
345
**Parameters**
A
Annie_wang 已提交
346

A
annie_wangli 已提交
347 348 349 350
  | 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 已提交
351

A
Annie_wang 已提交
352
**Example**
A
Annie_wang 已提交
353

A
annie_wangli 已提交
354
  ```js
A
Annie_wang 已提交
355 356
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
357 358 359 360 361 362 363 364 365
  fileio.close(fd, function (err) {
      // Do something.
  });
  ```


## fileio.closeSync

closeSync(fd: number): void
Z
zengyawen 已提交
366 367 368

Synchronously closes a file.

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

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

A
annie_wangli 已提交
373 374 375
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the file to close.|
A
annie_wangli 已提交
376

A
Annie_wang 已提交
377
**Example**
A
Annie_wang 已提交
378

A
annie_wangli 已提交
379
  ```js
A
Annie_wang 已提交
380 381
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
382 383 384 385 386 387
  fileio.closeSync(fd);
  ```


## fileio.copyFile

A
Annie_wang 已提交
388
copyFile(src: string | number, dest: string | number, mode?: number): Promise&lt;void&gt;
A
annie_wangli 已提交
389

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

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

A
Annie_wang 已提交
394
**Parameters**
A
Annie_wang 已提交
395

A
annie_wangli 已提交
396 397 398 399 400
  | 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 已提交
401

A
Annie_wang 已提交
402
**Return value**
A
Annie_wang 已提交
403

A
annie_wangli 已提交
404 405
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
406
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
407

A
Annie_wang 已提交
408
**Example**
A
Annie_wang 已提交
409

A
annie_wangli 已提交
410
  ```js
A
Annie_wang 已提交
411 412 413
  let srcPath = pathDir + "srcDir/test.txt";
  let dstPath = pathDir + "dstDir/test.txt";
  fileio.copyFile(srcPath, dstPath).then(function(){
A
Annie_wang 已提交
414
      console.info("File copied");
A
annie_wangli 已提交
415
  }).catch(function(err){
A
Annie_wang 已提交
416
      console.info("Failed to copy the file. Error:"+ err);
A
annie_wangli 已提交
417 418 419 420 421 422
  });
  ```


## fileio.copyFile

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

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

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

A
Annie_wang 已提交
429
**Parameters**
A
Annie_wang 已提交
430

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

A
Annie_wang 已提交
438
**Example**
A
Annie_wang 已提交
439

A
annie_wangli 已提交
440
  ```js
A
Annie_wang 已提交
441 442 443
  let srcPath = pathDir + "srcDir/test.txt";
  let dstPath = pathDir + "dstDir/test.txt";
  fileio.copyFile(srcPath, dstPath, function (err) {
A
annie_wangli 已提交
444 445 446 447 448 449 450
      // Do something.
  });
  ```


## fileio.copyFileSync

A
annie_wangli 已提交
451
copyFileSync(src: string | number, dest: string | number, mode?: number): void
Z
zengyawen 已提交
452 453 454

Synchronously copies a file.

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

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

A
annie_wangli 已提交
459 460 461 462 463
  | 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 已提交
464

A
Annie_wang 已提交
465
**Example**
A
Annie_wang 已提交
466

A
annie_wangli 已提交
467
  ```js
A
Annie_wang 已提交
468 469 470
  let srcPath = pathDir + "srcDir/test.txt";
  let dstPath = pathDir + "dstDir/test.txt";
  fileio.copyFileSync(srcPath, dstPath);
A
annie_wangli 已提交
471 472 473 474 475
  ```


## fileio.mkdir

A
Annie_wang 已提交
476
mkdir(path: string, mode?: number): Promise&lt;void&gt;
A
annie_wangli 已提交
477

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

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

A
Annie_wang 已提交
482
**Parameters**
A
Annie_wang 已提交
483

A
Annie_wang 已提交
484 485
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
486
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
487
| mode   | number | No  | Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o775**.<br>-&nbsp;**0o775**: The owner has the read, write, and execute permissions, and other users have the read and execute permissions.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
A
annie_wangli 已提交
488

A
Annie_wang 已提交
489
**Return value**
A
Annie_wang 已提交
490

A
annie_wangli 已提交
491 492
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
493
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
494

A
Annie_wang 已提交
495
**Example**
A
Annie_wang 已提交
496

A
annie_wangli 已提交
497
  ```js
A
Annie_wang 已提交
498 499
  let dirPath = pathDir + '/testDir';
  fileio.mkdir(dirPath).then(function() {
A
Annie_wang 已提交
500
      console.info("Directory created");
A
annie_wangli 已提交
501
  }).catch(function (error){
A
Annie_wang 已提交
502
      console.info("Failed to create the directory. Error:"+ error);
A
annie_wangli 已提交
503 504 505 506 507 508
  });
  ```


## fileio.mkdir

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

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

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

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

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

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

A
annie_wangli 已提交
525
  ```js
A
Annie_wang 已提交
526 527
  let dirPath = pathDir + '/testDir';
  fileio.mkdir(dirPath, function(err) {
A
Annie_wang 已提交
528
    console.info("Directory created");
A
annie_wangli 已提交
529 530 531 532 533 534
  });
  ```


## fileio.mkdirSync

A
annie_wangli 已提交
535
mkdirSync(path: string, mode?: number): void
Z
zengyawen 已提交
536 537 538

Synchronously creates a directory.

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

A
Annie_wang 已提交
541
**Parameters**
A
Annie_wang 已提交
542

A
Annie_wang 已提交
543 544
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
545
| path   | string | Yes  | Application sandbox path of the directory.                                  |
A
Annie_wang 已提交
546
| 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 已提交
547

A
Annie_wang 已提交
548
**Example**
A
Annie_wang 已提交
549

A
annie_wangli 已提交
550
  ```js
A
Annie_wang 已提交
551 552
  let dirPath = path + '/testDir';
  fileio.mkdirSync(dirPath);
A
annie_wangli 已提交
553 554 555 556 557 558 559
  ```


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

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

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

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

A
Annie_wang 已提交
564
**Parameters**
A
Annie_wang 已提交
565

A
Annie_wang 已提交
566 567
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
568 569
| 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 已提交
570
| 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 已提交
571

A
Annie_wang 已提交
572
**Return value**
A
Annie_wang 已提交
573

A
annie_wangli 已提交
574 575
  | Type                   | Description         |
  | --------------------- | ----------- |
A
Annie_wang 已提交
576
  | Promise&lt;number&gt; | Promise used to return the file descriptor of the file opened.|
A
annie_wangli 已提交
577

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

A
annie_wangli 已提交
580
  ```js
A
Annie_wang 已提交
581 582
  let filePath = pathDir + "/test.txt";
  fileio.open(filePath, 0o1, 0o0200).then(function(number){
A
Annie_wang 已提交
583 584 585
      console.info("File opened");
  }).catch(function(err){
      console.info("Failed to open the file. Error:"+ err);
A
annie_wangli 已提交
586 587 588 589 590 591 592 593
  });
  ```


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

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

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

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

A
Annie_wang 已提交
598
**Parameters**
A
Annie_wang 已提交
599

A
Annie_wang 已提交
600 601
| Name  | Type                           | Mandatory| Description                                                        |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
602
| path     | string                          | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
603 604
| 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;**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_wang 已提交
605
| callback | AsyncCallback&nbsp;&lt;void&gt; | Yes  | Callback invoked when the file is open asynchronously.                                    |
A
annie_wangli 已提交
606

A
Annie_wang 已提交
607
**Example**
A
Annie_wang 已提交
608

A
annie_wangli 已提交
609
  ```js
A
Annie_wang 已提交
610 611
  let filePath = pathDir + "/test.txt";
  fileio.open(filePath, 0, function(err, fd) {
A
annie_wangli 已提交
612 613 614 615 616 617 618
      // Do something.
  });
  ```


## fileio.openSync

A
Annie_wang 已提交
619
openSync(path: string, flags?: number, mode?: number): number
Z
zengyawen 已提交
620 621 622

Synchronously opens a file.

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

A
Annie_wang 已提交
625
**Parameters**
A
Annie_wang 已提交
626

A
Annie_wang 已提交
627 628
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
629 630 631
| 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 已提交
632

A
Annie_wang 已提交
633
**Return value**
A
Annie_wang 已提交
634

A
annie_wangli 已提交
635 636
  | Type    | Description         |
  | ------ | ----------- |
A
annie_wangli 已提交
637 638
  | number | File descriptor of the file opened.|

A
Annie_wang 已提交
639
**Example**
A
Annie_wang 已提交
640

A
annie_wangli 已提交
641
  ```js
A
Annie_wang 已提交
642 643
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o102, 0o640);
A
Annie_wang 已提交
644 645
  ```
  ```js
A
Annie_wang 已提交
646 647
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o102, 0o666);
A
Annie_wang 已提交
648
  fileio.writeSync(fd, 'hello world');
A
Annie_wang 已提交
649
  let fd1 = fileio.openSync(filePath, 0o2002);
A
Annie_wang 已提交
650 651 652
  fileio.writeSync(fd1, 'hello world');
  let num = fileio.readSync(fd1, new ArrayBuffer(4096), {position: 0});
  console.info("num == " + num);
A
annie_wangli 已提交
653 654 655 656 657
  ```


## fileio.read

A
annie_wangli 已提交
658 659 660 661 662
read(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): Promise&lt;ReadOut&gt;
A
annie_wangli 已提交
663

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

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

A
Annie_wang 已提交
668
**Parameters**
A
Annie_wang 已提交
669

A
Annie_wang 已提交
670 671 672 673 674 675 676
| 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 已提交
677

A
annie_wangli 已提交
678 679
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
680
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
681

A
Annie_wang 已提交
682
**Example**
A
Annie_wang 已提交
683

A
annie_wangli 已提交
684
  ```js
A
Annie_wang 已提交
685 686
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
A
annie_wangli 已提交
687
  let buf = new ArrayBuffer(4096);
A
Annie_wang 已提交
688
  fileio.read(fd, buf).then(function(readOut){
A
Annie_wang 已提交
689
      console.info("Read file data successfully");
A
Annie_wang 已提交
690
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
Annie_wang 已提交
691 692
  }).catch(function(err){
      console.info("Failed to read file data. Error:"+ err);
A
annie_wangli 已提交
693 694 695 696 697 698
  });
  ```


## fileio.read

A
annie_wangli 已提交
699 700 701 702 703
read(fd: number, buffer: ArrayBuffer, options: {
    offset?: number;
    length?: number;
    position?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
A
annie_wangli 已提交
704

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

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

A
Annie_wang 已提交
709
**Parameters**
A
Annie_wang 已提交
710

A
annie_wangli 已提交
711 712 713 714
  | 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 已提交
715
  | 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 已提交
716
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when the data is read asynchronously.                            |
A
annie_wangli 已提交
717

A
Annie_wang 已提交
718
**Example**
A
Annie_wang 已提交
719

A
annie_wangli 已提交
720
  ```js
A
Annie_wang 已提交
721 722
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
A
annie_wangli 已提交
723 724
  let buf = new ArrayBuffer(4096);
  fileio.read(fd, buf, function (err, readOut) {
A
Annie_wang 已提交
725
      if (readOut) {
A
Annie_wang 已提交
726
          console.info("Read file data successfully");
A
Annie_wang 已提交
727
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
728 729 730 731 732 733 734
      }
  });
  ```


## fileio.readSync

A
annie_wangli 已提交
735 736 737 738 739
readSync(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): number
Z
zengyawen 已提交
740 741 742

Synchronously reads data from a file.

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

A
Annie_wang 已提交
745
**Parameters**
A
Annie_wang 已提交
746

A
annie_wangli 已提交
747 748 749 750
  | 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 已提交
751
  | 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 已提交
752

A
Annie_wang 已提交
753
**Return value**
A
Annie_wang 已提交
754

A
annie_wangli 已提交
755 756
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
757 758
  | number | Length of the data read.|

A
Annie_wang 已提交
759
**Example**
A
Annie_wang 已提交
760

A
annie_wangli 已提交
761
  ```js
A
Annie_wang 已提交
762 763
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
A
annie_wangli 已提交
764 765 766 767 768 769 770 771 772
  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 已提交
773
Deletes a directory. This API uses a promise to return the result.
A
annie_wangli 已提交
774

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

A
Annie_wang 已提交
777
**Parameters**
A
Annie_wang 已提交
778

A
Annie_wang 已提交
779 780
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
781
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
782

A
Annie_wang 已提交
783
**Return value**
A
Annie_wang 已提交
784

A
annie_wangli 已提交
785 786
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
787
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
788

A
Annie_wang 已提交
789
**Example**
A
Annie_wang 已提交
790

A
annie_wangli 已提交
791
  ```js
A
Annie_wang 已提交
792 793
  let dirPath = pathDir + '/testDir';
  fileio.rmdir(dirPath).then(function() {
A
Annie_wang 已提交
794
      console.info("Directory deleted");
A
annie_wangli 已提交
795
  }).catch(function(err){
A
Annie_wang 已提交
796
      console.info("Failed to delete the directory. Error:"+ err);
A
annie_wangli 已提交
797 798 799 800 801 802
  });
  ```


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

A
Annie_wang 已提交
803
rmdir(path: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
804

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

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

A
Annie_wang 已提交
809
**Parameters**
A
Annie_wang 已提交
810

A
Annie_wang 已提交
811 812
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
813
| path     | string                    | Yes  | Application sandbox path of the directory.|
A
Annie_wang 已提交
814
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the directory is deleted asynchronously.  |
A
annie_wangli 已提交
815

A
Annie_wang 已提交
816
**Example**
A
Annie_wang 已提交
817

A
annie_wangli 已提交
818
  ```js
A
Annie_wang 已提交
819 820
  let dirPath = pathDir + '/testDir';
  fileio.rmdir(dirPath, function(err){
A
annie_wangli 已提交
821
      // Do something.
A
Annie_wang 已提交
822
      console.info("Directory deleted");
A
annie_wangli 已提交
823 824 825 826
  });
  ```


A
annie_wangli 已提交
827
## fileio.rmdirSync<sup>7+</sup>
A
annie_wangli 已提交
828

A
annie_wangli 已提交
829
rmdirSync(path: string): void
A
annie_wangli 已提交
830 831 832

Synchronously deletes a directory.

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

A
Annie_wang 已提交
835
**Parameters**
A
Annie_wang 已提交
836

A
Annie_wang 已提交
837 838
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
839
| path   | string | Yes  | Application sandbox path of the directory.|
A
annie_wangli 已提交
840

A
Annie_wang 已提交
841
**Example**
A
Annie_wang 已提交
842

A
annie_wangli 已提交
843
  ```js
A
Annie_wang 已提交
844 845
  let dirPath = pathDir + '/testDir';
  fileio.rmdirSync(dirPath);
A
annie_wangli 已提交
846 847 848 849 850
  ```


## fileio.unlink

A
Annie_wang 已提交
851
unlink(path: string): Promise&lt;void&gt;
A
annie_wangli 已提交
852

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

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

A
Annie_wang 已提交
857
**Parameters**
A
Annie_wang 已提交
858

A
Annie_wang 已提交
859 860
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
861
| path   | string | Yes  | Application sandbox path of the file.|
A
annie_wangli 已提交
862

A
Annie_wang 已提交
863
**Return value**
A
Annie_wang 已提交
864

A
annie_wangli 已提交
865 866
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
867
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
868

A
Annie_wang 已提交
869
**Example**
A
Annie_wang 已提交
870

A
annie_wangli 已提交
871
  ```js
A
Annie_wang 已提交
872 873
  let filePath = pathDir + "/test.txt";
  fileio.unlink(filePath).then(function(){
A
Annie_wang 已提交
874
      console.info("File deleted");
A
annie_wangli 已提交
875
  }).catch(function(error){
A
Annie_wang 已提交
876
      console.info("Failed to delete the file. Error:"+ error);
A
annie_wangli 已提交
877 878 879 880 881 882
  });
  ```


## fileio.unlink

A
Annie_wang 已提交
883
unlink(path: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
884

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

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

A
Annie_wang 已提交
889
**Parameters**
A
Annie_wang 已提交
890

A
Annie_wang 已提交
891 892
| Name  | Type                     | Mandatory| Description                      |
| -------- | ------------------------- | ---- | -------------------------- |
A
Annie_wang 已提交
893
| path     | string                    | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
894
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is deleted asynchronously.  |
A
annie_wangli 已提交
895

A
Annie_wang 已提交
896
**Example**
A
Annie_wang 已提交
897

A
annie_wangli 已提交
898
  ```js
A
Annie_wang 已提交
899 900
  let filePath = pathDir + "/test.txt";
  fileio.unlink(filePath, function(err) {
A
Annie_wang 已提交
901
      console.info("File deleted");
A
annie_wangli 已提交
902 903 904 905 906 907 908
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void
Z
zengyawen 已提交
909 910 911

Synchronously deletes a file.

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

A
Annie_wang 已提交
914
**Parameters**
A
Annie_wang 已提交
915

A
Annie_wang 已提交
916 917
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
918
| path   | string | Yes  | Application sandbox path of the file.|
Z
zengyawen 已提交
919

A
Annie_wang 已提交
920
**Example**
A
Annie_wang 已提交
921

A
annie_wangli 已提交
922
  ```js
A
Annie_wang 已提交
923 924
  let filePath = pathDir + "/test.txt";
  fileio.unlinkSync(filePath);
A
annie_wangli 已提交
925
  ```
Z
zengyawen 已提交
926 927


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

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

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

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

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

A
annie_wangli 已提交
943 944 945 946
  | 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 已提交
947
  | 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 已提交
948

A
Annie_wang 已提交
949
**Return value**
A
Annie_wang 已提交
950

A
annie_wangli 已提交
951 952
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
953
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
Z
zengyawen 已提交
954

A
Annie_wang 已提交
955
**Example**
A
Annie_wang 已提交
956

A
annie_wangli 已提交
957
  ```js
A
Annie_wang 已提交
958 959
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
A
annie_wangli 已提交
960
  fileio.write(fd, "hello, world").then(function(number){
A
Annie_wang 已提交
961
       console.info("Data written to the file. Size is:"+ number);
A
annie_wangli 已提交
962
  }).catch(function(err){
A
Annie_wang 已提交
963
      console.info("Failed to write data to the file. Error:"+ err);
A
annie_wangli 已提交
964 965
  });
  ```
Z
zengyawen 已提交
966 967


A
annie_wangli 已提交
968
## fileio.write
Z
zengyawen 已提交
969

A
annie_wangli 已提交
970 971 972 973 974 975
write(fd: number, buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
976

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

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

A
Annie_wang 已提交
981
**Parameters**
A
Annie_wang 已提交
982

A
annie_wangli 已提交
983 984 985 986
  | 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 已提交
987
  | 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 已提交
988
  | callback | AsyncCallback&lt;number&gt;     | Yes   | Callback invoked when the data is written asynchronously.                      |
Z
zengyawen 已提交
989

A
Annie_wang 已提交
990
**Example**
A
Annie_wang 已提交
991

A
annie_wangli 已提交
992
  ```js
A
Annie_wang 已提交
993 994
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
A
annie_wangli 已提交
995
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
A
Annie_wang 已提交
996
      if (bytesWritten) {
A
Annie_wang 已提交
997
         console.info("Data written to the file. Size is:"+ bytesWritten);
A
annie_wangli 已提交
998 999 1000
      }
  });
  ```
Z
zengyawen 已提交
1001 1002


A
annie_wangli 已提交
1003
## fileio.writeSync
Z
zengyawen 已提交
1004

A
annie_wangli 已提交
1005 1006 1007 1008 1009 1010
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
1011

A
annie_wangli 已提交
1012
Synchronously writes data into a file.
Z
zengyawen 已提交
1013

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

A
Annie_wang 已提交
1016
**Parameters**
A
Annie_wang 已提交
1017

A
annie_wangli 已提交
1018 1019 1020 1021
  | 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 已提交
1022
  | 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 已提交
1023

A
Annie_wang 已提交
1024
**Return value**
A
Annie_wang 已提交
1025

A
annie_wangli 已提交
1026 1027
  | Type    | Description      |
  | ------ | -------- |
A
annie_wangli 已提交
1028
  | number | Length of the data written in the file.|
Z
zengyawen 已提交
1029

A
Annie_wang 已提交
1030
**Example**
A
Annie_wang 已提交
1031

A
annie_wangli 已提交
1032
  ```js
A
Annie_wang 已提交
1033 1034
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
A
annie_wangli 已提交
1035 1036
  let num = fileio.writeSync(fd, "hello, world");
  ```
Z
zengyawen 已提交
1037 1038


A
annie_wangli 已提交
1039
## fileio.hash
Z
zengyawen 已提交
1040

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

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

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

A
Annie_wang 已提交
1047
**Parameters**
A
Annie_wang 已提交
1048

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

A
Annie_wang 已提交
1054
**Return value**
A
Annie_wang 已提交
1055

A
annie_wangli 已提交
1056 1057
  | Type                   | Description                        |
  | --------------------- | -------------------------- |
A
Annie_wang 已提交
1058
  | 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 已提交
1059

A
Annie_wang 已提交
1060
**Example**
A
Annie_wang 已提交
1061

A
annie_wangli 已提交
1062
  ```js
A
Annie_wang 已提交
1063 1064
  let filePath = pathDir + "/test.txt";
  fileio.hash(filePath, "sha256").then(function(str){
A
Annie_wang 已提交
1065
      console.info("Calculated file hash:"+ str);
A
Annie_wang 已提交
1066
  }).catch(function(err){
A
Annie_wang 已提交
1067
      console.info("Failed to calculate the file hash. Error:"+ err);
A
annie_wangli 已提交
1068 1069
  });
  ```
Z
zengyawen 已提交
1070 1071


A
annie_wangli 已提交
1072
## fileio.hash
Z
zengyawen 已提交
1073

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

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

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

A
Annie_wang 已提交
1080
**Parameters**
A
Annie_wang 已提交
1081

A
Annie_wang 已提交
1082 1083
| Name   | Type                       | Mandatory| Description                                                        |
| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1084
| path      | string                      | Yes  | Application sandbox path of the file.                            |
A
Annie_wang 已提交
1085
| 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 已提交
1086
| 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 已提交
1087

A
Annie_wang 已提交
1088
**Example**
A
Annie_wang 已提交
1089

A
annie_wangli 已提交
1090
  ```js
A
Annie_wang 已提交
1091 1092
  let filePath = pathDir + "/test.txt";
  fileio.hash(filePath, "sha256", function(err, hashStr) {
A
Annie_wang 已提交
1093
      if (hashStr) {
A
Annie_wang 已提交
1094
          console.info("Calculated file hash:"+ hashStr);
A
annie_wangli 已提交
1095 1096 1097
      }
  });
  ```
Z
zengyawen 已提交
1098 1099


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

A
Annie_wang 已提交
1102
chmod(path: string, mode: number): Promise&lt;void&gt;
Z
zengyawen 已提交
1103

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

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

A
Annie_wang 已提交
1108
**Parameters**
A
Annie_wang 已提交
1109

A
Annie_wang 已提交
1110 1111
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1112
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1113
| 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 已提交
1114

A
Annie_wang 已提交
1115
**Return value**
A
Annie_wang 已提交
1116

A
annie_wangli 已提交
1117 1118
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1119
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1120

A
Annie_wang 已提交
1121
**Example**
A
Annie_wang 已提交
1122

A
annie_wangli 已提交
1123
  ```js
A
Annie_wang 已提交
1124 1125
  let filePath = pathDir + "/test.txt";
  fileio.chmod(filePath, 0o700).then(function() {
A
Annie_wang 已提交
1126
      console.info("File permissions changed");
A
annie_wangli 已提交
1127
  }).catch(function(err){
A
Annie_wang 已提交
1128
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
1129 1130
  });
  ```
Z
zengyawen 已提交
1131 1132


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

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

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

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

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

A
Annie_wang 已提交
1143 1144
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1145
| path     | string                    | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1146 1147
| 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 已提交
1148

A
Annie_wang 已提交
1149
**Example**
A
Annie_wang 已提交
1150

A
annie_wangli 已提交
1151
  ```js
A
Annie_wang 已提交
1152 1153
  let filePath = pathDir + "/test.txt";
  fileio.chmod(filePath, 0o700, function (err) {
A
annie_wangli 已提交
1154 1155 1156
      // Do something.
  });
  ```
Z
zengyawen 已提交
1157 1158


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

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

A
Annie_wang 已提交
1163
Synchronously changes file permissions.
Z
zengyawen 已提交
1164

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

A
Annie_wang 已提交
1167
**Parameters**
A
Annie_wang 已提交
1168

A
Annie_wang 已提交
1169 1170
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1171
| path   | string | Yes  | Application sandbox path of the file.                              |
A
Annie_wang 已提交
1172
| 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 已提交
1173

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

A
annie_wangli 已提交
1176
  ```js
A
Annie_wang 已提交
1177 1178
  let filePath = pathDir + "/test.txt";
  fileio.chmodSync(filePath, 0o700);
A
annie_wangli 已提交
1179
  ```
Z
zengyawen 已提交
1180 1181


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

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

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

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

A
Annie_wang 已提交
1190
**Parameters**
A
Annie_wang 已提交
1191

A
annie_wangli 已提交
1192 1193 1194
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1195

A
Annie_wang 已提交
1196
**Return value**
A
Annie_wang 已提交
1197

A
annie_wangli 已提交
1198
  | Type                          | Description        |
A
Annie_wang 已提交
1199
  | ---------------------------- | ---------- |
A
Annie_wang 已提交
1200
  | Promise&lt;[Stat](#stat)&gt; | Promise used to return the file information.|
Z
zengyawen 已提交
1201

A
Annie_wang 已提交
1202
**Example**
A
Annie_wang 已提交
1203

A
annie_wangli 已提交
1204
  ```js
A
Annie_wang 已提交
1205 1206
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1207
  fileio.fstat(fd).then(function(stat){
A
Annie_wang 已提交
1208
      console.info("Obtained file info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1209
  }).catch(function(err){
A
Annie_wang 已提交
1210
      console.info("Failed to obtain file info. Error:"+ err);
A
annie_wangli 已提交
1211 1212
  });
  ```
Z
zengyawen 已提交
1213 1214


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

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

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

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

A
Annie_wang 已提交
1223
**Parameters**
A
Annie_wang 已提交
1224

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

A
Annie_wang 已提交
1230
**Example**
A
Annie_wang 已提交
1231

A
annie_wangli 已提交
1232
  ```js
A
Annie_wang 已提交
1233 1234
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1235 1236 1237 1238
  fileio.fstat(fd, function (err) {
      // Do something.
  });
  ```
Z
zengyawen 已提交
1239 1240


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

A
annie_wangli 已提交
1243
fstatSync(fd: number): Stat
Z
zengyawen 已提交
1244

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

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

A
Annie_wang 已提交
1249
**Parameters**
A
Annie_wang 已提交
1250

A
annie_wangli 已提交
1251 1252 1253
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | Yes   | File descriptor of the target file.|
Z
zengyawen 已提交
1254

A
Annie_wang 已提交
1255
**Return value**
A
Annie_wang 已提交
1256

A
annie_wangli 已提交
1257 1258
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1259
  | [Stat](#stat) | File information obtained.|
Z
zengyawen 已提交
1260

A
Annie_wang 已提交
1261
**Example**
A
Annie_wang 已提交
1262

A
annie_wangli 已提交
1263
  ```js
A
Annie_wang 已提交
1264 1265
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1266 1267
  let stat = fileio.fstatSync(fd);
  ```
Z
zengyawen 已提交
1268 1269


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

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

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

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

A
Annie_wang 已提交
1278
**Parameters**
A
Annie_wang 已提交
1279

A
annie_wangli 已提交
1280 1281 1282
  | Name | Type    | Mandatory  | Description              |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | Yes   | File descriptor of the file to truncate.    |
A
Annie_wang 已提交
1283
  | len  | number | No   | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1284

A
Annie_wang 已提交
1285
**Return value**
A
Annie_wang 已提交
1286

A
annie_wangli 已提交
1287 1288
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1289
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1290

A
Annie_wang 已提交
1291
**Example**
A
Annie_wang 已提交
1292

A
annie_wangli 已提交
1293
  ```js
A
Annie_wang 已提交
1294 1295
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1296
  fileio.ftruncate(fd, 5).then(function(err) {    
A
Annie_wang 已提交
1297
      console.info("File truncated");
A
annie_wangli 已提交
1298 1299 1300 1301
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1302 1303


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

A
Annie_wang 已提交
1306
ftruncate(fd: number, len: number, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
1307

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

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

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

A
annie_wangli 已提交
1314 1315 1316
  | Name     | Type                       | Mandatory  | Description              |
  | -------- | ------------------------- | ---- | ---------------- |
  | fd       | number                    | Yes   | File descriptor of the file to truncate.    |
A
Annie_wang 已提交
1317 1318
  | len      | number                    | No   | File length, in bytes, after truncation.|
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback that returns no value. |
Z
zengyawen 已提交
1319

A
Annie_wang 已提交
1320
**Example**
A
Annie_wang 已提交
1321

A
annie_wangli 已提交
1322
  ```js
A
Annie_wang 已提交
1323 1324
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1325 1326
  let len = 5;
  fileio.ftruncate(fd, 5, function(err){
A
annie_wangli 已提交
1327 1328 1329
      // Do something.
  });
  ```
Z
zengyawen 已提交
1330 1331


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

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

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

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

A
Annie_wang 已提交
1340
**Parameters**
A
Annie_wang 已提交
1341

A
annie_wangli 已提交
1342 1343 1344 1345
  | 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 已提交
1346

A
Annie_wang 已提交
1347
**Example**
A
Annie_wang 已提交
1348

A
annie_wangli 已提交
1349
  ```js
A
Annie_wang 已提交
1350 1351
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1352
  let len = 5;
P
Peter_1988 已提交
1353
  fileio.ftruncateSync(fd, len);
A
annie_wangli 已提交
1354
  ```
Z
zengyawen 已提交
1355 1356


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

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

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

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

A
Annie_wang 已提交
1365
**Parameters**
A
Annie_wang 已提交
1366

A
Annie_wang 已提交
1367 1368
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
A
Annie_wang 已提交
1369
| path   | string | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1370
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1371

A
Annie_wang 已提交
1372
**Return value**
A
Annie_wang 已提交
1373

A
annie_wangli 已提交
1374 1375
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1376
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
1377

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

A
annie_wangli 已提交
1380
  ```js
A
Annie_wang 已提交
1381
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1382
  let len = 5;
A
Annie_wang 已提交
1383
  fileio.truncate(filePath, len).then(function(){
A
Annie_wang 已提交
1384
      console.info("File truncated");
A
annie_wangli 已提交
1385 1386 1387 1388
  }).catch(function(err){
      console.info("Failed to truncate the file. Error:"+ err);
  });
  ```
Z
zengyawen 已提交
1389 1390


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

A
Annie_wang 已提交
1393
truncate(path: string, len: number, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
1394

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

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

A
Annie_wang 已提交
1399
**Parameters**
A
Annie_wang 已提交
1400

A
Annie_wang 已提交
1401 1402
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1403
| path     | string                    | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1404 1405
| len      | number                    | No  | File length, in bytes, after truncation.|
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback that returns no value.  |
Z
zengyawen 已提交
1406

A
Annie_wang 已提交
1407
**Example**
A
Annie_wang 已提交
1408

A
annie_wangli 已提交
1409
  ```js
A
Annie_wang 已提交
1410
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1411
  let len = 5;
A
Annie_wang 已提交
1412
  fileio.truncate(filePath, len, function(err){
A
annie_wangli 已提交
1413 1414 1415
      // Do something.
  });
  ```
Z
zengyawen 已提交
1416 1417


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

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

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

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

A
Annie_wang 已提交
1426
**Parameters**
A
Annie_wang 已提交
1427

A
Annie_wang 已提交
1428 1429
| Name| Type  | Mandatory| Description                            |
| ------ | ------ | ---- | -------------------------------- |
A
Annie_wang 已提交
1430
| path   | string | Yes  | Application sandbox path of the file to truncate.|
A
Annie_wang 已提交
1431
| len    | number | No  | File length, in bytes, after truncation.|
Z
zengyawen 已提交
1432

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

A
annie_wangli 已提交
1435
  ```js
A
Annie_wang 已提交
1436
  let filePath = pathDir + "/test.txt";
A
Annie_wang 已提交
1437
  let len = 5;
A
Annie_wang 已提交
1438
  fileio.truncateSync(filePath, len);
A
annie_wangli 已提交
1439
  ```
Z
zengyawen 已提交
1440 1441


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

A
annie_wangli 已提交
1444 1445 1446 1447 1448
readText(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): Promise&lt;string&gt;
Z
zengyawen 已提交
1449

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

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

A
Annie_wang 已提交
1454
**Parameters**
A
Annie_wang 已提交
1455

A
Annie_wang 已提交
1456 1457 1458 1459
| 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 已提交
1460

A
Annie_wang 已提交
1461
**Return value**
A
Annie_wang 已提交
1462

A
annie_wangli 已提交
1463 1464
  | Type                   | Description        |
  | --------------------- | ---------- |
A
Annie_wang 已提交
1465
  | Promise&lt;string&gt; | Promise used to return the content read.|
Z
zengyawen 已提交
1466

A
Annie_wang 已提交
1467
**Example**
A
Annie_wang 已提交
1468

A
annie_wangli 已提交
1469
  ```js
A
Annie_wang 已提交
1470 1471
  let filePath = pathDir + "/test.txt";
  fileio.readText(filePath).then(function(str) {
A
Annie_wang 已提交
1472
      console.info("Read text successfully:"+ str);
A
annie_wangli 已提交
1473
  }).catch(function(err){
A
Annie_wang 已提交
1474
      console.info("Failed to read the text. Error:"+ err);
A
annie_wangli 已提交
1475 1476
  });
  ```
Z
zengyawen 已提交
1477 1478


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

A
annie_wangli 已提交
1481 1482 1483 1484 1485
readText(filePath: string, options: {
    position?: number;
    length?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1486

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

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

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

A
Annie_wang 已提交
1493 1494 1495
| Name  | Type                       | Mandatory| Description                                                        |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| filePath | string                      | Yes  | Application sandbox path of the file to read.                                  |
A
Annie_wang 已提交
1496
| 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.|
A
Annie_wang 已提交
1497
| callback | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the content read.                        |
Z
zengyawen 已提交
1498

A
Annie_wang 已提交
1499
**Example**
A
Annie_wang 已提交
1500

A
annie_wangli 已提交
1501
  ```js
A
Annie_wang 已提交
1502 1503
  let filePath = pathDir + "/test.txt";
  fileio.readText(filePath, { position: 1, encoding: 'UTF-8' }, function(err, str){
A
annie_wangli 已提交
1504 1505 1506
      // Do something.
  });
  ```
Z
zengyawen 已提交
1507 1508


A
annie_wangli 已提交
1509 1510
## fileio.readTextSync<sup>7+</sup>

A
annie_wangli 已提交
1511 1512 1513 1514 1515
readTextSync(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): string
A
annie_wangli 已提交
1516 1517 1518

Synchronously reads the text of a file. 

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

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

A
Annie_wang 已提交
1523 1524
| Name  | Type  | Mandatory| Description                                                        |
| -------- | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
1525
| filePath | string | Yes  | Application sandbox path of the file to read.                                  |
A
Annie_wang 已提交
1526
| 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 已提交
1527

A
Annie_wang 已提交
1528
**Return value**
A
Annie_wang 已提交
1529

A
annie_wangli 已提交
1530 1531
  | Type  | Description                |
  | ------ | -------------------- |
A
Annie_wang 已提交
1532
  | string | Promise used to return the content of the file read.|
A
annie_wangli 已提交
1533

A
Annie_wang 已提交
1534
**Example**
A
Annie_wang 已提交
1535

A
annie_wangli 已提交
1536
  ```js
A
Annie_wang 已提交
1537 1538
  let filePath = pathDir + "/test.txt";
  let str = fileio.readTextSync(filePath, {position: 1, length: 3});
A
annie_wangli 已提交
1539 1540 1541 1542 1543 1544 1545
  ```


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

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

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

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

A
Annie_wang 已提交
1550
**Parameters**
A
Annie_wang 已提交
1551

A
Annie_wang 已提交
1552 1553
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1554
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1555

A
Annie_wang 已提交
1556
**Return value**
A
Annie_wang 已提交
1557

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

A
Annie_wang 已提交
1562
**Example**
A
Annie_wang 已提交
1563

A
annie_wangli 已提交
1564
  ```js
A
Annie_wang 已提交
1565 1566 1567
  let filePath = pathDir + "/test.txt";
  fileio.lstat(filePath).then(function(stat){
      console.info("Get link info:"+ JSON.stringify(stat));
A
annie_wangli 已提交
1568
  }).catch(function(err){
A
Annie_wang 已提交
1569
      console.info("Failed to obtain link info. Error:"+ err);
A
annie_wangli 已提交
1570 1571 1572 1573 1574 1575
  });
  ```


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

A
Annie_wang 已提交
1576
lstat(path: string, callback: AsyncCallback&lt;Stat&gt;): void
A
annie_wangli 已提交
1577

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

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

A
Annie_wang 已提交
1582
**Parameters**
A
Annie_wang 已提交
1583

A
Annie_wang 已提交
1584 1585
| Name  | Type                              | Mandatory| Description                                  |
| -------- | ---------------------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1586 1587
| 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 已提交
1588

A
Annie_wang 已提交
1589
**Example**
A
Annie_wang 已提交
1590

A
annie_wangli 已提交
1591
  ```js
A
Annie_wang 已提交
1592 1593
  let filePath = pathDir + "/test.txt";
  fileio.lstat(filePath, function (err, stat) {
A
annie_wangli 已提交
1594
      // Do something.
A
annie_wangli 已提交
1595
  });
A
annie_wangli 已提交
1596 1597 1598 1599 1600
  ```


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

A
Annie_wang 已提交
1601
lstatSync(path: string): Stat
A
annie_wangli 已提交
1602

A
Annie_wang 已提交
1603
Synchronously obtains the link information.
A
annie_wangli 已提交
1604

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

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

A
Annie_wang 已提交
1609 1610
| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
A
Annie_wang 已提交
1611
| path   | string | Yes  | Application sandbox path of the target file.|
A
annie_wangli 已提交
1612

A
Annie_wang 已提交
1613
**Return value**
A
Annie_wang 已提交
1614

A
annie_wangli 已提交
1615 1616
  | Type           | Description        |
  | ------------- | ---------- |
A
Annie_wang 已提交
1617
  | [Stat](#stat) | Link information obtained.|
A
annie_wangli 已提交
1618

A
Annie_wang 已提交
1619
**Example**
A
Annie_wang 已提交
1620

A
annie_wangli 已提交
1621
  ```js
A
Annie_wang 已提交
1622 1623
  let filePath = pathDir + "/test.txt";
  let stat = fileio.lstatSync(filePath);
A
annie_wangli 已提交
1624 1625 1626 1627 1628 1629 1630
  ```


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

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

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

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

A
Annie_wang 已提交
1635
**Parameters**
A
Annie_wang 已提交
1636

A
Annie_wang 已提交
1637 1638 1639 1640
| 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 已提交
1641

A
Annie_wang 已提交
1642
**Return value**
A
Annie_wang 已提交
1643

A
annie_wangli 已提交
1644 1645
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1646
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1647

A
Annie_wang 已提交
1648
**Example**
A
Annie_wang 已提交
1649

A
annie_wangli 已提交
1650
  ```js
A
Annie_wang 已提交
1651 1652 1653
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
  fileio.rename(srcFile, dstFile).then(function() {
A
Annie_wang 已提交
1654
      console.info("File renamed");
A
annie_wangli 已提交
1655
  }).catch(function(err){
A
Annie_wang 已提交
1656
      console.info("Failed to rename the file. Error:"+ err);
A
annie_wangli 已提交
1657 1658 1659 1660 1661 1662 1663 1664
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1669
**Parameters**
A
Annie_wang 已提交
1670

A
Annie_wang 已提交
1671 1672 1673
| Name  | Type                     | Mandatory| Description                        |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | Yes  | Application sandbox path of the file to rename.|
A
Annie_wang 已提交
1674
| newPath  | String                    | Yes  | Application sandbox path of the file renamed.  |
A
Annie_wang 已提交
1675
| Callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the file is asynchronously renamed.  |
A
annie_wangli 已提交
1676

A
Annie_wang 已提交
1677
**Example**
A
Annie_wang 已提交
1678

A
annie_wangli 已提交
1679
  ```js
A
Annie_wang 已提交
1680 1681 1682
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
  fileio.rename(srcFile, dstFile, function(err){
A
annie_wangli 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
  });
  ```


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

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

Synchronously renames a file.

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

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

A
Annie_wang 已提交
1697 1698 1699 1700
| 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 已提交
1701

A
Annie_wang 已提交
1702
**Example**
A
Annie_wang 已提交
1703

A
annie_wangli 已提交
1704
  ```js
A
Annie_wang 已提交
1705 1706 1707
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
  fileio.renameSync(srcFile, dstFile);
A
annie_wangli 已提交
1708 1709 1710 1711 1712 1713 1714
  ```


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

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

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

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

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

A
annie_wangli 已提交
1721 1722
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1723
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1724

A
Annie_wang 已提交
1725
**Return value**
A
Annie_wang 已提交
1726

A
annie_wangli 已提交
1727 1728
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1729
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1730

A
Annie_wang 已提交
1731
**Example**
A
Annie_wang 已提交
1732

A
annie_wangli 已提交
1733
  ```js
A
Annie_wang 已提交
1734 1735
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1736
  fileio.fsync(fd).then(function(){
A
Annie_wang 已提交
1737
      console.info("Data flushed");
A
annie_wangli 已提交
1738
  }).catch(function(err){
A
Annie_wang 已提交
1739
      console.info("Failed to flush data. Error:"+ err);
A
annie_wangli 已提交
1740 1741 1742 1743 1744 1745 1746 1747
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1752
**Parameters**
A
Annie_wang 已提交
1753

A
annie_wangli 已提交
1754 1755
  | Name     | Type                       | Mandatory  | Description             |
  | -------- | ------------------------- | ---- | --------------- |
A
Annie_wang 已提交
1756
  | fd       | number                    | Yes   | File descriptor of the file to flush.   |
A
annie_wangli 已提交
1757
  | Callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the file is synchronized in asynchronous mode.|
A
annie_wangli 已提交
1758

A
Annie_wang 已提交
1759
**Example**
A
Annie_wang 已提交
1760

A
annie_wangli 已提交
1761
  ```js
A
Annie_wang 已提交
1762 1763
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
  fileio.fsync(fd, function(err){
      // Do something.
  });
  ```


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

fsyncSync(fd: number): void

A
Annie_wang 已提交
1774
Flushes data of a file to disk in synchronous mode.
A
annie_wangli 已提交
1775 1776

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

A
Annie_wang 已提交
1778
**Parameters**
A
Annie_wang 已提交
1779

A
annie_wangli 已提交
1780 1781
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1782
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1783

A
Annie_wang 已提交
1784
**Example**
A
Annie_wang 已提交
1785

A
annie_wangli 已提交
1786
  ```js
A
Annie_wang 已提交
1787 1788
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
1789
  fileio.fsyncSync(fd);
A
annie_wangli 已提交
1790 1791 1792 1793 1794 1795 1796
  ```


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

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

A
Annie_wang 已提交
1797
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 已提交
1798

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

A
Annie_wang 已提交
1801
**Parameters**
A
Annie_wang 已提交
1802

A
annie_wangli 已提交
1803 1804
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1805
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1806

A
Annie_wang 已提交
1807
**Return value**
A
Annie_wang 已提交
1808

A
annie_wangli 已提交
1809 1810
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1811
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1812

A
Annie_wang 已提交
1813
**Example**
A
Annie_wang 已提交
1814

A
annie_wangli 已提交
1815
  ```js
A
Annie_wang 已提交
1816 1817
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1818
  fileio.fdatasync(fd).then(function(err) {
A
Annie_wang 已提交
1819
      console.info("Data flushed");
A
annie_wangli 已提交
1820
  }).catch(function(err){
A
Annie_wang 已提交
1821
      console.info("Failed to flush data. Error:"+ err);
A
annie_wangli 已提交
1822 1823 1824 1825 1826 1827
  });
  ```


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

A
Annie_wang 已提交
1828
fdatasync(fd: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
1829

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

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

A
Annie_wang 已提交
1834
**Parameters**
A
Annie_wang 已提交
1835

A
annie_wangli 已提交
1836 1837 1838 1839
  | 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 已提交
1840

A
Annie_wang 已提交
1841
**Example**
A
Annie_wang 已提交
1842

A
annie_wangli 已提交
1843
  ```js
A
Annie_wang 已提交
1844 1845
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
  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 已提交
1858 1859
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1860
**Parameters**
A
Annie_wang 已提交
1861

A
annie_wangli 已提交
1862 1863
  | Name | Type    | Mandatory  | Description          |
  | ---- | ------ | ---- | ------------ |
A
Annie_wang 已提交
1864
  | fd   | number | Yes   | File descriptor of the file to flush.|
A
annie_wangli 已提交
1865

A
Annie_wang 已提交
1866
**Example**
A
Annie_wang 已提交
1867

A
annie_wangli 已提交
1868
  ```js
A
Annie_wang 已提交
1869 1870
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
1871 1872 1873 1874 1875 1876 1877 1878
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

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

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

A
Annie_wang 已提交
1883
**Parameters**
A
Annie_wang 已提交
1884

A
Annie_wang 已提交
1885 1886
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1887 1888
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1889

A
Annie_wang 已提交
1890
**Return value**
A
Annie_wang 已提交
1891

A
annie_wangli 已提交
1892 1893
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
1894
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1895

A
Annie_wang 已提交
1896
**Example**
A
Annie_wang 已提交
1897

A
annie_wangli 已提交
1898
  ```js
A
Annie_wang 已提交
1899 1900 1901
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlink(srcFile, dstFile).then(function() {
A
Annie_wang 已提交
1902
      console.info("Symbolic link created");
A
annie_wangli 已提交
1903
  }).catch(function(err){
A
Annie_wang 已提交
1904
      console.info("Failed to create the symbolic link. Error:"+ err);
A
annie_wangli 已提交
1905 1906 1907 1908 1909 1910 1911 1912
  });
  ```


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

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

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

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

A
Annie_wang 已提交
1917
**Parameters**
A
Annie_wang 已提交
1918

A
Annie_wang 已提交
1919 1920
| Name  | Type                     | Mandatory| Description                            |
| -------- | ------------------------- | ---- | -------------------------------- |
A
Annie_wang 已提交
1921 1922
| target   | string                    | Yes  | Application sandbox path of the target file.        |
| srcPath  | string                    | Yes  | Application sandbox path of the symbolic link file.    |
A
Annie_wang 已提交
1923
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked when the symbolic link is created asynchronously.|
A
annie_wangli 已提交
1924

A
Annie_wang 已提交
1925
**Example**
A
Annie_wang 已提交
1926

A
annie_wangli 已提交
1927
  ```js
A
Annie_wang 已提交
1928 1929 1930
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlink(srcFile, dstFile, function (err) {
A
annie_wangli 已提交
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
      // 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 已提交
1942 1943
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
1944
**Parameters**
A
Annie_wang 已提交
1945

A
Annie_wang 已提交
1946 1947
| Name | Type  | Mandatory| Description                        |
| ------- | ------ | ---- | ---------------------------- |
A
Annie_wang 已提交
1948 1949
| target  | string | Yes  | Application sandbox path of the target file.    |
| srcPath | string | Yes  | Application sandbox path of the symbolic link file.|
A
annie_wangli 已提交
1950

A
Annie_wang 已提交
1951
**Example**
A
Annie_wang 已提交
1952

A
annie_wangli 已提交
1953
  ```js
A
Annie_wang 已提交
1954 1955 1956
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlinkSync(srcFile, dstFile);
A
annie_wangli 已提交
1957 1958 1959 1960 1961 1962 1963
  ```


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

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

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

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

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

A
Annie_wang 已提交
1970 1971
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
1972
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
1973 1974
| uid    | number | Yes  | New user ID (UID).       |
| gid    | number | Yes  | New group ID (GID).      |
A
annie_wangli 已提交
1975

A
Annie_wang 已提交
1976
**Return value**
A
Annie_wang 已提交
1977

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

A
Annie_wang 已提交
1982
**Example**
A
Annie_wang 已提交
1983

A
annie_wangli 已提交
1984
  ```js
A
Annie_wang 已提交
1985 1986 1987
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.chown(filePath, stat.uid, stat.gid).then(function(){
A
Annie_wang 已提交
1988
      console.info("File owner changed");
A
annie_wangli 已提交
1989
  }).catch(function(err){
A
Annie_wang 已提交
1990
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
1991 1992 1993 1994 1995 1996 1997 1998
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2003
**Parameters**
A
Annie_wang 已提交
2004

A
Annie_wang 已提交
2005 2006
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
2007
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
2008 2009 2010
| 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 已提交
2011

A
Annie_wang 已提交
2012
**Example**
A
Annie_wang 已提交
2013

A
annie_wangli 已提交
2014
  ```js
A
Annie_wang 已提交
2015 2016 2017
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath)
  fileio.chown(filePath, stat.uid, stat.gid, function (err){
A
annie_wangli 已提交
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
      // 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 已提交
2029 2030
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2031
**Parameters**
A
Annie_wang 已提交
2032

A
Annie_wang 已提交
2033 2034
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2035
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2036 2037
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2038

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

A
annie_wangli 已提交
2041
  ```js
A
Annie_wang 已提交
2042 2043 2044
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath)
  fileio.chownSync(filePath, stat.uid, stat.gid);
A
annie_wangli 已提交
2045 2046 2047 2048 2049 2050 2051
  ```


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

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

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

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

A
Annie_wang 已提交
2056
**Parameters**
A
Annie_wang 已提交
2057

A
annie_wangli 已提交
2058 2059 2060
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
2061

A
Annie_wang 已提交
2062
**Return value**
A
Annie_wang 已提交
2063

A
Annie_wang 已提交
2064
  | Type                  | Description        |
A
annie_wangli 已提交
2065
  | --------------------- | ---------- |
A
Annie_wang 已提交
2066
  | Promise&lt;string&gt; | Promise used to return the unique directory generated.|
A
annie_wangli 已提交
2067

A
Annie_wang 已提交
2068
**Example**
A
Annie_wang 已提交
2069

A
annie_wangli 已提交
2070
  ```js
A
Annie_wang 已提交
2071 2072
  fileio.mkdtemp(pathDir + "/XXXXXX").then(function(pathDir){
      console.info("mkdtemp succeed:"+ pathDir);
A
annie_wangli 已提交
2073
  }).catch(function(err){
A
Annie_wang 已提交
2074
      console.info("Failed to create the temporary directory. Error:"+ err);
A
annie_wangli 已提交
2075 2076 2077 2078 2079 2080 2081 2082
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2087
**Parameters**
A
Annie_wang 已提交
2088

A
annie_wangli 已提交
2089 2090 2091 2092
  | 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 已提交
2093

A
Annie_wang 已提交
2094
**Example**
A
Annie_wang 已提交
2095

A
annie_wangli 已提交
2096
  ```js
A
Annie_wang 已提交
2097
  fileio.mkdtemp(pathDir + "/XXXXXX", function (err, res) {
A
annie_wangli 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
      // Do something.
  });
  ```


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

mkdtempSync(prefix: string): string

Synchronously creates a temporary directory.

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

A
Annie_wang 已提交
2111
**Parameters**
A
Annie_wang 已提交
2112

A
annie_wangli 已提交
2113 2114 2115
  | Name   | Type    | Mandatory  | Description                         |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | Yes   | A randomly generated string used to replace "XXXXXX" in a directory.|
A
annie_wangli 已提交
2116

A
Annie_wang 已提交
2117
**Return value**
A
Annie_wang 已提交
2118

A
Annie_wang 已提交
2119
  | Type   | Description        |
A
annie_wangli 已提交
2120
  | ------ | ---------- |
A
annie_wangli 已提交
2121 2122
  | string | Unique path generated.|

A
Annie_wang 已提交
2123
**Example**
A
Annie_wang 已提交
2124

A
annie_wangli 已提交
2125
  ```js
A
Annie_wang 已提交
2126
  let res = fileio.mkdtempSync(pathDir + "/XXXXXX");
A
annie_wangli 已提交
2127 2128 2129 2130 2131 2132 2133
  ```


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

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

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

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

A
Annie_wang 已提交
2138
**Parameters**
A
Annie_wang 已提交
2139

A
annie_wangli 已提交
2140 2141 2142 2143
  | 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 已提交
2144

A
Annie_wang 已提交
2145
**Return value**
A
Annie_wang 已提交
2146

A
Annie_wang 已提交
2147
  | Type                | Description                          |
A
annie_wangli 已提交
2148
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2149
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2150

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

A
annie_wangli 已提交
2153
  ```js
A
Annie_wang 已提交
2154 2155
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2156
  let mode = 0o700;
A
annie_wangli 已提交
2157
  fileio.fchmod(fd, mode).then(function() {
A
Annie_wang 已提交
2158
      console.info("File permissions changed");
A
annie_wangli 已提交
2159
  }).catch(function(err){
A
Annie_wang 已提交
2160
      console.info("Failed to change file permissions. Error:"+ err);
A
annie_wangli 已提交
2161 2162 2163 2164 2165 2166 2167 2168
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2173
**Parameters**
A
Annie_wang 已提交
2174

A
annie_wangli 已提交
2175 2176 2177 2178 2179
  | 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 已提交
2180

A
Annie_wang 已提交
2181
**Example**
A
Annie_wang 已提交
2182

A
annie_wangli 已提交
2183
  ```js
A
Annie_wang 已提交
2184 2185
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2186
  let mode = 0o700;
A
annie_wangli 已提交
2187 2188 2189 2190 2191 2192 2193 2194
  fileio.fchmod(fd, mode, function (err) {
      // Do something.
  });
  ```


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

A
annie_wangli 已提交
2195
fchmodSync(fd: number, mode: number): void
A
annie_wangli 已提交
2196 2197 2198

Synchronously changes the file permissions based on the file descriptor.

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

A
Annie_wang 已提交
2201
**Parameters**
A
Annie_wang 已提交
2202

A
annie_wangli 已提交
2203 2204 2205 2206
  | 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 已提交
2207

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

A
annie_wangli 已提交
2210
  ```js
A
Annie_wang 已提交
2211 2212
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2213
  let mode = 0o700;
A
annie_wangli 已提交
2214 2215 2216 2217 2218 2219 2220 2221
   fileio.fchmodSync(fd, mode);
  ```


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

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

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

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

A
Annie_wang 已提交
2226
**Parameters**
A
Annie_wang 已提交
2227

A
Annie_wang 已提交
2228 2229
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2230
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2231
| 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 已提交
2232

A
Annie_wang 已提交
2233
**Return value**
A
Annie_wang 已提交
2234

A
annie_wangli 已提交
2235 2236
  | Type                               | Description       |
  | --------------------------------- | --------- |
A
Annie_wang 已提交
2237
  | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
A
annie_wangli 已提交
2238

A
Annie_wang 已提交
2239
**Example**
A
Annie_wang 已提交
2240

A
annie_wangli 已提交
2241
  ```js
A
Annie_wang 已提交
2242 2243 2244
  let filePath = pathDir + "/test.txt";
  fileio.createStream(filePath, "r+").then(function(stream){
      console.info("Stream created");
A
annie_wangli 已提交
2245
  }).catch(function(err){
A
Annie_wang 已提交
2246
      console.info("Failed to create the stream. Error:"+ err);
A
annie_wangli 已提交
2247 2248 2249 2250 2251 2252 2253 2254
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2259
**Parameters**
A
Annie_wang 已提交
2260

A
Annie_wang 已提交
2261 2262
| Name  | Type                                   | Mandatory| Description                                                        |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2263
| path     | string                                  | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2264
| 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_wang 已提交
2265
| callback | AsyncCallback&lt;[Stream](#stream)&gt; | Yes  | Callback invoked when the stream is open asynchronously.                                  |
A
annie_wangli 已提交
2266

A
Annie_wang 已提交
2267
**Example**
A
Annie_wang 已提交
2268

A
annie_wangli 已提交
2269
  ```js
A
Annie_wang 已提交
2270 2271
  let filePath = pathDir + "/test.txt";
  fileio.createStream(filePath, "r+", function(err, stream){
A
annie_wangli 已提交
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
      // 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 已提交
2283 2284
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2285
**Parameters**
A
Annie_wang 已提交
2286

A
Annie_wang 已提交
2287 2288
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2289
| path   | string | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2290
| 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 已提交
2291

A
Annie_wang 已提交
2292
**Return value**
A
Annie_wang 已提交
2293

A
Annie_wang 已提交
2294
  | Type               | Description       |
A
annie_wangli 已提交
2295
  | ------------------ | --------- |
A
Annie_wang 已提交
2296
  | [Stream](#stream) | Stream opened.|
A
annie_wangli 已提交
2297

A
Annie_wang 已提交
2298
**Example**
A
Annie_wang 已提交
2299

A
annie_wangli 已提交
2300
  ```js
A
Annie_wang 已提交
2301 2302
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2303 2304 2305 2306 2307 2308 2309
  ```


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

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

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

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

A
Annie_wang 已提交
2314
**Parameters**
A
Annie_wang 已提交
2315

A
annie_wangli 已提交
2316 2317 2318 2319
  | 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 已提交
2320

A
Annie_wang 已提交
2321
**Return value**
A
Annie_wang 已提交
2322

A
Annie_wang 已提交
2323
  | Type                              | Description       |
A
annie_wangli 已提交
2324
  | --------------------------------- | --------- |
A
Annie_wang 已提交
2325
  | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
A
annie_wangli 已提交
2326

A
Annie_wang 已提交
2327
**Example**
A
Annie_wang 已提交
2328

A
annie_wangli 已提交
2329
  ```js
A
Annie_wang 已提交
2330 2331
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2332 2333
  fileio.fdopenStream(fd, "r+").then(function(stream){
      console.info("Stream opened");
A
annie_wangli 已提交
2334
  }).catch(function(err){
A
Annie_wang 已提交
2335
      console.info("Failed to open the stream. Error:"+ err);
A
annie_wangli 已提交
2336 2337 2338 2339 2340 2341 2342 2343
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2348
**Parameters**
A
Annie_wang 已提交
2349

A
annie_wangli 已提交
2350 2351 2352 2353
  | 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_wang 已提交
2354
  | callback | AsyncCallback&nbsp;&lt;[Stream](#stream)&gt; | Yes   | Callback invoked when the stream is open asynchronously.                           |
A
annie_wangli 已提交
2355

A
Annie_wang 已提交
2356
**Example**
A
Annie_wang 已提交
2357

A
annie_wangli 已提交
2358
  ```js
A
Annie_wang 已提交
2359 2360
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
Annie_wang 已提交
2361
  fileio.fdopenStream(fd, "r+", function (err, stream) {
A
annie_wangli 已提交
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
      // 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 已提交
2373 2374
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2375
**Parameters**
A
Annie_wang 已提交
2376

A
annie_wangli 已提交
2377 2378 2379 2380
  | 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 已提交
2381

A
Annie_wang 已提交
2382
**Return value**
A
Annie_wang 已提交
2383

A
Annie_wang 已提交
2384
  | Type               | Description       |
A
annie_wangli 已提交
2385
  | ------------------ | --------- |
A
Annie_wang 已提交
2386
  | [Stream](#stream) | Stream opened.|
A
annie_wangli 已提交
2387

A
Annie_wang 已提交
2388
**Example**
A
Annie_wang 已提交
2389

A
annie_wangli 已提交
2390
  ```js
A
Annie_wang 已提交
2391 2392
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
A
annie_wangli 已提交
2393 2394 2395 2396 2397 2398 2399 2400
  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 已提交
2401
Changes the file owner based on the file descriptor. This API uses a promise to return the result.
A
annie_wangli 已提交
2402

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

A
Annie_wang 已提交
2405
**Parameters**
A
Annie_wang 已提交
2406

A
annie_wangli 已提交
2407 2408 2409 2410 2411
  | 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 已提交
2412

A
Annie_wang 已提交
2413
**Return value**
A
Annie_wang 已提交
2414

A
annie_wangli 已提交
2415 2416
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2417
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2418

A
Annie_wang 已提交
2419
**Example**
A
Annie_wang 已提交
2420

A
annie_wangli 已提交
2421
  ```js
A
Annie_wang 已提交
2422 2423 2424
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
A
annie_wangli 已提交
2425
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2426
      console.info("File owner changed");
A
annie_wangli 已提交
2427
  }).catch(function(err){
A
Annie_wang 已提交
2428
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2429 2430 2431 2432 2433 2434 2435 2436
  });
  ```


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

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

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

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

A
Annie_wang 已提交
2441
**Parameters**
A
Annie_wang 已提交
2442

A
annie_wangli 已提交
2443 2444 2445 2446 2447 2448
  | 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 已提交
2449

A
Annie_wang 已提交
2450
**Example**
A
Annie_wang 已提交
2451

A
annie_wangli 已提交
2452
  ```js
A
Annie_wang 已提交
2453 2454 2455
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
A
annie_wangli 已提交
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
  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 已提交
2468 2469
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2470
**Parameters**
A
Annie_wang 已提交
2471

A
annie_wangli 已提交
2472 2473 2474 2475 2476
  | 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 已提交
2477

A
Annie_wang 已提交
2478
**Example**
A
Annie_wang 已提交
2479

A
annie_wangli 已提交
2480
  ```js
A
Annie_wang 已提交
2481 2482 2483
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
A
annie_wangli 已提交
2484 2485 2486 2487 2488 2489 2490 2491
  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 已提交
2492
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 已提交
2493

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

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

A
Annie_wang 已提交
2498 2499
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2500
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2501 2502
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2503

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

A
annie_wangli 已提交
2506 2507
  | Type                 | Description                          |
  | ------------------- | ---------------------------- |
A
Annie_wang 已提交
2508
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
2509

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

A
annie_wangli 已提交
2512
  ```js
A
Annie_wang 已提交
2513 2514 2515
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchown(filePath, stat.uid, stat.gid).then(function() {
A
Annie_wang 已提交
2516
      console.info("File owner changed");
A
annie_wangli 已提交
2517
  }).catch(function(err){
A
Annie_wang 已提交
2518
      console.info("Failed to change the file owner. Error:"+ err);
A
annie_wangli 已提交
2519 2520 2521 2522 2523 2524 2525 2526
  });
  ```


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

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

A
Annie_wang 已提交
2527
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 已提交
2528

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

A
Annie_wang 已提交
2531
**Parameters**
A
Annie_wang 已提交
2532

A
Annie_wang 已提交
2533 2534
| Name  | Type                     | Mandatory| Description                          |
| -------- | ------------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
2535
| path     | string                    | Yes  | Application sandbox path of the file.    |
A
Annie_wang 已提交
2536 2537 2538
| 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 已提交
2539

A
Annie_wang 已提交
2540
**Example**
A
Annie_wang 已提交
2541

A
annie_wangli 已提交
2542
  ```js
A
Annie_wang 已提交
2543 2544 2545
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchown(filePath, stat.uid, stat.gid, function (err){
A
annie_wangli 已提交
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556
      // 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 已提交
2557 2558
**System capability**: SystemCapability.FileManagement.File.FileIO

A
Annie_wang 已提交
2559
**Parameters**
A
Annie_wang 已提交
2560

A
Annie_wang 已提交
2561 2562
| Name| Type  | Mandatory| Description                      |
| ------ | ------ | ---- | -------------------------- |
A
Annie_wang 已提交
2563
| path   | string | Yes  | Application sandbox path of the file.|
A
Annie_wang 已提交
2564 2565
| uid    | number | Yes  | New UID.                 |
| gid    | number | Yes  | New GID.                 |
A
annie_wangli 已提交
2566

A
Annie_wang 已提交
2567
**Example**
A
Annie_wang 已提交
2568

A
annie_wangli 已提交
2569
  ```js
A
Annie_wang 已提交
2570 2571 2572
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchownSync(filePath, stat.uid, stat.gid);
A
annie_wangli 已提交
2573 2574 2575 2576 2577 2578 2579
  ```


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

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

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

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

A
Annie_wang 已提交
2584
**Parameters**
A
Annie_wang 已提交
2585

A
Annie_wang 已提交
2586 2587
| Name  | Type                             | Mandatory| Description                                                        |
| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
2588
| filePath | string                            | Yes  | Application sandbox path of the file.                                  |
A
Annie_wang 已提交
2589 2590
| 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 已提交
2591

A
Annie_wang 已提交
2592
**Return value**
A
Annie_wang 已提交
2593

A
Annie_wang 已提交
2594
  | Type                 | Description        |
A
annie_wangli 已提交
2595
  | -------------------- | ---------- |
A
Annie_wang 已提交
2596
  | [Watcher](#watcher7) | Promise used to return the **Watcher** instance.|
A
annie_wangli 已提交
2597

A
Annie_wang 已提交
2598
**Example**
A
Annie_wang 已提交
2599

A
annie_wangli 已提交
2600
  ```js
A
Annie_wang 已提交
2601 2602
  let filePath = pathDir +"/test.txt";
  fileio.createWatcher(filePath, 1, function(number){
A
Annie_wang 已提交
2603
     console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2604
  });
A
Annie_wang 已提交
2605
  
A
annie_wangli 已提交
2606 2607 2608 2609 2610 2611 2612
  ```


## Readout

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

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

A
annie_wangli 已提交
2615 2616 2617 2618 2619
| 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 已提交
2620 2621 2622 2623 2624 2625


## 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 已提交
2626
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
2627 2628 2629

### Attributes

A
annie_wangli 已提交
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643
| 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 已提交
2644 2645 2646 2647 2648 2649


### isBlockDevice

isBlockDevice(): boolean

A
Annie_wang 已提交
2650
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 已提交
2651

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

A
Annie_wang 已提交
2654
**Return value**
A
Annie_wang 已提交
2655

A
annie_wangli 已提交
2656 2657
  | Type     | Description              |
  | ------- | ---------------- |
A
Annie_wang 已提交
2658
  | boolean | Whether the file is a block special file.|
A
annie_wangli 已提交
2659

A
Annie_wang 已提交
2660
**Example**
A
Annie_wang 已提交
2661

A
annie_wangli 已提交
2662
  ```js
A
Annie_wang 已提交
2663 2664
  let filePath = pathDir + "/test.txt";
  let isBLockDevice = fileio.statSync(filePath).isBlockDevice();
A
annie_wangli 已提交
2665 2666 2667 2668 2669 2670 2671
  ```


### isCharacterDevice

isCharacterDevice(): boolean

A
Annie_wang 已提交
2672
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 已提交
2673

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

A
Annie_wang 已提交
2676
**Return value**
A
Annie_wang 已提交
2677

A
annie_wangli 已提交
2678 2679
  | Type     | Description               |
  | ------- | ----------------- |
A
Annie_wang 已提交
2680
  | boolean | Whether the file is a character special file.|
A
annie_wangli 已提交
2681

A
Annie_wang 已提交
2682
**Example**
A
Annie_wang 已提交
2683

A
annie_wangli 已提交
2684
  ```js
A
Annie_wang 已提交
2685 2686
  let filePath = pathDir + "/test.txt";
  let isCharacterDevice = fileio.statSync(filePath).isCharacterDevice();
A
annie_wangli 已提交
2687 2688 2689 2690 2691 2692 2693
  ```


### isDirectory

isDirectory(): boolean

A
Annie_wang 已提交
2694
Checks whether this file is a directory.
A
annie_wangli 已提交
2695

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

A
Annie_wang 已提交
2698
**Return value**
A
Annie_wang 已提交
2699

A
annie_wangli 已提交
2700 2701
  | Type     | Description           |
  | ------- | ------------- |
A
Annie_wang 已提交
2702
  | boolean | Whether the file is a directory.|
A
annie_wangli 已提交
2703

A
Annie_wang 已提交
2704
**Example**
A
Annie_wang 已提交
2705

A
annie_wangli 已提交
2706
  ```js
A
Annie_wang 已提交
2707 2708
  let dirPath = pathDir + "/test";
  let isDirectory = fileio.statSync(dirPath).isDirectory(); 
A
annie_wangli 已提交
2709 2710 2711 2712 2713 2714 2715
  ```


### isFIFO

isFIFO(): boolean

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

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

A
Annie_wang 已提交
2720
**Return value**
A
Annie_wang 已提交
2721

A
annie_wangli 已提交
2722 2723
  | Type     | Description                   |
  | ------- | --------------------- |
A
Annie_wang 已提交
2724
  | boolean | Whether the file is an FIFO.|
A
annie_wangli 已提交
2725

A
Annie_wang 已提交
2726
**Example**
A
Annie_wang 已提交
2727

A
annie_wangli 已提交
2728
  ```js
A
Annie_wang 已提交
2729 2730
  let filePath = pathDir + "/test.txt";
  let isFIFO = fileio.statSync(filePath).isFIFO(); 
A
annie_wangli 已提交
2731 2732 2733 2734 2735 2736 2737
  ```


### isFile

isFile(): boolean

A
Annie_wang 已提交
2738
Checks whether this file is a regular file.
A
annie_wangli 已提交
2739

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

A
Annie_wang 已提交
2742
**Return value**
A
Annie_wang 已提交
2743

A
annie_wangli 已提交
2744 2745
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2746
  | boolean | Whether the file is a regular file.|
A
annie_wangli 已提交
2747

A
Annie_wang 已提交
2748
**Example**
A
Annie_wang 已提交
2749

A
annie_wangli 已提交
2750
  ```js
A
Annie_wang 已提交
2751 2752
  let filePath = pathDir + "/test.txt";
  let isFile = fileio.statSync(filePath).isFile();
A
annie_wangli 已提交
2753 2754 2755 2756 2757 2758 2759
  ```


### isSocket

isSocket(): boolean

A
Annie_wang 已提交
2760
Checks whether this file is a socket.
A
annie_wangli 已提交
2761

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

A
Annie_wang 已提交
2764
**Return value**
A
Annie_wang 已提交
2765

A
annie_wangli 已提交
2766 2767
  | Type     | Description            |
  | ------- | -------------- |
A
Annie_wang 已提交
2768
  | boolean | Whether the file is a socket.|
A
annie_wangli 已提交
2769

A
Annie_wang 已提交
2770
**Example**
A
Annie_wang 已提交
2771

A
annie_wangli 已提交
2772
  ```js
A
Annie_wang 已提交
2773 2774
  let filePath = pathDir + "/test.txt";
  let isSocket = fileio.statSync(filePath).isSocket(); 
A
annie_wangli 已提交
2775 2776 2777 2778 2779 2780 2781
  ```


### isSymbolicLink

isSymbolicLink(): boolean

A
Annie_wang 已提交
2782
Checks whether this file is a symbolic link.
A
annie_wangli 已提交
2783

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

A
Annie_wang 已提交
2786
**Return value**
A
Annie_wang 已提交
2787

A
annie_wangli 已提交
2788 2789
  | Type     | Description             |
  | ------- | --------------- |
A
Annie_wang 已提交
2790
  | boolean | Whether the file is a symbolic link.|
A
annie_wangli 已提交
2791

A
Annie_wang 已提交
2792
**Example**
A
Annie_wang 已提交
2793

A
annie_wangli 已提交
2794
  ```js
A
Annie_wang 已提交
2795 2796
  let filePath = pathDir + "/test";
  let isSymbolicLink = fileio.statSync(filePath).isSymbolicLink(); 
A
annie_wangli 已提交
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806
  ```


## 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 已提交
2807
stop(): Promise&lt;void&gt;
A
annie_wangli 已提交
2808

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

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

A
Annie_wang 已提交
2813
**Example**
A
Annie_wang 已提交
2814

A
annie_wangli 已提交
2815
  ```js
A
Annie_wang 已提交
2816 2817
  let filePath = path + "/test.txt";
  let watcher = fileio.createWatcher(filePath, 1, function(number){
A
Annie_wang 已提交
2818 2819 2820 2821 2822
      console.info("Monitoring times: "+number);
  });
  watcher.stop().then(function(){
       console.info("Watcher stopped");
  });
A
annie_wangli 已提交
2823 2824 2825 2826 2827
  ```


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

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

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

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

A
Annie_wang 已提交
2834
**Parameters**
A
Annie_wang 已提交
2835

A
annie_wangli 已提交
2836 2837 2838
  | Name     | Type                       | Mandatory  | Description                    |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when **watcher** is stopped asynchronously.|
A
annie_wangli 已提交
2839

A
Annie_wang 已提交
2840
**Example**
A
Annie_wang 已提交
2841

A
annie_wangli 已提交
2842
  ```js
A
Annie_wang 已提交
2843 2844
  let filePath = path +"/test.txt";
  let watcher = fileio.createWatcher(filePath, 1, function(number){
A
Annie_wang 已提交
2845
      console.info("Monitoring times: "+number);
A
annie_wangli 已提交
2846
  });
A
Annie_wang 已提交
2847 2848 2849
  watcher.stop(function(){
      console.info("Watcher stopped");
  })
A
annie_wangli 已提交
2850
  ```
A
annie_wangli 已提交
2851

A
Annie_wang 已提交
2852

A
Annie_wang 已提交
2853
## Stream
A
annie_wangli 已提交
2854

A
Annie_wang 已提交
2855
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 已提交
2856 2857 2858 2859 2860 2861


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

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

A
Annie_wang 已提交
2862
Closes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2863 2864 2865

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

A
Annie_wang 已提交
2866
**Return value**
A
Annie_wang 已提交
2867

A
annie_wangli 已提交
2868 2869 2870 2871
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream close result.|

A
Annie_wang 已提交
2872
**Example**
A
Annie_wang 已提交
2873

A
annie_wangli 已提交
2874
  ```js
A
Annie_wang 已提交
2875 2876
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2877
  ss.close().then(function(){
A
Annie_wang 已提交
2878
      console.info("File stream closed");
A
annie_wangli 已提交
2879
  }).catch(function(err){
A
Annie_wang 已提交
2880
      console.info("Failed to close the file stream. Error:"+ err);
A
annie_wangli 已提交
2881 2882 2883 2884 2885 2886 2887 2888
  });
  ```


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

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

A
Annie_wang 已提交
2889
Closes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2890 2891 2892

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

A
Annie_wang 已提交
2893
**Parameters**
A
Annie_wang 已提交
2894

A
annie_wangli 已提交
2895 2896 2897 2898
  | Name     | Type                       | Mandatory  | Description           |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is closed asynchronously.|

A
Annie_wang 已提交
2899
**Example**
A
Annie_wang 已提交
2900

A
annie_wangli 已提交
2901
  ```js
A
Annie_wang 已提交
2902 2903
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2904
  ss.close(function (err) {
A
Annie_wang 已提交
2905
      // Do something.
A
annie_wangli 已提交
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917
  });
  ```


### closeSync

closeSync(): void

Synchronously closes the stream.

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

A
Annie_wang 已提交
2918
**Example**
A
Annie_wang 已提交
2919

A
annie_wangli 已提交
2920
  ```js
A
Annie_wang 已提交
2921 2922
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2923 2924 2925 2926 2927 2928 2929 2930
  ss.closeSync();
  ```


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

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

A
Annie_wang 已提交
2931
Flushes the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
2932 2933 2934

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

A
Annie_wang 已提交
2935
**Return value**
A
Annie_wang 已提交
2936

A
annie_wangli 已提交
2937 2938 2939 2940
  | Type                 | Description           |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise used to return the stream flushing result.|

A
Annie_wang 已提交
2941
**Example**
A
Annie_wang 已提交
2942

A
annie_wangli 已提交
2943
  ```js
A
Annie_wang 已提交
2944 2945
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2946
  ss.flush().then(function (){
A
Annie_wang 已提交
2947
      console.info("Stream flushed");
A
annie_wangli 已提交
2948
  }).catch(function(err){
A
Annie_wang 已提交
2949
      console.info("Failed to flush the stream. Error:"+ err);
A
annie_wangli 已提交
2950 2951 2952 2953 2954 2955 2956 2957
  });
  ```


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

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

A
Annie_wang 已提交
2958
Flushes the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2959 2960 2961

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

A
Annie_wang 已提交
2962
**Parameters**
A
Annie_wang 已提交
2963

A
annie_wangli 已提交
2964 2965 2966 2967
  | Name     | Type                       | Mandatory  | Description            |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | Yes   | Callback invoked when the stream is asynchronously flushed.|

A
Annie_wang 已提交
2968
**Example**
A
Annie_wang 已提交
2969

A
annie_wangli 已提交
2970
  ```js
A
Annie_wang 已提交
2971 2972
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2973
  ss.flush(function (err) {
A
Annie_wang 已提交
2974
      // Do something.
A
annie_wangli 已提交
2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
  });
  ```


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

flushSync(): void

Synchronously flushes the stream.

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

A
Annie_wang 已提交
2987
**Example**
A
Annie_wang 已提交
2988

A
annie_wangli 已提交
2989
  ```js
A
Annie_wang 已提交
2990 2991
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004
  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 已提交
3005
Writes data into the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
3006 3007 3008

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

A
Annie_wang 已提交
3009
**Parameters**
A
Annie_wang 已提交
3010

A
annie_wangli 已提交
3011 3012 3013
  | 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 已提交
3014
  | 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 已提交
3015

A
Annie_wang 已提交
3016
**Return value**
A
Annie_wang 已提交
3017

A
annie_wangli 已提交
3018 3019
  | Type                   | Description      |
  | --------------------- | -------- |
A
Annie_wang 已提交
3020
  | Promise&lt;number&gt; | Promise used to return the length of the data written.|
A
annie_wangli 已提交
3021

A
Annie_wang 已提交
3022
**Example**
A
Annie_wang 已提交
3023

A
annie_wangli 已提交
3024
  ```js
A
Annie_wang 已提交
3025 3026
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
3027
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
A
Annie_wang 已提交
3028
      console.info("Data written to the stream. Size is:"+ number);
A
annie_wangli 已提交
3029
  }).catch(function(err){
A
Annie_wang 已提交
3030
      console.info("Failed to write data to the stream. Error:"+ err);
A
annie_wangli 已提交
3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
  });
  ```


### 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 已提交
3044
Writes data into the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3045 3046 3047

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

A
Annie_wang 已提交
3048
**Parameters**
A
Annie_wang 已提交
3049

A
Annie_wang 已提交
3050 3051 3052
  | 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 已提交
3053
  | 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 已提交
3054
  | callback | AsyncCallback&lt;number&gt;     | Yes  | Callback invoked when the data is written asynchronously.                              |
A
annie_wangli 已提交
3055

A
Annie_wang 已提交
3056
**Example**
A
Annie_wang 已提交
3057

A
annie_wangli 已提交
3058
  ```js
A
Annie_wang 已提交
3059 3060
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
3061
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
A
Annie_wang 已提交
3062
      if (bytesWritten) {
A
Annie_wang 已提交
3063
         // Do something.
A
Annie_wang 已提交
3064
         console.info("Data written to the stream. Size is:"+ bytesWritten);
A
annie_wangli 已提交
3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082
      }
  });
  ```


### 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 已提交
3083
**Parameters**
A
Annie_wang 已提交
3084

A
annie_wangli 已提交
3085 3086 3087
  | 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 已提交
3088
  | 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 已提交
3089

A
Annie_wang 已提交
3090
**Return value**
A
Annie_wang 已提交
3091

A
annie_wangli 已提交
3092 3093 3094 3095
  | Type    | Description      |
  | ------ | -------- |
  | number | Length of the data written in the file.|

A
Annie_wang 已提交
3096
**Example**
A
Annie_wang 已提交
3097

A
annie_wangli 已提交
3098
  ```js
A
Annie_wang 已提交
3099 3100
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath,"r+");
A
annie_wangli 已提交
3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
  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 已提交
3113
Reads data from the stream. This API uses a promise to return the result.
A
annie_wangli 已提交
3114 3115 3116

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

A
Annie_wang 已提交
3117
**Parameters**
A
Annie_wang 已提交
3118

A
annie_wangli 已提交
3119 3120 3121
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3122
  | 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 已提交
3123

A
Annie_wang 已提交
3124
**Return value**
A
Annie_wang 已提交
3125

A
annie_wangli 已提交
3126 3127
  | Type                                | Description    |
  | ---------------------------------- | ------ |
A
Annie_wang 已提交
3128
  | Promise&lt;[ReadOut](#readout)&gt; | Promise used to return the data read.|
A
annie_wangli 已提交
3129

A
Annie_wang 已提交
3130
**Example**
A
Annie_wang 已提交
3131

A
annie_wangli 已提交
3132
  ```js
A
Annie_wang 已提交
3133 3134
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
A
Annie_wang 已提交
3135
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut){
A
Annie_wang 已提交
3136
      console.info("Read data successfully");
A
Annie_wang 已提交
3137
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
3138
  }).catch(function(err){
A
Annie_wang 已提交
3139
      console.info("Failed to read data. Error:"+ err);
A
annie_wangli 已提交
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
  });
  ```


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

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

A
Annie_wang 已提交
3152
Reads data from the stream. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3153 3154 3155

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

A
Annie_wang 已提交
3156
**Parameters**
A
Annie_wang 已提交
3157

A
annie_wangli 已提交
3158 3159 3160
  | Name     | Type                                      | Mandatory  | Description                                      |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3161
  | 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 已提交
3162 3163
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | Yes   | Callback invoked when data is read asynchronously from the stream.                        |

A
Annie_wang 已提交
3164
**Example**
A
Annie_wang 已提交
3165

A
annie_wangli 已提交
3166
  ```js
A
Annie_wang 已提交
3167 3168
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
3169
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
A
Annie_wang 已提交
3170
      if (readOut) {
A
Annie_wang 已提交
3171
          console.info("Read data successfully");
A
Annie_wang 已提交
3172
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
A
annie_wangli 已提交
3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
      }
  });
  ```


### 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 已提交
3190 3191
**Parameters**

A
annie_wangli 已提交
3192 3193 3194
  | Name    | Type         | Mandatory  | Description                                      |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | Yes   | Buffer used to store the file read.                             |
A
Annie_wang 已提交
3195 3196 3197
  | 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 已提交
3198 3199 3200 3201 3202

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

A
Annie_wang 已提交
3203
**Example**
A
Annie_wang 已提交
3204

A
annie_wangli 已提交
3205
  ```js
A
Annie_wang 已提交
3206 3207
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
A
annie_wangli 已提交
3208 3209 3210 3211 3212 3213
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
  ```


## Dir

A
Annie_wang 已提交
3214
Manages directories. Before calling a method of the **Dir** class, use the **opendir()** method synchronously or asynchronously to create a **Dir** instance.
A
annie_wangli 已提交
3215 3216 3217 3218 3219 3220


### read

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

A
Annie_wang 已提交
3221
Reads the next directory entry. This API uses a promise to return the result.
A
annie_wangli 已提交
3222 3223 3224

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

A
Annie_wang 已提交
3225
**Return value**
A
Annie_wang 已提交
3226

A
annie_wangli 已提交
3227 3228
  | Type                              | Description           |
  | -------------------------------- | ------------- |
A
Annie_wang 已提交
3229
  | Promise&lt;[Dirent](#dirent)&gt; | Promise used to return the directory entry read.|
A
annie_wangli 已提交
3230

A
Annie_wang 已提交
3231
**Example**
A
Annie_wang 已提交
3232

A
annie_wangli 已提交
3233 3234
  ```js
  dir.read().then(function (dirent){
A
Annie_wang 已提交
3235
      console.log("Read the next directory entry:"+JSON.stringify(dirent));
A
annie_wangli 已提交
3236
  }).catch(function(err){
A
Annie_wang 已提交
3237
      console.info("Failed to read the next directory entry. Error:"+ err);
A
annie_wangli 已提交
3238 3239 3240 3241 3242 3243 3244 3245
  });
  ```


### read

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

A
Annie_wang 已提交
3246
Reads the next directory entry. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3247 3248 3249

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

A
Annie_wang 已提交
3250
**Parameters**
A
Annie_wang 已提交
3251

A
annie_wangli 已提交
3252 3253 3254 3255
  | Name     | Type                                    | Mandatory  | Description              |
  | -------- | -------------------------------------- | ---- | ---------------- |
  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | Yes   | Callback invoked when the next directory entry is asynchronously read.|

A
Annie_wang 已提交
3256
**Example**
A
Annie_wang 已提交
3257

A
annie_wangli 已提交
3258 3259
  ```js
  dir.read(function (err, dirent) {
A
Annie_wang 已提交
3260
      if (dirent) {
A
Annie_wang 已提交
3261
          // Do something.
A
Annie_wang 已提交
3262
          console.log("Read the next directory entry:"+JSON.stringify(dirent));
A
annie_wangli 已提交
3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275
      }
  });
  ```


### readSync

readSync(): Dirent

Synchronously reads the next directory entry.

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

A
Annie_wang 已提交
3276
**Return value**
A
Annie_wang 已提交
3277

A
annie_wangli 已提交
3278 3279 3280 3281
  | Type               | Description      |
  | ----------------- | -------- |
  | [Dirent](#dirent) | Directory entry read.|

A
Annie_wang 已提交
3282
**Example**
A
Annie_wang 已提交
3283

A
annie_wangli 已提交
3284 3285 3286 3287 3288
  ```js
  let dirent = dir.readSync();
  ```


A
Annie_wang 已提交
3289 3290 3291 3292 3293 3294 3295 3296 3297
### close<sup>7+</sup>

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

Closes a directory. This API uses a promise to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir.

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

**Example**
A
Annie_wang 已提交
3298

A
Annie_wang 已提交
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314
  ```js
  dir.close().then(function(err){
      console.info("close dir successfully");
  });
  ```


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

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

Closes a directory. This API uses an asynchronous callback to return the result. After a directory is closed, the file descriptor in Dir will be released and no directory entry can be read from Dir.

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

**Example**
A
Annie_wang 已提交
3315

A
Annie_wang 已提交
3316 3317 3318 3319 3320 3321 3322
  ```js
  dir.close(function(err){
      console.info("close dir successfully");
  });
  ```


A
annie_wangli 已提交
3323 3324 3325 3326 3327 3328 3329 3330
### 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 已提交
3331
**Example**
A
Annie_wang 已提交
3332

A
annie_wangli 已提交
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
  ```js
  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 已提交
3355
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 已提交
3356 3357 3358

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

A
Annie_wang 已提交
3359
**Return value**
A
Annie_wang 已提交
3360

A
annie_wangli 已提交
3361 3362 3363 3364
  | Type     | Description              |
  | ------- | ---------------- |
  | boolean | Whether the directory entry is a block special file.|

A
Annie_wang 已提交
3365
**Example**
A
Annie_wang 已提交
3366

A
annie_wangli 已提交
3367
  ```js
A
Annie_wang 已提交
3368
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380
  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 已提交
3381
**Return value**
A
Annie_wang 已提交
3382

A
annie_wangli 已提交
3383 3384 3385 3386
  | Type     | Description               |
  | ------- | ----------------- |
  | boolean | Whether the directory entry is a character special file.|

A
Annie_wang 已提交
3387
**Example**
A
Annie_wang 已提交
3388

A
annie_wangli 已提交
3389
  ```js
A
Annie_wang 已提交
3390
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
  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 已提交
3403
**Return value**
A
Annie_wang 已提交
3404

A
annie_wangli 已提交
3405 3406 3407 3408
  | Type     | Description           |
  | ------- | ------------- |
  | boolean | Whether the directory entry is a directory.|

A
Annie_wang 已提交
3409
**Example**
A
Annie_wang 已提交
3410

A
annie_wangli 已提交
3411
  ```js
A
Annie_wang 已提交
3412
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3413 3414 3415 3416 3417 3418 3419 3420
  let isDirectory = dir.readSync().isDirectory(); 
  ```


### isFIFO

isFIFO(): boolean

A
Annie_wang 已提交
3421
Checks whether this directory entry is a named pipe (or FIFO). Named pipes are used for inter-process communication.
A
annie_wangli 已提交
3422 3423 3424

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

A
Annie_wang 已提交
3425
**Return value**
A
Annie_wang 已提交
3426

A
annie_wangli 已提交
3427 3428 3429 3430
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a FIFO.|

A
Annie_wang 已提交
3431
**Example**
A
Annie_wang 已提交
3432

A
annie_wangli 已提交
3433
  ```js
A
Annie_wang 已提交
3434
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446
  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 已提交
3447
**Return value**
A
Annie_wang 已提交
3448

A
annie_wangli 已提交
3449 3450 3451 3452
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a regular file.|

A
Annie_wang 已提交
3453
**Example**
A
Annie_wang 已提交
3454

A
annie_wangli 已提交
3455
  ```js
A
Annie_wang 已提交
3456
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468
  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 已提交
3469
**Return value**
A
Annie_wang 已提交
3470

A
annie_wangli 已提交
3471 3472 3473 3474
  | Type     | Description            |
  | ------- | -------------- |
  | boolean | Whether the directory entry is a socket.|

A
Annie_wang 已提交
3475
**Example**
A
Annie_wang 已提交
3476

A
annie_wangli 已提交
3477
  ```js
A
Annie_wang 已提交
3478
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490
  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 已提交
3491
**Return value**
A
Annie_wang 已提交
3492

A
annie_wangli 已提交
3493 3494 3495 3496
  | Type     | Description             |
  | ------- | --------------- |
  | boolean | Whether the directory entry is a symbolic link.|

A
Annie_wang 已提交
3497
**Example**
A
Annie_wang 已提交
3498

A
annie_wangli 已提交
3499
  ```js
A
Annie_wang 已提交
3500
  let dir = fileio.opendirSync(pathDir);
A
annie_wangli 已提交
3501 3502
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```
A
Annie_wang 已提交
3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520

## Filter<sup>9+</sup>

Defines the file filter configuration.

**System API**: This is a system API.

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


| Name       | Type      | Description               |
| ----------- | --------------- | ------------------ |
| suffix | Array&lt;string&gt;     | File name extensions. The keywords in the array are of the OR relationship.          |
| displayName    | Array&lt;string&gt;     | File name for fuzzy match. The keywords in the array are of the OR relationship.|
| mimeType    | Array&lt;string&gt; | MIME types to match. The keywords in the array are of the OR relationship.      |
| fileSizeOver    | number | File size to match. The files which are of the same or a lager size are matched.      |
| lastModifiedAfter    | Date | File modification time to match. The files modified after the specified time are matched.      |
| excludeMedia    | Boolean | Whether to exclude the files already in Media.      |