js-apis-fileio.md 128.7 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.fileio (文件管理)
Z
zengyawen 已提交
2

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

zyjhandsome's avatar
zyjhandsome 已提交
5
> **说明:**
6 7
> - 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
> - 本模块从API version 9开始废弃,建议使用[@ohos.file.fs](js-apis-file-fs.md)替代。
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 24 25
import UIAbility from '@ohos.app.ability.UIAbility';

export default class EntryAbility extends UIAbility {
26 27
    onWindowStageCreate(windowStage) {
        let context = this.context;
Z
zhuhongtao666 已提交
28
        let pathDir = context.filesDir;
29 30 31 32
    }
}
 ```

Z
zengyawen 已提交
33
 Stage模型context的具体获取方法参见[Stage模型](js-apis-inner-application-uiAbilityContext.md)
34

35
**FA模型**
36

W
wangbo 已提交
37 38
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
39
 
W
wangbo 已提交
40
 let context = featureAbility.getContext();
W
wangbo 已提交
41
 context.getFilesDir().then((data) => {
Z
zhuhongtao666 已提交
42
      let pathDir = data;
W
wangbo 已提交
43
 })
Z
zhangxingxia 已提交
44
 ```
zyjhandsome's avatar
zyjhandsome 已提交
45

Z
zhongjianfei 已提交
46
 FA模型context的具体获取方法参见[FA模型](js-apis-inner-app-context.md#Context模块)
Z
zengyawen 已提交
47

Z
zengyawen 已提交
48 49 50 51
## fileio.stat

stat(path: string): Promise<Stat>

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

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

Z
zhangxingxia 已提交
56
**参数:**
Z
zhangxingxia 已提交
57

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

Z
zhangxingxia 已提交
62
**返回值:**
Z
zhangxingxia 已提交
63

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

Z
zhangxingxia 已提交
68
**示例:**
H
haonan_7 已提交
69

Z
zhangxingxia 已提交
70
  ```js
Z
zhuhongtao666 已提交
71
  let filePath = pathDir + "test.txt";
Z
zhuhongtao66 已提交
72
  fileio.stat(filePath).then(function (stat) {
Z
zhuhongtao666 已提交
73
      console.info("getFileInfo succeed, the size of file is " + stat.size);
Z
zhuhongtao66 已提交
74 75
  }).catch(function (err) {
      console.info("getFileInfo failed with error:" + err);
Z
zhangxingxia 已提交
76
  });
Z
zengyawen 已提交
77 78 79 80 81
  ```


## fileio.stat

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

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

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

Z
zhangxingxia 已提交
88
**参数:**
H
haonan_7 已提交
89

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

Z
zhangxingxia 已提交
95
**示例:**
H
haonan_7 已提交
96

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


Z
zengyawen 已提交
104 105
## fileio.statSync

Z
zhuhongtao666 已提交
106
statSync(path: string): Stat
Z
zengyawen 已提交
107 108 109

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

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

Z
zhangxingxia 已提交
112
**参数:**
H
haonan_7 已提交
113

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


Z
zhangxingxia 已提交
119
**返回值:**
H
haonan_7 已提交
120 121 122 123

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

Z
zhangxingxia 已提交
125
**示例:**
H
haonan_7 已提交
126

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


Z
zengyawen 已提交
133
## fileio.opendir
Z
zengyawen 已提交
134

Z
zengyawen 已提交
135
opendir(path: string): Promise<Dir>
Z
zengyawen 已提交
136

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

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

Z
zhangxingxia 已提交
141
**参数:**
H
haonan_7 已提交
142

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

Z
zhangxingxia 已提交
147
**返回值:**
H
haonan_7 已提交
148 149 150 151

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

Z
zhangxingxia 已提交
153
**示例:**
H
haonan_7 已提交
154

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


Z
zengyawen 已提交
165
## fileio.opendir
Z
zengyawen 已提交
166

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

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

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

Z
zhangxingxia 已提交
173
**参数:**
Z
zhangxingxia 已提交
174

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

Z
zhangxingxia 已提交
180
**示例:**
H
haonan_7 已提交
181

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


Z
zengyawen 已提交
190
## fileio.opendirSync
Z
zengyawen 已提交
191

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

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

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

Z
zengyawen 已提交
198

Z
zhangxingxia 已提交
199
**参数:**
Z
zhangxingxia 已提交
200

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

Z
zhangxingxia 已提交
205
**返回值:**
H
haonan_7 已提交
206 207 208 209

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

Z
zhangxingxia 已提交
211
**示例:**
H
haonan_7 已提交
212

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


Z
zengyawen 已提交
220
## fileio.access
Z
zengyawen 已提交
221

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

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

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

Z
zhangxingxia 已提交
228
**参数:**
Z
zhangxingxia 已提交
229

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

Z
zhangxingxia 已提交
235
**返回值:**
H
haonan_7 已提交
236 237 238 239

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

Z
zhangxingxia 已提交
241
**示例:**
H
haonan_7 已提交
242

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


Z
zengyawen 已提交
253
## fileio.access
Z
zengyawen 已提交
254

Z
zhuhongtao666 已提交
255
access(path: string, mode?: number, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
256

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

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

Z
zhangxingxia 已提交
261
**参数:**
H
haonan_7 已提交
262

Z
zengyawen 已提交
263 264 265 266 267
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
268

Z
zhangxingxia 已提交
269
**示例:**
H
haonan_7 已提交
270

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


Z
zengyawen 已提交
279
## fileio.accessSync
Z
zengyawen 已提交
280

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

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

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

Z
zhangxingxia 已提交
287
**参数:**
H
haonan_7 已提交
288

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

Z
zhangxingxia 已提交
294
**示例:**
H
haonan_7 已提交
295

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


Z
zengyawen 已提交
306
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
307

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

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

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

Z
zhangxingxia 已提交
314
**参数:**
H
haonan_7 已提交
315 316 317 318

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

Z
zhangxingxia 已提交
320
**返回值:**
H
haonan_7 已提交
321 322 323 324

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

Z
zhangxingxia 已提交
326
**示例:**
H
haonan_7 已提交
327

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


Z
zengyawen 已提交
339
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
340

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

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

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

Z
zhangxingxia 已提交
347
**参数:**
H
haonan_7 已提交
348 349 350 351 352

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

Z
zhangxingxia 已提交
354
**示例:**
H
haonan_7 已提交
355

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


Z
zengyawen 已提交
365
## fileio.closeSync
Z
zengyawen 已提交
366

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

Z
zengyawen 已提交
369
以同步方法关闭文件。
Z
zengyawen 已提交
370

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

Z
zhangxingxia 已提交
373
**参数:**
H
haonan_7 已提交
374 375 376 377

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

Z
zhangxingxia 已提交
379
**示例:**
H
haonan_7 已提交
380

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


Z
zengyawen 已提交
388
## fileio.copyFile
Z
zengyawen 已提交
389

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

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

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

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

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

Z
zhangxingxia 已提交
404
**返回值:**
H
haonan_7 已提交
405 406 407 408

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

Z
zhangxingxia 已提交
410
**示例:**
H
haonan_7 已提交
411

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


Z
zengyawen 已提交
423
## fileio.copyFile
Z
zengyawen 已提交
424

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

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

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

Z
zhangxingxia 已提交
431
**参数:**
H
haonan_7 已提交
432 433 434

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

Z
zhangxingxia 已提交
440
**示例:**
H
haonan_7 已提交
441

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


Z
zengyawen 已提交
451
## fileio.copyFileSync
Z
zengyawen 已提交
452

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

Z
zengyawen 已提交
455
以同步方法复制文件。
Z
zengyawen 已提交
456

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

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

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

Z
zhangxingxia 已提交
467
**示例:**
H
haonan_7 已提交
468

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


Z
zengyawen 已提交
476
## fileio.mkdir
Z
zengyawen 已提交
477

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

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

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

Z
zhangxingxia 已提交
484
**参数:**
H
haonan_7 已提交
485

Z
zengyawen 已提交
486 487 488 489
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
490

Z
zhangxingxia 已提交
491
**返回值:**
H
haonan_7 已提交
492 493 494 495

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

Z
zhangxingxia 已提交
497
**示例:**
H
haonan_7 已提交
498

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


Z
zengyawen 已提交
509
## fileio.mkdir
Z
zengyawen 已提交
510

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

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

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

Z
zhangxingxia 已提交
517
**参数:**
H
haonan_7 已提交
518

Z
zengyawen 已提交
519 520 521 522 523
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
524

Z
zhangxingxia 已提交
525
**示例:**
H
haonan_7 已提交
526

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


Z
zengyawen 已提交
535
## fileio.mkdirSync
Z
zengyawen 已提交
536

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

Z
zengyawen 已提交
539
以同步方法创建目录。
Z
zengyawen 已提交
540

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

Z
zhangxingxia 已提交
543
**参数:**
H
haonan_7 已提交
544

Z
zengyawen 已提交
545 546 547 548
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
549

Z
zhangxingxia 已提交
550
**示例:**
H
haonan_7 已提交
551

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


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

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

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

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

Z
zhangxingxia 已提交
566
**参数:**
H
haonan_7 已提交
567

Z
zengyawen 已提交
568 569 570
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待打开文件的应用沙箱路径。                                   |
R
raoxian 已提交
571
| 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的方式打开文件。 |
1
18721213663 已提交
572
| mode   | number | 否   | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o660。<br/>-&nbsp;0o660:所有者具有读、写权限,所有用户组具有读、写权限。<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 已提交
573

Z
zhangxingxia 已提交
574
**返回值:**
H
haonan_7 已提交
575 576 577 578

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

Z
zhangxingxia 已提交
580
**示例:**
H
haonan_7 已提交
581

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


Z
zengyawen 已提交
592
## fileio.open<sup>7+</sup>
Z
zengyawen 已提交
593

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

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

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

Z
zhangxingxia 已提交
600
**参数:**
H
haonan_7 已提交
601

Z
zengyawen 已提交
602 603 604
| 参数名   | 类型                            | 必填 | 说明                                                         |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| path     | string                          | 是   | 待打开文件的应用沙箱路径。                                   |
Z
zhuhongtao666 已提交
605
| 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的方式打开文件。 |
1
18721213663 已提交
606
| mode     | number                          | 否   | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o660。<br/>-&nbsp;0o660:所有者具有读、写权限,所有用户组具有读、写权限。<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 已提交
607
| callback | AsyncCallback&lt;number&gt; | 是   | 异步打开文件之后的回调。                                     |
Z
zengyawen 已提交
608

Z
zhangxingxia 已提交
609
**示例:**
H
haonan_7 已提交
610

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


Z
zengyawen 已提交
619
## fileio.openSync
Z
zengyawen 已提交
620

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

Z
zengyawen 已提交
623
以同步方法打开文件。
Z
zengyawen 已提交
624

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

Z
zhangxingxia 已提交
627
**参数:**
H
haonan_7 已提交
628

Z
zengyawen 已提交
629 630 631
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待打开文件的应用沙箱路径。                                   |
R
raoxian 已提交
632
| 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的方式打开文件。 |
1
18721213663 已提交
633
| mode   | number | 否   | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o660。<br/>-&nbsp;0o660:所有者具有读、写权限,所有用户组具有读、写权限。<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 已提交
634

Z
zhangxingxia 已提交
635
**返回值:**
H
haonan_7 已提交
636 637 638 639

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

Z
zhangxingxia 已提交
641
**示例:**
H
haonan_7 已提交
642

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


Z
zengyawen 已提交
658
## fileio.read
Z
zengyawen 已提交
659

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

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

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

Z
zhangxingxia 已提交
666
**参数:**
H
haonan_7 已提交
667

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

Z
zhangxingxia 已提交
674
**返回值:**
Z
zhangxingxia 已提交
675

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

Z
zhangxingxia 已提交
680
**示例:**
H
haonan_7 已提交
681

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


Z
zengyawen 已提交
695
## fileio.read
Z
zengyawen 已提交
696

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

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

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

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

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

Z
zhangxingxia 已提交
712
**示例:**
H
haonan_7 已提交
713

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


Z
zengyawen 已提交
727
## fileio.readSync
Z
zengyawen 已提交
728

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

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

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

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

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

Z
zhangxingxia 已提交
743
**返回值:**
H
haonan_7 已提交
744 745 746 747

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

Z
zhangxingxia 已提交
749
**示例:**
H
haonan_7 已提交
750

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

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

Z
zhangxingxia 已提交
767
**参数:**
H
haonan_7 已提交
768

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

Z
zhangxingxia 已提交
773
**返回值:**
H
haonan_7 已提交
774 775 776 777

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

Z
zhangxingxia 已提交
779
**示例:**
H
haonan_7 已提交
780

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


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

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

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

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

Z
zhangxingxia 已提交
799
**参数:**
H
haonan_7 已提交
800

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

Z
zhangxingxia 已提交
806
**示例:**
H
haonan_7 已提交
807

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


817
## fileio.rmdirSync<sup>7+</sup>
Z
zengyawen 已提交
818

819
rmdirSync(path: string): void
Z
zengyawen 已提交
820 821 822

以同步方法删除目录。

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

Z
zhangxingxia 已提交
825
**参数:**
H
haonan_7 已提交
826

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

Z
zhangxingxia 已提交
831
**示例:**
H
haonan_7 已提交
832

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


## fileio.unlink

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

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

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

Z
zhangxingxia 已提交
847
**参数:**
H
haonan_7 已提交
848

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

Z
zhangxingxia 已提交
853
**返回值:**
H
haonan_7 已提交
854 855 856 857

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

Z
zhangxingxia 已提交
859
**示例:**
H
haonan_7 已提交
860

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


## fileio.unlink

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

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

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

Z
zhangxingxia 已提交
879
**参数:**
H
haonan_7 已提交
880

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

Z
zhangxingxia 已提交
886
**示例:**
H
haonan_7 已提交
887

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


## fileio.unlinkSync

unlinkSync(path: string): void

以同步方法删除文件。

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

Z
zhangxingxia 已提交
904
**参数:**
H
haonan_7 已提交
905

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

Z
zhangxingxia 已提交
910
**示例:**
H
haonan_7 已提交
911

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


## fileio.write

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

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

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

Z
zhangxingxia 已提交
926
**参数:**
H
haonan_7 已提交
927 928 929 930

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

Z
zhangxingxia 已提交
934
**返回值:**
H
haonan_7 已提交
935 936 937 938

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

Z
zhangxingxia 已提交
940
**示例:**
H
haonan_7 已提交
941

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


## fileio.write

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

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

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

Z
zhangxingxia 已提交
961
**参数:**
H
haonan_7 已提交
962 963 964 965

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

Z
zhangxingxia 已提交
970
**示例:**
H
haonan_7 已提交
971

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


## fileio.writeSync

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

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

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

Z
zhangxingxia 已提交
991
**参数:**
H
haonan_7 已提交
992 993 994 995

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

Z
zhangxingxia 已提交
999
**返回值:**
H
haonan_7 已提交
1000 1001 1002 1003

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

Z
zhangxingxia 已提交
1005
**示例:**
H
haonan_7 已提交
1006

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


## fileio.hash

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

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

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

Z
zhangxingxia 已提交
1022
**参数:**
H
haonan_7 已提交
1023

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

Z
zhangxingxia 已提交
1029
**返回值:**
H
haonan_7 已提交
1030 1031 1032 1033

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

Z
zhangxingxia 已提交
1035
**示例:**
H
haonan_7 已提交
1036

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


## fileio.hash

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

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

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

Z
zhangxingxia 已提交
1055
**参数:**
H
haonan_7 已提交
1056

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

Z
zhangxingxia 已提交
1063
**示例:**
H
haonan_7 已提交
1064

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


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

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

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

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

Z
zhangxingxia 已提交
1083
**参数:**
H
haonan_7 已提交
1084

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

Z
zhangxingxia 已提交
1090
**返回值:**
H
haonan_7 已提交
1091 1092 1093 1094

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

Z
zhangxingxia 已提交
1096
**示例:**
H
haonan_7 已提交
1097

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


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

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

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

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

Z
zhangxingxia 已提交
1116
**参数:**
H
haonan_7 已提交
1117

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

Z
zhangxingxia 已提交
1124
**示例:**
H
haonan_7 已提交
1125

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


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

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

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

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

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

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

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

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


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

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

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

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

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

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

Z
zhangxingxia 已提交
1171
**返回值:**
H
haonan_7 已提交
1172 1173 1174 1175

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

Z
zhangxingxia 已提交
1177
**示例:**
H
haonan_7 已提交
1178

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


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

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

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

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

Z
zhangxingxia 已提交
1198
**参数:**
H
haonan_7 已提交
1199 1200 1201 1202 1203

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

Z
zhangxingxia 已提交
1205
**示例:**
H
haonan_7 已提交
1206

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


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

fstatSync(fd: number): Stat

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

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

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

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

Z
zhangxingxia 已提交
1230
**返回值:**
H
haonan_7 已提交
1231 1232 1233 1234

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

Z
zhangxingxia 已提交
1236
**示例:**
H
haonan_7 已提交
1237

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


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

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

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

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

Z
zhangxingxia 已提交
1253
**参数:**
H
haonan_7 已提交
1254 1255 1256 1257 1258

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

Z
zhangxingxia 已提交
1260
**返回值:**
H
haonan_7 已提交
1261 1262 1263 1264

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

Z
zhangxingxia 已提交
1266
**示例:**
H
haonan_7 已提交
1267

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


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

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

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

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

Z
zhangxingxia 已提交
1287
**参数:**
H
haonan_7 已提交
1288 1289 1290 1291

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

Z
zhangxingxia 已提交
1295
**示例:**
H
haonan_7 已提交
1296

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


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

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

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

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

Z
zhangxingxia 已提交
1315
**参数:**
H
haonan_7 已提交
1316 1317 1318 1319 1320

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

Z
zhangxingxia 已提交
1322
**示例:**
H
haonan_7 已提交
1323

Z
zhangxingxia 已提交
1324
  ```js
Z
zhuhongtao666 已提交
1325 1326
  let filePath = pathDir + "/test.txt";
  let fd = fileio.openSync(filePath);
1327
  let len = 5;
Z
zhangxingxia 已提交
1328
  fileio.ftruncateSync(fd, len);
Z
zengyawen 已提交
1329 1330 1331 1332 1333
  ```


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

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

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

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

Z
zhangxingxia 已提交
1340
**参数:**
H
haonan_7 已提交
1341

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

Z
zhangxingxia 已提交
1347
**返回值:**
H
haonan_7 已提交
1348 1349 1350 1351

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

Z
zhangxingxia 已提交
1353
**示例:**
H
haonan_7 已提交
1354

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


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

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

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

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

Z
zhangxingxia 已提交
1374
**参数:**
H
haonan_7 已提交
1375

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

Z
zhangxingxia 已提交
1382
**示例:**
H
haonan_7 已提交
1383

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


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

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

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

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

Z
zhangxingxia 已提交
1401
**参数:**
H
haonan_7 已提交
1402

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

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

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


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

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

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

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

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

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

Z
zhangxingxia 已提交
1432
**返回值:**
H
haonan_7 已提交
1433 1434 1435 1436

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

Z
zhangxingxia 已提交
1438
**示例:**
H
haonan_7 已提交
1439

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


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

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

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

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

Z
zhangxingxia 已提交
1458
**参数:**
H
haonan_7 已提交
1459

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

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

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


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

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

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

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

Z
zhangxingxia 已提交
1484
**参数:**
H
haonan_7 已提交
1485

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

Z
zhangxingxia 已提交
1491
**返回值:**
H
haonan_7 已提交
1492 1493 1494 1495

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

Z
zhangxingxia 已提交
1497
**示例:**
H
haonan_7 已提交
1498

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


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

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

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

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

Z
zhangxingxia 已提交
1513
**参数:**
H
haonan_7 已提交
1514

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

Z
zhangxingxia 已提交
1519
**返回值:**
H
haonan_7 已提交
1520 1521 1522 1523

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

Z
zhangxingxia 已提交
1525
**示例:**
H
haonan_7 已提交
1526

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


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

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

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

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

Z
zhangxingxia 已提交
1545
**参数:**
H
haonan_7 已提交
1546

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

Z
zhangxingxia 已提交
1552
**示例:**
H
haonan_7 已提交
1553

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


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

Z
zhuhongtao666 已提交
1564
lstatSync(path: string): Stat
Z
zengyawen 已提交
1565

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

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

Z
zhangxingxia 已提交
1570
**参数:**
H
haonan_7 已提交
1571

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

Z
zhangxingxia 已提交
1576
**返回值:**
H
haonan_7 已提交
1577 1578 1579 1580

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

Z
zhangxingxia 已提交
1582
**示例:**
H
haonan_7 已提交
1583

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


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

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

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

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

Z
zhangxingxia 已提交
1598
**参数:**
H
haonan_7 已提交
1599

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

Z
zhangxingxia 已提交
1605
**返回值:**
H
haonan_7 已提交
1606 1607 1608 1609

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

Z
zhangxingxia 已提交
1611
**示例:**
H
haonan_7 已提交
1612

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


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

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

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

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

Z
zhangxingxia 已提交
1632
**参数:**
H
haonan_7 已提交
1633

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

Z
zhangxingxia 已提交
1640
**示例:**
H
haonan_7 已提交
1641

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


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

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

以同步方法重命名文件。

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

Z
zhangxingxia 已提交
1658
**参数:**
H
haonan_7 已提交
1659

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

Z
zhangxingxia 已提交
1665
**示例:**
H
haonan_7 已提交
1666

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


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

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

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

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

Z
zhangxingxia 已提交
1682
**参数:**
H
haonan_7 已提交
1683 1684 1685 1686

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

Z
zhangxingxia 已提交
1688
**返回值:**
H
haonan_7 已提交
1689 1690 1691 1692

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

Z
zhangxingxia 已提交
1694
**示例:**
H
haonan_7 已提交
1695

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


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

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

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

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

Z
zhangxingxia 已提交
1715
**参数:**
H
haonan_7 已提交
1716 1717 1718 1719 1720

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

Z
zhangxingxia 已提交
1722
**示例:**
H
haonan_7 已提交
1723

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


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

fsyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1741
**参数:**
H
haonan_7 已提交
1742 1743 1744 1745

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

Z
zhangxingxia 已提交
1747
**示例:**
H
haonan_7 已提交
1748

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


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

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

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

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

Z
zhangxingxia 已提交
1764
**参数:**
H
haonan_7 已提交
1765 1766 1767 1768

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

Z
zhangxingxia 已提交
1770
**返回值:**
H
haonan_7 已提交
1771 1772 1773 1774

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

Z
zhangxingxia 已提交
1776
**示例:**
H
haonan_7 已提交
1777

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


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

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

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

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

Z
zhangxingxia 已提交
1797
**参数:**
H
haonan_7 已提交
1798 1799 1800 1801

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

Z
zhangxingxia 已提交
1804
**示例:**
H
haonan_7 已提交
1805

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


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

fdatasyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1823
**参数:**
H
haonan_7 已提交
1824 1825 1826 1827

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

Z
zhangxingxia 已提交
1829
**示例:**
H
haonan_7 已提交
1830

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


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

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

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

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

Z
zhangxingxia 已提交
1846
**参数:**
H
haonan_7 已提交
1847

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

Z
zhangxingxia 已提交
1853
**返回值:**
H
haonan_7 已提交
1854 1855 1856 1857

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

Z
zhangxingxia 已提交
1859
**示例:**
H
haonan_7 已提交
1860

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


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

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

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

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

Z
zhangxingxia 已提交
1880
**参数:**
H
haonan_7 已提交
1881

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

Z
zhangxingxia 已提交
1888
**示例:**
H
haonan_7 已提交
1889

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


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

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

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

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

Z
zhangxingxia 已提交
1907
**参数:**
H
haonan_7 已提交
1908

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

Z
zhangxingxia 已提交
1914
**示例:**
H
haonan_7 已提交
1915

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


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

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

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

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

Z
zhangxingxia 已提交
1931
**参数:**
H
haonan_7 已提交
1932

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

Z
zhangxingxia 已提交
1939
**返回值:**
H
haonan_7 已提交
1940 1941 1942 1943

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

Z
zhangxingxia 已提交
1945
**示例:**
H
haonan_7 已提交
1946

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


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

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

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

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

Z
zhangxingxia 已提交
1966
**参数:**
H
haonan_7 已提交
1967

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

Z
zhangxingxia 已提交
1975
**示例:**
H
haonan_7 已提交
1976

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


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

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

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

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

Z
zhangxingxia 已提交
1994
**参数:**
H
haonan_7 已提交
1995

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

Z
zhangxingxia 已提交
2002
**示例:**
H
haonan_7 已提交
2003

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


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

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

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

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

Z
zhangxingxia 已提交
2019
**参数:**
H
haonan_7 已提交
2020 2021 2022 2023

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

Z
zhangxingxia 已提交
2025
**返回值:**
H
haonan_7 已提交
2026 2027 2028 2029

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

Z
zhangxingxia 已提交
2031
**示例:**
H
haonan_7 已提交
2032

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


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

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

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

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

Z
zhangxingxia 已提交
2050
**参数:**
H
haonan_7 已提交
2051 2052 2053 2054 2055

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

Z
zhangxingxia 已提交
2057
**示例:**
H
haonan_7 已提交
2058

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


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

mkdtempSync(prefix: string): string

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

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

Z
zhangxingxia 已提交
2074
**参数:**
H
haonan_7 已提交
2075 2076 2077 2078

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

Z
zhangxingxia 已提交
2080
**返回值:**
H
haonan_7 已提交
2081 2082 2083 2084

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

Z
zhangxingxia 已提交
2086
**示例:**
H
haonan_7 已提交
2087

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


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

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

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

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

Z
zhangxingxia 已提交
2101
**参数:**
H
haonan_7 已提交
2102 2103 2104 2105 2106

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

Z
zhangxingxia 已提交
2108
**返回值:**
H
haonan_7 已提交
2109 2110 2111 2112

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

Z
zhangxingxia 已提交
2114
**示例:**
H
haonan_7 已提交
2115

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


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

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

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

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

Z
zhangxingxia 已提交
2136
**参数:**
H
haonan_7 已提交
2137 2138 2139 2140 2141

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

Z
zhangxingxia 已提交
2144
**示例:**
H
haonan_7 已提交
2145

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


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

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

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

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

Z
zhangxingxia 已提交
2164
**参数:**
H
haonan_7 已提交
2165 2166 2167 2168 2169

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

Z
zhangxingxia 已提交
2171
**示例:**
H
haonan_7 已提交
2172

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


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

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

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

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

Z
zhangxingxia 已提交
2189
**参数:**
H
haonan_7 已提交
2190

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

Z
zhangxingxia 已提交
2196
**返回值:**
H
haonan_7 已提交
2197 2198 2199

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

Z
zhangxingxia 已提交
2202
**示例:**
H
haonan_7 已提交
2203

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


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

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

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

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

Z
zhangxingxia 已提交
2222
**参数:**
H
haonan_7 已提交
2223

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

Z
zhangxingxia 已提交
2230
**示例:**
H
haonan_7 已提交
2231

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


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

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

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

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

Z
zhangxingxia 已提交
2248
**参数:**
H
haonan_7 已提交
2249

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

Z
zhangxingxia 已提交
2255
**返回值:**
H
haonan_7 已提交
2256 2257 2258

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

Z
zhangxingxia 已提交
2261
**示例:**
H
haonan_7 已提交
2262

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


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

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

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

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

Z
zhangxingxia 已提交
2277
**参数:**
H
haonan_7 已提交
2278 2279 2280 2281 2282

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

Z
zhangxingxia 已提交
2284
**返回值:**
H
haonan_7 已提交
2285 2286 2287

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

Z
zhangxingxia 已提交
2290
**示例:**
H
haonan_7 已提交
2291

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


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

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

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

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

Z
zhangxingxia 已提交
2311
**参数:**
H
haonan_7 已提交
2312 2313 2314 2315 2316

  | 参数名      | 类型                                       | 必填   | 说明                                       |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | 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 已提交
2317
  | callback | AsyncCallback&lt;[Stream](#stream)&gt; | 是    | 异步打开文件流之后的回调。                            |
Z
zengyawen 已提交
2318

Z
zhangxingxia 已提交
2319
**示例:**
H
haonan_7 已提交
2320

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


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

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

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

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

Z
zhangxingxia 已提交
2338
**参数:**
H
haonan_7 已提交
2339 2340 2341 2342 2343

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

Z
zhangxingxia 已提交
2345
**返回值:**
H
haonan_7 已提交
2346 2347 2348

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

Z
zhangxingxia 已提交
2351
**示例:**
H
haonan_7 已提交
2352

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

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

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

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

Z
zhangxingxia 已提交
2376
**返回值:**
H
haonan_7 已提交
2377 2378 2379 2380

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

Z
zhangxingxia 已提交
2382
**示例:**
H
haonan_7 已提交
2383

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


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

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

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

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

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

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

Z
zhangxingxia 已提交
2413
**示例:**
H
haonan_7 已提交
2414

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


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

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

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

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

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

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

Z
zhangxingxia 已提交
2441
**示例:**
H
haonan_7 已提交
2442

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

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

Z
zhangxingxia 已提交
2459
**参数:**
H
haonan_7 已提交
2460

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

Z
zhangxingxia 已提交
2467
**返回值:**
H
haonan_7 已提交
2468 2469 2470 2471

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

Z
zhangxingxia 已提交
2473
**示例:**
H
haonan_7 已提交
2474

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


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

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

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

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

Z
zhangxingxia 已提交
2494
**参数:**
H
haonan_7 已提交
2495

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

Z
zhangxingxia 已提交
2503
**示例:**
H
haonan_7 已提交
2504

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


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

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

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

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

Z
zhangxingxia 已提交
2522
**参数:**
H
haonan_7 已提交
2523

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

Z
zhangxingxia 已提交
2530
**示例:**
H
haonan_7 已提交
2531

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


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

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

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

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

Z
zhangxingxia 已提交
2547
**参数:**
H
haonan_7 已提交
2548

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

Z
zhangxingxia 已提交
2555
**返回值:**
H
haonan_7 已提交
2556 2557 2558 2559

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

Z
zhangxingxia 已提交
2561
**示例:**
H
haonan_7 已提交
2562

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


## Readout

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

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

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


## Stat

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

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

Z
zengyawen 已提交
2591
### 属性
Z
zengyawen 已提交
2592

Z
zhuhongtao666 已提交
2593
| 名称     | 类型   | 可读   | 可写   | 说明                                       |
H
HelloCrease 已提交
2594 2595 2596
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| dev    | number | 是    | 否    | 标识包含该文件的主设备号。                            |
| ino    | number | 是    | 否    | 标识该文件。通常同设备上的不同文件的INO不同。                 |
Z
zhuhongtao66 已提交
2597
| 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:其他执行,对于普通文件,其余用户可执行文件;对于目录,其他用户组可在目录中搜索给定路径名。 |
H
HelloCrease 已提交
2598 2599 2600 2601 2602 2603 2604 2605 2606
| 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 已提交
2607 2608


Z
zengyawen 已提交
2609
### isBlockDevice
Z
zengyawen 已提交
2610

Z
zengyawen 已提交
2611
isBlockDevice(): boolean
Z
zengyawen 已提交
2612

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

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

Z
zhangxingxia 已提交
2617
**返回值:**
H
haonan_7 已提交
2618 2619 2620 2621

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

Z
zhangxingxia 已提交
2623
**示例:**
H
haonan_7 已提交
2624

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


Z
zengyawen 已提交
2631
### isCharacterDevice
Z
zengyawen 已提交
2632

Z
zengyawen 已提交
2633
isCharacterDevice(): boolean
Z
zengyawen 已提交
2634

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

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

Z
zhangxingxia 已提交
2639
**返回值:**
H
haonan_7 已提交
2640 2641 2642 2643

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

Z
zhangxingxia 已提交
2645
**示例:**
H
haonan_7 已提交
2646

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


Z
zengyawen 已提交
2653
### isDirectory
Z
zengyawen 已提交
2654

Z
zengyawen 已提交
2655
isDirectory(): boolean
Z
zengyawen 已提交
2656

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

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

Z
zhangxingxia 已提交
2661
**返回值:**
H
haonan_7 已提交
2662 2663 2664 2665

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

Z
zhangxingxia 已提交
2667
**示例:**
H
haonan_7 已提交
2668

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


Z
zengyawen 已提交
2675
### isFIFO
Z
zengyawen 已提交
2676

Z
zengyawen 已提交
2677
isFIFO(): boolean
Z
zengyawen 已提交
2678

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

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

Z
zhangxingxia 已提交
2683
**返回值:**
H
haonan_7 已提交
2684 2685 2686 2687

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

Z
zhangxingxia 已提交
2689
**示例:**
H
haonan_7 已提交
2690

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


Z
zengyawen 已提交
2697
### isFile
Z
zengyawen 已提交
2698

Z
zengyawen 已提交
2699
isFile(): boolean
Z
zengyawen 已提交
2700

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

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

Z
zhangxingxia 已提交
2705
**返回值:**
H
haonan_7 已提交
2706 2707 2708 2709

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

Z
zhangxingxia 已提交
2711
**示例:**
H
haonan_7 已提交
2712

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


Z
zengyawen 已提交
2719
### isSocket
Z
zengyawen 已提交
2720

Z
zengyawen 已提交
2721
isSocket(): boolean
Z
zengyawen 已提交
2722

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

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

Z
zhangxingxia 已提交
2727
**返回值:**
H
haonan_7 已提交
2728 2729 2730 2731

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

Z
zhangxingxia 已提交
2733
**示例:**
H
haonan_7 已提交
2734

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


Z
zengyawen 已提交
2741
### isSymbolicLink
Z
zengyawen 已提交
2742

Z
zengyawen 已提交
2743
isSymbolicLink(): boolean
Z
zengyawen 已提交
2744

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

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

Z
zhangxingxia 已提交
2749
**返回值:**
H
haonan_7 已提交
2750 2751 2752 2753

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

Z
zhangxingxia 已提交
2755
**示例:**
H
haonan_7 已提交
2756

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


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

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


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

2770
stop(): Promise&lt;void&gt;
Z
zengyawen 已提交
2771

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

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

Z
zhangxingxia 已提交
2776
**示例:**
H
haonan_7 已提交
2777

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


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

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

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

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

Z
zhangxingxia 已提交
2797
**参数:**
H
haonan_7 已提交
2798 2799 2800 2801

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

Z
zhangxingxia 已提交
2803
**示例:**
H
haonan_7 已提交
2804

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


Z
zhangxingxia 已提交
2816
## Stream
Z
zengyawen 已提交
2817

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


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

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

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

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

Z
zhangxingxia 已提交
2829
**返回值:**
H
haonan_7 已提交
2830 2831 2832 2833

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

Z
zhangxingxia 已提交
2835
**示例:**
H
haonan_7 已提交
2836

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


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

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

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

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

Z
zhangxingxia 已提交
2856
**参数:**
H
haonan_7 已提交
2857 2858 2859 2860

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

Z
zhangxingxia 已提交
2862
**示例:**
H
haonan_7 已提交
2863

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


2873
### closeSync
Z
zengyawen 已提交
2874

Z
zengyawen 已提交
2875
closeSync(): void
Z
zengyawen 已提交
2876

Z
zengyawen 已提交
2877
同步关闭文件流。
Z
zengyawen 已提交
2878

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

Z
zhangxingxia 已提交
2881
**示例:**
H
haonan_7 已提交
2882

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


Z
zengyawen 已提交
2890 2891 2892 2893
### flush<sup>7+</sup>

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

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

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

Z
zhangxingxia 已提交
2898
**返回值:**
H
haonan_7 已提交
2899 2900 2901 2902

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

Z
zhangxingxia 已提交
2904
**示例:**
H
haonan_7 已提交
2905

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


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

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

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

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

Z
zhangxingxia 已提交
2925
**参数:**
H
haonan_7 已提交
2926 2927 2928 2929

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

Z
zhangxingxia 已提交
2931
**示例:**
H
haonan_7 已提交
2932

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


Z
zengyawen 已提交
2942
### flushSync<sup>7+</sup>
Z
zengyawen 已提交
2943

Z
zengyawen 已提交
2944
flushSync(): void
Z
zengyawen 已提交
2945

Z
zengyawen 已提交
2946
同步刷新文件流。
Z
zengyawen 已提交
2947

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

Z
zhangxingxia 已提交
2950
**示例:**
H
haonan_7 已提交
2951

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


Z
zengyawen 已提交
2959 2960
### write<sup>7+</sup>

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

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

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

Z
zhangxingxia 已提交
2967
**参数:**
H
haonan_7 已提交
2968 2969 2970

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

Z
zhangxingxia 已提交
2974
**返回值:**
H
haonan_7 已提交
2975 2976 2977 2978

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

Z
zhangxingxia 已提交
2980
**示例:**
H
haonan_7 已提交
2981

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


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

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

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

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

Z
zhangxingxia 已提交
3001
**参数:**
H
haonan_7 已提交
3002 3003 3004

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

Z
zhangxingxia 已提交
3009
**示例:**
H
haonan_7 已提交
3010

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


Z
zengyawen 已提交
3023
### writeSync<sup>7+</sup>
Z
zengyawen 已提交
3024

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

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

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

Z
zhangxingxia 已提交
3031
**参数:**
H
haonan_7 已提交
3032 3033 3034

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

Z
zhangxingxia 已提交
3038
**返回值:**
H
haonan_7 已提交
3039 3040 3041 3042

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

Z
zhangxingxia 已提交
3044
**示例:**
H
haonan_7 已提交
3045

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


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

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

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

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

Z
zhangxingxia 已提交
3061
**参数:**
H
haonan_7 已提交
3062 3063 3064 3065 3066

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

Z
zhangxingxia 已提交
3068
**返回值:**
H
haonan_7 已提交
3069 3070 3071 3072

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

Z
zhangxingxia 已提交
3074
**示例:**
H
haonan_7 已提交
3075

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


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

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

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

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

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

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

Z
zhangxingxia 已提交
3104
**示例:**
H
haonan_7 已提交
3105

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


Z
zengyawen 已提交
3118
### readSync<sup>7+</sup>
Z
zengyawen 已提交
3119

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

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

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

Z
zhangxingxia 已提交
3126
**参数:**
Z
zhangxingxia 已提交
3127

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

Z
zhangxingxia 已提交
3133
**返回值:**
Z
zhangxingxia 已提交
3134

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

Z
zhangxingxia 已提交
3139
**示例:**
H
haonan_7 已提交
3140

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


Z
zengyawen 已提交
3148
## Dir
Z
zengyawen 已提交
3149

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


### read

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

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

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

Z
zhangxingxia 已提交
3161
**返回值:**
H
haonan_7 已提交
3162 3163 3164 3165

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

Z
zhangxingxia 已提交
3167
**示例:**
H
haonan_7 已提交
3168

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


### read

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

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

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

Z
zhangxingxia 已提交
3186
**参数:**
H
haonan_7 已提交
3187 3188 3189 3190

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

Z
zhangxingxia 已提交
3192
**示例:**
H
haonan_7 已提交
3193

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


Z
zengyawen 已提交
3204
### readSync
Z
zengyawen 已提交
3205

Z
zengyawen 已提交
3206
readSync(): Dirent
Z
zengyawen 已提交
3207

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

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

Z
zhangxingxia 已提交
3212
**返回值:**
H
haonan_7 已提交
3213 3214 3215 3216

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

Z
zhangxingxia 已提交
3218
**示例:**
H
haonan_7 已提交
3219

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


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

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

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

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

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

W
wangbo 已提交
3235
  ```js
Z
zhuhongtao66 已提交
3236
  dir.close().then(function (err) {
W
wangbo 已提交
3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250
      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 已提交
3251

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


Z
zengyawen 已提交
3259
### closeSync
Z
zengyawen 已提交
3260

Z
zengyawen 已提交
3261
closeSync(): void
Z
zengyawen 已提交
3262

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

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

Z
zhangxingxia 已提交
3267
**示例:**
H
haonan_7 已提交
3268

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


Z
zengyawen 已提交
3274
## Dirent
Z
zengyawen 已提交
3275

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

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

Z
zengyawen 已提交
3280
### 属性
Z
zengyawen 已提交
3281

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


Z
zengyawen 已提交
3287
### isBlockDevice
Z
zengyawen 已提交
3288

Z
zengyawen 已提交
3289
isBlockDevice(): boolean
Z
zengyawen 已提交
3290

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

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

Z
zhangxingxia 已提交
3295
**返回值:**
H
haonan_7 已提交
3296 3297 3298 3299

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

Z
zhangxingxia 已提交
3301
**示例:**
H
haonan_7 已提交
3302

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


Z
zengyawen 已提交
3309
### isCharacterDevice
Z
zengyawen 已提交
3310

Z
zengyawen 已提交
3311
isCharacterDevice(): boolean
Z
zengyawen 已提交
3312

Z
zhangxingxia 已提交
3313
用于判断当前目录项是否是字符特殊设备。一个字符特殊设备可进行随机访问,且访问的时候不带缓存。
Z
zengyawen 已提交
3314

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

Z
zhangxingxia 已提交
3317
**返回值:**
H
haonan_7 已提交
3318 3319 3320 3321

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

Z
zhangxingxia 已提交
3323
**示例:**
H
haonan_7 已提交
3324

Z
zhangxingxia 已提交
3325
  ```js
Z
zhuhongtao666 已提交
3326
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3327 3328
  let isCharacterDevice = dir.readSync().isCharacterDevice(); 
  ```
Z
zengyawen 已提交
3329 3330


Z
zengyawen 已提交
3331
### isDirectory
Z
zengyawen 已提交
3332

Z
zengyawen 已提交
3333
isDirectory(): boolean
Z
zengyawen 已提交
3334

Z
zhangxingxia 已提交
3335
用于判断当前目录项是否是目录。
Z
zengyawen 已提交
3336

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

Z
zhangxingxia 已提交
3339
**返回值:**
H
haonan_7 已提交
3340 3341 3342 3343

  | 类型      | 说明            |
  | ------- | ------------- |
  | boolean | 表示当前目录项是否是目录。 |
Z
zengyawen 已提交
3344

Z
zhangxingxia 已提交
3345
**示例:**
H
haonan_7 已提交
3346

Z
zhangxingxia 已提交
3347
  ```js
Z
zhuhongtao666 已提交
3348
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3349 3350
  let isDirectory = dir.readSync().isDirectory(); 
  ```
Z
zengyawen 已提交
3351 3352


Z
zengyawen 已提交
3353
### isFIFO
Z
zengyawen 已提交
3354

Z
zengyawen 已提交
3355
isFIFO(): boolean
Z
zengyawen 已提交
3356

Z
zhangxingxia 已提交
3357
用于判断当前目录项是否是命名管道(有时也称为FIFO)。命名管道通常用于进程间通信。
Z
zengyawen 已提交
3358

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

Z
zhangxingxia 已提交
3361
**返回值:**
H
haonan_7 已提交
3362 3363 3364 3365

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示当前目录项是否是FIFO。 |
Z
zengyawen 已提交
3366

Z
zhangxingxia 已提交
3367
**示例:**
H
haonan_7 已提交
3368

Z
zhangxingxia 已提交
3369
  ```js
Z
zhuhongtao666 已提交
3370
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3371 3372
  let isFIFO = dir.readSync().isFIFO(); 
  ```
Z
zengyawen 已提交
3373 3374


Z
zengyawen 已提交
3375
### isFile
Z
zengyawen 已提交
3376

Z
zengyawen 已提交
3377
isFile(): boolean
Z
zengyawen 已提交
3378

Z
zhangxingxia 已提交
3379
用于判断当前目录项是否是普通文件。
Z
zengyawen 已提交
3380

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

Z
zhangxingxia 已提交
3383
**返回值:**
H
haonan_7 已提交
3384 3385 3386 3387

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示当前目录项是否是普通文件。 |
Z
zengyawen 已提交
3388

Z
zhangxingxia 已提交
3389
**示例:**
H
haonan_7 已提交
3390

Z
zhangxingxia 已提交
3391
  ```js
Z
zhuhongtao666 已提交
3392
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3393 3394
  let isFile = dir.readSync().isFile(); 
  ```
Z
zengyawen 已提交
3395 3396


Z
zengyawen 已提交
3397
### isSocket
Z
zengyawen 已提交
3398

Z
zengyawen 已提交
3399
isSocket(): boolean
Z
zengyawen 已提交
3400

Z
zhangxingxia 已提交
3401
用于判断当前目录项是否是套接字。
Z
zengyawen 已提交
3402

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

Z
zhangxingxia 已提交
3405
**返回值:**
H
haonan_7 已提交
3406 3407 3408 3409

  | 类型      | 说明             |
  | ------- | -------------- |
  | boolean | 表示当前目录项是否是套接字。 |
Z
zengyawen 已提交
3410

Z
zhangxingxia 已提交
3411
**示例:**
H
haonan_7 已提交
3412

Z
zhangxingxia 已提交
3413
  ```js
Z
zhuhongtao666 已提交
3414
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3415 3416
  let isSocket = dir.readSync().isSocket(); 
  ```
Z
zengyawen 已提交
3417 3418


Z
zengyawen 已提交
3419
### isSymbolicLink
Z
zengyawen 已提交
3420

Z
zengyawen 已提交
3421
isSymbolicLink(): boolean
Z
zengyawen 已提交
3422

Z
zhangxingxia 已提交
3423
用于判断当前目录项是否是符号链接。
Z
zengyawen 已提交
3424

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

Z
zhangxingxia 已提交
3427
**返回值:**
H
haonan_7 已提交
3428 3429 3430 3431

  | 类型      | 说明              |
  | ------- | --------------- |
  | boolean | 表示当前目录项是否是符号链接。 |
Z
zengyawen 已提交
3432

Z
zhangxingxia 已提交
3433
**示例:**
H
haonan_7 已提交
3434

Z
zhangxingxia 已提交
3435
  ```js
Z
zhuhongtao666 已提交
3436
  let dir = fileio.opendirSync(pathDir);
Z
zengyawen 已提交
3437 3438
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```