js-apis-fileio.md 129.7 KB
Newer Older
T
explain  
tianyu 已提交
1
# 文件管理
Z
zengyawen 已提交
2

H
haonan_7 已提交
3 4
该模块提供文件存储管理能力,包括文件基本管理、文件目录管理、文件信息统计、文件流式读写等常用功能。

Z
zengyawen 已提交
5 6
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
Z
zengyawen 已提交
7

Z
zhangxingxia 已提交
8

Z
zengyawen 已提交
9
## 导入模块
Z
zengyawen 已提交
10

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

Z
zengyawen 已提交
15 16

## 使用说明
Z
zengyawen 已提交
17

W
wangbo 已提交
18
使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:
19

20
**Stage模型**
21

22
 ```js
23
import Ability from '@ohos.application.Ability';
24 25 26
class MainAbility extends Ability {
    onWindowStageCreate(windowStage) {
        let context = this.context;
Z
zhuhongtao666 已提交
27
        let pathDir = context.filesDir;
28 29 30 31
    }
}
 ```

Z
zhuhongtao666 已提交
32
 Stage模型context的具体获取方法参见[Stage模型](js-apis-ability-context.md#abilitycontext)
33

34
**FA模型**
35

W
wangbo 已提交
36 37 38
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
 let context = featureAbility.getContext();
W
wangbo 已提交
39
 context.getFilesDir().then((data) => {
Z
zhuhongtao666 已提交
40
      let pathDir = data;
W
wangbo 已提交
41
 })
Z
zhangxingxia 已提交
42
 ```
43
 
Z
zhuhongtao666 已提交
44
 FA模型context的具体获取方法参见[FA模型](js-apis-Context.md#Context模块)
Z
zengyawen 已提交
45

Z
zengyawen 已提交
46 47 48 49
## fileio.stat

stat(path: string): Promise<Stat>

H
haonan_7 已提交
50
获取文件信息,使用Promise异步回调。
Z
zengyawen 已提交
51

52 53
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
54
**参数:**
Z
zhangxingxia 已提交
55

Z
zengyawen 已提交
56 57 58
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待获取文件的应用沙箱路径。 |
Z
zhangxingxia 已提交
59

Z
zhangxingxia 已提交
60
**返回值:**
Z
zhangxingxia 已提交
61

H
haonan_7 已提交
62 63 64
  | 类型                           | 说明         |
  | ---------------------------- | ---------- |
  | Promise<[Stat](#stat)> | Promise对象。返回文件的具体信息。 |
Z
zhangxingxia 已提交
65

Z
zhangxingxia 已提交
66
**示例:**
H
haonan_7 已提交
67

Z
zhangxingxia 已提交
68
  ```js
Z
zhuhongtao666 已提交
69 70
  let filePath = pathDir + "test.txt";
  fileio.stat(filePath).then(function(stat){
H
haonan_7 已提交
71
      console.info("getFileInfo succeed:"+ JSON.stringify(stat));
Z
zhangxingxia 已提交
72 73 74
  }).catch(function(err){
      console.info("getFileInfo failed with error:"+ err);
  });
Z
zengyawen 已提交
75 76 77 78 79
  ```


## fileio.stat

Z
zhuhongtao666 已提交
80
stat(path: string, callback: AsyncCallback<Stat>): void
Z
zengyawen 已提交
81

H
haonan_7 已提交
82
获取文件信息,使用callback异步回调。
Z
zengyawen 已提交
83

84 85
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
86
**参数:**
H
haonan_7 已提交
87

Z
zengyawen 已提交
88 89 90 91
| 参数名   | 类型                               | 必填 | 说明                           |
| -------- | ---------------------------------- | ---- | ------------------------------ |
| path     | string                             | 是   | 待获取文件的应用沙箱路径。     |
| callback | AsyncCallback<[Stat](#stat)> | 是   | 异步获取文件的信息之后的回调。 |
Z
zengyawen 已提交
92

Z
zhangxingxia 已提交
93
**示例:**
H
haonan_7 已提交
94

Z
zhangxingxia 已提交
95
  ```js
Z
zhuhongtao666 已提交
96
  fileio.stat(pathDir, function (err, stat) {
Z
zhangxingxia 已提交
97
      // example code in Stat
Z
zengyawen 已提交
98 99 100 101
  });
  ```


Z
zengyawen 已提交
102 103
## fileio.statSync

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

以同步方法获取文件的信息。

108 109
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
110
**参数:**
H
haonan_7 已提交
111

Z
zengyawen 已提交
112 113 114
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待获取文件的应用沙箱路径。 |
Z
zengyawen 已提交
115 116


Z
zhangxingxia 已提交
117
**返回值:**
H
haonan_7 已提交
118 119 120 121

  | 类型            | 说明         |
  | ------------- | ---------- |
  | [Stat](#stat) | 表示文件的具体信息。 |
Z
zengyawen 已提交
122

Z
zhangxingxia 已提交
123
**示例:**
H
haonan_7 已提交
124

Z
zhangxingxia 已提交
125
  ```js
Z
zhuhongtao666 已提交
126
  let stat = fileio.statSync(pathDir);
Z
zengyawen 已提交
127
  // example code in Stat
Z
zengyawen 已提交
128 129 130
  ```


Z
zengyawen 已提交
131
## fileio.opendir
Z
zengyawen 已提交
132

Z
zengyawen 已提交
133
opendir(path: string): Promise<Dir>
Z
zengyawen 已提交
134

H
haonan_7 已提交
135
打开文件目录,使用Promise异步回调。
Z
zengyawen 已提交
136

137 138
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
139
**参数:**
H
haonan_7 已提交
140

Z
zengyawen 已提交
141 142 143
| 参数名 | 类型   | 必填 | 说明                           |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | 是   | 待打开文件目录的应用沙箱路径。 |
Z
zengyawen 已提交
144

Z
zhangxingxia 已提交
145
**返回值:**
H
haonan_7 已提交
146 147 148 149

  | 类型                         | 说明       |
  | -------------------------- | -------- |
  | Promise<[Dir](#dir)> | Promise对象。返回Dir对象。 |
Z
zengyawen 已提交
150

Z
zhangxingxia 已提交
151
**示例:**
H
haonan_7 已提交
152

Z
zhangxingxia 已提交
153
  ```js
Z
zhuhongtao666 已提交
154 155
  let dirPath = pathDir + "/testDir";
  fileio.opendir(dirPath).then(function(dir){
H
haonan_7 已提交
156
      console.info("opendir succeed:"+ JSON.stringify(dir));
Z
zhangxingxia 已提交
157 158 159
  }).catch(function(err){
      console.info("opendir failed with error:"+ err);
  });
Z
zengyawen 已提交
160 161 162
  ```


Z
zengyawen 已提交
163
## fileio.opendir
Z
zengyawen 已提交
164

Z
zengyawen 已提交
165
opendir(path: string, callback: AsyncCallback<Dir>): void
Z
zengyawen 已提交
166

H
haonan_7 已提交
167
打开文件目录,使用callback异步回调。
Z
zengyawen 已提交
168

169 170
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
171
**参数:**
Z
zhangxingxia 已提交
172

Z
zengyawen 已提交
173 174 175 176
| 参数名   | 类型                             | 必填 | 说明                           |
| -------- | -------------------------------- | ---- | ------------------------------ |
| path     | string                           | 是   | 待打开文件目录的应用沙箱路径。 |
| callback | AsyncCallback<[Dir](#dir)> | 是   | 异步打开文件目录之后的回调。   |
Z
zhangxingxia 已提交
177

Z
zhangxingxia 已提交
178
**示例:**
H
haonan_7 已提交
179

Z
zhangxingxia 已提交
180
  ```js
Z
zhuhongtao666 已提交
181
  fileio.opendir(pathDir, function (err, dir) { 
Z
zhangxingxia 已提交
182 183
      // example code in Dir struct
      // use read/readSync/close
Z
zengyawen 已提交
184
  });
Z
zengyawen 已提交
185 186 187
  ```


Z
zengyawen 已提交
188
## fileio.opendirSync
Z
zengyawen 已提交
189

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

以同步方法打开文件目录。
Z
zengyawen 已提交
193

194 195
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zengyawen 已提交
196

Z
zhangxingxia 已提交
197
**参数:**
Z
zhangxingxia 已提交
198

Z
zengyawen 已提交
199 200 201
| 参数名 | 类型   | 必填 | 说明                           |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | 是   | 待打开文件目录的应用沙箱路径。 |
Z
zhangxingxia 已提交
202

Z
zhangxingxia 已提交
203
**返回值:**
H
haonan_7 已提交
204 205 206 207

  | 类型          | 说明       |
  | ----------- | -------- |
  | [Dir](#dir) | 返回Dir对象。 |
Z
zengyawen 已提交
208

Z
zhangxingxia 已提交
209
**示例:**
H
haonan_7 已提交
210

Z
zhangxingxia 已提交
211
  ```js
Z
zhuhongtao666 已提交
212
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
213 214
  // example code in Dir struct
  // use read/readSync/close
Z
zengyawen 已提交
215 216 217
  ```


Z
zengyawen 已提交
218
## fileio.access
Z
zengyawen 已提交
219

Z
zengyawen 已提交
220
access(path: string, mode?: number): Promise<void>
Z
zengyawen 已提交
221

H
haonan_7 已提交
222
检查当前进程是否可访问某文件,使用Promise异步回调。
Z
zengyawen 已提交
223

224 225
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
226
**参数:**
Z
zhangxingxia 已提交
227

Z
zengyawen 已提交
228 229 230 231
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待访问文件的应用沙箱路径。                                   |
| mode   | number | 否   | 访问文件时的选项,可给定如下选项,以按位或的方式使用多个选项,默认给定0。<br/>确认当前进程是否具有对应权限:<br/>-&nbsp;0:确认文件是否存在。<br/>-&nbsp;1:确认当前进程是否具有可执行权限。<br/>-&nbsp;2:确认当前进程是否具有写权限。<br/>-&nbsp;4:确认当前进程是否具有读权限。 |
Z
zhangxingxia 已提交
232

Z
zhangxingxia 已提交
233
**返回值:**
H
haonan_7 已提交
234 235 236 237

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
238

Z
zhangxingxia 已提交
239
**示例:**
H
haonan_7 已提交
240

Z
zhangxingxia 已提交
241
  ```js
Z
zhuhongtao666 已提交
242 243
  let filePath = pathDir + "/test.txt";
  fileio.access(filePath).then(function() {
H
haonan_7 已提交
244
      console.info("access succeed");
Z
zhangxingxia 已提交
245 246
  }).catch(function(err){
      console.info("access failed with error:"+ err);
Z
zengyawen 已提交
247
  });
Z
zengyawen 已提交
248 249 250
  ```


Z
zengyawen 已提交
251
## fileio.access
Z
zengyawen 已提交
252

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

H
haonan_7 已提交
255
检查当前进程是否可访问某文件,使用callback异步回调。
Z
zengyawen 已提交
256

257 258
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
259
**参数:**
H
haonan_7 已提交
260

Z
zengyawen 已提交
261 262 263 264 265
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| path     | string                    | 是   | 待访问文件的应用沙箱路径。                                   |
| mode     | number                    | 否   | 访问文件时的选项,可给定如下选项,以按位或的方式使用多个选项,默认给定0。<br/>确认当前进程是否具有对应权限:<br/>-&nbsp;0:确认文件是否存在。<br/>-&nbsp;1:确认当前进程是否具有可执行权限。<br/>-&nbsp;2:确认当前进程是否具有写权限。<br/>-&nbsp;4:确认当前进程是否具有读权限。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步检查当前进程是否可访问某文件之后的回调。                 |
Z
zengyawen 已提交
266

Z
zhangxingxia 已提交
267
**示例:**
H
haonan_7 已提交
268

Z
zhangxingxia 已提交
269
  ```js
Z
zhuhongtao666 已提交
270 271
  let filePath = pathDir + "/test.txt";
  fileio.access(filePath, function (err) {
Z
zhangxingxia 已提交
272
      // do something
Z
zengyawen 已提交
273
  });
Z
zengyawen 已提交
274 275 276
  ```


Z
zengyawen 已提交
277
## fileio.accessSync
Z
zengyawen 已提交
278

Z
zengyawen 已提交
279 280 281
accessSync(path: string, mode?: number): void

以同步方法检查当前进程是否可访问某文件。
Z
zengyawen 已提交
282

283
**系统能力**:SystemCapability.FileManagement.File.FileIO
Z
zengyawen 已提交
284

Z
zhangxingxia 已提交
285
**参数:**
H
haonan_7 已提交
286

Z
zengyawen 已提交
287 288 289 290
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待访问文件的应用沙箱路径。                                   |
| mode   | number | 否   | 访问文件时的选项,可给定如下选项,以按位或的方式使用多个选项,默认给定0。<br/>确认当前进程是否具有对应权限:<br/>-&nbsp;0:确认文件是否存在。<br/>-&nbsp;1:确认当前进程是否具有可执行权限。<br/>-&nbsp;2:确认当前进程是否具有写权限。<br/>-&nbsp;4:确认当前进程是否具有读权限。 |
Z
zengyawen 已提交
291

Z
zhangxingxia 已提交
292
**示例:**
H
haonan_7 已提交
293

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


Z
zengyawen 已提交
304
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
305

Z
zhuhongtao666 已提交
306
close(fd: number): Promise&lt;void&gt;
Z
zengyawen 已提交
307

H
haonan_7 已提交
308
关闭文件,使用Promise异步回调。
Z
zengyawen 已提交
309

310 311
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
312
**参数:**
H
haonan_7 已提交
313 314 315 316

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待关闭文件的文件描述符。 |
Z
zengyawen 已提交
317

Z
zhangxingxia 已提交
318
**返回值:**
H
haonan_7 已提交
319 320 321 322

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
323

Z
zhangxingxia 已提交
324
**示例:**
H
haonan_7 已提交
325

Z
zhangxingxia 已提交
326
  ```js
Z
zhuhongtao666 已提交
327 328
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
329
  fileio.close(fd).then(function(){
H
haonan_7 已提交
330
      console.info("close file succeed");
Z
zhangxingxia 已提交
331 332 333
  }).catch(function(err){
      console.info("close file failed with error:"+ err);
  });
Z
zengyawen 已提交
334 335 336
  ```


Z
zengyawen 已提交
337
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
338

Z
zhuhongtao666 已提交
339
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
340

H
haonan_7 已提交
341
关闭文件,使用callback异步回调。
Z
zengyawen 已提交
342

343 344
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
345
**参数:**
H
haonan_7 已提交
346 347 348 349 350

  | 参数名      | 类型                        | 必填   | 说明           |
  | -------- | ------------------------- | ---- | ------------ |
  | fd       | number                    | 是    | 待关闭文件的文件描述符。 |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步关闭文件之后的回调。 |
Z
zengyawen 已提交
351

Z
zhangxingxia 已提交
352
**示例:**
H
haonan_7 已提交
353

Z
zhangxingxia 已提交
354
  ```js
Z
zhuhongtao666 已提交
355 356
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
357 358
  fileio.close(fd, function (err) {
      // do something
Z
zengyawen 已提交
359
  });
Z
zengyawen 已提交
360 361 362
  ```


Z
zengyawen 已提交
363
## fileio.closeSync
Z
zengyawen 已提交
364

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

Z
zengyawen 已提交
367
以同步方法关闭文件。
Z
zengyawen 已提交
368

369 370
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
371
**参数:**
H
haonan_7 已提交
372 373 374 375

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待关闭文件的文件描述符。 |
Z
zengyawen 已提交
376

Z
zhangxingxia 已提交
377
**示例:**
H
haonan_7 已提交
378

Z
zhangxingxia 已提交
379
  ```js
Z
zhuhongtao666 已提交
380 381
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
382
  fileio.closeSync(fd);
Z
zengyawen 已提交
383 384 385
  ```


Z
zengyawen 已提交
386
## fileio.copyFile
Z
zengyawen 已提交
387

Z
zhuhongtao666 已提交
388
copyFile(src: string | number, dest: string | number, mode?: number): Promise&lt;void&gt;
Z
zengyawen 已提交
389

H
haonan_7 已提交
390
复制文件,使用Promise异步回调。
Z
zengyawen 已提交
391

392 393
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
394
**参数:**
H
haonan_7 已提交
395 396 397 398 399 400

  | 参数名  | 类型                         | 必填   | 说明                                       |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
  | src  | string&nbsp;\|&nbsp;number | 是    | 待复制文件的路径或待复制文件的描述符。                      |
  | dest | string&nbsp;\|&nbsp;number | 是    | 目标文件路径或目标文件描述符。                          |
  | mode | number                     | 否    | mode提供覆盖文件的选项,当前仅支持0,且默认为0。<br/>0:完全覆盖目标文件,未覆盖部分将被裁切掉。 |
Z
zengyawen 已提交
401

Z
zhangxingxia 已提交
402
**返回值:**
H
haonan_7 已提交
403 404 405 406

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
407

Z
zhangxingxia 已提交
408
**示例:**
H
haonan_7 已提交
409

Z
zhangxingxia 已提交
410
  ```js
Z
zhuhongtao666 已提交
411 412 413
  let srcPath = pathDir + "srcDir/test.txt";
  let dstPath = pathDir + "dstDir/test.txt";
  fileio.copyFile(srcPath, dstPath).then(function(){
H
haonan_7 已提交
414
      console.info("copyFile succeed");
Z
zhangxingxia 已提交
415 416 417
  }).catch(function(err){
      console.info("copyFile failed with error:"+ err);
  });
Z
zengyawen 已提交
418 419 420
  ```


Z
zengyawen 已提交
421
## fileio.copyFile
Z
zengyawen 已提交
422

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

H
haonan_7 已提交
425
复制文件,使用callback异步回调。
Z
zengyawen 已提交
426

427 428
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
429
**参数:**
H
haonan_7 已提交
430 431 432 433 434 435 436

  | 参数名      | 类型                         | 必填   | 说明                                       |
  | -------- | -------------------------- | ---- | ---------------------------------------- |
  | src      | string&nbsp;\|&nbsp;number | 是    | 待复制文件的路径或待复制文件的描述符。                      |
  | dest     | string&nbsp;\|&nbsp;number | 是    | 目标文件路径或目标文件描述符。                          |
  | mode     | number                     | 否    | mode提供覆盖文件的选项,当前仅支持0,且默认为0。<br/>0:完全覆盖目标文件,未覆盖部分将被裁切掉。 |
  | callback | AsyncCallback&lt;void&gt;  | 是    | 异步复制文件之后的回调。                             |
Z
zengyawen 已提交
437

Z
zhangxingxia 已提交
438
**示例:**
H
haonan_7 已提交
439

Z
zhangxingxia 已提交
440
  ```js
Z
zhuhongtao666 已提交
441 442 443
  let srcPath = pathDir + "srcDir/test.txt";
  let dstPath = pathDir + "dstDir/test.txt";
  fileio.copyFile(srcPath, dstPath, function (err) {
Z
zhangxingxia 已提交
444
      // do something
Z
zengyawen 已提交
445
  });
Z
zengyawen 已提交
446 447 448
  ```


Z
zengyawen 已提交
449
## fileio.copyFileSync
Z
zengyawen 已提交
450

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

Z
zengyawen 已提交
453
以同步方法复制文件。
Z
zengyawen 已提交
454

455 456
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
457
**参数:**
H
haonan_7 已提交
458 459 460 461 462 463

  | 参数名  | 类型                         | 必填   | 说明                                       |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
  | src  | string&nbsp;\|&nbsp;number | 是    | 待复制文件的路径或待复制文件的描述符。                      |
  | dest | string&nbsp;\|&nbsp;number | 是    | 目标文件路径或目标文件描述符。                          |
  | mode | number                     | 否    | mode提供覆盖文件的选项,当前仅支持0,且默认为0。<br/>0:完全覆盖目标文件,未覆盖部分将被裁切掉。 |
Z
zengyawen 已提交
464

Z
zhangxingxia 已提交
465
**示例:**
H
haonan_7 已提交
466

Z
zhangxingxia 已提交
467
  ```js
Z
zhuhongtao666 已提交
468 469 470
  let srcPath = pathDir + "srcDir/test.txt";
  let dstPath = pathDir + "dstDir/test.txt";
  fileio.copyFileSync(srcPath, dstPath);
Z
zengyawen 已提交
471 472 473
  ```


Z
zengyawen 已提交
474
## fileio.mkdir
Z
zengyawen 已提交
475

Z
zhuhongtao666 已提交
476
mkdir(path: string, mode?: number): Promise&lt;void&gt;
Z
zengyawen 已提交
477

H
haonan_7 已提交
478
创建目录,使用Promise异步回调。
Z
zengyawen 已提交
479

480
**系统能力**:SystemCapability.FileManagement.File.FileIO
Z
zengyawen 已提交
481

Z
zhangxingxia 已提交
482
**参数:**
H
haonan_7 已提交
483

Z
zengyawen 已提交
484 485 486 487
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待创建目录的应用沙箱路径。                                   |
| mode   | number | 否   | 创建目录的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o775。<br/>-&nbsp;0o775:所有者具有读、写及可执行权限,其余用户具有读及可执行权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
Z
zengyawen 已提交
488

Z
zhangxingxia 已提交
489
**返回值:**
H
haonan_7 已提交
490 491 492 493

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
494

Z
zhangxingxia 已提交
495
**示例:**
H
haonan_7 已提交
496

Z
zhangxingxia 已提交
497
  ```js
Z
zhuhongtao666 已提交
498 499
  let dirPath = pathDir + '/testDir';
  fileio.mkdir(dirPath).then(function() {
H
haonan_7 已提交
500
      console.info("mkdir succeed");
Z
zhangxingxia 已提交
501 502 503
  }).catch(function (error){
      console.info("mkdir failed with error:"+ error);
  });
Z
zengyawen 已提交
504 505 506
  ```


Z
zengyawen 已提交
507
## fileio.mkdir
Z
zengyawen 已提交
508

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

H
haonan_7 已提交
511
创建目录,使用callback异步回调。
Z
zengyawen 已提交
512

513 514
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
515
**参数:**
H
haonan_7 已提交
516

Z
zengyawen 已提交
517 518 519 520 521
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| path     | string                    | 是   | 待创建目录的应用沙箱路径。                                   |
| mode     | number                    | 否   | 创建目录的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o775。<br/>-&nbsp;0o775:所有者具有读、写及可执行权限,其余用户具有读及可执行权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步创建目录操作完成之后的回调。                             |
Z
zengyawen 已提交
522

Z
zhangxingxia 已提交
523
**示例:**
H
haonan_7 已提交
524

Z
zhangxingxia 已提交
525
  ```js
Z
zhuhongtao666 已提交
526 527
  let dirPath = pathDir + '/testDir';
  fileio.mkdir(dirPath, function(err) {
H
haonan_7 已提交
528
    console.info("mkdir succeed");
Z
zengyawen 已提交
529
  });
Z
zengyawen 已提交
530 531 532
  ```


Z
zengyawen 已提交
533
## fileio.mkdirSync
Z
zengyawen 已提交
534

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

Z
zengyawen 已提交
537
以同步方法创建目录。
Z
zengyawen 已提交
538

539 540
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
541
**参数:**
H
haonan_7 已提交
542

Z
zengyawen 已提交
543 544 545 546
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待创建目录的应用沙箱路径。                                   |
| mode   | number | 否   | 创建目录的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o775。<br/>-&nbsp;0o775:所有者具有读、写及可执行权限,其余用户具有读及可执行权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
Z
zengyawen 已提交
547

Z
zhangxingxia 已提交
548
**示例:**
H
haonan_7 已提交
549

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


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

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

H
haonan_7 已提交
560
打开文件,使用Promise异步回调。
Z
zengyawen 已提交
561

562 563
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
564
**参数:**
H
haonan_7 已提交
565

Z
zengyawen 已提交
566 567 568
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待打开文件的应用沙箱路径。                                   |
R
raoxian 已提交
569
| flags  | number | 否   | 打开文件的选项,必须指定如下选项中的一个,默认以只读方式打开:<br/>-&nbsp;0o0:只读打开。<br/>-&nbsp;0o1:只写打开。<br/>-&nbsp;0o2:读写打开。<br/>同时,也可给定如下选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;0o100:若文件不存在,则创建文件。使用该选项时必须指定第三个参数&nbsp;mode。<br/>-&nbsp;0o200:如果追加了0o100选项,且文件已经存在,则出错。<br/>-&nbsp;0o1000:如果文件存在且以只写或读写的方式打开文件,则将其长度裁剪为零。<br/>-&nbsp;0o2000:以追加方式打开,后续写将追加到文件末尾。<br/>-&nbsp;0o4000:如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;0o200000:如果path不指向目录,则出错。<br/>-&nbsp;0o400000:如果path指向符号链接,则出错。<br/>-&nbsp;0o4010000:以同步IO的方式打开文件。 |
Z
zengyawen 已提交
570
| mode   | number | 否   | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o666。<br/>-&nbsp;0o666:所有者具有读、写权限,所有用户组具有读、写权限,其余用户具有读、写权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
Z
zengyawen 已提交
571

Z
zhangxingxia 已提交
572
**返回值:**
H
haonan_7 已提交
573 574 575 576

  | 类型                    | 说明          |
  | --------------------- | ----------- |
  | Promise&lt;number&gt; | Promise对象。返回打开文件的文件描述符。 |
Z
zengyawen 已提交
577

Z
zhangxingxia 已提交
578
**示例:**
H
haonan_7 已提交
579

Z
zhangxingxia 已提交
580
  ```js
Z
zhuhongtao666 已提交
581 582
  let filePath = pathDir + "/test.txt";
  fileio.open(filePath, 0o1, 0o0200).then(function(number){
H
haonan_7 已提交
583
      console.info("open file succeed");
Z
zhangxingxia 已提交
584
  }).catch(function(err){
Z
zhangxingxia 已提交
585 586
      console.info("open file failed with error:"+ err);
  });
Z
zengyawen 已提交
587 588 589
  ```


Z
zengyawen 已提交
590
## fileio.open<sup>7+</sup>
Z
zengyawen 已提交
591

Z
zengyawen 已提交
592
open(path: string, flags: number, mode: number, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
593

H
haonan_7 已提交
594
打开文件,使用callback异步回调。
Z
zengyawen 已提交
595

596 597
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
598
**参数:**
H
haonan_7 已提交
599

Z
zengyawen 已提交
600 601 602
| 参数名   | 类型                            | 必填 | 说明                                                         |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| path     | string                          | 是   | 待打开文件的应用沙箱路径。                                   |
Z
zhuhongtao666 已提交
603 604
| flags    | number                          | 否   | 打开文件的选项,必须指定如下选项中的一个,默认以只读方式打开:<br/>-&nbsp;0o0:只读打开。<br/>-&nbsp;0o1:只写打开。<br/>-&nbsp;0o2:读写打开。<br/>同时,也可给定如下选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;0o100:若文件不存在,则创建文件。使用该选项时必须指定第三个参数&nbsp;mode。<br/>-&nbsp;0o200:如果追加了0o100选项,且文件已经存在,则出错。<br/>-&nbsp;0o1000:如果文件存在且以只写或读写的方式打开文件,则将其长度裁剪为零。<br/>-&nbsp;0o2000:以追加方式打开,后续写将追加到文件末尾。<br/>-&nbsp;0o4000:如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;0o200000:如果path不指向目录,则出错。<br/>-&nbsp;0o400000:如果path指向符号链接,则出错。<br/>-&nbsp;0o4010000:以同步IO的方式打开文件。 |
| mode     | number                          | 否   | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o666。<br/>-&nbsp;0o666:所有者具有读、写权限,所有用户组具有读、写权限,其余用户具有读、写权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
Z
zengyawen 已提交
605
| callback | AsyncCallback&nbsp;&lt;void&gt; | 是   | 异步打开文件之后的回调。                                     |
Z
zengyawen 已提交
606

Z
zhangxingxia 已提交
607
**示例:**
H
haonan_7 已提交
608

Z
zhangxingxia 已提交
609
  ```js
Z
zhuhongtao666 已提交
610 611
  let filePath = pathDir + "/test.txt";
  fileio.open(filePath, 0, function(err, fd) {
Z
zhangxingxia 已提交
612
      // do something
Z
zengyawen 已提交
613
  });
Z
zengyawen 已提交
614 615 616
  ```


Z
zengyawen 已提交
617
## fileio.openSync
Z
zengyawen 已提交
618

Z
zhuhongtao666 已提交
619
openSync(path: string, flags?: number, mode?: number): number
Z
zengyawen 已提交
620

Z
zengyawen 已提交
621
以同步方法打开文件。
Z
zengyawen 已提交
622

623 624
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
625
**参数:**
H
haonan_7 已提交
626

Z
zengyawen 已提交
627 628 629
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待打开文件的应用沙箱路径。                                   |
R
raoxian 已提交
630
| flags  | number | 否   | 打开文件的选项,必须指定如下选项中的一个,默认以只读方式打开:<br/>-&nbsp;0o0:只读打开。<br/>-&nbsp;0o1:只写打开。<br/>-&nbsp;0o2:读写打开。<br/>同时,也可给定如下选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;0o100:若文件不存在,则创建文件。使用该选项时必须指定第三个参数&nbsp;mode。<br/>-&nbsp;0o200:如果追加了0o100选项,且文件已经存在,则出错。<br/>-&nbsp;0o1000:如果文件存在且以只写或读写的方式打开文件,则将其长度裁剪为零。<br/>-&nbsp;0o2000:以追加方式打开,后续写将追加到文件末尾。<br/>-&nbsp;0o4000:如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;0o200000:如果path不指向目录,则出错。<br/>-&nbsp;0o400000:如果path指向符号链接,则出错。<br/>-&nbsp;0o4010000:以同步IO的方式打开文件。 |
W
wangbo 已提交
631
| mode   | number | 否   | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o666。<br/>-&nbsp;0o666:所有者具有读、写权限,所有用户组具有读、写权限,其余用户具有读、写权限。<br/>-&nbsp;0o640:所有者具有读、写权限,所有用户组具有读权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。<br/>创建出的文件权限受umask影响,umask随进程启动确定,其修改当前不开放。 |
Z
zengyawen 已提交
632

Z
zhangxingxia 已提交
633
**返回值:**
H
haonan_7 已提交
634 635 636 637

  | 类型     | 说明          |
  | ------ | ----------- |
  | number | 打开文件的文件描述符。 |
Z
zengyawen 已提交
638

Z
zhangxingxia 已提交
639
**示例:**
H
haonan_7 已提交
640

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


Z
zengyawen 已提交
656
## fileio.read
Z
zengyawen 已提交
657

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

H
haonan_7 已提交
664
从文件读取数据,使用Promise异步回调。
Z
zengyawen 已提交
665

666 667
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
668
**参数:**
H
haonan_7 已提交
669

Z
zhangxingxia 已提交
670 671 672 673
| 参数名  | 类型        | 必填 | 说明                                                         |
| ------- | ----------- | ---- | ------------------------------------------------------------ |
| fd      | number      | 是   | 待读取文件的文件描述符。                                     |
| buffer  | ArrayBuffer | 是   | 用于保存读取到的文件数据的缓冲区。                           |
Z
zhangxingxia 已提交
674
| options | Object      | 否   | 支持如下选项:<br/>-&nbsp;offset,number类型,表示将数据读取到缓冲区的位置,即相对于缓冲区首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>约束:offset+length<=buffer.size。 |
Z
zengyawen 已提交
675

Z
zhangxingxia 已提交
676
**返回值:**
Z
zhangxingxia 已提交
677

H
haonan_7 已提交
678 679 680
  | 类型                                 | 说明     |
  | ---------------------------------- | ------ |
  | Promise&lt;[ReadOut](#readout)&gt; | Promise对象。返回读取的结果。 |
Z
zengyawen 已提交
681

Z
zhangxingxia 已提交
682
**示例:**
H
haonan_7 已提交
683

Z
zhangxingxia 已提交
684
  ```js
Z
zhuhongtao666 已提交
685 686
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
Z
zengyawen 已提交
687
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
688
  fileio.read(fd, buf).then(function(readOut){
H
haonan_7 已提交
689
      console.info("read file data succeed");
Z
zhangxingxia 已提交
690
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
691 692
  }).catch(function(err){
      console.info("read file data failed with error:"+ err);
Z
zhangxingxia 已提交
693
  });
Z
zengyawen 已提交
694 695 696
  ```


Z
zengyawen 已提交
697
## fileio.read
Z
zengyawen 已提交
698

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

H
haonan_7 已提交
705
从文件读取数据,使用callback异步回调。
Z
zengyawen 已提交
706

707 708
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
709
**参数:**
H
haonan_7 已提交
710 711 712 713 714 715 716

  | 参数名      | 类型                                       | 必填   | 说明                                       |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                                   | 是    | 待读取文件的文件描述符。                             |
  | buffer   | ArrayBuffer                              | 是    | 用于保存读取到的文件数据的缓冲区。                        |
  | options  | Object                                   | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示将数据读取到缓冲区的位置,即相对于缓冲区首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>约束:offset+length<=buffer.size。  |
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | 是    | 异步读取数据之后的回调。                             |
Z
zengyawen 已提交
717

Z
zhangxingxia 已提交
718
**示例:**
H
haonan_7 已提交
719

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


Z
zengyawen 已提交
733
## fileio.readSync
Z
zengyawen 已提交
734

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

以同步方法从文件读取数据。

743 744
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
745
**参数:**
H
haonan_7 已提交
746 747 748 749 750 751

  | 参数名     | 类型          | 必填   | 说明                                       |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | fd      | number      | 是    | 待读取文件的文件描述符。                             |
  | buffer  | ArrayBuffer | 是    | 用于保存读取到的文件数据的缓冲区。                        |
  | options | Object      | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示将数据读取到缓冲区的位置,即相对于缓冲区首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>约束:offset+length<=buffer.size。  |
Z
zengyawen 已提交
752

Z
zhangxingxia 已提交
753
**返回值:**
H
haonan_7 已提交
754 755 756 757

  | 类型     | 说明       |
  | ------ | -------- |
  | number | 实际读取的长度。 |
Z
zengyawen 已提交
758

Z
zhangxingxia 已提交
759
**示例:**
H
haonan_7 已提交
760

Z
zhangxingxia 已提交
761
  ```js
Z
zhuhongtao666 已提交
762 763
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
Z
zengyawen 已提交
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;

H
haonan_7 已提交
773
删除目录,使用Promise异步回调。
Z
zengyawen 已提交
774

775 776
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
777
**参数:**
H
haonan_7 已提交
778

Z
zengyawen 已提交
779 780 781
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除目录的应用沙箱路径。 |
Z
zengyawen 已提交
782

Z
zhangxingxia 已提交
783
**返回值:**
H
haonan_7 已提交
784 785 786 787

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
788

Z
zhangxingxia 已提交
789
**示例:**
H
haonan_7 已提交
790

Z
zhangxingxia 已提交
791
  ```js
Z
zhuhongtao666 已提交
792 793
  let dirPath = pathDir + '/testDir';
  fileio.rmdir(dirPath).then(function() {
H
haonan_7 已提交
794
      console.info("rmdir succeed");
Z
zhangxingxia 已提交
795 796
  }).catch(function(err){
      console.info("rmdir failed with error:"+ err);
Z
zengyawen 已提交
797 798 799 800 801 802
  });
  ```


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

Z
zhuhongtao666 已提交
803
rmdir(path: string, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
804

H
haonan_7 已提交
805
删除目录,使用callback异步回调。
Z
zengyawen 已提交
806

807 808
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
809
**参数:**
H
haonan_7 已提交
810

Z
zengyawen 已提交
811 812 813 814
| 参数名   | 类型                      | 必填 | 说明                       |
| -------- | ------------------------- | ---- | -------------------------- |
| path     | string                    | 是   | 待删除目录的应用沙箱路径。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步删除目录之后的回调。   |
Z
zengyawen 已提交
815

Z
zhangxingxia 已提交
816
**示例:**
H
haonan_7 已提交
817

Z
zhangxingxia 已提交
818
  ```js
Z
zhuhongtao666 已提交
819 820
  let dirPath = pathDir + '/testDir';
  fileio.rmdir(dirPath, function(err){
Z
zhangxingxia 已提交
821
      // do something
H
haonan_7 已提交
822
      console.info("rmdir succeed");
Z
zengyawen 已提交
823 824 825 826
  });
  ```


827
## fileio.rmdirSync<sup>7+</sup>
Z
zengyawen 已提交
828

829
rmdirSync(path: string): void
Z
zengyawen 已提交
830 831 832

以同步方法删除目录。

833 834
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
835
**参数:**
H
haonan_7 已提交
836

Z
zengyawen 已提交
837 838 839
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除目录的应用沙箱路径。 |
Z
zengyawen 已提交
840

Z
zhangxingxia 已提交
841
**示例:**
H
haonan_7 已提交
842

Z
zhangxingxia 已提交
843
  ```js
Z
zhuhongtao666 已提交
844 845
  let dirPath = pathDir + '/testDir';
  fileio.rmdirSync(dirPath);
Z
zengyawen 已提交
846 847 848 849 850
  ```


## fileio.unlink

Z
zhuhongtao666 已提交
851
unlink(path: string): Promise&lt;void&gt;
Z
zengyawen 已提交
852

H
haonan_7 已提交
853
删除文件,使用Promise异步回调。
Z
zengyawen 已提交
854

855 856
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
857
**参数:**
H
haonan_7 已提交
858

Z
zengyawen 已提交
859 860 861
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除文件的应用沙箱路径。 |
Z
zengyawen 已提交
862

Z
zhangxingxia 已提交
863
**返回值:**
H
haonan_7 已提交
864 865 866 867

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
868

Z
zhangxingxia 已提交
869
**示例:**
H
haonan_7 已提交
870

Z
zhangxingxia 已提交
871
  ```js
Z
zhuhongtao666 已提交
872 873
  let filePath = pathDir + "/test.txt";
  fileio.unlink(filePath).then(function(){
H
haonan_7 已提交
874
      console.info("remove file succeed");
Z
zhangxingxia 已提交
875 876 877
  }).catch(function(error){
      console.info("remove file failed with error:"+ error);
  });
Z
zengyawen 已提交
878 879 880 881 882
  ```


## fileio.unlink

Z
zhuhongtao666 已提交
883
unlink(path: string, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
884

H
haonan_7 已提交
885
删除文件,使用callback异步回调。
Z
zengyawen 已提交
886

887 888
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
889
**参数:**
H
haonan_7 已提交
890

Z
zengyawen 已提交
891 892 893 894
| 参数名   | 类型                      | 必填 | 说明                       |
| -------- | ------------------------- | ---- | -------------------------- |
| path     | string                    | 是   | 待删除文件的应用沙箱路径。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步删除文件之后的回调。   |
Z
zengyawen 已提交
895

Z
zhangxingxia 已提交
896
**示例:**
H
haonan_7 已提交
897

Z
zhangxingxia 已提交
898
  ```js
Z
zhuhongtao666 已提交
899 900
  let filePath = pathDir + "/test.txt";
  fileio.unlink(filePath, function(err) {
H
haonan_7 已提交
901
      console.info("remove file succeed");
Z
zengyawen 已提交
902 903 904 905 906 907 908 909 910 911
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void

以同步方法删除文件。

912 913
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
914
**参数:**
H
haonan_7 已提交
915

Z
zengyawen 已提交
916 917 918
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除文件的应用沙箱路径。 |
Z
zengyawen 已提交
919

Z
zhangxingxia 已提交
920
**示例:**
H
haonan_7 已提交
921

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


## fileio.write

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

H
haonan_7 已提交
937
将数据写入文件,使用Promise异步回调。
Z
zengyawen 已提交
938

939 940
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
941
**参数:**
H
haonan_7 已提交
942 943 944 945 946 947

  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | 是    | 待写入文件的文件描述符。                             |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
  | options | Object                          | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望写入数据的位置相对于数据首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。<br/>约束:offset+length<=buffer.size。 |
Z
zengyawen 已提交
948

Z
zhangxingxia 已提交
949
**返回值:**
H
haonan_7 已提交
950 951 952 953

  | 类型                    | 说明       |
  | --------------------- | -------- |
  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
Z
zengyawen 已提交
954

Z
zhangxingxia 已提交
955
**示例:**
H
haonan_7 已提交
956

Z
zhangxingxia 已提交
957
  ```js
Z
zhuhongtao666 已提交
958 959
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
Z
zhangxingxia 已提交
960
  fileio.write(fd, "hello, world").then(function(number){
H
haonan_7 已提交
961
       console.info("write data to file succeed and size is:"+ number);
Z
zhangxingxia 已提交
962 963 964
  }).catch(function(err){
      console.info("write data to file failed with error:"+ err);
  });
Z
zengyawen 已提交
965 966 967 968 969
  ```


## fileio.write

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

H
haonan_7 已提交
977
将数据写入文件,使用callback异步回调。
Z
zengyawen 已提交
978

979 980
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
981
**参数:**
H
haonan_7 已提交
982 983 984 985 986 987 988

  | 参数名      | 类型                              | 必填   | 说明                                       |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | 是    | 待写入文件的文件描述符。                             |
  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
  | options  | Object                          | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望写入数据的位置相对于数据首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。<br/>约束:offset+length<=buffer.size。 |
  | callback | AsyncCallback&lt;number&gt;     | 是    | 异步将数据写入完成后执行的回调函数。                       |
Z
zengyawen 已提交
989

Z
zhangxingxia 已提交
990
**示例:**
H
haonan_7 已提交
991

Z
zhangxingxia 已提交
992
  ```js
Z
zhuhongtao666 已提交
993 994
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
Z
zengyawen 已提交
995
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
Z
zhangxingxia 已提交
996
      if (bytesWritten) {
H
haonan_7 已提交
997
         console.info("write data to file succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
998 999 1000 1001 1002 1003 1004
      }
  });
  ```


## fileio.writeSync

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 1012 1013

以同步方法将数据写入文件。

1014 1015
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1016
**参数:**
H
haonan_7 已提交
1017 1018 1019 1020 1021 1022

  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | 是    | 待写入文件的文件描述符。                             |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
  | options | Object                          | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望写入数据的位置相对于数据首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。<br/>约束:offset+length<=buffer.size。 |
Z
zengyawen 已提交
1023

Z
zhangxingxia 已提交
1024
**返回值:**
H
haonan_7 已提交
1025 1026 1027 1028

  | 类型     | 说明       |
  | ------ | -------- |
  | number | 实际写入的长度。 |
Z
zengyawen 已提交
1029

Z
zhangxingxia 已提交
1030
**示例:**
H
haonan_7 已提交
1031

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


## fileio.hash

hash(path: string, algorithm: string): Promise&lt;string&gt;

H
haonan_7 已提交
1043
计算文件的哈希值,使用Promise异步回调。
Z
zengyawen 已提交
1044

1045 1046
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1047
**参数:**
H
haonan_7 已提交
1048

Z
zengyawen 已提交
1049 1050 1051 1052
| 参数名    | 类型   | 必填 | 说明                                                         |
| --------- | ------ | ---- | ------------------------------------------------------------ |
| path      | string | 是   | 待计算哈希值文件的应用沙箱路径。                             |
| algorithm | string | 是   | 哈希计算采用的算法。可选&nbsp;"md5"、"sha1"&nbsp;&nbsp;"sha256"。建议采用安全强度更高的&nbsp;"sha256"。 |
Z
zengyawen 已提交
1053

Z
zhangxingxia 已提交
1054
**返回值:**
H
haonan_7 已提交
1055 1056 1057 1058

  | 类型                    | 说明                         |
  | --------------------- | -------------------------- |
  | Promise&lt;string&gt; | Promise对象。返回文件的哈希值。表示为十六进制数字串,所有字母均大写。 |
Z
zengyawen 已提交
1059

Z
zhangxingxia 已提交
1060
**示例:**
H
haonan_7 已提交
1061

Z
zhangxingxia 已提交
1062
  ```js
Z
zhuhongtao666 已提交
1063 1064
  let filePath = pathDir + "/test.txt";
  fileio.hash(filePath, "sha256").then(function(str){
H
haonan_7 已提交
1065
      console.info("calculate file hash succeed:"+ str);
1066
  }).catch(function(err){
W
wangbo 已提交
1067
      console.info("calculate file hash failed with error:"+ err);
Z
zhangxingxia 已提交
1068
  });
Z
zengyawen 已提交
1069 1070 1071 1072 1073
  ```


## fileio.hash

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

H
haonan_7 已提交
1076
计算文件的哈希值,使用callback异步回调。
Z
zengyawen 已提交
1077

1078 1079
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1080
**参数:**
H
haonan_7 已提交
1081

Z
zengyawen 已提交
1082 1083 1084 1085
| 参数名    | 类型                        | 必填 | 说明                                                         |
| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
| path      | string                      | 是   | 待计算哈希值文件的应用沙箱路径。                             |
| algorithm | string                      | 是   | 哈希计算采用的算法。可选&nbsp;"md5"、"sha1"&nbsp;&nbsp;"sha256"。建议采用安全强度更高的&nbsp;"sha256"。 |
H
haonan_7 已提交
1086
| callback  | AsyncCallback&lt;string&gt; | 是   | 异步计算文件哈希操作之后的回调函数(其中给定文件哈希值表示为十六进制数字串,所有字母均大写)。 |
Z
zengyawen 已提交
1087

Z
zhangxingxia 已提交
1088
**示例:**
H
haonan_7 已提交
1089

Z
zhangxingxia 已提交
1090
  ```js
Z
zhuhongtao666 已提交
1091 1092
  let filePath = pathDir + "/test.txt";
  fileio.hash(filePath, "sha256", function(err, hashStr) {
Z
zhangxingxia 已提交
1093
      if (hashStr) {
H
haonan_7 已提交
1094
          console.info("calculate file hash succeed:"+ hashStr);
Z
zengyawen 已提交
1095 1096 1097 1098 1099 1100 1101
      }
  });
  ```


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

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

H
haonan_7 已提交
1104
改变文件权限,使用Promise异步回调。
Z
zengyawen 已提交
1105

1106 1107
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1108
**参数:**
H
haonan_7 已提交
1109

Z
zengyawen 已提交
1110 1111
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1112
| path   | string | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1113
| mode   | number | 是   | 改变文件权限,可给定如下权限,以按位或的方式追加权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
Z
zengyawen 已提交
1114

Z
zhangxingxia 已提交
1115
**返回值:**
H
haonan_7 已提交
1116 1117 1118 1119

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1120

Z
zhangxingxia 已提交
1121
**示例:**
H
haonan_7 已提交
1122

Z
zhangxingxia 已提交
1123
  ```js
Z
zhuhongtao666 已提交
1124 1125
  let filePath = pathDir + "/test.txt";
  fileio.chmod(filePath, 0o700).then(function() {
H
haonan_7 已提交
1126
      console.info("chmod succeed");
Z
zhangxingxia 已提交
1127 1128
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
1129 1130 1131 1132 1133 1134 1135 1136
  });
  ```


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

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

H
haonan_7 已提交
1137
改变文件权限,使用callback异步回调。
Z
zengyawen 已提交
1138

1139 1140
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1141
**参数:**
H
haonan_7 已提交
1142

Z
zengyawen 已提交
1143 1144
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1145
| path     | string                    | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1146 1147
| mode     | number                    | 是   | 改变文件权限,可给定如下权限,以按位或的方式追加权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件权限之后的回调。                                 |
Z
zengyawen 已提交
1148

Z
zhangxingxia 已提交
1149
**示例:**
H
haonan_7 已提交
1150

Z
zhangxingxia 已提交
1151
  ```js
Z
zhuhongtao666 已提交
1152 1153
  let filePath = pathDir + "/test.txt";
  fileio.chmod(filePath, 0o700, function (err) {
Z
zhangxingxia 已提交
1154
      // do something
Z
zengyawen 已提交
1155 1156 1157 1158 1159 1160 1161 1162
  });
  ```


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

chmodSync(path: string, mode: number): void

H
haonan_7 已提交
1163
以同步方法改变文件权限。
Z
zengyawen 已提交
1164

1165 1166
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1167
**参数:**
H
haonan_7 已提交
1168

Z
zengyawen 已提交
1169 1170
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1171
| path   | string | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1172
| mode   | number | 是   | 改变文件权限,可给定如下权限,以按位或的方式追加权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
Z
zengyawen 已提交
1173

Z
zhangxingxia 已提交
1174
**示例:**
H
haonan_7 已提交
1175

Z
zhangxingxia 已提交
1176
  ```js
Z
zhuhongtao666 已提交
1177 1178
  let filePath = pathDir + "/test.txt";
  fileio.chmodSync(filePath, 0o700);
Z
zengyawen 已提交
1179 1180 1181 1182 1183 1184 1185
  ```


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

fstat(fd: number): Promise&lt;Stat&gt;

H
haonan_7 已提交
1186
基于文件描述符获取文件状态信息,使用Promise异步回调。
Z
zengyawen 已提交
1187

1188 1189
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1190
**参数:**
H
haonan_7 已提交
1191 1192 1193 1194

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待获取文件状态的文件描述符。 |
Z
zengyawen 已提交
1195

Z
zhangxingxia 已提交
1196
**返回值:**
H
haonan_7 已提交
1197 1198 1199 1200

  | 类型                           | 说明         |
  | ---------------------------- | ---------- |
  | Promise&lt;[Stat](#stat)&gt; | Promise对象。返回表示文件状态的具体信息。 |
Z
zengyawen 已提交
1201

Z
zhangxingxia 已提交
1202
**示例:**
H
haonan_7 已提交
1203

Z
zhangxingxia 已提交
1204
  ```js
Z
zhuhongtao666 已提交
1205 1206
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1207
  fileio.fstat(fd).then(function(stat){
H
haonan_7 已提交
1208
      console.info("fstat succeed:"+ JSON.stringify(stat));
Z
zhangxingxia 已提交
1209 1210 1211
  }).catch(function(err){
      console.info("fstat failed with error:"+ err);
  });
Z
zengyawen 已提交
1212 1213 1214 1215 1216 1217 1218
  ```


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

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

H
haonan_7 已提交
1219
基于文件描述符获取文件状态信息,使用callback异步回调。
Z
zengyawen 已提交
1220

1221 1222
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1223
**参数:**
H
haonan_7 已提交
1224 1225 1226 1227 1228

  | 参数名      | 类型                                 | 必填   | 说明               |
  | -------- | ---------------------------------- | ---- | ---------------- |
  | fd       | number                             | 是    | 待获取文件状态的文件描述符。     |
  | callback | AsyncCallback&lt;[Stat](#stat)&gt; | 是    | 异步获取文件状态信息之后的回调。 |
Z
zengyawen 已提交
1229

Z
zhangxingxia 已提交
1230
**示例:**
H
haonan_7 已提交
1231

Z
zhangxingxia 已提交
1232
  ```js
Z
zhuhongtao666 已提交
1233 1234
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1235 1236
  fileio.fstat(fd, function (err) {
      // do something
Z
zengyawen 已提交
1237 1238 1239 1240 1241 1242 1243 1244
  });
  ```


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

fstatSync(fd: number): Stat

H
haonan_7 已提交
1245
以同步方法基于文件描述符获取文件状态信息。
Z
zengyawen 已提交
1246

1247 1248
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1249
**参数:**
H
haonan_7 已提交
1250 1251 1252 1253

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待获取文件状态的文件描述符。 |
Z
zengyawen 已提交
1254

Z
zhangxingxia 已提交
1255
**返回值:**
H
haonan_7 已提交
1256 1257 1258 1259

  | 类型            | 说明         |
  | ------------- | ---------- |
  | [Stat](#stat) | 表示文件状态的具体信息。 |
Z
zengyawen 已提交
1260

Z
zhangxingxia 已提交
1261
**示例:**
H
haonan_7 已提交
1262

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


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

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

H
haonan_7 已提交
1274
基于文件描述符截断文件,使用Promise异步回调。
Z
zengyawen 已提交
1275

1276 1277
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1278
**参数:**
H
haonan_7 已提交
1279 1280 1281 1282 1283

  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
  | len  | number | 否    | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1284

Z
zhangxingxia 已提交
1285
**返回值:**
H
haonan_7 已提交
1286 1287 1288 1289

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。|
Z
zengyawen 已提交
1290

Z
zhangxingxia 已提交
1291
**示例:**
H
haonan_7 已提交
1292

Z
zhangxingxia 已提交
1293
  ```js
Z
zhuhongtao666 已提交
1294 1295
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1296
  fileio.ftruncate(fd, 5).then(function(err) {    
H
haonan_7 已提交
1297
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1298 1299
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1300 1301 1302 1303 1304 1305
  });
  ```


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

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

H
haonan_7 已提交
1308
基于文件描述符截断文件,使用callback异步回调。
Z
zengyawen 已提交
1309

1310 1311
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1312
**参数:**
H
haonan_7 已提交
1313 1314 1315 1316

  | 参数名      | 类型                        | 必填   | 说明               |
  | -------- | ------------------------- | ---- | ---------------- |
  | fd       | number                    | 是    | 待截断文件的文件描述符。     |
Z
zhuhongtao666 已提交
1317
  | len      | number                    | 否    | 文件截断后的长度,以字节为单位。 |
H
haonan_7 已提交
1318
  | callback | AsyncCallback&lt;void&gt; | 是    | 回调函数,本调用无返回值。  |
Z
zengyawen 已提交
1319

Z
zhangxingxia 已提交
1320
**示例:**
H
haonan_7 已提交
1321

Z
zhangxingxia 已提交
1322
  ```js
Z
zhuhongtao666 已提交
1323 1324
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
1325 1326
  let len = 5;
  fileio.ftruncate(fd, 5, function(err){
Z
zhangxingxia 已提交
1327
      // do something
Z
zengyawen 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
  });
  ```


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

ftruncateSync(fd: number, len?: number): void

以同步方法基于文件描述符截断文件。

1338 1339
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1340
**参数:**
H
haonan_7 已提交
1341 1342 1343 1344 1345

  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
  | len  | number | 否    | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1346

Z
zhangxingxia 已提交
1347
**示例:**
H
haonan_7 已提交
1348

Z
zhangxingxia 已提交
1349
  ```js
Z
zhuhongtao666 已提交
1350 1351
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
1352
  let len = 5;
Z
zhangxingxia 已提交
1353
  fileio.ftruncateSync(fd, len);
Z
zengyawen 已提交
1354 1355 1356 1357 1358
  ```


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

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

H
haonan_7 已提交
1361
基于文件路径截断文件,使用Promise异步回调。
Z
zengyawen 已提交
1362

1363 1364
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1365
**参数:**
H
haonan_7 已提交
1366

Z
zengyawen 已提交
1367 1368 1369
| 参数名 | 类型   | 必填 | 说明                             |
| ------ | ------ | ---- | -------------------------------- |
| path   | string | 是   | 待截断文件的应用沙箱路径。       |
H
haonan_7 已提交
1370
| len    | number | 否   | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1371

Z
zhangxingxia 已提交
1372
**返回值:**
H
haonan_7 已提交
1373 1374 1375 1376

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1377

Z
zhangxingxia 已提交
1378
**示例:**
H
haonan_7 已提交
1379

Z
zhangxingxia 已提交
1380
  ```js
Z
zhuhongtao666 已提交
1381
  let filePath = pathDir + "/test.txt";
1382
  let len = 5;
Z
zhuhongtao666 已提交
1383
  fileio.truncate(filePath, len).then(function(){
H
haonan_7 已提交
1384
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1385 1386
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1387 1388 1389 1390 1391 1392
  });
  ```


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

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

H
haonan_7 已提交
1395
基于文件路径截断文件,使用callback异步回调。
Z
zengyawen 已提交
1396

1397 1398
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1399
**参数:**
H
haonan_7 已提交
1400

Z
zengyawen 已提交
1401 1402 1403
| 参数名   | 类型                      | 必填 | 说明                             |
| -------- | ------------------------- | ---- | -------------------------------- |
| path     | string                    | 是   | 待截断文件的应用沙箱路径。       |
Z
zhuhongtao666 已提交
1404
| len      | number                    | 否   | 文件截断后的长度,以字节为单位。 |
W
wangbo 已提交
1405
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数,本调用无返回值。   |
Z
zengyawen 已提交
1406

Z
zhangxingxia 已提交
1407
**示例:**
H
haonan_7 已提交
1408

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


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

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

以同步方法基于文件路径截断文件。

1424 1425
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1426
**参数:**
H
haonan_7 已提交
1427

Z
zengyawen 已提交
1428 1429 1430 1431
| 参数名 | 类型   | 必填 | 说明                             |
| ------ | ------ | ---- | -------------------------------- |
| path   | string | 是   | 待截断文件的应用沙箱路径。       |
| len    | number | 否   | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1432

Z
zhangxingxia 已提交
1433
**示例:**
H
haonan_7 已提交
1434

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


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

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

H
haonan_7 已提交
1450
基于文本方式读取文件(即直接读取文件的文本内容),使用Promise异步回调。
Z
zengyawen 已提交
1451

1452 1453
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1454
**参数:**
H
haonan_7 已提交
1455

Z
zengyawen 已提交
1456 1457 1458 1459
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filePath | string | 是   | 待读取文件的应用沙箱路径。                                   |
| options  | Object | 否   | 支持如下选项:<br/>-&nbsp;position,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读取。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;encoding,string类型,当数据是&nbsp;string&nbsp;类型时有效,表示数据的编码方式,默认&nbsp;'utf-8',仅支持&nbsp;'utf-8'。 |
Z
zengyawen 已提交
1460

Z
zhangxingxia 已提交
1461
**返回值:**
H
haonan_7 已提交
1462 1463 1464 1465

  | 类型                    | 说明         |
  | --------------------- | ---------- |
  | Promise&lt;string&gt; | Promise对象。返回读取文件的内容。 |
Z
zengyawen 已提交
1466

Z
zhangxingxia 已提交
1467
**示例:**
H
haonan_7 已提交
1468

Z
zhangxingxia 已提交
1469
  ```js
Z
zhuhongtao666 已提交
1470 1471
  let filePath = pathDir + "/test.txt";
  fileio.readText(filePath).then(function(str) {
H
haonan_7 已提交
1472
      console.info("readText succeed:"+ str);
Z
zhangxingxia 已提交
1473 1474
  }).catch(function(err){
      console.info("readText failed with error:"+ err);
Z
zengyawen 已提交
1475 1476 1477 1478 1479 1480
  });
  ```


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

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

H
haonan_7 已提交
1487
基于文本方式读取文件(即直接读取文件的文本内容),使用callback异步回调。
Z
zengyawen 已提交
1488

1489 1490
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1491
**参数:**
H
haonan_7 已提交
1492

Z
zengyawen 已提交
1493 1494 1495
| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| filePath | string                      | 是   | 待读取文件的应用沙箱路径。                                   |
Z
zhuhongtao666 已提交
1496
| options  | Object                      | 否   | 支持如下选项:<br/>-&nbsp;position,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读取。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;encoding,string类型,表示数据的编码方式,默认&nbsp;'utf-8',仅支持&nbsp;'utf-8'。 |
W
wangbo 已提交
1497
| callback | AsyncCallback&lt;string&gt; | 是   | 回调函数,返回读取文件的内容。                         |
Z
zengyawen 已提交
1498

Z
zhangxingxia 已提交
1499
**示例:**
H
haonan_7 已提交
1500

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


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

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

以同步方法基于文本方式读取文件(即直接读取文件的文本内容)。

1519 1520
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1521
**参数:**
H
haonan_7 已提交
1522

Z
zengyawen 已提交
1523 1524 1525 1526
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filePath | string | 是   | 待读取文件的应用沙箱路径。                                   |
| options  | Object | 否   | 支持如下选项:<br/>-&nbsp;position,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读取。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;encoding,string类型,当数据是&nbsp;string&nbsp;类型时有效,表示数据的编码方式,默认&nbsp;'utf-8',仅支持&nbsp;'utf-8'。 |
Z
zengyawen 已提交
1527

Z
zhangxingxia 已提交
1528
**返回值:**
H
haonan_7 已提交
1529 1530 1531 1532

  | 类型   | 说明                 |
  | ------ | -------------------- |
  | string | 返回读取文件的内容。 |
Z
zengyawen 已提交
1533

Z
zhangxingxia 已提交
1534
**示例:**
H
haonan_7 已提交
1535

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


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

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

H
haonan_7 已提交
1546
获取链接信息,使用Promise异步回调。
Z
zengyawen 已提交
1547

1548 1549
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1550
**参数:**
H
haonan_7 已提交
1551

Z
zengyawen 已提交
1552 1553
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
W
wangbo 已提交
1554
| path   | string | 是   | 目标文件的应用沙箱路径。 |
Z
zengyawen 已提交
1555

Z
zhangxingxia 已提交
1556
**返回值:**
H
haonan_7 已提交
1557 1558 1559 1560

  | 类型                           | 说明         |
  | ---------------------------- | ---------- |
  | Promise&lt;[Stat](#stat)&gt; | promise对象,返回文件对象,表示文件的具体信息,详情见stat。 |
Z
zengyawen 已提交
1561

Z
zhangxingxia 已提交
1562
**示例:**
H
haonan_7 已提交
1563

Z
zhangxingxia 已提交
1564
  ```js
Z
zhuhongtao666 已提交
1565 1566
  let filePath = pathDir + "/test.txt";
  fileio.lstat(filePath).then(function(stat){
Z
zhangxingxia 已提交
1567
      console.info("get link status succeed:"+ JSON.stringify(stat));
Z
zhangxingxia 已提交
1568 1569 1570
  }).catch(function(err){
      console.info("get link status failed with error:"+ err);
  });
Z
zengyawen 已提交
1571 1572 1573 1574 1575
  ```


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

Z
zhuhongtao666 已提交
1576
lstat(path: string, callback: AsyncCallback&lt;Stat&gt;): void
Z
zengyawen 已提交
1577

H
haonan_7 已提交
1578
获取链接信息,使用callback异步回调。
Z
zengyawen 已提交
1579

1580 1581
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1582
**参数:**
H
haonan_7 已提交
1583

Z
zengyawen 已提交
1584 1585
| 参数名   | 类型                               | 必填 | 说明                                   |
| -------- | ---------------------------------- | ---- | -------------------------------------- |
W
wangbo 已提交
1586 1587
| path     | string                             | 是   | 目标文件的应用沙箱路径。 |
| callback | AsyncCallback&lt;[Stat](#stat)&gt; | 是   | 回调函数,返回文件的具体信息。       |
Z
zengyawen 已提交
1588

Z
zhangxingxia 已提交
1589
**示例:**
H
haonan_7 已提交
1590

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


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

Z
zhuhongtao666 已提交
1601
lstatSync(path: string): Stat
Z
zengyawen 已提交
1602

1603
以同步方法获取链接信息。
Z
zengyawen 已提交
1604

1605 1606
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1607
**参数:**
H
haonan_7 已提交
1608

Z
zengyawen 已提交
1609 1610
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
W
wangbo 已提交
1611
| path   | string | 是   | 目标文件的应用沙箱路径。 |
Z
zengyawen 已提交
1612

Z
zhangxingxia 已提交
1613
**返回值:**
H
haonan_7 已提交
1614 1615 1616 1617

  | 类型            | 说明         |
  | ------------- | ---------- |
  | [Stat](#stat) | 表示文件的具体信息。 |
Z
zengyawen 已提交
1618

Z
zhangxingxia 已提交
1619
**示例:**
H
haonan_7 已提交
1620

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


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

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

H
haonan_7 已提交
1631
重命名文件,使用Promise异步回调。
Z
zengyawen 已提交
1632

1633 1634
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1635
**参数:**
H
haonan_7 已提交
1636

Z
zengyawen 已提交
1637 1638 1639 1640
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1641

Z
zhangxingxia 已提交
1642
**返回值:**
H
haonan_7 已提交
1643 1644 1645 1646

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1647

Z
zhangxingxia 已提交
1648
**示例:**
H
haonan_7 已提交
1649

Z
zhangxingxia 已提交
1650
  ```js
Z
zhuhongtao666 已提交
1651 1652 1653
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
  fileio.rename(srcFile, dstFile).then(function() {
H
haonan_7 已提交
1654
      console.info("rename succeed");
Z
zhangxingxia 已提交
1655 1656
  }).catch(function(err){
      console.info("rename failed with error:"+ err);
Z
zengyawen 已提交
1657 1658 1659 1660 1661 1662 1663 1664
  });
  ```


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

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

H
haonan_7 已提交
1665
重命名文件,使用callback异步回调。
Z
zengyawen 已提交
1666

1667 1668
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1669
**参数:**
H
haonan_7 已提交
1670

Z
zengyawen 已提交
1671 1672 1673 1674 1675
| 参数名   | 类型                      | 必填 | 说明                         |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | 是   | 目标文件的当前应用沙箱路径。 |
| newPath  | String                    | 是   | 目标文件的新应用沙箱路径。   |
| Callback | AsyncCallback&lt;void&gt; | 是   | 异步重命名文件之后的回调。   |
Z
zengyawen 已提交
1676

Z
zhangxingxia 已提交
1677
**示例:**
H
haonan_7 已提交
1678

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


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

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

以同步方法重命名文件。

1693 1694
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1695
**参数:**
H
haonan_7 已提交
1696

Z
zengyawen 已提交
1697 1698 1699 1700
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1701

Z
zhangxingxia 已提交
1702
**示例:**
H
haonan_7 已提交
1703

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


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

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

H
haonan_7 已提交
1715
同步文件数据,使用Promise异步回调。
Z
zengyawen 已提交
1716

1717 1718
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1719
**参数:**
H
haonan_7 已提交
1720 1721 1722 1723

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1724

Z
zhangxingxia 已提交
1725
**返回值:**
H
haonan_7 已提交
1726 1727 1728 1729

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1730

Z
zhangxingxia 已提交
1731
**示例:**
H
haonan_7 已提交
1732

Z
zhangxingxia 已提交
1733
  ```js
Z
zhuhongtao666 已提交
1734 1735
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1736
  fileio.fsync(fd).then(function(){
H
haonan_7 已提交
1737
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1738 1739 1740
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
  });
Z
zengyawen 已提交
1741 1742 1743 1744 1745 1746 1747
  ```


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

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

H
haonan_7 已提交
1748
同步文件数据,使用callback异步回调。
Z
zengyawen 已提交
1749

1750 1751
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1752
**参数:**
H
haonan_7 已提交
1753 1754 1755 1756 1757

  | 参数名      | 类型                        | 必填   | 说明              |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | 是    | 待同步文件的文件描述符。    |
  | Callback | AsyncCallback&lt;void&gt; | 是    | 异步将文件数据同步之后的回调。 |
Z
zengyawen 已提交
1758

Z
zhangxingxia 已提交
1759
**示例:**
H
haonan_7 已提交
1760

W
wangbo 已提交
1761
  ```js
Z
zhuhongtao666 已提交
1762 1763
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
1764
  fileio.fsync(fd, function(err){
Z
zhangxingxia 已提交
1765
      // do something
Z
zengyawen 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
  });
  ```


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

fsyncSync(fd: number): void

以同步方法同步文件数据。

1776 1777
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1778
**参数:**
H
haonan_7 已提交
1779 1780 1781 1782

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1783

Z
zhangxingxia 已提交
1784
**示例:**
H
haonan_7 已提交
1785

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


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

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

H
haonan_7 已提交
1797
实现文件内容数据同步,使用Promise异步回调。
Z
zengyawen 已提交
1798

1799 1800
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1801
**参数:**
H
haonan_7 已提交
1802 1803 1804 1805

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1806

Z
zhangxingxia 已提交
1807
**返回值:**
H
haonan_7 已提交
1808 1809 1810 1811

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1812

Z
zhangxingxia 已提交
1813
**示例:**
H
haonan_7 已提交
1814

W
wangbo 已提交
1815
  ```js
Z
zhuhongtao666 已提交
1816 1817
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1818
  fileio.fdatasync(fd).then(function(err) {
H
haonan_7 已提交
1819
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1820 1821
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
Z
zengyawen 已提交
1822 1823 1824 1825 1826 1827
  });
  ```


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

Z
zhuhongtao666 已提交
1828
fdatasync(fd: number, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
1829

H
haonan_7 已提交
1830
实现文件内容数据同步,使用callback异步回调。
Z
zengyawen 已提交
1831

1832 1833
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1834
**参数:**
H
haonan_7 已提交
1835 1836 1837 1838 1839

  | 参数名      | 类型                              | 必填   | 说明                |
  | -------- | ------------------------------- | ---- | ----------------- |
  | fd       | number                          | 是    | 待同步文件的文件描述符。      |
  | callback | AsyncCallback&nbsp;&lt;void&gt; | 是    | 异步将文件内容数据同步之后的回调。 |
Z
zengyawen 已提交
1840

Z
zhangxingxia 已提交
1841
**示例:**
H
haonan_7 已提交
1842

Z
zhangxingxia 已提交
1843
  ```js
Z
zhuhongtao666 已提交
1844 1845
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
1846
  fileio.fdatasync (fd, function (err) {
Z
zhangxingxia 已提交
1847
      // do something
Z
zengyawen 已提交
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
  });
  ```


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

fdatasyncSync(fd: number): void

以同步方法实现文件内容数据同步。

1858 1859
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1860
**参数:**
H
haonan_7 已提交
1861 1862 1863 1864

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1865

Z
zhangxingxia 已提交
1866
**示例:**
H
haonan_7 已提交
1867

Z
zhangxingxia 已提交
1868
  ```js
Z
zhuhongtao666 已提交
1869 1870
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
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;

H
haonan_7 已提交
1879
基于文件路径创建符号链接,使用Promise异步回调。
Z
zengyawen 已提交
1880

1881 1882
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1883
**参数:**
H
haonan_7 已提交
1884

Z
zengyawen 已提交
1885 1886 1887 1888
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1889

Z
zhangxingxia 已提交
1890
**返回值:**
H
haonan_7 已提交
1891 1892 1893 1894

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1895

Z
zhangxingxia 已提交
1896
**示例:**
H
haonan_7 已提交
1897

Z
zhangxingxia 已提交
1898
  ```js
Z
zhuhongtao666 已提交
1899 1900 1901
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlink(srcFile, dstFile).then(function() {
H
haonan_7 已提交
1902
      console.info("symlink succeed");
Z
zhangxingxia 已提交
1903 1904
  }).catch(function(err){
      console.info("symlink failed with error:"+ err);
Z
zengyawen 已提交
1905 1906 1907 1908 1909 1910 1911 1912
  });
  ```


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

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

H
haonan_7 已提交
1913
基于文件路径创建符号链接,使用callback异步回调。
Z
zengyawen 已提交
1914

1915 1916
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1917
**参数:**
H
haonan_7 已提交
1918

Z
zengyawen 已提交
1919 1920 1921 1922 1923
| 参数名   | 类型                      | 必填 | 说明                             |
| -------- | ------------------------- | ---- | -------------------------------- |
| target   | string                    | 是   | 目标文件的应用沙箱路径。         |
| srcPath  | string                    | 是   | 符号链接文件的应用沙箱路径。     |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步创建符号链接信息之后的回调。 |
Z
zengyawen 已提交
1924

Z
zhangxingxia 已提交
1925
**示例:**
H
haonan_7 已提交
1926

Z
zhangxingxia 已提交
1927
  ```js
Z
zhuhongtao666 已提交
1928 1929 1930
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlink(srcFile, dstFile, function (err) {
Z
zhangxingxia 已提交
1931
      // do something
Z
zengyawen 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
  });
  ```


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

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

以同步的方法基于文件路径创建符号链接。

1942 1943
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1944
**参数:**
H
haonan_7 已提交
1945

Z
zengyawen 已提交
1946 1947 1948 1949
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1950

Z
zhangxingxia 已提交
1951
**示例:**
H
haonan_7 已提交
1952

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


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

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

H
haonan_7 已提交
1964
基于文件路径改变文件所有者,使用Promise异步回调。
Z
zengyawen 已提交
1965

1966 1967
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1968
**参数:**
H
haonan_7 已提交
1969

Z
zengyawen 已提交
1970 1971 1972 1973 1974
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID(UserID)。        |
| gid    | number | 是   | 新的GID(GroupID)。       |
Z
zengyawen 已提交
1975

Z
zhangxingxia 已提交
1976
**返回值:**
H
haonan_7 已提交
1977 1978 1979 1980

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1981

Z
zhangxingxia 已提交
1982
**示例:**
H
haonan_7 已提交
1983

Z
zhangxingxia 已提交
1984
  ```js
Z
zhuhongtao666 已提交
1985 1986 1987
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.chown(filePath, stat.uid, stat.gid).then(function(){
H
haonan_7 已提交
1988
      console.info("chown succeed");
Z
zhangxingxia 已提交
1989 1990 1991
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
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

H
haonan_7 已提交
1999
基于文件路径改变文件所有者,使用callback异步回调。
Z
zengyawen 已提交
2000

2001 2002
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2003
**参数:**
H
haonan_7 已提交
2004

Z
zengyawen 已提交
2005 2006 2007 2008 2009 2010
| 参数名   | 类型                      | 必填 | 说明                           |
| -------- | ------------------------- | ---- | ------------------------------ |
| path     | string                    | 是   | 待改变文件的应用沙箱路径。     |
| uid      | number                    | 是   | 新的UID。                      |
| gid      | number                    | 是   | 新的GID。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
2011

Z
zhangxingxia 已提交
2012
**示例:**
H
haonan_7 已提交
2013

Z
zhangxingxia 已提交
2014
  ```js
Z
zhuhongtao666 已提交
2015 2016 2017
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath)
  fileio.chown(filePath, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2018
      // do something
Z
zengyawen 已提交
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
  });
  ```


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

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

以同步的方法基于文件路径改变文件所有者。

2029 2030
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2031
**参数:**
H
haonan_7 已提交
2032

Z
zengyawen 已提交
2033 2034 2035 2036 2037
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2038

Z
zhangxingxia 已提交
2039
**示例:**
H
haonan_7 已提交
2040

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


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

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

H
haonan_7 已提交
2052
创建临时目录,使用Promise异步回调。
Z
zengyawen 已提交
2053

2054 2055
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2056
**参数:**
H
haonan_7 已提交
2057 2058 2059 2060

  | 参数名    | 类型     | 必填   | 说明                          |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | 是    | 用随机产生的字符串替换以“XXXXXX”结尾目录路径。 |
Z
zengyawen 已提交
2061

Z
zhangxingxia 已提交
2062
**返回值:**
H
haonan_7 已提交
2063 2064 2065 2066

  | 类型                   | 说明         |
  | --------------------- | ---------- |
  | Promise&lt;string&gt; | Promise对象。返回生成的唯一目录路径。 |
Z
zengyawen 已提交
2067

Z
zhangxingxia 已提交
2068
**示例:**
H
haonan_7 已提交
2069

Z
zhangxingxia 已提交
2070
  ```js
