js-apis-fileio.md 129.3 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){
Z
zhuhongtao666 已提交
71
      console.info("getFileInfo succeed, the size of file is " + stat.size);
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){
Z
zhuhongtao666 已提交
156
      console.info("opendir succeed");
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

Z
zhuhongtao666 已提交
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

  | 参数名  | 类型                         | 必填   | 说明                                       |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
Z
zhuhongtao666 已提交
398 399
  | src  | string\|number | 是    | 待复制文件的路径或待复制文件的描述符。                      |
  | dest | string\|number | 是    | 目标文件路径或目标文件描述符。                          |
H
haonan_7 已提交
400
  | 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

Z
zhuhongtao666 已提交
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

  | 参数名      | 类型                         | 必填   | 说明                                       |
  | -------- | -------------------------- | ---- | ---------------------------------------- |
Z
zhuhongtao666 已提交
433 434
  | src      | string\|number | 是    | 待复制文件的路径或待复制文件的描述符。                      |
  | dest     | string\|number | 是    | 目标文件路径或目标文件描述符。                          |
H
haonan_7 已提交
435 436
  | 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

Z
zhuhongtao666 已提交
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

  | 参数名  | 类型                         | 必填   | 说明                                       |
  | ---- | -------------------------- | ---- | ---------------------------------------- |
Z
zhuhongtao666 已提交
461 462
  | src  | string\|number | 是    | 待复制文件的路径或待复制文件的描述符。                      |
  | dest | string\|number | 是    | 目标文件路径或目标文件描述符。                          |
H
haonan_7 已提交
463
  | 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
zhuhongtao666 已提交
605
| callback | AsyncCallback&lt;number&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

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

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

662 663
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
664
**参数:**
H
haonan_7 已提交
665

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

Z
zhangxingxia 已提交
672
**返回值:**
Z
zhangxingxia 已提交
673

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

Z
zhangxingxia 已提交
678
**示例:**
H
haonan_7 已提交
679

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


Z
zengyawen 已提交
693
## fileio.read
Z
zengyawen 已提交
694

Z
zhuhongtao666 已提交
695
read(fd: number, buffer: ArrayBuffer, options: { offset?: number; length?: number; position?: number; }, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
696

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

699 700
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
701
**参数:**
H
haonan_7 已提交
702 703 704 705 706 707 708

  | 参数名      | 类型                                       | 必填   | 说明                                       |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | 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 已提交
709

Z
zhangxingxia 已提交
710
**示例:**
H
haonan_7 已提交
711

Z
zhangxingxia 已提交
712
  ```js
Z
zhuhongtao666 已提交
713 714
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
Z
zengyawen 已提交
715
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
716
  fileio.read(fd, buf, function (err, readOut) {
Z
zhangxingxia 已提交
717
      if (readOut) {
H
haonan_7 已提交
718
          console.info("read file data succeed");
Z
zhangxingxia 已提交
719
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
720 721
      }
  });
Z
zengyawen 已提交
722
  ```
Z
zengyawen 已提交
723 724


Z
zengyawen 已提交
725
## fileio.readSync
Z
zengyawen 已提交
726

Z
zhuhongtao666 已提交
727
readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): number
Z
zengyawen 已提交
728 729 730

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

731 732
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
733
**参数:**
H
haonan_7 已提交
734 735 736 737 738 739

  | 参数名     | 类型          | 必填   | 说明                                       |
  | ------- | ----------- | ---- | ---------------------------------------- |
  | 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 已提交
740

Z
zhangxingxia 已提交
741
**返回值:**
H
haonan_7 已提交
742 743 744 745

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

Z
zhangxingxia 已提交
747
**示例:**
H
haonan_7 已提交
748

Z
zhangxingxia 已提交
749
  ```js
Z
zhuhongtao666 已提交
750 751
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o2);
Z
zengyawen 已提交
752 753 754 755 756 757 758 759 760
  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 已提交
761
删除目录,使用Promise异步回调。
Z
zengyawen 已提交
762

763 764
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
765
**参数:**
H
haonan_7 已提交
766

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

Z
zhangxingxia 已提交
771
**返回值:**
H
haonan_7 已提交
772 773 774 775

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

Z
zhangxingxia 已提交
777
**示例:**
H
haonan_7 已提交
778

Z
zhangxingxia 已提交
779
  ```js
Z
zhuhongtao666 已提交
780 781
  let dirPath = pathDir + '/testDir';
  fileio.rmdir(dirPath).then(function() {
H
haonan_7 已提交
782
      console.info("rmdir succeed");
Z
zhangxingxia 已提交
783 784
  }).catch(function(err){
      console.info("rmdir failed with error:"+ err);
Z
zengyawen 已提交
785 786 787 788 789 790
  });
  ```


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

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

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

795 796
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
797
**参数:**
H
haonan_7 已提交
798

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

Z
zhangxingxia 已提交
804
**示例:**
H
haonan_7 已提交
805

Z
zhangxingxia 已提交
806
  ```js
Z
zhuhongtao666 已提交
807 808
  let dirPath = pathDir + '/testDir';
  fileio.rmdir(dirPath, function(err){
Z
zhangxingxia 已提交
809
      // do something
H
haonan_7 已提交
810
      console.info("rmdir succeed");
Z
zengyawen 已提交
811 812 813 814
  });
  ```


815
## fileio.rmdirSync<sup>7+</sup>
Z
zengyawen 已提交
816

817
rmdirSync(path: string): void
Z
zengyawen 已提交
818 819 820

以同步方法删除目录。

821 822
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
823
**参数:**
H
haonan_7 已提交
824

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

Z
zhangxingxia 已提交
829
**示例:**
H
haonan_7 已提交
830

Z
zhangxingxia 已提交
831
  ```js
Z
zhuhongtao666 已提交
832 833
  let dirPath = pathDir + '/testDir';
  fileio.rmdirSync(dirPath);
Z
zengyawen 已提交
834 835 836 837 838
  ```


## fileio.unlink

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

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

843 844
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
845
**参数:**
H
haonan_7 已提交
846

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

Z
zhangxingxia 已提交
851
**返回值:**
H
haonan_7 已提交
852 853 854 855

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

Z
zhangxingxia 已提交
857
**示例:**
H
haonan_7 已提交
858

Z
zhangxingxia 已提交
859
  ```js
Z
zhuhongtao666 已提交
860 861
  let filePath = pathDir + "/test.txt";
  fileio.unlink(filePath).then(function(){
H
haonan_7 已提交
862
      console.info("remove file succeed");
Z
zhangxingxia 已提交
863 864 865
  }).catch(function(error){
      console.info("remove file failed with error:"+ error);
  });
Z
zengyawen 已提交
866 867 868 869 870
  ```


## fileio.unlink

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

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

875 876
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
877
**参数:**
H
haonan_7 已提交
878

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

Z
zhangxingxia 已提交
884
**示例:**
H
haonan_7 已提交
885

Z
zhangxingxia 已提交
886
  ```js
Z
zhuhongtao666 已提交
887 888
  let filePath = pathDir + "/test.txt";
  fileio.unlink(filePath, function(err) {
H
haonan_7 已提交
889
      console.info("remove file succeed");
Z
zengyawen 已提交
890 891 892 893 894 895 896 897 898 899
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void

以同步方法删除文件。

900 901
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
902
**参数:**
H
haonan_7 已提交
903

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

Z
zhangxingxia 已提交
908
**示例:**
H
haonan_7 已提交
909

Z
zhangxingxia 已提交
910
  ```js
Z
zhuhongtao666 已提交
911 912
  let filePath = pathDir + "/test.txt";
  fileio.unlinkSync(filePath);
Z
zengyawen 已提交
913 914 915 916 917
  ```


## fileio.write

Z
zhuhongtao666 已提交
918
write(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise&lt;number&gt;
Z
zengyawen 已提交
919

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

922 923
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
924
**参数:**
H
haonan_7 已提交
925 926 927 928

  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | 是    | 待写入文件的文件描述符。                             |
Z
zhuhongtao666 已提交
929
  | buffer  | ArrayBuffer\|string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
H
haonan_7 已提交
930
  | 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 已提交
931

Z
zhangxingxia 已提交
932
**返回值:**
H
haonan_7 已提交
933 934 935 936

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

Z
zhangxingxia 已提交
938
**示例:**
H
haonan_7 已提交
939

Z
zhangxingxia 已提交
940
  ```js
Z
zhuhongtao666 已提交
941 942
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
Z
zhangxingxia 已提交
943
  fileio.write(fd, "hello, world").then(function(number){
H
haonan_7 已提交
944
       console.info("write data to file succeed and size is:"+ number);
Z
zhangxingxia 已提交
945 946 947
  }).catch(function(err){
      console.info("write data to file failed with error:"+ err);
  });
Z
zengyawen 已提交
948 949 950 951 952
  ```


## fileio.write

Z
zhuhongtao666 已提交
953
write(fd: number, buffer: ArrayBuffer|string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
954

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

957 958
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
959
**参数:**
H
haonan_7 已提交
960 961 962 963

  | 参数名      | 类型                              | 必填   | 说明                                       |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | 是    | 待写入文件的文件描述符。                             |
Z
zhuhongtao666 已提交
964
  | buffer   | ArrayBuffer\|string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
H
haonan_7 已提交
965 966
  | 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 已提交
967

Z
zhangxingxia 已提交
968
**示例:**
H
haonan_7 已提交
969

Z
zhangxingxia 已提交
970
  ```js
Z
zhuhongtao666 已提交
971 972
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
Z
zengyawen 已提交
973
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
Z
zhangxingxia 已提交
974
      if (bytesWritten) {
H
haonan_7 已提交
975
         console.info("write data to file succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
976 977 978 979 980 981 982
      }
  });
  ```


## fileio.writeSync

Z
zhuhongtao666 已提交
983
writeSync(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number
Z
zengyawen 已提交
984 985 986

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

987 988
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
989
**参数:**
H
haonan_7 已提交
990 991 992 993

  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | 是    | 待写入文件的文件描述符。                             |
Z
zhuhongtao666 已提交
994
  | buffer  | ArrayBuffer\|string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
H
haonan_7 已提交
995
  | 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 已提交
996

Z
zhangxingxia 已提交
997
**返回值:**
H
haonan_7 已提交
998 999 1000 1001

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

Z
zhangxingxia 已提交
1003
**示例:**
H
haonan_7 已提交
1004

Z
zhangxingxia 已提交
1005
  ```js
Z
zhuhongtao666 已提交
1006 1007
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
Z
zengyawen 已提交
1008 1009 1010 1011 1012 1013 1014 1015
  let num = fileio.writeSync(fd, "hello, world");
  ```


## fileio.hash

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

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

1018 1019
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1020
**参数:**
H
haonan_7 已提交
1021

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

Z
zhangxingxia 已提交
1027
**返回值:**
H
haonan_7 已提交
1028 1029 1030 1031

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

Z
zhangxingxia 已提交
1033
**示例:**
H
haonan_7 已提交
1034

Z
zhangxingxia 已提交
1035
  ```js
Z
zhuhongtao666 已提交
1036 1037
  let filePath = pathDir + "/test.txt";
  fileio.hash(filePath, "sha256").then(function(str){
H
haonan_7 已提交
1038
      console.info("calculate file hash succeed:"+ str);
1039
  }).catch(function(err){
W
wangbo 已提交
1040
      console.info("calculate file hash failed with error:"+ err);
Z
zhangxingxia 已提交
1041
  });
Z
zengyawen 已提交
1042 1043 1044 1045 1046
  ```


## fileio.hash

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

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

1051 1052
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1053
**参数:**
H
haonan_7 已提交
1054

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

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

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


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

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

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

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

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

Z
zengyawen 已提交
1083 1084
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1085
| path   | string | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1086
| 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 已提交
1087

Z
zhangxingxia 已提交
1088
**返回值:**
H
haonan_7 已提交
1089 1090 1091 1092

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

Z
zhangxingxia 已提交
1094
**示例:**
H
haonan_7 已提交
1095

Z
zhangxingxia 已提交
1096
  ```js
Z
zhuhongtao666 已提交
1097 1098
  let filePath = pathDir + "/test.txt";
  fileio.chmod(filePath, 0o700).then(function() {
H
haonan_7 已提交
1099
      console.info("chmod succeed");
Z
zhangxingxia 已提交
1100 1101
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
1102 1103 1104 1105 1106 1107 1108 1109
  });
  ```


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

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

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

1112 1113
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1114
**参数:**
H
haonan_7 已提交
1115

Z
zengyawen 已提交
1116 1117
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1118
| path     | string                    | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1119 1120
| 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 已提交
1121

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

Z
zhangxingxia 已提交
1124
  ```js
Z
zhuhongtao666 已提交
1125 1126
  let filePath = pathDir + "/test.txt";
  fileio.chmod(filePath, 0o700, function (err) {
Z
zhangxingxia 已提交
1127
      // do something
Z
zengyawen 已提交
1128 1129 1130 1131 1132 1133 1134 1135
  });
  ```


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

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

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

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

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

Z
zengyawen 已提交
1142 1143
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1144
| path   | string | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1145
| 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 已提交
1146

Z
zhangxingxia 已提交
1147
**示例:**
H
haonan_7 已提交
1148

Z
zhangxingxia 已提交
1149
  ```js
Z
zhuhongtao666 已提交
1150 1151
  let filePath = pathDir + "/test.txt";
  fileio.chmodSync(filePath, 0o700);
Z
zengyawen 已提交
1152 1153 1154 1155 1156 1157 1158
  ```


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

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

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

1161 1162
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1163
**参数:**
H
haonan_7 已提交
1164 1165 1166 1167

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

Z
zhangxingxia 已提交
1169
**返回值:**
H
haonan_7 已提交
1170 1171 1172 1173

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

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

Z
zhangxingxia 已提交
1177
  ```js
Z
zhuhongtao666 已提交
1178 1179
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1180
  fileio.fstat(fd).then(function(stat){
Z
zhuhongtao666 已提交
1181
      console.info("fstat succeed, the size of file is "+ stat.size);
Z
zhangxingxia 已提交
1182 1183 1184
  }).catch(function(err){
      console.info("fstat failed with error:"+ err);
  });
Z
zengyawen 已提交
1185 1186 1187 1188 1189 1190 1191
  ```


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

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

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

1194 1195
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1196
**参数:**
H
haonan_7 已提交
1197 1198 1199 1200 1201

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

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

Z
zhangxingxia 已提交
1205
  ```js
Z
zhuhongtao666 已提交
1206 1207
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1208 1209
  fileio.fstat(fd, function (err) {
      // do something
Z
zengyawen 已提交
1210 1211 1212 1213 1214 1215 1216 1217
  });
  ```


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

fstatSync(fd: number): Stat

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

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

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

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

Z
zhangxingxia 已提交
1228
**返回值:**
H
haonan_7 已提交
1229 1230 1231 1232

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

Z
zhangxingxia 已提交
1234
**示例:**
H
haonan_7 已提交
1235

Z
zhangxingxia 已提交
1236
  ```js
Z
zhuhongtao666 已提交
1237 1238
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
1239 1240 1241 1242 1243 1244
  let stat = fileio.fstatSync(fd);
  ```


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

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

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

1249 1250
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1251
**参数:**
H
haonan_7 已提交
1252 1253 1254 1255 1256

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

Z
zhangxingxia 已提交
1258
**返回值:**
H
haonan_7 已提交
1259 1260 1261 1262

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

Z
zhangxingxia 已提交
1264
**示例:**
H
haonan_7 已提交
1265

Z
zhangxingxia 已提交
1266
  ```js
Z
zhuhongtao666 已提交
1267 1268
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1269
  fileio.ftruncate(fd, 5).then(function(err) {    
H
haonan_7 已提交
1270
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1271 1272
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1273 1274 1275 1276 1277 1278
  });
  ```


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

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

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

1283 1284
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1285
**参数:**
H
haonan_7 已提交
1286 1287 1288 1289

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

Z
zhangxingxia 已提交
1293
**示例:**
H
haonan_7 已提交
1294

Z
zhangxingxia 已提交
1295
  ```js
Z
zhuhongtao666 已提交
1296 1297
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
1298 1299
  let len = 5;
  fileio.ftruncate(fd, 5, function(err){
Z
zhangxingxia 已提交
1300
      // do something
Z
zengyawen 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
  });
  ```


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

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

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

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

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

  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
  | len  | number | 否    | 文件截断后的长度,以字节为单位。 |
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
  let len = 5;
Z
zhangxingxia 已提交
1326
  fileio.ftruncateSync(fd, len);
Z
zengyawen 已提交
1327 1328 1329 1330 1331
  ```


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

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

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

1336 1337
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1338
**参数:**
H
haonan_7 已提交
1339

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

Z
zhangxingxia 已提交
1345
**返回值:**
H
haonan_7 已提交
1346 1347 1348 1349

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

Z
zhangxingxia 已提交
1351
**示例:**
H
haonan_7 已提交
1352

Z
zhangxingxia 已提交
1353
  ```js
Z
zhuhongtao666 已提交
1354
  let filePath = pathDir + "/test.txt";
1355
  let len = 5;
Z
zhuhongtao666 已提交
1356
  fileio.truncate(filePath, len).then(function(){
H
haonan_7 已提交
1357
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1358 1359
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1360 1361 1362 1363 1364 1365
  });
  ```


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

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

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

1370 1371
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1372
**参数:**
H
haonan_7 已提交
1373

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

Z
zhangxingxia 已提交
1380
**示例:**
H
haonan_7 已提交
1381

Z
zhangxingxia 已提交
1382
  ```js
Z
zhuhongtao666 已提交
1383
  let filePath = pathDir + "/test.txt";
1384
  let len = 5;
Z
zhuhongtao666 已提交
1385
  fileio.truncate(filePath, len, function(err){
Z
zhangxingxia 已提交
1386
      // do something
Z
zengyawen 已提交
1387 1388 1389 1390 1391 1392
  });
  ```


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

1393
truncateSync(path: string, len?: number): void
Z
zengyawen 已提交
1394 1395 1396

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

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

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

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

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

Z
zhangxingxia 已提交
1408
  ```js
Z
zhuhongtao666 已提交
1409
  let filePath = pathDir + "/test.txt";
1410
  let len = 5;
Z
zhuhongtao666 已提交
1411
  fileio.truncateSync(filePath, len);
Z
zengyawen 已提交
1412 1413 1414 1415 1416
  ```


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

Z
zhuhongtao666 已提交
1417
readText(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): Promise&lt;string&gt;
Z
zengyawen 已提交
1418

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

1421 1422
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1423
**参数:**
H
haonan_7 已提交
1424

Z
zengyawen 已提交
1425 1426 1427 1428
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1429

Z
zhangxingxia 已提交
1430
**返回值:**
H
haonan_7 已提交
1431 1432 1433 1434

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

Z
zhangxingxia 已提交
1436
**示例:**
H
haonan_7 已提交
1437

Z
zhangxingxia 已提交
1438
  ```js
Z
zhuhongtao666 已提交
1439 1440
  let filePath = pathDir + "/test.txt";
  fileio.readText(filePath).then(function(str) {
H
haonan_7 已提交
1441
      console.info("readText succeed:"+ str);
Z
zhangxingxia 已提交
1442 1443
  }).catch(function(err){
      console.info("readText failed with error:"+ err);
Z
zengyawen 已提交
1444 1445 1446 1447 1448 1449
  });
  ```


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

Z
zhuhongtao666 已提交
1450
readText(filePath: string, options: { position?: number; length?: number; encoding?: string; }, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1451

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

1454 1455
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1456
**参数:**
H
haonan_7 已提交
1457

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

Z
zhangxingxia 已提交
1464
**示例:**
H
haonan_7 已提交
1465

Z
zhangxingxia 已提交
1466
  ```js
Z
zhuhongtao666 已提交
1467 1468
  let filePath = pathDir + "/test.txt";
  fileio.readText(filePath, { position: 1, encoding: 'UTF-8' }, function(err, str){
Z
zhangxingxia 已提交
1469
      // do something
Z
zengyawen 已提交
1470 1471 1472 1473 1474 1475
  });
  ```


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

Z
zhuhongtao666 已提交
1476
readTextSync(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): string
Z
zengyawen 已提交
1477 1478 1479

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

1480 1481
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1482
**参数:**
H
haonan_7 已提交
1483

Z
zengyawen 已提交
1484 1485 1486 1487
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1488

Z
zhangxingxia 已提交
1489
**返回值:**
H
haonan_7 已提交
1490 1491 1492 1493

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

Z
zhangxingxia 已提交
1495
**示例:**
H
haonan_7 已提交
1496

Z
zhangxingxia 已提交
1497
  ```js
Z
zhuhongtao666 已提交
1498 1499
  let filePath = pathDir + "/test.txt";
  let str = fileio.readTextSync(filePath, {position: 1, length: 3});
Z
zengyawen 已提交
1500 1501 1502 1503 1504 1505 1506
  ```


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

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

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

1509 1510
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1511
**参数:**
H
haonan_7 已提交
1512

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

Z
zhangxingxia 已提交
1517
**返回值:**
H
haonan_7 已提交
1518 1519 1520 1521

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

Z
zhangxingxia 已提交
1523
**示例:**
H
haonan_7 已提交
1524

Z
zhangxingxia 已提交
1525
  ```js
Z
zhuhongtao666 已提交
1526 1527
  let filePath = pathDir + "/test.txt";
  fileio.lstat(filePath).then(function(stat){
Z
zhuhongtao666 已提交
1528
      console.info("get link status succeed, the size of file is" + stat.size);
Z
zhangxingxia 已提交
1529 1530 1531
  }).catch(function(err){
      console.info("get link status failed with error:"+ err);
  });
Z
zengyawen 已提交
1532 1533 1534 1535 1536
  ```


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

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

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

1541 1542
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1543
**参数:**
H
haonan_7 已提交
1544

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

Z
zhangxingxia 已提交
1550
**示例:**
H
haonan_7 已提交
1551

Z
zhangxingxia 已提交
1552
  ```js
Z
zhuhongtao666 已提交
1553 1554
  let filePath = pathDir + "/test.txt";
  fileio.lstat(filePath, function (err, stat) {
Z
zhangxingxia 已提交
1555
      // do something
Z
zengyawen 已提交
1556
  });
Z
zengyawen 已提交
1557 1558 1559 1560 1561
  ```


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

Z
zhuhongtao666 已提交
1562
lstatSync(path: string): Stat
Z
zengyawen 已提交
1563

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

1566 1567
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1568
**参数:**
H
haonan_7 已提交
1569

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

Z
zhangxingxia 已提交
1574
**返回值:**
H
haonan_7 已提交
1575 1576 1577 1578

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

Z
zhangxingxia 已提交
1580
**示例:**
H
haonan_7 已提交
1581

Z
zhangxingxia 已提交
1582
  ```js
Z
zhuhongtao666 已提交
1583 1584
  let filePath = pathDir + "/test.txt";
  let stat = fileio.lstatSync(filePath);
Z
zengyawen 已提交
1585 1586 1587 1588 1589 1590 1591
  ```


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

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

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

1594 1595
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1596
**参数:**
H
haonan_7 已提交
1597

Z
zengyawen 已提交
1598 1599 1600
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
Z
zhuhongtao666 已提交
1601
| newPath | string | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1602

Z
zhangxingxia 已提交
1603
**返回值:**
H
haonan_7 已提交
1604 1605 1606 1607

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

Z
zhangxingxia 已提交
1609
**示例:**
H
haonan_7 已提交
1610

Z
zhangxingxia 已提交
1611
  ```js
Z
zhuhongtao666 已提交
1612 1613 1614
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
  fileio.rename(srcFile, dstFile).then(function() {
H
haonan_7 已提交
1615
      console.info("rename succeed");
Z
zhangxingxia 已提交
1616 1617
  }).catch(function(err){
      console.info("rename failed with error:"+ err);
Z
zengyawen 已提交
1618 1619 1620 1621 1622 1623 1624 1625
  });
  ```


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

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

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

1628 1629
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1630
**参数:**
H
haonan_7 已提交
1631

Z
zengyawen 已提交
1632 1633 1634
| 参数名   | 类型                      | 必填 | 说明                         |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | 是   | 目标文件的当前应用沙箱路径。 |
Z
zhuhongtao666 已提交
1635 1636
| newPath  | string                    | 是   | 目标文件的新应用沙箱路径。   |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步重命名文件之后的回调。   |
Z
zengyawen 已提交
1637

Z
zhangxingxia 已提交
1638
**示例:**
H
haonan_7 已提交
1639

Z
zhangxingxia 已提交
1640
  ```js
Z
zhuhongtao666 已提交
1641 1642 1643
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
  fileio.rename(srcFile, dstFile, function(err){
Z
zengyawen 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
  });
  ```


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

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

以同步方法重命名文件。

1654 1655
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1656
**参数:**
H
haonan_7 已提交
1657

Z
zengyawen 已提交
1658 1659 1660
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
Z
zhuhongtao666 已提交
1661
| newPath | string | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1662

Z
zhangxingxia 已提交
1663
**示例:**
H
haonan_7 已提交
1664

Z
zhangxingxia 已提交
1665
  ```js
Z
zhuhongtao666 已提交
1666 1667 1668
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/new.txt';
  fileio.renameSync(srcFile, dstFile);
Z
zengyawen 已提交
1669 1670 1671 1672 1673 1674 1675
  ```


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

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

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

1678 1679
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1680
**参数:**
H
haonan_7 已提交
1681 1682 1683 1684

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

Z
zhangxingxia 已提交
1686
**返回值:**
H
haonan_7 已提交
1687 1688 1689 1690

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

Z
zhangxingxia 已提交
1692
**示例:**
H
haonan_7 已提交
1693

Z
zhangxingxia 已提交
1694
  ```js
Z
zhuhongtao666 已提交
1695 1696
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1697
  fileio.fsync(fd).then(function(){
H
haonan_7 已提交
1698
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1699 1700 1701
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
  });
Z
zengyawen 已提交
1702 1703 1704 1705 1706 1707 1708
  ```


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

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

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

1711 1712
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1713
**参数:**
H
haonan_7 已提交
1714 1715 1716 1717 1718

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

Z
zhangxingxia 已提交
1720
**示例:**
H
haonan_7 已提交
1721

W
wangbo 已提交
1722
  ```js
Z
zhuhongtao666 已提交
1723 1724
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
1725
  fileio.fsync(fd, function(err){
Z
zhangxingxia 已提交
1726
      // do something
Z
zengyawen 已提交
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
  });
  ```


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

fsyncSync(fd: number): void

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

1737 1738
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1739
**参数:**
H
haonan_7 已提交
1740 1741 1742 1743

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

Z
zhangxingxia 已提交
1745
**示例:**
H
haonan_7 已提交
1746

Z
zhangxingxia 已提交
1747
  ```js
Z
zhuhongtao666 已提交
1748 1749
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1750
  fileio.fsyncSync(fd);
Z
zengyawen 已提交
1751 1752 1753 1754 1755 1756 1757
  ```


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

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

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

1760 1761
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1762
**参数:**
H
haonan_7 已提交
1763 1764 1765 1766

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

Z
zhangxingxia 已提交
1768
**返回值:**
H
haonan_7 已提交
1769 1770 1771 1772

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

Z
zhangxingxia 已提交
1774
**示例:**
H
haonan_7 已提交
1775

W
wangbo 已提交
1776
  ```js
Z
zhuhongtao666 已提交
1777 1778
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
1779
  fileio.fdatasync(fd).then(function(err) {
H
haonan_7 已提交
1780
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1781 1782
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
Z
zengyawen 已提交
1783 1784 1785 1786 1787 1788
  });
  ```


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

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

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

1793 1794
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1795
**参数:**
H
haonan_7 已提交
1796 1797 1798 1799

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

Z
zhangxingxia 已提交
1802
**示例:**
H
haonan_7 已提交
1803

Z
zhangxingxia 已提交
1804
  ```js
Z
zhuhongtao666 已提交
1805 1806
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
1807
  fileio.fdatasync (fd, function (err) {
Z
zhangxingxia 已提交
1808
      // do something
Z
zengyawen 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
  });
  ```


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

fdatasyncSync(fd: number): void

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

1819 1820
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1821
**参数:**
H
haonan_7 已提交
1822 1823 1824 1825

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

Z
zhangxingxia 已提交
1827
**示例:**
H
haonan_7 已提交
1828

Z
zhangxingxia 已提交
1829
  ```js
Z
zhuhongtao666 已提交
1830 1831
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
1832 1833 1834 1835 1836 1837 1838 1839
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

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

1842 1843
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1844
**参数:**
H
haonan_7 已提交
1845

Z
zengyawen 已提交
1846 1847 1848 1849
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1850

Z
zhangxingxia 已提交
1851
**返回值:**
H
haonan_7 已提交
1852 1853 1854 1855

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

Z
zhangxingxia 已提交
1857
**示例:**
H
haonan_7 已提交
1858

Z
zhangxingxia 已提交
1859
  ```js
Z
zhuhongtao666 已提交
1860 1861 1862
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlink(srcFile, dstFile).then(function() {
H
haonan_7 已提交
1863
      console.info("symlink succeed");
Z
zhangxingxia 已提交
1864 1865
  }).catch(function(err){
      console.info("symlink failed with error:"+ err);
Z
zengyawen 已提交
1866 1867 1868 1869 1870 1871 1872 1873
  });
  ```


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

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

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

1876 1877
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1878
**参数:**
H
haonan_7 已提交
1879

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

Z
zhangxingxia 已提交
1886
**示例:**
H
haonan_7 已提交
1887

Z
zhangxingxia 已提交
1888
  ```js
Z
zhuhongtao666 已提交
1889 1890 1891
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlink(srcFile, dstFile, function (err) {
Z
zhangxingxia 已提交
1892
      // do something
Z
zengyawen 已提交
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
  });
  ```


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

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

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

1903 1904
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1905
**参数:**
H
haonan_7 已提交
1906

Z
zengyawen 已提交
1907 1908 1909 1910
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1911

Z
zhangxingxia 已提交
1912
**示例:**
H
haonan_7 已提交
1913

Z
zhangxingxia 已提交
1914
  ```js
Z
zhuhongtao666 已提交
1915 1916 1917
  let srcFile = pathDir + "/test.txt";
  let dstFile = pathDir + '/test';
  fileio.symlinkSync(srcFile, dstFile);
Z
zengyawen 已提交
1918 1919 1920 1921 1922 1923 1924
  ```


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

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

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

1927 1928
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1929
**参数:**
H
haonan_7 已提交
1930

Z
zengyawen 已提交
1931 1932 1933 1934 1935
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID(UserID)。        |
| gid    | number | 是   | 新的GID(GroupID)。       |
Z
zengyawen 已提交
1936

Z
zhangxingxia 已提交
1937
**返回值:**
H
haonan_7 已提交
1938 1939 1940 1941

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

Z
zhangxingxia 已提交
1943
**示例:**
H
haonan_7 已提交
1944

Z
zhangxingxia 已提交
1945
  ```js
Z
zhuhongtao666 已提交
1946 1947 1948
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.chown(filePath, stat.uid, stat.gid).then(function(){
H
haonan_7 已提交
1949
      console.info("chown succeed");
Z
zhangxingxia 已提交
1950 1951 1952
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
1953 1954 1955 1956 1957 1958 1959
  ```


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

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

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

1962 1963
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1964
**参数:**
H
haonan_7 已提交
1965

Z
zengyawen 已提交
1966 1967 1968 1969 1970 1971
| 参数名   | 类型                      | 必填 | 说明                           |
| -------- | ------------------------- | ---- | ------------------------------ |
| path     | string                    | 是   | 待改变文件的应用沙箱路径。     |
| uid      | number                    | 是   | 新的UID。                      |
| gid      | number                    | 是   | 新的GID。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
1972

Z
zhangxingxia 已提交
1973
**示例:**
H
haonan_7 已提交
1974

Z
zhangxingxia 已提交
1975
  ```js
Z
zhuhongtao666 已提交
1976 1977 1978
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath)
  fileio.chown(filePath, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
1979
      // do something
Z
zengyawen 已提交
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
  });
  ```


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

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

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

1990 1991
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
1992
**参数:**
H
haonan_7 已提交
1993

Z
zengyawen 已提交
1994 1995 1996 1997 1998
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
1999

Z
zhangxingxia 已提交
2000
**示例:**
H
haonan_7 已提交
2001

Z
zhangxingxia 已提交
2002
  ```js
Z
zhuhongtao666 已提交
2003 2004 2005
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath)
  fileio.chownSync(filePath, stat.uid, stat.gid);
Z
zengyawen 已提交
2006 2007 2008 2009 2010 2011 2012
  ```


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

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

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

2015 2016
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2017
**参数:**
H
haonan_7 已提交
2018 2019 2020 2021

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

Z
zhangxingxia 已提交
2023
**返回值:**
H
haonan_7 已提交
2024 2025 2026 2027

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

Z
zhangxingxia 已提交
2029
**示例:**
H
haonan_7 已提交
2030

Z
zhangxingxia 已提交
2031
  ```js
Z
zhuhongtao666 已提交
2032 2033
  fileio.mkdtemp(pathDir + "/XXXXXX").then(function(pathDir){
      console.info("mkdtemp succeed:"+ pathDir);
Z
zhangxingxia 已提交
2034 2035 2036
  }).catch(function(err){
      console.info("mkdtemp failed with error:"+ err);
  });
Z
zengyawen 已提交
2037 2038 2039 2040 2041 2042 2043
  ```


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

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

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

2046 2047
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2048
**参数:**
H
haonan_7 已提交
2049 2050 2051 2052 2053

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

Z
zhangxingxia 已提交
2055
**示例:**
H
haonan_7 已提交
2056

Z
zhangxingxia 已提交
2057
  ```js
Z
zhuhongtao666 已提交
2058
  fileio.mkdtemp(pathDir + "/XXXXXX", function (err, res) {
Z
zhangxingxia 已提交
2059
      // do something
Z
zengyawen 已提交
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
  });
  ```


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

mkdtempSync(prefix: string): string

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

2070 2071
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2072
**参数:**
H
haonan_7 已提交
2073 2074 2075 2076

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

Z
zhangxingxia 已提交
2078
**返回值:**
H
haonan_7 已提交
2079 2080 2081 2082

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

Z
zhangxingxia 已提交
2084
**示例:**
H
haonan_7 已提交
2085

Z
zhangxingxia 已提交
2086
  ```js
Z
zhuhongtao666 已提交
2087
  let res = fileio.mkdtempSync(pathDir + "/XXXXXX");
Z
zengyawen 已提交
2088 2089 2090 2091 2092 2093 2094
  ```


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

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

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

2097 2098
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2099
**参数:**
H
haonan_7 已提交
2100 2101 2102 2103 2104

  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2105

Z
zhangxingxia 已提交
2106
**返回值:**
H
haonan_7 已提交
2107 2108 2109 2110

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

Z
zhangxingxia 已提交
2112
**示例:**
H
haonan_7 已提交
2113

Z
zhangxingxia 已提交
2114
  ```js
Z
zhuhongtao666 已提交
2115 2116
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
2117
  let mode = 0o700;
Z
zhangxingxia 已提交
2118
  fileio.fchmod(fd, mode).then(function() {
H
haonan_7 已提交
2119
      console.info("chmod succeed");
Z
zhangxingxia 已提交
2120 2121
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
2122 2123 2124 2125 2126 2127 2128 2129
  });
  ```


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

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

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

2132 2133
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2134
**参数:**
H
haonan_7 已提交
2135 2136 2137 2138 2139

  | 参数名      | 类型                              | 必填   | 说明                                       |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | 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
zhuhongtao666 已提交
2140
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步改变文件权限之后的回调。                           |
Z
zengyawen 已提交
2141

Z
zhangxingxia 已提交
2142
**示例:**
H
haonan_7 已提交
2143

Z
zhangxingxia 已提交
2144
  ```js
Z
zhuhongtao666 已提交
2145 2146
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
2147
  let mode = 0o700;
Z
zengyawen 已提交
2148
  fileio.fchmod(fd, mode, function (err) {
Z
zhangxingxia 已提交
2149
      // do something
Z
zengyawen 已提交
2150 2151 2152 2153 2154 2155
  });
  ```


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

2156
fchmodSync(fd: number, mode: number): void
Z
zengyawen 已提交
2157 2158 2159

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

2160 2161
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2162
**参数:**
H
haonan_7 已提交
2163 2164 2165 2166 2167

  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2168

Z
zhangxingxia 已提交
2169
**示例:**
H
haonan_7 已提交
2170

Z
zhangxingxia 已提交
2171
  ```js
Z
zhuhongtao666 已提交
2172 2173
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
2174
  let mode = 0o700;
W
wangbo 已提交
2175
   fileio.fchmodSync(fd, mode);
Z
zengyawen 已提交
2176 2177 2178 2179 2180 2181 2182
  ```


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

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

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

2185 2186
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2187
**参数:**
H
haonan_7 已提交
2188

Z
zengyawen 已提交
2189 2190 2191 2192
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2193

Z
zhangxingxia 已提交
2194
**返回值:**
H
haonan_7 已提交
2195 2196 2197

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

Z
zhangxingxia 已提交
2200
**示例:**
H
haonan_7 已提交
2201

Z
zhangxingxia 已提交
2202
  ```js
Z
zhuhongtao666 已提交
2203 2204
  let filePath = pathDir + "/test.txt";
  fileio.createStream(filePath, "r+").then(function(stream){
H
haonan_7 已提交
2205
      console.info("createStream succeed");
Z
zhangxingxia 已提交
2206 2207 2208
  }).catch(function(err){
      console.info("createStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2209 2210 2211 2212 2213 2214 2215
  ```


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

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

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

2218 2219
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2220
**参数:**
H
haonan_7 已提交
2221

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

Z
zhangxingxia 已提交
2228
**示例:**
H
haonan_7 已提交
2229

Z
zhangxingxia 已提交
2230
  ```js
Z
zhuhongtao666 已提交
2231 2232
  let filePath = pathDir + "/test.txt";
  fileio.createStream(filePath, "r+", function(err, stream){
Z
zhangxingxia 已提交
2233
      // do something
Z
zengyawen 已提交
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
  });
  ```


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

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

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

2244 2245
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2246
**参数:**
H
haonan_7 已提交
2247

Z
zengyawen 已提交
2248 2249 2250 2251
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2252

Z
zhangxingxia 已提交
2253
**返回值:**
H
haonan_7 已提交
2254 2255 2256

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

Z
zhangxingxia 已提交
2259
**示例:**
H
haonan_7 已提交
2260

Z
zhangxingxia 已提交
2261
  ```js
Z
zhuhongtao666 已提交
2262 2263
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
2264 2265 2266 2267 2268 2269 2270
  ```


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

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

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

2273 2274
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2275
**参数:**
H
haonan_7 已提交
2276 2277 2278 2279 2280

  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2281

Z
zhangxingxia 已提交
2282
**返回值:**
H
haonan_7 已提交
2283 2284 2285

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

Z
zhangxingxia 已提交
2288
**示例:**
H
haonan_7 已提交
2289

Z
zhangxingxia 已提交
2290
  ```js
Z
zhuhongtao666 已提交
2291 2292
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
2293
  fileio.fdopenStream(fd, "r+").then(function(stream){
H
haonan_7 已提交
2294
      console.info("openStream succeed");
Z
zhangxingxia 已提交
2295 2296 2297
  }).catch(function(err){
      console.info("openStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2298 2299 2300 2301 2302 2303 2304
  ```


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

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

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

2307 2308
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2309
**参数:**
H
haonan_7 已提交
2310 2311 2312 2313 2314

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

Z
zhangxingxia 已提交
2317
**示例:**
H
haonan_7 已提交
2318

Z
zhangxingxia 已提交
2319
  ```js
Z
zhuhongtao666 已提交
2320 2321
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zhangxingxia 已提交
2322
  fileio.fdopenStream(fd, "r+", function (err, stream) {
Z
zhangxingxia 已提交
2323
      // do something
Z
zengyawen 已提交
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
  });
  ```


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

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

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

2334 2335
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2336
**参数:**
H
haonan_7 已提交
2337 2338 2339 2340 2341

  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2342

Z
zhangxingxia 已提交
2343
**返回值:**
H
haonan_7 已提交
2344 2345 2346

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

Z
zhangxingxia 已提交
2349
**示例:**
H
haonan_7 已提交
2350

Z
zhangxingxia 已提交
2351
  ```js
Z
zhuhongtao666 已提交
2352 2353
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
Z
zengyawen 已提交
2354 2355 2356 2357 2358 2359 2360 2361
  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 已提交
2362
基于文件描述符改变文件所有者,使用Promise异步回调。
Z
zengyawen 已提交
2363

2364 2365
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2366
**参数:**
H
haonan_7 已提交
2367 2368 2369 2370 2371 2372

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

Z
zhangxingxia 已提交
2374
**返回值:**
H
haonan_7 已提交
2375 2376 2377 2378

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

Z
zhangxingxia 已提交
2380
**示例:**
H
haonan_7 已提交
2381

Z
zhangxingxia 已提交
2382
  ```js
Z
zhuhongtao666 已提交
2383 2384 2385
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
Z
zhangxingxia 已提交
2386
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
H
haonan_7 已提交
2387
      console.info("chown succeed");
Z
zhangxingxia 已提交
2388 2389
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
Z
zengyawen 已提交
2390 2391 2392 2393 2394 2395 2396 2397
  });
  ```


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

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

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

2400 2401
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2402
**参数:**
H
haonan_7 已提交
2403 2404 2405 2406 2407 2408 2409

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

Z
zhangxingxia 已提交
2411
**示例:**
H
haonan_7 已提交
2412

Z
zhangxingxia 已提交
2413
  ```js
Z
zhuhongtao666 已提交
2414 2415 2416
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
Z
zengyawen 已提交
2417
  fileio.fchown(fd, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2418
      // do something
Z
zengyawen 已提交
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
  });
  ```


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

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

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

2429 2430
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2431
**参数:**
H
haonan_7 已提交
2432 2433 2434 2435 2436 2437

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

Z
zhangxingxia 已提交
2439
**示例:**
H
haonan_7 已提交
2440

Z
zhangxingxia 已提交
2441
  ```js
Z
zhuhongtao666 已提交
2442 2443 2444
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
  let stat = fileio.statSync(filePath);
Z
zengyawen 已提交
2445 2446 2447 2448 2449 2450 2451 2452
  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 已提交
2453
基于文件路径改变文件所有者,更改符号链接本身的所有者,而不是符号链接所指向的实际文件,使用Promise异步回调。
Z
zengyawen 已提交
2454

2455 2456
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2457
**参数:**
H
haonan_7 已提交
2458

Z
zengyawen 已提交
2459 2460 2461 2462 2463
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2464

Z
zhangxingxia 已提交
2465
**返回值:**
H
haonan_7 已提交
2466 2467 2468 2469

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

Z
zhangxingxia 已提交
2471
**示例:**
H
haonan_7 已提交
2472

Z
zhangxingxia 已提交
2473
  ```js
Z
zhuhongtao666 已提交
2474 2475 2476
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchown(filePath, stat.uid, stat.gid).then(function() {
H
haonan_7 已提交
2477
      console.info("chown succeed");
Z
zhangxingxia 已提交
2478 2479 2480
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
2481 2482 2483 2484 2485 2486 2487
  ```


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

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

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

2490 2491
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2492
**参数:**
H
haonan_7 已提交
2493

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

Z
zhangxingxia 已提交
2501
**示例:**
H
haonan_7 已提交
2502

Z
zhangxingxia 已提交
2503
  ```js
Z
zhuhongtao666 已提交
2504 2505 2506
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchown(filePath, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2507
      // do something
Z
zengyawen 已提交
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
  });
  ```


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

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

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

2518 2519
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2520
**参数:**
H
haonan_7 已提交
2521

Z
zengyawen 已提交
2522 2523 2524 2525 2526
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2527

Z
zhangxingxia 已提交
2528
**示例:**
H
haonan_7 已提交
2529

Z
zhangxingxia 已提交
2530
  ```js
Z
zhuhongtao666 已提交
2531 2532 2533
  let filePath = pathDir + "/test.txt";
  let stat = fileio.statSync(filePath);
  fileio.lchownSync(filePath, stat.uid, stat.gid);
Z
zengyawen 已提交
2534 2535 2536 2537 2538
  ```


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

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

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

2543 2544
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2545
**参数:**
H
haonan_7 已提交
2546

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

Z
zhangxingxia 已提交
2553
**返回值:**
H
haonan_7 已提交
2554 2555 2556 2557

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

Z
zhangxingxia 已提交
2559
**示例:**
H
haonan_7 已提交
2560

Z
zhangxingxia 已提交
2561
  ```js
Z
zhuhongtao666 已提交
2562 2563
  let filePath = pathDir +"/test.txt";
  fileio.createWatcher(filePath, 1, function(number){
Z
zhangxingxia 已提交
2564
     console.info("Monitoring times: "+number);
Z
zengyawen 已提交
2565
  });
Z
zhangxingxia 已提交
2566
  
Z
zengyawen 已提交
2567 2568 2569 2570 2571 2572 2573
  ```


## Readout

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

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

Z
zhuhongtao666 已提交
2576
| 名称        | 类型       | 可读   | 可写   | 说明                |
H
HelloCrease 已提交
2577 2578 2579
| --------- | ---------- | ---- | ---- | ----------------- |
| bytesRead | number     | 是    | 是    | 实际读取长度。           |
| offset    | number     | 是    | 是    | 读取数据相对于缓冲区首地址的偏移。 |
Z
zhuhongtao666 已提交
2580
| buffer    | ArrayBuffer | 是    | 是    | 保存读取数据的缓冲区。       |
Z
zengyawen 已提交
2581 2582 2583 2584 2585


## Stat

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

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

Z
zengyawen 已提交
2589
### 属性
Z
zengyawen 已提交
2590

Z
zhuhongtao666 已提交
2591
| 名称     | 类型   | 可读   | 可写   | 说明                                       |
H
HelloCrease 已提交
2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| 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 已提交
2605 2606


Z
zengyawen 已提交
2607
### isBlockDevice
Z
zengyawen 已提交
2608

Z
zengyawen 已提交
2609
isBlockDevice(): boolean
Z
zengyawen 已提交
2610

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

2613 2614
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2615
**返回值:**
H
haonan_7 已提交
2616 2617 2618 2619

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

Z
zhangxingxia 已提交
2621
**示例:**
H
haonan_7 已提交
2622

Z
zhangxingxia 已提交
2623
  ```js
Z
zhuhongtao666 已提交
2624 2625
  let filePath = pathDir + "/test.txt";
  let isBLockDevice = fileio.statSync(filePath).isBlockDevice();
Z
zengyawen 已提交
2626
  ```
Z
zengyawen 已提交
2627 2628


Z
zengyawen 已提交
2629
### isCharacterDevice
Z
zengyawen 已提交
2630

Z
zengyawen 已提交
2631
isCharacterDevice(): boolean
Z
zengyawen 已提交
2632

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

2635 2636
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2637
**返回值:**
H
haonan_7 已提交
2638 2639 2640 2641

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

Z
zhangxingxia 已提交
2643
**示例:**
H
haonan_7 已提交
2644

Z
zhangxingxia 已提交
2645
  ```js
Z
zhuhongtao666 已提交
2646 2647
  let filePath = pathDir + "/test.txt";
  let isCharacterDevice = fileio.statSync(filePath).isCharacterDevice();
Z
zengyawen 已提交
2648
  ```
Z
zengyawen 已提交
2649 2650


Z
zengyawen 已提交
2651
### isDirectory
Z
zengyawen 已提交
2652

Z
zengyawen 已提交
2653
isDirectory(): boolean
Z
zengyawen 已提交
2654

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

2657 2658
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2659
**返回值:**
H
haonan_7 已提交
2660 2661 2662 2663

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

Z
zhangxingxia 已提交
2665
**示例:**
H
haonan_7 已提交
2666

Z
zhangxingxia 已提交
2667
  ```js
Z
zhuhongtao666 已提交
2668 2669
  let dirPath = pathDir + "/test";
  let isDirectory = fileio.statSync(dirPath).isDirectory(); 
Z
zengyawen 已提交
2670
  ```
Z
zengyawen 已提交
2671 2672


Z
zengyawen 已提交
2673
### isFIFO
Z
zengyawen 已提交
2674

Z
zengyawen 已提交
2675
isFIFO(): boolean
Z
zengyawen 已提交
2676

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

2679 2680
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2681
**返回值:**
H
haonan_7 已提交
2682 2683 2684 2685

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

Z
zhangxingxia 已提交
2687
**示例:**
H
haonan_7 已提交
2688

Z
zhangxingxia 已提交
2689
  ```js
Z
zhuhongtao666 已提交
2690 2691
  let filePath = pathDir + "/test.txt";
  let isFIFO = fileio.statSync(filePath).isFIFO(); 
Z
zengyawen 已提交
2692
  ```
Z
zengyawen 已提交
2693 2694


Z
zengyawen 已提交
2695
### isFile
Z
zengyawen 已提交
2696

Z
zengyawen 已提交
2697
isFile(): boolean
Z
zengyawen 已提交
2698

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

2701 2702
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2703
**返回值:**
H
haonan_7 已提交
2704 2705 2706 2707

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

Z
zhangxingxia 已提交
2709
**示例:**
H
haonan_7 已提交
2710

Z
zhangxingxia 已提交
2711
  ```js
Z
zhuhongtao666 已提交
2712 2713
  let filePath = pathDir + "/test.txt";
  let isFile = fileio.statSync(filePath).isFile();
Z
zengyawen 已提交
2714
  ```
Z
zengyawen 已提交
2715 2716


Z
zengyawen 已提交
2717
### isSocket
Z
zengyawen 已提交
2718

Z
zengyawen 已提交
2719
isSocket(): boolean
Z
zengyawen 已提交
2720

Z
zhangxingxia 已提交
2721
用于判断文件是否是套接字。
Z
zengyawen 已提交
2722

2723 2724
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2725
**返回值:**
H
haonan_7 已提交
2726 2727 2728 2729

  | 类型      | 说明             |
  | ------- | -------------- |
  | boolean | 表示文件是否是套接字。 |
Z
zengyawen 已提交
2730

Z
zhangxingxia 已提交
2731
**示例:**
H
haonan_7 已提交
2732

Z
zhangxingxia 已提交
2733
  ```js
Z
zhuhongtao666 已提交
2734 2735
  let filePath = pathDir + "/test.txt";
  let isSocket = fileio.statSync(filePath).isSocket(); 
Z
zengyawen 已提交
2736
  ```
Z
zengyawen 已提交
2737 2738


Z
zengyawen 已提交
2739
### isSymbolicLink
Z
zengyawen 已提交
2740

Z
zengyawen 已提交
2741
isSymbolicLink(): boolean
Z
zengyawen 已提交
2742

Z
zhangxingxia 已提交
2743
用于判断文件是否是符号链接。
Z
zengyawen 已提交
2744

2745 2746
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2747
**返回值:**
H
haonan_7 已提交
2748 2749 2750 2751

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示文件是否是符号链接。 |
Z
zengyawen 已提交
2752

Z
zhangxingxia 已提交
2753
**示例:**
H
haonan_7 已提交
2754

Z
zhangxingxia 已提交
2755
  ```js
Z
zhuhongtao666 已提交
2756 2757
  let filePath = pathDir + "/test";
  let isSymbolicLink = fileio.statSync(filePath).isSymbolicLink(); 
Z
zengyawen 已提交
2758
  ```
Z
zengyawen 已提交
2759 2760


Z
zengyawen 已提交
2761 2762 2763 2764 2765 2766 2767
## Watcher<sup>7+</sup>

Watcher是文件变化监听的实例,调用Watcher.stop()方法(同步或异步)来停止文件监听。


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

2768
stop(): Promise&lt;void&gt;
Z
zengyawen 已提交
2769

H
haonan_7 已提交
2770
关闭watcher监听,使用Promise异步回调。
Z
zengyawen 已提交
2771

2772 2773
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2774
**示例:**
H
haonan_7 已提交
2775

Z
zhangxingxia 已提交
2776
  ```js
Z
zhuhongtao666 已提交
2777 2778
  let filePath = path + "/test.txt";
  let watcher = fileio.createWatcher(filePath, 1, function(number){
Z
zhangxingxia 已提交
2779
      console.info("Monitoring times: "+number);
Z
zhangxingxia 已提交
2780 2781
  });
  watcher.stop().then(function(){
Z
zhangxingxia 已提交
2782
       console.info("close watcher succeed");
Z
zhangxingxia 已提交
2783
  });
Z
zengyawen 已提交
2784 2785 2786 2787 2788
  ```


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

2789
stop(callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
2790

H
haonan_7 已提交
2791
关闭watcher监听,使用callback异步回调。
Z
zengyawen 已提交
2792

2793 2794
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2795
**参数:**
H
haonan_7 已提交
2796 2797 2798 2799

  | 参数名      | 类型                        | 必填   | 说明                     |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | 是    | 以异步方法关闭watcher监听之后的回调。 |
Z
zengyawen 已提交
2800

Z
zhangxingxia 已提交
2801
**示例:**
H
haonan_7 已提交
2802

Z
zhangxingxia 已提交
2803
  ```js
Z
zhuhongtao666 已提交
2804 2805
  let filePath = path +"/test.txt";
  let watcher = fileio.createWatcher(filePath, 1, function(number){
Z
zhangxingxia 已提交
2806
      console.info("Monitoring times: "+number);
Z
zengyawen 已提交
2807
  });
Z
zhangxingxia 已提交
2808 2809 2810
  watcher.stop(function(){
      console.info("close watcher succeed");
  })
Z
zengyawen 已提交
2811 2812 2813
  ```


Z
zhangxingxia 已提交
2814
## Stream
Z
zengyawen 已提交
2815

Z
zengyawen 已提交
2816 2817 2818 2819 2820 2821 2822
文件流,在调用Stream的方法前,需要先通过createStream()方法(同步或异步)来构建一个Stream实例。


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

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

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

2825 2826
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2827
**返回值:**
H
haonan_7 已提交
2828 2829 2830 2831

  | 类型                  | 说明            |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise对象。返回表示异步关闭文件流的结果。 |
Z
zengyawen 已提交
2832

Z
zhangxingxia 已提交
2833
**示例:**
H
haonan_7 已提交
2834

Z
zhangxingxia 已提交
2835
  ```js
Z
zhuhongtao666 已提交
2836 2837
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zhangxingxia 已提交
2838
  ss.close().then(function(){
H
haonan_7 已提交
2839
      console.info("close fileStream succeed");
Z
zhangxingxia 已提交
2840 2841 2842
  }).catch(function(err){
      console.info("close fileStream  failed with error:"+ err);
  });
Z
zengyawen 已提交
2843 2844 2845 2846 2847 2848 2849
  ```


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

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

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

2852 2853
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2854
**参数:**
H
haonan_7 已提交
2855 2856 2857 2858

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

Z
zhangxingxia 已提交
2860
**示例:**
H
haonan_7 已提交
2861

Z
zhangxingxia 已提交
2862
  ```js
Z
zhuhongtao666 已提交
2863 2864
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
2865
  ss.close(function (err) {
Z
zhangxingxia 已提交
2866
      // do something
Z
zengyawen 已提交
2867 2868
  });
  ```
Z
zengyawen 已提交
2869 2870


2871
### closeSync
Z
zengyawen 已提交
2872

Z
zengyawen 已提交
2873
closeSync(): void
Z
zengyawen 已提交
2874

Z
zengyawen 已提交
2875
同步关闭文件流。
Z
zengyawen 已提交
2876

2877 2878
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2879
**示例:**
H
haonan_7 已提交
2880

Z
zhangxingxia 已提交
2881
  ```js
Z
zhuhongtao666 已提交
2882 2883
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
2884 2885
  ss.closeSync();
  ```
Z
zengyawen 已提交
2886 2887


Z
zengyawen 已提交
2888 2889 2890 2891
### flush<sup>7+</sup>

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

H
haonan_7 已提交
2892
刷新文件流,使用Promise异步回调。
Z
zengyawen 已提交
2893

2894 2895
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2896
**返回值:**
H
haonan_7 已提交
2897 2898 2899 2900

  | 类型                  | 说明            |
  | ------------------- | ------------- |
  | Promise&lt;void&gt; | Promise对象。返回表示异步刷新文件流的结果。 |
Z
zengyawen 已提交
2901

Z
zhangxingxia 已提交
2902
**示例:**
H
haonan_7 已提交
2903

Z
zhangxingxia 已提交
2904
  ```js
Z
zhuhongtao666 已提交
2905 2906
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zhangxingxia 已提交
2907
  ss.flush().then(function (){
H
haonan_7 已提交
2908
      console.info("flush succeed");
Z
zhangxingxia 已提交
2909 2910 2911
  }).catch(function(err){
      console.info("flush failed with error:"+ err);
  });
Z
zengyawen 已提交
2912 2913 2914 2915 2916 2917 2918
  ```


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

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

H
haonan_7 已提交
2919
异步刷新文件流,使用callback异步回调。
Z
zengyawen 已提交
2920

2921 2922
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2923
**参数:**
H
haonan_7 已提交
2924 2925 2926 2927

  | 参数名      | 类型                        | 必填   | 说明             |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步刷新文件流后的回调函数。 |
Z
zengyawen 已提交
2928

Z
zhangxingxia 已提交
2929
**示例:**
H
haonan_7 已提交
2930

Z
zhangxingxia 已提交
2931
  ```js
Z
zhuhongtao666 已提交
2932 2933
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
2934
  ss.flush(function (err) {
Z
zhangxingxia 已提交
2935
      // do something
Z
zengyawen 已提交
2936 2937 2938 2939
  });
  ```


Z
zengyawen 已提交
2940
### flushSync<sup>7+</sup>
Z
zengyawen 已提交
2941

Z
zengyawen 已提交
2942
flushSync(): void
Z
zengyawen 已提交
2943

Z
zengyawen 已提交
2944
同步刷新文件流。
Z
zengyawen 已提交
2945

2946 2947
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2948
**示例:**
H
haonan_7 已提交
2949

Z
zhangxingxia 已提交
2950
  ```js
Z
zhuhongtao666 已提交
2951 2952
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
2953 2954
  ss.flushSync();
  ```
Z
zengyawen 已提交
2955 2956


Z
zengyawen 已提交
2957 2958
### write<sup>7+</sup>

Z
zhuhongtao666 已提交
2959
write(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise&lt;number&gt;
Z
zengyawen 已提交
2960

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

2963 2964
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2965
**参数:**
H
haonan_7 已提交
2966 2967 2968

  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
Z
zhuhongtao666 已提交
2969
  | buffer  | ArrayBuffer\|string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
H
haonan_7 已提交
2970
  | 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 已提交
2971

Z
zhangxingxia 已提交
2972
**返回值:**
H
haonan_7 已提交
2973 2974 2975 2976

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

Z
zhangxingxia 已提交
2978
**示例:**
H
haonan_7 已提交
2979

Z
zhangxingxia 已提交
2980
  ```js
Z
zhuhongtao666 已提交
2981 2982
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zhangxingxia 已提交
2983
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
H
haonan_7 已提交
2984
      console.info("write succeed and size is:"+ number);
Z
zhangxingxia 已提交
2985 2986 2987
  }).catch(function(err){
      console.info("write failed with error:"+ err);
  });
Z
zengyawen 已提交
2988 2989 2990 2991 2992
  ```


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

Z
zhuhongtao666 已提交
2993
write(buffer: ArrayBuffer|string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
2994

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

2997 2998
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
2999
**参数:**
H
haonan_7 已提交
3000 3001 3002

  | 参数名   | 类型                            | 必填 | 说明                                                         |
  | -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
Z
zhuhongtao666 已提交
3003
  | buffer   | ArrayBuffer\|string | 是   | 待写入文件的数据,可来自缓冲区或字符串。                     |
H
haonan_7 已提交
3004 3005
  | 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 已提交
3006

Z
zhangxingxia 已提交
3007
**示例:**
H
haonan_7 已提交
3008

Z
zhangxingxia 已提交
3009
  ```js
Z
zhuhongtao666 已提交
3010 3011
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
3012
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
Z
zhangxingxia 已提交
3013
      if (bytesWritten) {
Z
zhangxingxia 已提交
3014
         // do something
H
haonan_7 已提交
3015
         console.info("write succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
3016 3017 3018 3019 3020
      }
  });
  ```


Z
zengyawen 已提交
3021
### writeSync<sup>7+</sup>
Z
zengyawen 已提交
3022

Z
zhuhongtao666 已提交
3023
writeSync(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number
Z
zengyawen 已提交
3024

Z
zengyawen 已提交
3025
以同步方法将数据写入流文件。
Z
zengyawen 已提交
3026

3027 3028
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3029
**参数:**
H
haonan_7 已提交
3030 3031 3032

  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
Z
zhuhongtao666 已提交
3033
  | buffer  | ArrayBuffer\|string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
H
haonan_7 已提交
3034
  | 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 已提交
3035

Z
zhangxingxia 已提交
3036
**返回值:**
H
haonan_7 已提交
3037 3038 3039 3040

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

Z
zhangxingxia 已提交
3042
**示例:**
H
haonan_7 已提交
3043

Z
zhangxingxia 已提交
3044
  ```js
Z
zhuhongtao666 已提交
3045 3046
  let filePath = pathDir + "/test.txt";
  let ss= fileio.createStreamSync(filePath,"r+");
Z
zengyawen 已提交
3047 3048 3049 3050 3051 3052
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


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

Z
zhuhongtao666 已提交
3053
read(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
3054

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

3057 3058
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3059
**参数:**
H
haonan_7 已提交
3060 3061 3062 3063 3064

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

Z
zhangxingxia 已提交
3066
**返回值:**
H
haonan_7 已提交
3067 3068 3069 3070

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

Z
zhangxingxia 已提交
3072
**示例:**
H
haonan_7 已提交
3073

Z
zhangxingxia 已提交
3074
  ```js
Z
zhuhongtao666 已提交
3075 3076
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
3077
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut){
H
haonan_7 已提交
3078
      console.info("read data succeed");
W
wangbo 已提交
3079
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
3080 3081 3082
  }).catch(function(err){
      console.info("read data failed with error:"+ err);
  });
Z
zengyawen 已提交
3083 3084 3085 3086 3087
  ```


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

Z
zhuhongtao666 已提交
3088
read(buffer: ArrayBuffer, options: { position?: number; offset?: number; length?: number; }, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
3089

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

3092 3093
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3094
**参数:**
H
haonan_7 已提交
3095 3096 3097 3098 3099 3100

  | 参数名      | 类型                                       | 必填   | 说明                                       |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | 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 已提交
3101

Z
zhangxingxia 已提交
3102
**示例:**
H
haonan_7 已提交
3103

Z
zhangxingxia 已提交
3104
  ```js
Z
zhuhongtao666 已提交
3105 3106
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
3107
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
Z
zhangxingxia 已提交
3108
      if (readOut) {
H
haonan_7 已提交
3109
          console.info("read data succeed");
Z
zhangxingxia 已提交
3110
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
3111 3112
      }
  });
Z
zengyawen 已提交
3113
  ```
Z
zengyawen 已提交
3114 3115


Z
zengyawen 已提交
3116
### readSync<sup>7+</sup>
Z
zengyawen 已提交
3117

Z
zhuhongtao666 已提交
3118
readSync(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): number
Z
zengyawen 已提交
3119 3120 3121

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

3122 3123
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3124
**参数:**
Z
zhangxingxia 已提交
3125

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

Z
zhangxingxia 已提交
3131
**返回值:**
Z
zhangxingxia 已提交
3132

H
haonan_7 已提交
3133 3134 3135
  | 类型     | 说明       |
  | ------ | -------- |
  | number | 实际读取的长度。 |
Z
zhangxingxia 已提交
3136

Z
zhangxingxia 已提交
3137
**示例:**
H
haonan_7 已提交
3138

Z
zhangxingxia 已提交
3139
  ```js
Z
zhuhongtao666 已提交
3140 3141
  let filePath = pathDir + "/test.txt";
  let ss = fileio.createStreamSync(filePath, "r+");
Z
zengyawen 已提交
3142
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
Z
zengyawen 已提交
3143
  ```
Z
zengyawen 已提交
3144 3145


Z
zengyawen 已提交
3146
## Dir
Z
zengyawen 已提交
3147

3148
管理目录,在调用Dir的方法前,需要先通过opendir方法(同步或异步)来构建一个Dir实例。
Z
zengyawen 已提交
3149 3150 3151 3152 3153 3154


### read

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

H
haonan_7 已提交
3155
读取下一个目录项,使用Promise异步回调。
Z
zengyawen 已提交
3156

3157 3158
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3159
**返回值:**
H
haonan_7 已提交
3160 3161 3162 3163

  | 类型                               | 说明            |
  | -------------------------------- | ------------- |
  | Promise&lt;[Dirent](#dirent)&gt; | Promise对象。返回表示异步读取目录项的结果。 |
Z
zengyawen 已提交
3164

Z
zhangxingxia 已提交
3165
**示例:**
H
haonan_7 已提交
3166

Z
zhangxingxia 已提交
3167 3168
  ```js
  dir.read().then(function (dirent){
Z
zhuhongtao666 已提交
3169
      console.log("read succeed, the name of dirent is " + dirent.name);
Z
zhangxingxia 已提交
3170 3171 3172
  }).catch(function(err){
      console.info("read failed with error:"+ err);
  });
Z
zengyawen 已提交
3173 3174 3175 3176 3177 3178 3179
  ```


### read

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

H
haonan_7 已提交
3180
读取下一个目录项,使用callback异步回调。
Z
zengyawen 已提交
3181

3182 3183
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3184
**参数:**
H
haonan_7 已提交
3185 3186 3187 3188

  | 参数名      | 类型                                     | 必填   | 说明               |
  | -------- | -------------------------------------- | ---- | ---------------- |
  | callback | AsyncCallback&lt;[Dirent](#dirent)&gt; | 是    | 异步读取下一个目录项之后的回调。 |
Z
zengyawen 已提交
3189

Z
zhangxingxia 已提交
3190
**示例:**
H
haonan_7 已提交
3191

Z
zhangxingxia 已提交
3192
  ```js
Z
zengyawen 已提交
3193
  dir.read(function (err, dirent) {
Z
zhangxingxia 已提交
3194
      if (dirent) {
Z
zhangxingxia 已提交
3195
          // do something
Z
zhuhongtao666 已提交
3196
          console.log("read succeed, the name of file is " + dirent.name);
Z
zengyawen 已提交
3197 3198 3199
      }
  });
  ```
Z
zengyawen 已提交
3200 3201


Z
zengyawen 已提交
3202
### readSync
Z
zengyawen 已提交
3203

Z
zengyawen 已提交
3204
readSync(): Dirent
Z
zengyawen 已提交
3205

Z
zengyawen 已提交
3206
同步读取下一个目录项。
Z
zengyawen 已提交
3207

3208 3209
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3210
**返回值:**
H
haonan_7 已提交
3211 3212 3213 3214

  | 类型                | 说明       |
  | ----------------- | -------- |
  | [Dirent](#dirent) | 表示一个目录项。 |
Z
zengyawen 已提交
3215

Z
zhangxingxia 已提交
3216
**示例:**
H
haonan_7 已提交
3217

Z
zhangxingxia 已提交
3218
  ```js
Z
zengyawen 已提交
3219 3220
  let dirent = dir.readSync();
  ```
Z
zengyawen 已提交
3221 3222


W
wangbo 已提交
3223 3224 3225 3226 3227 3228 3229 3230 3231
### close<sup>7+</sup>

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

异步关闭目录,使用promise形式返回结果。目录被关闭后,Dir中持有的文件描述将被释放,后续将无法从Dir中读取目录项。

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

**示例:**
H
haonan_7 已提交
3232

W
wangbo 已提交
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
  ```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 已提交
3249

W
wangbo 已提交
3250 3251 3252 3253 3254 3255 3256
  ```js
  dir.close(function(err){
      console.info("close dir successfully");
  });
  ```


Z
zengyawen 已提交
3257
### closeSync
Z
zengyawen 已提交
3258

Z
zengyawen 已提交
3259
closeSync(): void
Z
zengyawen 已提交
3260

Z
zengyawen 已提交
3261
用于关闭目录。目录被关闭后,Dir中持有的文件描述将被释放,后续将无法从Dir中读取目录项。
Z
zengyawen 已提交
3262

3263 3264
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3265
**示例:**
H
haonan_7 已提交
3266

Z
zhangxingxia 已提交
3267
  ```js
Z
zengyawen 已提交
3268 3269
  dir.closeSync();
  ```
Z
zengyawen 已提交
3270 3271


Z
zengyawen 已提交
3272
## Dirent
Z
zengyawen 已提交
3273

Z
zengyawen 已提交
3274
在调用Dirent的方法前,需要先通过[dir.read()](#read)方法(同步或异步)来构建一个Dirent实例。
Z
zengyawen 已提交
3275

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

Z
zengyawen 已提交
3278
### 属性
Z
zengyawen 已提交
3279

Z
zhuhongtao666 已提交
3280
| 名称   | 类型   | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
3281 3282
| ---- | ------ | ---- | ---- | ------- |
| name | string | 是    | 否    | 目录项的名称。 |
Z
zengyawen 已提交
3283 3284


Z
zengyawen 已提交
3285
### isBlockDevice
Z
zengyawen 已提交
3286

Z
zengyawen 已提交
3287
isBlockDevice(): boolean
Z
zengyawen 已提交
3288

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

3291 3292
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3293
**返回值:**
H
haonan_7 已提交
3294 3295 3296 3297

  | 类型      | 说明               |
  | ------- | ---------------- |
  | boolean | 表示当前目录项是否是块特殊设备。 |
Z
zengyawen 已提交
3298

Z
zhangxingxia 已提交
3299
**示例:**
H
haonan_7 已提交
3300

Z
zhangxingxia 已提交
3301
  ```js
Z
zhuhongtao666 已提交
3302
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3303 3304
  let isBLockDevice = dir.readSync().isBlockDevice();
  ```
Z
zengyawen 已提交
3305 3306


Z
zengyawen 已提交
3307
### isCharacterDevice
Z
zengyawen 已提交
3308

Z
zengyawen 已提交
3309
isCharacterDevice(): boolean
Z
zengyawen 已提交
3310

Z
zhangxingxia 已提交
3311
用于判断当前目录项是否是字符特殊设备。一个字符特殊设备可进行随机访问,且访问的时候不带缓存。
Z
zengyawen 已提交
3312

3313 3314
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3315
**返回值:**
H
haonan_7 已提交
3316 3317 3318 3319

  | 类型      | 说明                |
  | ------- | ----------------- |
  | boolean | 表示当前目录项是否是字符特殊设备。 |
Z
zengyawen 已提交
3320

Z
zhangxingxia 已提交
3321
**示例:**
H
haonan_7 已提交
3322

Z
zhangxingxia 已提交
3323
  ```js
Z
zhuhongtao666 已提交
3324
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3325 3326
  let isCharacterDevice = dir.readSync().isCharacterDevice(); 
  ```
Z
zengyawen 已提交
3327 3328


Z
zengyawen 已提交
3329
### isDirectory
Z
zengyawen 已提交
3330

Z
zengyawen 已提交
3331
isDirectory(): boolean
Z
zengyawen 已提交
3332

Z
zhangxingxia 已提交
3333
用于判断当前目录项是否是目录。
Z
zengyawen 已提交
3334

3335 3336
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3337
**返回值:**
H
haonan_7 已提交
3338 3339 3340 3341

  | 类型      | 说明            |
  | ------- | ------------- |
  | boolean | 表示当前目录项是否是目录。 |
Z
zengyawen 已提交
3342

Z
zhangxingxia 已提交
3343
**示例:**
H
haonan_7 已提交
3344

Z
zhangxingxia 已提交
3345
  ```js
Z
zhuhongtao666 已提交
3346
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3347 3348
  let isDirectory = dir.readSync().isDirectory(); 
  ```
Z
zengyawen 已提交
3349 3350


Z
zengyawen 已提交
3351
### isFIFO
Z
zengyawen 已提交
3352

Z
zengyawen 已提交
3353
isFIFO(): boolean
Z
zengyawen 已提交
3354

Z
zhangxingxia 已提交
3355
用于判断当前目录项是否是命名管道(有时也称为FIFO)。命名管道通常用于进程间通信。
Z
zengyawen 已提交
3356

3357 3358
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3359
**返回值:**
H
haonan_7 已提交
3360 3361 3362 3363

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示当前目录项是否是FIFO。 |
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 isFIFO = dir.readSync().isFIFO(); 
  ```
Z
zengyawen 已提交
3371 3372


Z
zengyawen 已提交
3373
### isFile
Z
zengyawen 已提交
3374

Z
zengyawen 已提交
3375
isFile(): 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 isFile = dir.readSync().isFile(); 
  ```
Z
zengyawen 已提交
3393 3394


Z
zengyawen 已提交
3395
### isSocket
Z
zengyawen 已提交
3396

Z
zengyawen 已提交
3397
isSocket(): 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 isSocket = dir.readSync().isSocket(); 
  ```
Z
zengyawen 已提交
3415 3416


Z
zengyawen 已提交
3417
### isSymbolicLink
Z
zengyawen 已提交
3418

Z
zengyawen 已提交
3419
isSymbolicLink(): boolean
Z
zengyawen 已提交
3420

Z
zhangxingxia 已提交
3421
用于判断当前目录项是否是符号链接。
Z
zengyawen 已提交
3422

3423 3424
**系统能力**:SystemCapability.FileManagement.File.FileIO

Z
zhangxingxia 已提交
3425
**返回值:**
H
haonan_7 已提交
3426 3427 3428 3429

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示当前目录项是否是符号链接。 |
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 isSymbolicLink = dir.readSync().isSymbolicLink();
  ```
Z
zhuhongtao666 已提交
3437

Z
zhuhongtao666 已提交
3438
## Filter<sup>9+</sup>
Z
zhuhongtao666 已提交
3439 3440 3441 3442 3443 3444 3445

**系统接口**:此接口为系统接口。

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

文件过滤器配置项。

Z
zhuhongtao666 已提交
3446
| 名称        | 类型       | 说明                |
Z
zhuhongtao666 已提交
3447 3448 3449 3450 3451 3452 3453
| ----------- | --------------- | ------------------ |
| 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中已有的文件。       |