Z
zhuhongtao666 已提交
2071 2072
  fileio.mkdtemp(pathDir + "/XXXXXX").then(function(pathDir){
      console.info("mkdtemp succeed:"+ pathDir);
Z
zhangxingxia 已提交
2073 2074 2075
  }).catch(function(err){
      console.info("mkdtemp failed with error:"+ err);
  });
Z
zengyawen 已提交
2076 2077 2078 2079 2080 2081 2082
  ```


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

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

H
haonan_7 已提交
2083
创建临时目录,使用callback异步回调。
Z
zengyawen 已提交
2084

2085 2086
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2087
**参数:**
H
haonan_7 已提交
2088 2089 2090 2091 2092

  | 参数名      | 类型                          | 必填   | 说明                          |
  | -------- | --------------------------- | ---- | --------------------------- |
  | prefix   | string                      | 是    | 用随机产生的字符串替换以“XXXXXX”结尾目录路径。 |
  | callback | AsyncCallback&lt;string&gt; | 是    | 异步创建临时目录之后的回调。              |
Z
zengyawen 已提交
2093

Z
zhangxingxia 已提交
2094
**示例:**
H
haonan_7 已提交
2095

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


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

mkdtempSync(prefix: string): string

以同步的方法创建临时目录。

2109 2110
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2111
**参数:**
H
haonan_7 已提交
2112 2113 2114 2115

  | 参数名    | 类型     | 必填   | 说明                          |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | 是    | 用随机产生的字符串替换以“XXXXXX”结尾目录路径。 |
Z
zengyawen 已提交
2116

Z
zhangxingxia 已提交
2117
**返回值:**
H
haonan_7 已提交
2118 2119 2120 2121

  | 类型    | 说明         |
  | ------ | ---------- |
  | string | 产生的唯一目录路径。 |
Z
zengyawen 已提交
2122

Z
zhangxingxia 已提交
2123
**示例:**
H
haonan_7 已提交
2124

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


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

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

H
haonan_7 已提交
2134
基于文件描述符改变文件权限,使用Promise异步回调。
Z
zengyawen 已提交
2135

2136 2137
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2138
**参数:**
H
haonan_7 已提交
2139 2140 2141 2142 2143

  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | 是    | 待改变文件的文件描述符。                             |
  | mode | number | 是    | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
Z
zengyawen 已提交
2144

Z
zhangxingxia 已提交
2145
**返回值:**
H
haonan_7 已提交
2146 2147 2148 2149

  | 类型                 | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
2150

Z
zhangxingxia 已提交
2151
**示例:**
H
haonan_7 已提交
2152

Z
zhangxingxia 已提交
2153
  ```js
Z
zhuhongtao666 已提交
2154 2155
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
2156
  let mode = 0o700;
Z
zhangxingxia 已提交
2157
  fileio.fchmod(fd, mode).then(function() {
H
haonan_7 已提交
2158
      console.info("chmod succeed");
Z
zhangxingxia 已提交
2159 2160
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
2161 2162 2163 2164 2165 2166 2167 2168
  });
  ```


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

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

H
haonan_7 已提交
2169
基于文件描述符改变文件权限,使用callback异步回调。
Z
zengyawen 已提交
2170

2171 2172
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2173
**参数:**
H
haonan_7 已提交
2174 2175 2176 2177 2178 2179

  | 参数名      | 类型                              | 必填   | 说明                                       |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | 是    | 待改变文件的文件描述符。                             |
  | mode     | number                          | 是    | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
  | callback | AsyncCallback&nbsp;&lt;void&gt; | 是    | 异步改变文件权限之后的回调。                           |
Z
zengyawen 已提交
2180

Z
zhangxingxia 已提交
2181
**示例:**
H
haonan_7 已提交
2182

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


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

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

以同步方法基于文件描述符改变文件权限。

2199 2200
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2201
**参数:**
H
haonan_7 已提交
2202 2203 2204 2205 2206

  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | 是    | 待改变文件的文件描述符。                             |
  | mode | number | 是    | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。 |
Z
zengyawen 已提交
2207

Z
zhangxingxia 已提交
2208
**示例:**
H
haonan_7 已提交
2209

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


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

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

H
haonan_7 已提交
2222
基于文件路径打开文件流,使用Promise异步回调。
Z
zengyawen 已提交
2223

2224 2225
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2226
**参数:**
H
haonan_7 已提交
2227

Z
zengyawen 已提交
2228 2229 2230 2231
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待打开文件的应用沙箱路径。                                   |
| mode   | string | 是   | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
Z
zengyawen 已提交
2232

Z
zhangxingxia 已提交
2233
**返回值:**
H
haonan_7 已提交
2234 2235 2236

  | 类型                                | 说明        |
  | --------------------------------- | --------- |
2237
  | Promise&lt;[Stream](#stream)&gt; | Promise对象。返回文件流的结果。 |
Z
zengyawen 已提交
2238

Z
zhangxingxia 已提交
2239
**示例:**
H
haonan_7 已提交
2240

Z
zhangxingxia 已提交
2241
  ```js
Z
zhuhongtao666 已提交
2242 2243
  let filePath = pathDir + "/test.txt";
  fileio.createStream(filePath, "r+").then(function(stream){
H
haonan_7 已提交
2244
      console.info("createStream succeed");
Z
zhangxingxia 已提交
2245 2246 2247
  }).catch(function(err){
      console.info("createStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2248 2249 2250 2251 2252 2253 2254
  ```


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

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

H
haonan_7 已提交
2255
基于文件路径打开文件流,使用callback异步回调。
Z
zengyawen 已提交
2256

2257 2258
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2259
**参数:**
H
haonan_7 已提交
2260

Z
zengyawen 已提交
2261 2262 2263 2264
| 参数名   | 类型                                    | 必填 | 说明                                                         |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| path     | string                                  | 是   | 待打开文件的应用沙箱路径。                                   |
| mode     | string                                  | 是   | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
2265
| callback | AsyncCallback&lt;[Stream](#stream)&gt; | 是   | 异步打开文件流之后的回调。                                   |
Z
zengyawen 已提交
2266

Z
zhangxingxia 已提交
2267
**示例:**
H
haonan_7 已提交
2268

Z
zhangxingxia 已提交
2269
  ```js
Z
zhuhongtao666 已提交
2270 2271
  let filePath = pathDir + "/test.txt";
  fileio.createStream(filePath, "r+", function(err, stream){
Z
zhangxingxia 已提交
2272
      // do something
Z
zengyawen 已提交
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
  });
  ```


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

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

以同步方法基于文件路径打开文件流。

2283 2284
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2285
**参数:**
H
haonan_7 已提交
2286

Z
zengyawen 已提交
2287 2288 2289 2290
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待打开文件的应用沙箱路径。                                   |
| mode   | string | 是   | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
Z
zengyawen 已提交
2291

Z
zhangxingxia 已提交
2292
**返回值:**
H
haonan_7 已提交
2293 2294 2295

  | 类型                | 说明        |
  | ------------------ | --------- |
2296
  | [Stream](#stream) | 返回文件流的结果。 |
Z
zengyawen 已提交
2297

Z
zhangxingxia 已提交
2298
**示例:**
H
haonan_7 已提交
2299

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


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

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

H
haonan_7 已提交
2310
基于文件描述符打开文件流,使用Promise异步回调。
Z
zengyawen 已提交
2311

2312 2313
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2314
**参数:**
H
haonan_7 已提交
2315 2316 2317 2318 2319

  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | 是    | 待打开文件的文件描述符。                             |
  | mode | string | 是    | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
Z
zengyawen 已提交
2320

Z
zhangxingxia 已提交
2321
**返回值:**
H
haonan_7 已提交
2322 2323 2324

  | 类型                               | 说明        |
  | --------------------------------- | --------- |
2325
  | Promise&lt;[Stream](#stream)&gt; | Promise对象。返回文件流的结果。 |
Z
zengyawen 已提交
2326

Z
zhangxingxia 已提交
2327
**示例:**
H
haonan_7 已提交
2328

Z
zhangxingxia 已提交
2329
  ```js
Z
zhuhongtao666 已提交
2330 2331
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
2332
  fileio.fdopenStream(fd, "r+").then(function(stream){
H
haonan_7 已提交
2333
      console.info("openStream succeed");
Z
zhangxingxia 已提交
2334 2335 2336
  }).catch(function(err){
      console.info("openStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2337 2338 2339 2340 2341 2342 2343
  ```


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

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

H
haonan_7 已提交
2344
基于文件描述符打开文件流,使用callback异步回调。
Z
zengyawen 已提交
2345

2346 2347
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2348
**参数:**
H
haonan_7 已提交
2349 2350 2351 2352 2353

  | 参数名      | 类型                                       | 必填   | 说明                                       |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                                   | 是    | 待打开文件的文件描述符。                             |
  | mode     | string                                   | 是    | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
2354
  | callback | AsyncCallback&nbsp;&lt;[Stream](#stream)&gt; | 是    | 异步打开文件流之后的回调。                            |
Z
zengyawen 已提交
2355

Z
zhangxingxia 已提交
2356
**示例:**
H
haonan_7 已提交
2357

Z
zhangxingxia 已提交
2358
  ```js
Z
zhuhongtao666 已提交
2359 2360
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
2361
  fileio.fdopenStream(fd, "r+", function (err, stream) {
Z
zhangxingxia 已提交
2362
      // do something
Z
zengyawen 已提交
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
  });
  ```


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

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

以同步方法基于文件描述符打开文件流。

2373 2374
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2375
**参数:**
H
haonan_7 已提交
2376 2377 2378 2379 2380

  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | fd   | number | 是    | 待打开文件的文件描述符。                             |
  | mode | string | 是    | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
Z
zengyawen 已提交
2381

Z
zhangxingxia 已提交
2382
**返回值:**
H
haonan_7 已提交
2383 2384 2385

  | 类型                | 说明        |
  | ------------------ | --------- |
2386
  | [Stream](#stream) | 返回文件流的结果。 |
Z
zengyawen 已提交
2387

Z
zhangxingxia 已提交
2388
**示例:**
H
haonan_7 已提交
2389

Z
zhangxingxia 已提交
2390
  ```js
Z
zhuhongtao666 已提交
2391 2392
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
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;

H
haonan_7 已提交
2401
基于文件描述符改变文件所有者,使用Promise异步回调。
Z
zengyawen 已提交
2402

2403 2404
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2405
**参数:**
H
haonan_7 已提交
2406 2407 2408 2409 2410 2411

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2412

Z
zhangxingxia 已提交
2413
**返回值:**
H
haonan_7 已提交
2414 2415 2416 2417

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
2418

Z
zhangxingxia 已提交
2419
**示例:**
H
haonan_7 已提交
2420

Z
zhangxingxia 已提交
2421
  ```js
Z
zhuhongtao666 已提交
2422 2423 2424
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
Z
zhangxingxia 已提交
2425
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
H
haonan_7 已提交
2426
      console.info("chown succeed");
Z
zhangxingxia 已提交
2427 2428
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
Z
zengyawen 已提交
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

H
haonan_7 已提交
2437
基于文件描述符改变文件所有者,使用callback异步回调。
Z
zengyawen 已提交
2438

2439 2440
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2441
**参数:**
H
haonan_7 已提交
2442 2443 2444 2445 2446 2447 2448

  | 参数名      | 类型                        | 必填   | 说明              |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | 是    | 待改变文件的文件描述符。    |
  | uid      | number                    | 是    | 文件所有者的UID。      |
  | gid      | number                    | 是    | 文件所有组的GID。      |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
2449

Z
zhangxingxia 已提交
2450
**示例:**
H
haonan_7 已提交
2451

Z
zhangxingxia 已提交
2452
  ```js
Z
zhuhongtao666 已提交
2453 2454 2455
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
Z
zengyawen 已提交
2456
  fileio.fchown(fd, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2457
      // do something
Z
zengyawen 已提交
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
  });
  ```


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

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

以同步方法基于文件描述符改变文件所有者。

2468 2469
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2470
**参数:**
H
haonan_7 已提交
2471 2472 2473 2474 2475 2476

  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2477

Z
zhangxingxia 已提交
2478
**示例:**
H
haonan_7 已提交
2479

Z
zhangxingxia 已提交
2480
  ```js
Z
zhuhongtao666 已提交
2481 2482 2483
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
Z
zengyawen 已提交
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;

H
haonan_7 已提交
2492
基于文件路径改变文件所有者,更改符号链接本身的所有者,而不是符号链接所指向的实际文件,使用Promise异步回调。
Z
zengyawen 已提交
2493

2494 2495
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2496
**参数:**
H
haonan_7 已提交
2497

Z
zengyawen 已提交
2498 2499 2500 2501 2502
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2503

Z
zhangxingxia 已提交
2504
**返回值:**
H
haonan_7 已提交
2505 2506 2507 2508

  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
2509

Z
zhangxingxia 已提交
2510
**示例:**
H
haonan_7 已提交
2511

Z
zhangxingxia 已提交
2512
  ```js
Z
zhuhongtao666 已提交
2513 2514 2515
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchown(filePath, stat.uid, stat.gid).then(function() {
H
haonan_7 已提交
2516
      console.info("chown succeed");
Z
zhangxingxia 已提交
2517 2518 2519
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
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

H
haonan_7 已提交
2527
基于文件路径改变文件所有者,更改符号链接本身的所有者,而不是更改符号链接所指向的实际文件,使用callback异步回调。
Z
zengyawen 已提交
2528

2529 2530
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2531
**参数:**
H
haonan_7 已提交
2532

Z
zengyawen 已提交
2533 2534 2535 2536 2537 2538
| 参数名   | 类型                      | 必填 | 说明                           |
| -------- | ------------------------- | ---- | ------------------------------ |
| path     | string                    | 是   | 待打开文件的应用沙箱路径。     |
| uid      | number                    | 是   | 新的UID。                      |
| gid      | number                    | 是   | 新的GID。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
2539

Z
zhangxingxia 已提交
2540
**示例:**
H
haonan_7 已提交
2541

Z
zhangxingxia 已提交
2542
  ```js
Z
zhuhongtao666 已提交
2543 2544 2545
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchown(filePath, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2546
      // do something
Z
zengyawen 已提交
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556
  });
  ```


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

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

以同步方法基于文件路径改变文件所有者,更改符号链接本身的所有者,而不是更改符号链接所指向的实际文件。

2557 2558
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2559
**参数:**
H
haonan_7 已提交
2560

Z
zengyawen 已提交
2561 2562 2563 2564 2565
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2566

Z
zhangxingxia 已提交
2567
**示例:**
H
haonan_7 已提交
2568

Z
zhangxingxia 已提交
2569
  ```js
Z
zhuhongtao666 已提交
2570 2571 2572
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchownSync(filePath, stat.uid, stat.gid);
Z
zengyawen 已提交
2573 2574 2575 2576 2577
  ```


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

Z
zhuhongtao666 已提交
2578
createWatcher(filename: string, events: number, callback: AsyncCallback&lt;number&gt;): Watcher
Z
zengyawen 已提交
2579

H
haonan_7 已提交
2580
监听文件或者目录的变化,使用callback异步回调。
Z
zengyawen 已提交
2581

2582 2583
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2584
**参数:**
H
haonan_7 已提交
2585

Z
zengyawen 已提交
2586 2587
| 参数名   | 类型                              | 必填 | 说明                                                         |
| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
Z
zhuhongtao666 已提交
2588
| filePath | string                            | 是   | 待监视文件的应用沙箱路径。                                   |
Z
zengyawen 已提交
2589 2590
| events   | Number                            | 是   | -&nbsp;1:&nbsp;监听文件或者目录是否发生重命名。<br/>-&nbsp;2:监听文件或者目录内容的是否修改。<br/>-&nbsp;3:两者都有。 |
| callback | AsyncCallback&lt;number&nbsp;&gt; | 是   | 每发生变化一次,调用一次此函数。                             |
Z
zengyawen 已提交
2591

Z
zhangxingxia 已提交
2592
**返回值:**
H
haonan_7 已提交
2593 2594 2595 2596

  | 类型                  | 说明         |
  | -------------------- | ---------- |
  | [Watcher](#watcher7) | Promise对象。返回文件变化监听的实例。 |
Z
zengyawen 已提交
2597

Z
zhangxingxia 已提交
2598
**示例:**
H
haonan_7 已提交
2599

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


## Readout

仅用于read方法,获取文件的读取结果。

2613 2614
**系统能力**:以下各项对应的系统能力均为SystemCapability.FileManagement.File.FileIO。

H
HelloCrease 已提交
2615 2616 2617 2618 2619
| 名称        | 参数类型       | 可读   | 可写   | 说明                |
| --------- | ---------- | ---- | ---- | ----------------- |
| bytesRead | number     | 是    | 是    | 实际读取长度。           |
| offset    | number     | 是    | 是    | 读取数据相对于缓冲区首地址的偏移。 |
| buffer    | ArrayBufer | 是    | 是    | 保存读取数据的缓冲区。       |
Z
zengyawen 已提交
2620 2621 2622 2623 2624


## Stat

文件具体信息,在调用Stat的方法前,需要先通过[stat()](#fileiostat)方法(同步或异步)来构建一个Stat实例。
Z
zengyawen 已提交
2625

2626
**系统能力**:以下各项对应的系统能力均为SystemCapability.FileManagement.File.FileIO。
Z
zengyawen 已提交
2627

Z
zengyawen 已提交
2628
### 属性
Z
zengyawen 已提交
2629

H
HelloCrease 已提交
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643
| 名称     | 参数类型   | 可读   | 可写   | 说明                                       |
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| dev    | number | 是    | 否    | 标识包含该文件的主设备号。                            |
| ino    | number | 是    | 否    | 标识该文件。通常同设备上的不同文件的INO不同。                 |
| mode   | number | 是    | 否    | 表示文件类型及权限,其首&nbsp;4&nbsp;位表示文件类型,后&nbsp;12&nbsp;位表示权限。各特征位的含义如下:<br/>-&nbsp;0o170000:可用于获取文件类型的掩码。<br/>-&nbsp;0o140000:文件是套接字。<br/>-&nbsp;0o120000:文件是符号链接。<br/>-&nbsp;0o100000:文件是一般文件。<br/>-&nbsp;0o060000:文件属于块设备。<br/>-&nbsp;0o040000:文件是目录。<br/>-&nbsp;0o020000:文件是字符设备。<br/>-&nbsp;0o010000:文件是具名管道,即FIFO。<br/>-&nbsp;0o0700:可用于获取用户权限的掩码。<br/>-&nbsp;0o0400:用户读,对于普通文件,所有者可读取文件;对于目录,所有者可读取目录项。<br/>-&nbsp;0o0200:用户写,对于普通文件,所有者可写入文件;对于目录,所有者可创建/删除目录项。<br/>-&nbsp;0o0100:用户执行,对于普通文件,所有者可执行文件;对于目录,所有者可在目录中搜索给定路径名。<br/>-&nbsp;0o0070:可用于获取用户组权限的掩码。<br/>-&nbsp;0o0040:用户组读,对于普通文件,所有用户组可读取文件;对于目录,所有用户组可读取目录项。<br/>-&nbsp;0o0020:用户组写,对于普通文件,所有用户组可写入文件;对于目录,所有用户组可创建/删除目录项。<br/>-&nbsp;0o0010:用户组执行,对于普通文件,所有用户组可执行文件;对于目录,所有用户组是否可在目录中搜索给定路径名。<br/>-&nbsp;0o0007:可用于获取其他用户权限的掩码。<br/>-&nbsp;0o0004:其他读,对于普通文件,其余用户可读取文件;对于目录,其他用户组可读取目录项。<br/>-&nbsp;0o0002:其他写,对于普通文件,其余用户可写入文件;对于目录,其他用户组可创建/删除目录项。<br/>-&nbsp;0o0001:其他执行,对于普通文件,其余用户可执行文件;对于目录,其他用户组可在目录中搜索给定路径名。 |
| nlink  | number | 是    | 否    | 文件的硬链接数。                                 |
| uid    | number | 是    | 否    | 文件所有者的ID。                                |
| gid    | number | 是    | 否    | 文件所有组的ID。                                |
| rdev   | number | 是    | 否    | 标识包含该文件的从设备号。                            |
| size   | number | 是    | 否    | 文件的大小,以字节为单位。仅对普通文件有效。                   |
| blocks | number | 是    | 否    | 文件占用的块数,计算时块大小按512B计算。                   |
| atime  | number | 是    | 否    | 上次访问该文件的时间,表示距1970年1月1日0时0分0秒的秒数。        |
| mtime  | number | 是    | 否    | 上次修改该文件的时间,表示距1970年1月1日0时0分0秒的秒数。        |
| ctime  | number | 是    | 否    | 最近改变文件状态的时间,表示距1970年1月1日0时0分0秒的秒数。       |
Z
zengyawen 已提交
2644 2645


Z
zengyawen 已提交
2646
### isBlockDevice
Z
zengyawen 已提交
2647

Z
zengyawen 已提交
2648
isBlockDevice(): boolean
Z
zengyawen 已提交
2649

Z
zhangxingxia 已提交
2650
用于判断文件是否是块特殊文件。一个块特殊文件只能以块为粒度进行访问,且访问的时候带缓存。
Z
zengyawen 已提交
2651

2652 2653
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2654
**返回值:**
H
haonan_7 已提交
2655 2656 2657 2658

  | 类型      | 说明               |
  | ------- | ---------------- |
  | boolean | 表示文件是否是块特殊设备。 |
Z
zengyawen 已提交
2659

Z
zhangxingxia 已提交
2660
**示例:**
H
haonan_7 已提交
2661

Z
zhangxingxia 已提交
2662
  ```js
Z
zhuhongtao666 已提交
2663 2664
  let filePath = pathDir + "/test.txt";
  let isBLockDevice = fileio.statSync(filePath).isBlockDevice();
Z
zengyawen 已提交
2665
  ```
Z
zengyawen 已提交
2666 2667


Z
zengyawen 已提交
2668
### isCharacterDevice
Z
zengyawen 已提交
2669

Z
zengyawen 已提交
2670
isCharacterDevice(): boolean
Z
zengyawen 已提交
2671

Z
zhangxingxia 已提交
2672
用于判断文件是否是字符特殊文件。一个字符特殊设备可进行随机访问,且访问的时候不带缓存。
Z
zengyawen 已提交
2673

2674 2675
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2676
**返回值:**
H
haonan_7 已提交
2677 2678 2679 2680

  | 类型      | 说明                |
  | ------- | ----------------- |
  | boolean | 表示文件是否是字符特殊设备。 |
Z
zengyawen 已提交
2681

Z
zhangxingxia 已提交
2682
**示例:**
H
haonan_7 已提交
2683

Z
zhangxingxia 已提交
2684
  ```js
Z
zhuhongtao666 已提交
2685 2686
  let filePath = pathDir + "/test.txt";
  let isCharacterDevice = fileio.statSync(filePath).isCharacterDevice();
Z
zengyawen 已提交
2687
  ```
Z
zengyawen 已提交
2688 2689


Z
zengyawen 已提交
2690
### isDirectory
Z
zengyawen 已提交
2691

Z
zengyawen 已提交
2692
isDirectory(): boolean
Z
zengyawen 已提交
2693

Z
zhangxingxia 已提交
2694
用于判断文件是否是目录。
Z
zengyawen 已提交
2695

2696 2697
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2698
**返回值:**
H
haonan_7 已提交
2699 2700 2701 2702

  | 类型      | 说明            |
  | ------- | ------------- |
  | boolean | 表示文件是否是目录。 |
Z
zengyawen 已提交
2703

Z
zhangxingxia 已提交
2704
**示例:**
H
haonan_7 已提交
2705

Z
zhangxingxia 已提交
2706
  ```js
Z
zhuhongtao666 已提交
2707 2708
  let dirPath = pathDir + "/test";
  let isDirectory = fileio.statSync(dirPath).isDirectory(); 
Z
zengyawen 已提交
2709
  ```
Z
zengyawen 已提交
2710 2711


Z
zengyawen 已提交
2712
### isFIFO
Z
zengyawen 已提交
2713

Z
zengyawen 已提交
2714
isFIFO(): boolean
Z
zengyawen 已提交
2715

Z
zhangxingxia 已提交
2716
用于判断文件是否是命名管道(有时也称为FIFO)。命名管道通常用于进程间通信。
Z
zengyawen 已提交
2717

2718 2719
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2720
**返回值:**
H
haonan_7 已提交
2721 2722 2723 2724

  | 类型      | 说明                    |
  | ------- | --------------------- |
  | boolean | 表示文件是否是&nbsp;FIFO。 |
Z
zengyawen 已提交
2725

Z
zhangxingxia 已提交
2726
**示例:**
H
haonan_7 已提交
2727

Z
zhangxingxia 已提交
2728
  ```js
Z
zhuhongtao666 已提交
2729 2730
  let filePath = pathDir + "/test.txt";
  let isFIFO = fileio.statSync(filePath).isFIFO(); 
Z
zengyawen 已提交
2731
  ```
Z
zengyawen 已提交
2732 2733


Z
zengyawen 已提交
2734
### isFile
Z
zengyawen 已提交
2735

Z
zengyawen 已提交
2736
isFile(): boolean
Z
zengyawen 已提交
2737

Z
zhangxingxia 已提交
2738
用于判断文件是否是普通文件。
Z
zengyawen 已提交
2739

2740 2741
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2742
**返回值:**
H
haonan_7 已提交
2743 2744 2745 2746

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示文件是否是普通文件。 |
Z
zengyawen 已提交
2747

Z
zhangxingxia 已提交
2748
**示例:**
H
haonan_7 已提交
2749

Z
zhangxingxia 已提交
2750
  ```js
Z
zhuhongtao666 已提交
2751 2752
  let filePath = pathDir + "/test.txt";
  let isFile = fileio.statSync(filePath).isFile();
Z
zengyawen 已提交
2753
  ```
Z
zengyawen 已提交
2754 2755


Z
zengyawen 已提交
2756
### isSocket
Z
zengyawen 已提交
2757

Z
zengyawen 已提交
2758
isSocket(): boolean
Z
zengyawen 已提交
2759

Z
zhangxingxia 已提交
2760
用于判断文件是否是套接字。
Z
zengyawen 已提交
2761

2762 2763
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2764
**返回值:**
H
haonan_7 已提交
2765 2766 2767 2768

  | 类型      | 说明             |
  | ------- | -------------- |
  | boolean | 表示文件是否是套接字。 |
Z
zengyawen 已提交
2769

Z
zhangxingxia 已提交
2770
**示例:**
H
haonan_7 已提交
2771

Z
zhangxingxia 已提交
2772
  ```js
Z
zhuhongtao666 已提交
2773 2774
  let filePath = pathDir + "/test.txt";
  let isSocket = fileio.statSync(filePath).isSocket(); 
Z
zengyawen 已提交
2775
  ```
Z
zengyawen 已提交
2776 2777


Z
zengyawen 已提交
2778
### isSymbolicLink
Z
zengyawen 已提交
2779

Z
zengyawen 已提交
2780
isSymbolicLink(): boolean
Z
zengyawen 已提交
2781

Z
zhangxingxia 已提交
2782
用于判断文件是否是符号链接。
Z
zengyawen 已提交
2783

2784 2785
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2786
**返回值:**
H
haonan_7 已提交
2787 2788 2789 2790

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示文件是否是符号链接。 |
Z
zengyawen 已提交
2791

Z
zhangxingxia 已提交
2792
**示例:**
H
haonan_7 已提交
2793

Z
zhangxingxia 已提交
2794
  ```js
Z
zhuhongtao666 已提交
2795 2796
  let filePath = pathDir + "/test";
  let isSymbolicLink = fileio.statSync(filePath).isSymbolicLink(); 
Z
zengyawen 已提交
2797
  ```
Z
zengyawen 已提交
2798 2799


Z
zengyawen 已提交
2800 2801 2802 2803 2804 2805 2806
## Watcher<sup>7+</sup>

Watcher是文件变化监听的实例,调用Watcher.stop()方法(同步或异步)来停止文件监听。


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

2807
stop(): Promise&lt;void&gt;
Z
zengyawen 已提交
2808

H
haonan_7 已提交
2809
关闭watcher监听,使用Promise异步回调。
Z
zengyawen 已提交
2810

2811 2812
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2813
**示例:**
H
haonan_7 已提交
2814

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


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

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

H
haonan_7 已提交
2830
关闭watcher监听,使用callback异步回调。
Z
zengyawen 已提交
2831

2832 2833
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2834
**参数:**
H
haonan_7 已提交
2835 2836 2837 2838

  | 参数名      | 类型                        | 必填   | 说明                     |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | 是    | 以异步方法关闭watcher监听之后的回调。 |
Z
zengyawen 已提交
2839

Z
zhangxingxia 已提交
2840
**示例:**
H
haonan_7 已提交
2841

Z
zhangxingxia 已提交
2842
  ```js
Z
zhuhongtao666 已提交
2843 2844
  let filePath = path +"/test.txt";
  let watcher = fileio.createWatcher(filePath, 1, function(number){
Z
zhangxingxia 已提交
2845
      console.info("Monitoring times: "+number);
Z
zengyawen 已提交
2846
  });
Z
zhangxingxia 已提交
2847 2848 2849
  watcher.stop(function(){
      console.info("close watcher succeed");
  })
Z
zengyawen 已提交
2850 2851 2852
  ```


Z
zhangxingxia 已提交
2853
## Stream
Z
zengyawen 已提交
2854

Z
zengyawen 已提交
2855 2856 2857 2858 2859 2860 2861
文件流,在调用Stream的方法前,需要先通过createStream()方法(同步或异步)来构建一个Stream实例。


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

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

H
haonan_7 已提交
2862
关闭文件流,使用Promise异步回调。
Z
zengyawen 已提交
2863

2864 2865
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2866
**返回值:**
H
haonan_7 已提交
2867 2868 2869 2870

  | 类型                  | 说明            |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise对象。返回表示异步关闭文件流的结果。 |
Z
zengyawen 已提交
2871

Z
zhangxingxia 已提交
2872
**示例:**
H
haonan_7 已提交
2873

Z
zhangxingxia 已提交
2874
  ```js
Z
zhuhongtao666 已提交
2875 2876
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zhangxingxia 已提交
2877
  ss.close().then(function(){
H
haonan_7 已提交
2878
      console.info("close fileStream succeed");
Z
zhangxingxia 已提交
2879 2880 2881
  }).catch(function(err){
      console.info("close fileStream  failed with error:"+ err);
  });
Z
zengyawen 已提交
2882 2883 2884 2885 2886 2887 2888
  ```


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

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

H
haonan_7 已提交
2889
异步关闭文件流,使用callback异步回调。
Z
zengyawen 已提交
2890

2891 2892
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2893
**参数:**
H
haonan_7 已提交
2894 2895 2896 2897

  | 参数名      | 类型                        | 必填   | 说明            |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步关闭文件流之后的回调。 |
Z
zengyawen 已提交
2898

Z
zhangxingxia 已提交
2899
**示例:**
H
haonan_7 已提交
2900

Z
zhangxingxia 已提交
2901
  ```js
Z
zhuhongtao666 已提交
2902 2903
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
2904
  ss.close(function (err) {
Z
zhangxingxia 已提交
2905
      // do something
Z
zengyawen 已提交
2906 2907
  });
  ```
Z
zengyawen 已提交
2908 2909


2910
### closeSync
Z
zengyawen 已提交
2911

Z
zengyawen 已提交
2912
closeSync(): void
Z
zengyawen 已提交
2913

Z
zengyawen 已提交
2914
同步关闭文件流。
Z
zengyawen 已提交
2915

2916 2917
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2918
**示例:**
H
haonan_7 已提交
2919

Z
zhangxingxia 已提交
2920
  ```js
Z
zhuhongtao666 已提交
2921 2922
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
2923 2924
  ss.closeSync();
  ```
Z
zengyawen 已提交
2925 2926


Z
zengyawen 已提交
2927 2928 2929 2930
### flush<sup>7+</sup>

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

H
haonan_7 已提交
2931
刷新文件流,使用Promise异步回调。
Z
zengyawen 已提交
2932

2933 2934
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2935
**返回值:**
H
haonan_7 已提交
2936 2937 2938 2939

  | 类型                  | 说明            |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise对象。返回表示异步刷新文件流的结果。 |
Z
zengyawen 已提交
2940

Z
zhangxingxia 已提交
2941
**示例:**
H
haonan_7 已提交
2942

Z
zhangxingxia 已提交
2943
  ```js
Z
zhuhongtao666 已提交
2944 2945
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zhangxingxia 已提交
2946
  ss.flush().then(function (){
H
haonan_7 已提交
2947
      console.info("flush succeed");
Z
zhangxingxia 已提交
2948 2949 2950
  }).catch(function(err){
      console.info("flush failed with error:"+ err);
  });
Z
zengyawen 已提交
2951 2952 2953 2954 2955 2956 2957
  ```


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

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

H
haonan_7 已提交
2958
异步刷新文件流,使用callback异步回调。
Z
zengyawen 已提交
2959

2960 2961
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2962
**参数:**
H
haonan_7 已提交
2963 2964 2965 2966

  | 参数名      | 类型                        | 必填   | 说明             |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步刷新文件流后的回调函数。 |
Z
zengyawen 已提交
2967

Z
zhangxingxia 已提交
2968
**示例:**
H
haonan_7 已提交
2969

Z
zhangxingxia 已提交
2970
  ```js
Z
zhuhongtao666 已提交
2971 2972
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
2973
  ss.flush(function (err) {
Z
zhangxingxia 已提交
2974
      // do something
Z
zengyawen 已提交
2975 2976 2977 2978
  });
  ```


Z
zengyawen 已提交
2979
### flushSync<sup>7+</sup>
Z
zengyawen 已提交
2980

Z
zengyawen 已提交
2981
flushSync(): void
Z
zengyawen 已提交
2982

Z
zengyawen 已提交
2983
同步刷新文件流。
Z
zengyawen 已提交
2984

2985 2986
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2987
**示例:**
H
haonan_7 已提交
2988

Z
zhangxingxia 已提交
2989
  ```js
Z
zhuhongtao666 已提交
2990 2991
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
2992 2993
  ss.flushSync();
  ```
Z
zengyawen 已提交
2994 2995


Z
zengyawen 已提交
2996 2997
### write<sup>7+</sup>

2998 2999 3000 3001 3002 3003
write(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
3004

H
haonan_7 已提交
3005
将数据写入流文件,使用Promise异步回调。
Z
zengyawen 已提交
3006

3007 3008
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3009
**参数:**
H
haonan_7 已提交
3010 3011 3012 3013 3014

  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
  | options | Object                          | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望写入数据的位置相对于数据首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。<br/>约束:offset+length<=buffer.size。  |
Z
zengyawen 已提交
3015

Z
zhangxingxia 已提交
3016
**返回值:**
H
haonan_7 已提交
3017 3018 3019 3020

  | 类型                    | 说明       |
  | --------------------- | -------- |
  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
Z
zengyawen 已提交
3021

Z
zhangxingxia 已提交
3022
**示例:**
H
haonan_7 已提交
3023

Z
zhangxingxia 已提交
3024
  ```js
Z
zhuhongtao666 已提交
3025 3026
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zhangxingxia 已提交
3027
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
H
haonan_7 已提交
3028
      console.info("write succeed and size is:"+ number);
Z
zhangxingxia 已提交
3029 3030 3031
  }).catch(function(err){
      console.info("write failed with error:"+ err);
  });
Z
zengyawen 已提交
3032 3033 3034 3035 3036
  ```


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

3037 3038 3039 3040 3041 3042
write(buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
3043

H
haonan_7 已提交
3044
将数据写入流文件,使用callback异步回调。
Z
zengyawen 已提交
3045

3046 3047
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3048
**参数:**
H
haonan_7 已提交
3049 3050 3051 3052 3053 3054

  | 参数名   | 类型                            | 必填 | 说明                                                         |
  | -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | 是   | 待写入文件的数据,可来自缓冲区或字符串。                     |
  | options  | Object                          | 否   | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望写入数据的位置相对于数据首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。<br/>约束:offset+length<=buffer.size。 |
  | callback | AsyncCallback&lt;number&gt;     | 是   | 异步写入完成后执行的回调函数。                               |
Z
zengyawen 已提交
3055

Z
zhangxingxia 已提交
3056
**示例:**
H
haonan_7 已提交
3057

Z
zhangxingxia 已提交
3058
  ```js
Z
zhuhongtao666 已提交
3059 3060
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
3061
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
Z
zhangxingxia 已提交
3062
      if (bytesWritten) {
Z
zhangxingxia 已提交
3063
         // do something
H
haonan_7 已提交
3064
         console.info("write succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
3065 3066 3067 3068 3069
      }
  });
  ```


Z
zengyawen 已提交
3070
### writeSync<sup>7+</sup>
Z
zengyawen 已提交
3071

3072 3073 3074 3075 3076 3077
writeSync(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
3078

Z
zengyawen 已提交
3079
以同步方法将数据写入流文件。
Z
zengyawen 已提交
3080

3081 3082
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3083
**参数:**
H
haonan_7 已提交
3084 3085 3086 3087 3088

  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
  | options | Object                          | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望写入数据的位置相对于数据首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。<br/>约束:offset+length<=buffer.size。  |
Z
zengyawen 已提交
3089

Z
zhangxingxia 已提交
3090
**返回值:**
H
haonan_7 已提交
3091 3092 3093 3094

  | 类型     | 说明       |
  | ------ | -------- |
  | number | 实际写入的长度。 |
Z
zengyawen 已提交
3095

Z
zhangxingxia 已提交
3096
**示例:**
H
haonan_7 已提交
3097

Z
zhangxingxia 已提交
3098
  ```js
Z
zhuhongtao666 已提交
3099 3100
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath,"r+");
Z
zengyawen 已提交
3101 3102 3103 3104 3105 3106
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


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

3107 3108 3109 3110 3111
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
3112

H
haonan_7 已提交
3113
从流文件读取数据,使用Promise异步回调。
Z
zengyawen 已提交
3114

3115 3116
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3117
**参数:**
H
haonan_7 已提交
3118 3119 3120 3121 3122

  | 参数名     | 类型          | 必填   | 说明                                       |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | 是    | 用于读取文件的缓冲区。                              |
  | options | Object      | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示将数据读取到缓冲区的位置,即相对于缓冲区首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>约束:offset+length<=buffer.size。  |
Z
zengyawen 已提交
3123

Z
zhangxingxia 已提交
3124
**返回值:**
H
haonan_7 已提交
3125 3126 3127 3128

  | 类型                                 | 说明     |
  | ---------------------------------- | ------ |
  | Promise&lt;[ReadOut](#readout)&gt; | Promise对象。返回读取的结果。 |
Z
zengyawen 已提交
3129

Z
zhangxingxia 已提交
3130
**示例:**
H
haonan_7 已提交
3131

Z
zhangxingxia 已提交
3132
  ```js
Z
zhuhongtao666 已提交
3133 3134
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
3135
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut){
H
haonan_7 已提交
3136
      console.info("read data succeed");
W
wangbo 已提交
3137
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
3138 3139 3140
  }).catch(function(err){
      console.info("read data failed with error:"+ err);
  });
Z
zengyawen 已提交
3141 3142 3143 3144 3145
  ```


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

3146 3147 3148 3149 3150
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
3151

H
haonan_7 已提交
3152
从流文件读取数据,使用callback异步回调。
Z
zengyawen 已提交
3153

3154 3155
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3156
**参数:**
H
haonan_7 已提交
3157 3158 3159 3160 3161 3162

  | 参数名      | 类型                                       | 必填   | 说明                                       |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | buffer   | ArrayBuffer                              | 是    | 用于读取文件的缓冲区。                              |
  | options  | Object                                   | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示将数据读取到缓冲区的位置,即相对于缓冲区首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>约束:offset+length<=buffer.size。  |
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | 是    | 异步从流文件读取数据之后的回调。                         |
Z
zengyawen 已提交
3163

Z
zhangxingxia 已提交
3164
**示例:**
H
haonan_7 已提交
3165

Z
zhangxingxia 已提交
3166
  ```js
Z
zhuhongtao666 已提交
3167 3168
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
3169
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
Z
zhangxingxia 已提交
3170
      if (readOut) {
H
haonan_7 已提交
3171
          console.info("read data succeed");
Z
zhangxingxia 已提交
3172
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
3173 3174
      }
  });
Z
zengyawen 已提交
3175
  ```
Z
zengyawen 已提交
3176 3177


Z
zengyawen 已提交
3178
### readSync<sup>7+</sup>
Z
zengyawen 已提交
3179

3180 3181 3182 3183 3184
readSync(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): number
Z
zengyawen 已提交
3185 3186 3187

以同步方法从流文件读取数据。

3188 3189
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3190
**参数:**
Z
zhangxingxia 已提交
3191

H
haonan_7 已提交
3192 3193 3194 3195
  | 参数名     | 类型          | 必填   | 说明                                       |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer | 是    | 用于读取文件的缓冲区。                              |
  | options | Object      | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示将数据读取到缓冲区的位置,即相对于缓冲区首地址的偏移。可选,默认为0。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度减去偏移长度。<br/>-&nbsp;position,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>约束:offset+length<=buffer.size。  |
Z
zhangxingxia 已提交
3196

Z
zhangxingxia 已提交
3197
**返回值:**
Z
zhangxingxia 已提交
3198

H
haonan_7 已提交
3199 3200 3201
  | 类型     | 说明       |
  | ------ | -------- |
  | number | 实际读取的长度。 |
Z
zhangxingxia 已提交
3202

Z
zhangxingxia 已提交
3203
**示例:**
H
haonan_7 已提交
3204

Z
zhangxingxia 已提交
3205
  ```js
Z
zhuhongtao666 已提交
3206 3207
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
3208
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
Z
zengyawen 已提交
3209
  ```
Z
zengyawen 已提交
3210 3211


Z
zengyawen 已提交
3212
## Dir
Z
zengyawen 已提交
3213

3214
管理目录,在调用Dir的方法前,需要先通过opendir方法(同步或异步)来构建一个Dir实例。
Z
zengyawen 已提交
3215 3216 3217 3218 3219 3220


### read

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

H
haonan_7 已提交
3221
读取下一个目录项,使用Promise异步回调。
Z
zengyawen 已提交
3222

3223 3224
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3225
**返回值:**
H
haonan_7 已提交
3226 3227 3228 3229

  | 类型                               | 说明            |
  | -------------------------------- | ------------- |
  | Promise&lt;[Dirent](#dirent)&gt; | Promise对象。返回表示异步读取目录项的结果。 |
Z
zengyawen 已提交
3230

Z
zhangxingxia 已提交
3231
**示例:**
H
haonan_7 已提交
3232

Z
zhangxingxia 已提交
3233 3234
  ```js
  dir.read().then(function (dirent){
H
haonan_7 已提交
3235
      console.log("read succeed:"+JSON.stringify(dirent));
Z
zhangxingxia 已提交
3236 3237 3238
  }).catch(function(err){
      console.info("read failed with error:"+ err);
  });
Z
zengyawen 已提交
3239 3240 3241 3242 3243 3244 3245
  ```


### read

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

H
haonan_7 已提交
3246
读取下一个目录项,使用callback异步回调。
Z
zengyawen 已提交
3247

3248 3249
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3250
**参数:**
H
haonan_7 已提交
3251 3252 3253 3254

  | 参数名      | 类型                                     | 必填   | 说明               |
  | -------- | -------------------------------------- | ---- | ---------------- |
  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | 是    | 异步读取下一个目录项之后的回调。 |
Z
zengyawen 已提交
3255

Z
zhangxingxia 已提交
3256
**示例:**
H
haonan_7 已提交
3257

Z
zhangxingxia 已提交
3258
  ```js
Z
zengyawen 已提交
3259
  dir.read(function (err, dirent) {
Z
zhangxingxia 已提交
3260
      if (dirent) {
Z
zhangxingxia 已提交
3261
          // do something
H
haonan_7 已提交
3262
          console.log("read succeed:"+JSON.stringify(dirent));
Z
zengyawen 已提交
3263 3264 3265
      }
  });
  ```
Z
zengyawen 已提交
3266 3267


Z
zengyawen 已提交
3268
### readSync
Z
zengyawen 已提交
3269

Z
zengyawen 已提交
3270
readSync(): Dirent
Z
zengyawen 已提交
3271

Z
zengyawen 已提交
3272
同步读取下一个目录项。
Z
zengyawen 已提交
3273

3274 3275
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3276
**返回值:**
H
haonan_7 已提交
3277 3278 3279 3280

  | 类型                | 说明       |
  | ----------------- | -------- |
  | [Dirent](#dirent) | 表示一个目录项。 |
Z
zengyawen 已提交
3281

Z
zhangxingxia 已提交
3282
**示例:**
H
haonan_7 已提交
3283

Z
zhangxingxia 已提交
3284
  ```js
Z
zengyawen 已提交
3285 3286
  let dirent = dir.readSync();
  ```
Z
zengyawen 已提交
3287 3288


W
wangbo 已提交
3289 3290 3291 3292 3293 3294 3295 3296 3297
### close<sup>7+</sup>

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

异步关闭目录,使用promise形式返回结果。目录被关闭后,Dir中持有的文件描述将被释放,后续将无法从Dir中读取目录项。

**系统能力**:SystemCapability.FileManagement.File.FileIO

**示例:**
H
haonan_7 已提交
3298

W
wangbo 已提交
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

异步关闭目录,使用callback形式返回结果。目录被关闭后,Dir中持有的文件描述将被释放,后续将无法从Dir中读取目录项。

**系统能力**:SystemCapability.FileManagement.File.FileIO

**示例:**
H
haonan_7 已提交
3315

W
wangbo 已提交
3316 3317 3318 3319 3320 3321 3322
  ```js
  dir.close(function(err){
      console.info("close dir successfully");
  });
  ```


Z
zengyawen 已提交
3323
### closeSync
Z
zengyawen 已提交
3324

Z
zengyawen 已提交
3325
closeSync(): void
Z
zengyawen 已提交
3326

Z
zengyawen 已提交
3327
用于关闭目录。目录被关闭后,Dir中持有的文件描述将被释放,后续将无法从Dir中读取目录项。
Z
zengyawen 已提交
3328

3329 3330
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3331
**示例:**
H
haonan_7 已提交
3332

Z
zhangxingxia 已提交
3333
  ```js
Z
zengyawen 已提交
3334 3335
  dir.closeSync();
  ```
Z
zengyawen 已提交
3336 3337


Z
zengyawen 已提交
3338
## Dirent
Z
zengyawen 已提交
3339

Z
zengyawen 已提交
3340
在调用Dirent的方法前,需要先通过[dir.read()](#read)方法(同步或异步)来构建一个Dirent实例。
Z
zengyawen 已提交
3341

3342
**系统能力**:以下各项对应的系统能力均为SystemCapability.FileManagement.File.FileIO。
Z
zengyawen 已提交
3343

Z
zengyawen 已提交
3344
### 属性
Z
zengyawen 已提交
3345

H
HelloCrease 已提交
3346 3347 3348
| 名称   | 参数类型   | 可读   | 可写   | 说明      |
| ---- | ------ | ---- | ---- | ------- |
| name | string | 是    | 否    | 目录项的名称。 |
Z
zengyawen 已提交
3349 3350


Z
zengyawen 已提交
3351
### isBlockDevice
Z
zengyawen 已提交
3352

Z
zengyawen 已提交
3353
isBlockDevice(): boolean
Z
zengyawen 已提交
3354

Z
zhangxingxia 已提交
3355
用于判断当前目录项是否是块特殊文件。一个块特殊文件只能以块为粒度进行访问,且访问的时候带缓存。
Z
zengyawen 已提交
3356

3357 3358
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3359
**返回值:**
H
haonan_7 已提交
3360 3361 3362 3363

  | 类型      | 说明               |
  | ------- | ---------------- |
  | boolean | 表示当前目录项是否是块特殊设备。 |
Z
zengyawen 已提交
3364

Z
zhangxingxia 已提交
3365
**示例:**
H
haonan_7 已提交
3366

Z
zhangxingxia 已提交
3367
  ```js
Z
zhuhongtao666 已提交
3368
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3369 3370
  let isBLockDevice = dir.readSync().isBlockDevice();
  ```
Z
zengyawen 已提交
3371 3372


Z
zengyawen 已提交
3373
### isCharacterDevice
Z
zengyawen 已提交
3374

Z
zengyawen 已提交
3375
isCharacterDevice(): boolean
Z
zengyawen 已提交
3376

Z
zhangxingxia 已提交
3377
用于判断当前目录项是否是字符特殊设备。一个字符特殊设备可进行随机访问,且访问的时候不带缓存。
Z
zengyawen 已提交
3378

3379 3380
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3381
**返回值:**
H
haonan_7 已提交
3382 3383 3384 3385

  | 类型      | 说明                |
  | ------- | ----------------- |
  | boolean | 表示当前目录项是否是字符特殊设备。 |
Z
zengyawen 已提交
3386

Z
zhangxingxia 已提交
3387
**示例:**
H
haonan_7 已提交
3388

Z
zhangxingxia 已提交
3389
  ```js
Z
zhuhongtao666 已提交
3390
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3391 3392
  let isCharacterDevice = dir.readSync().isCharacterDevice(); 
  ```
Z
zengyawen 已提交
3393 3394


Z
zengyawen 已提交
3395
### isDirectory
Z
zengyawen 已提交
3396

Z
zengyawen 已提交
3397
isDirectory(): boolean
Z
zengyawen 已提交
3398

Z
zhangxingxia 已提交
3399
用于判断当前目录项是否是目录。
Z
zengyawen 已提交
3400

3401 3402
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3403
**返回值:**
H
haonan_7 已提交
3404 3405 3406 3407

  | 类型      | 说明            |
  | ------- | ------------- |
  | boolean | 表示当前目录项是否是目录。 |
Z
zengyawen 已提交
3408

Z
zhangxingxia 已提交
3409
**示例:**
H
haonan_7 已提交
3410

Z
zhangxingxia 已提交
3411
  ```js
Z
zhuhongtao666 已提交
3412
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3413 3414
  let isDirectory = dir.readSync().isDirectory(); 
  ```
Z
zengyawen 已提交
3415 3416


Z
zengyawen 已提交
3417
### isFIFO
Z
zengyawen 已提交
3418

Z
zengyawen 已提交
3419
isFIFO(): boolean
Z
zengyawen 已提交
3420

Z
zhangxingxia 已提交
3421
用于判断当前目录项是否是命名管道(有时也称为FIFO)。命名管道通常用于进程间通信。
Z
zengyawen 已提交
3422

3423 3424
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3425
**返回值:**
H
haonan_7 已提交
3426 3427 3428 3429

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示当前目录项是否是FIFO。 |
Z
zengyawen 已提交
3430

Z
zhangxingxia 已提交
3431
**示例:**
H
haonan_7 已提交
3432

Z
zhangxingxia 已提交
3433
  ```js
Z
zhuhongtao666 已提交
3434
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3435 3436
  let isFIFO = dir.readSync().isFIFO(); 
  ```
Z
zengyawen 已提交
3437 3438


Z
zengyawen 已提交
3439
### isFile
Z
zengyawen 已提交
3440

Z
zengyawen 已提交
3441
isFile(): boolean
Z
zengyawen 已提交
3442

Z
zhangxingxia 已提交
3443
用于判断当前目录项是否是普通文件。
Z
zengyawen 已提交
3444

3445 3446
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3447
**返回值:**
H
haonan_7 已提交
3448 3449 3450 3451

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示当前目录项是否是普通文件。 |
Z
zengyawen 已提交
3452

Z
zhangxingxia 已提交
3453
**示例:**
H
haonan_7 已提交
3454

Z
zhangxingxia 已提交
3455
  ```js
Z
zhuhongtao666 已提交
3456
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3457 3458
  let isFile = dir.readSync().isFile(); 
  ```
Z
zengyawen 已提交
3459 3460


Z
zengyawen 已提交
3461
### isSocket
Z
zengyawen 已提交
3462

Z
zengyawen 已提交
3463
isSocket(): boolean
Z
zengyawen 已提交
3464

Z
zhangxingxia 已提交
3465
用于判断当前目录项是否是套接字。
Z
zengyawen 已提交
3466

3467 3468
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3469
**返回值:**
H
haonan_7 已提交
3470 3471 3472 3473

  | 类型      | 说明             |
  | ------- | -------------- |
  | boolean | 表示当前目录项是否是套接字。 |
Z
zengyawen 已提交
3474

Z
zhangxingxia 已提交
3475
**示例:**
H
haonan_7 已提交
3476

Z
zhangxingxia 已提交
3477
  ```js
Z
zhuhongtao666 已提交
3478
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3479 3480
  let isSocket = dir.readSync().isSocket(); 
  ```
Z
zengyawen 已提交
3481 3482


Z
zengyawen 已提交
3483
### isSymbolicLink
Z
zengyawen 已提交
3484

Z
zengyawen 已提交
3485
isSymbolicLink(): boolean
Z
zengyawen 已提交
3486

Z
zhangxingxia 已提交
3487
用于判断当前目录项是否是符号链接。
Z
zengyawen 已提交
3488

3489 3490
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3491
**返回值:**
H
haonan_7 已提交
3492 3493 3494 3495

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示当前目录项是否是符号链接。 |
Z
zengyawen 已提交
3496

Z
zhangxingxia 已提交
3497
**示例:**
H
haonan_7 已提交
3498

Z
zhangxingxia 已提交
3499
  ```js
Z
zhuhongtao666 已提交
3500
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3501 3502
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```
Z
zhuhongtao666 已提交
3503

Z
zhuhongtao666 已提交
3504
## Filter<sup>9+</sup>
Z
zhuhongtao666 已提交
3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519

**系统接口**:此接口为系统接口。

**系统能力**:SystemCapability.FileManagement.File.FileIO

文件过滤器配置项。

| 名称        | 参数类型       | 说明                |
| ----------- | --------------- | ------------------ |
| suffix | Array&lt;string&gt;     | 文件后缀名,各个关键词OR关系。           |
| displayName    | Array&lt;string&gt;     | 文件名模糊匹配,各个关键词OR关系。 |
| mimeType    | Array&lt;string&gt; | mime类型匹配,各个关键词OR关系。       |
| fileSizeOver    | number | 文件大小匹配,大于等于指定大小的文件。       |
| lastModifiedAfter    | Date | 修改时间匹配,在指定时间点后的文件。       |
| excludeMedia    | Boolean | 是否排除Media中已有的文件。       |