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

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

6 7 8 9 10 11 12
该模块提供文件存储相关的常用功能,向应用程序提供用于IO的JS接口,包括:

- 用于管理文件的基本文件接口
- 用于管理目录的基本目录接口
- 用于获取文件信息的统计接口
- 用于流式读写文件的流式接口

Z
zhangxingxia 已提交
13

Z
zengyawen 已提交
14
## 导入模块
Z
zengyawen 已提交
15

Z
zhangxingxia 已提交
16
```js
Z
zengyawen 已提交
17 18 19
import fileio from '@ohos.fileio';
```

Z
zengyawen 已提交
20 21

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

Z
zengyawen 已提交
23
使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:[Context模块的接口getOrCreateLocalDir](js-apis-Context.md)
Z
zengyawen 已提交
24

Z
zengyawen 已提交
25
“文件/目录应用沙箱路径”=“应用目录路径”+“文件/目录名”
Z
zengyawen 已提交
26

Z
zengyawen 已提交
27
通过上述接口获取到应用目录路径dir,文件名为“xxx.txt”,文件所在应用沙箱路径为:
Z
zengyawen 已提交
28

Z
zhangxingxia 已提交
29
```js
Z
zengyawen 已提交
30
let path = dir + "/xxx.txt";
Z
zengyawen 已提交
31 32
```

Z
zengyawen 已提交
33

Z
zengyawen 已提交
34 35
文件描述符fd:

Z
zengyawen 已提交
36

Z
zhangxingxia 已提交
37
```js
Z
zengyawen 已提交
38 39 40 41
let fd = fileio.openSync(path);
```


Z
zengyawen 已提交
42 43 44 45 46 47
## fileio.stat

stat(path: string): Promise<Stat>

以异步方法获取文件信息,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
50
**参数:**
Z
zhangxingxia 已提交
51

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

Z
zhangxingxia 已提交
56
**返回值:**
Z
zhangxingxia 已提交
57

H
HelloCrease 已提交
58 59
  | 类型                           | 说明         |
  | ---------------------------- | ---------- |
Z
zengyawen 已提交
60
  | Promise<[Stat](#stat)> | 表示文件的具体信息。 |
Z
zhangxingxia 已提交
61

Z
zhangxingxia 已提交
62
**示例:**
Z
zhangxingxia 已提交
63 64 65 66 67 68
  ```js
  fileio.stat(path).then(function(stat){
      console.info("getFileInfo successfully:"+ JSON.stringify(stat));
  }).catch(function(err){
      console.info("getFileInfo failed with error:"+ err);
  });
Z
zengyawen 已提交
69 70 71 72 73 74 75 76 77
  ```


## fileio.stat

stat(path:string, callback:AsyncCallback<Stat>): void

以异步方法获取文件信息,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
80
**参数:**
Z
zengyawen 已提交
81 82 83 84
| 参数名   | 类型                               | 必填 | 说明                           |
| -------- | ---------------------------------- | ---- | ------------------------------ |
| path     | string                             | 是   | 待获取文件的应用沙箱路径。     |
| callback | AsyncCallback<[Stat](#stat)> | 是   | 异步获取文件的信息之后的回调。 |
Z
zengyawen 已提交
85

Z
zhangxingxia 已提交
86
**示例:**
Z
zhangxingxia 已提交
87
  ```js
Z
zengyawen 已提交
88
  fileio.stat(path, function (err, stat) {
Z
zhangxingxia 已提交
89
      // example code in Stat
Z
zengyawen 已提交
90 91 92 93
  });
  ```


Z
zengyawen 已提交
94 95 96
## fileio.statSync

statSync(path:string): Stat
Z
zengyawen 已提交
97 98 99

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

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

Z
zhangxingxia 已提交
102
**参数:**
Z
zengyawen 已提交
103 104 105
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待获取文件的应用沙箱路径。 |
Z
zengyawen 已提交
106 107


Z
zhangxingxia 已提交
108
**返回值:**
H
HelloCrease 已提交
109 110
  | 类型            | 说明         |
  | ------------- | ---------- |
Z
zengyawen 已提交
111 112
  | [Stat](#stat) | 表示文件的具体信息。 |

Z
zhangxingxia 已提交
113
**示例:**
Z
zhangxingxia 已提交
114
  ```js
Z
zengyawen 已提交
115
  let stat = fileio.statSync(path);
Z
zengyawen 已提交
116
  // example code in Stat
Z
zengyawen 已提交
117 118 119
  ```


Z
zengyawen 已提交
120
## fileio.opendir
Z
zengyawen 已提交
121

Z
zengyawen 已提交
122
opendir(path: string): Promise<Dir>
Z
zengyawen 已提交
123

Z
zengyawen 已提交
124
以异步方法打开文件目录,使用promise形式返回结果。
Z
zengyawen 已提交
125

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

Z
zhangxingxia 已提交
128
**参数:**
Z
zengyawen 已提交
129 130 131
| 参数名 | 类型   | 必填 | 说明                           |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | 是   | 待打开文件目录的应用沙箱路径。 |
Z
zengyawen 已提交
132

Z
zhangxingxia 已提交
133
**返回值:**
H
HelloCrease 已提交
134 135
  | 类型                         | 说明       |
  | -------------------------- | -------- |
Z
zengyawen 已提交
136
  | Promise<[Dir](#dir)> | 返回Dir对象。 |
Z
zengyawen 已提交
137

Z
zhangxingxia 已提交
138
**示例:**
Z
zhangxingxia 已提交
139 140 141 142 143 144
  ```js
  fileio.opendir(path).then(function(dir){
      console.info("opendir successfully:"+ JSON.stringify(dir));
  }).catch(function(err){
      console.info("opendir failed with error:"+ err);
  });
Z
zengyawen 已提交
145 146 147
  ```


Z
zengyawen 已提交
148
## fileio.opendir
Z
zengyawen 已提交
149

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

Z
zengyawen 已提交
152
以异步方法打开文件目录,使用callback形式返回结果。
Z
zengyawen 已提交
153

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

Z
zhangxingxia 已提交
156
**参数:**
Z
zhangxingxia 已提交
157

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

Z
zhangxingxia 已提交
163
**示例:**
Z
zhangxingxia 已提交
164
  ```js
Z
zengyawen 已提交
165
  fileio.opendir(path, function (err, dir) { 
Z
zhangxingxia 已提交
166 167
      // example code in Dir struct
      // use read/readSync/close
Z
zengyawen 已提交
168
  });
Z
zengyawen 已提交
169 170 171
  ```


Z
zengyawen 已提交
172
## fileio.opendirSync
Z
zengyawen 已提交
173

Z
zengyawen 已提交
174 175 176
opendirSync(path: string): Dir

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

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

Z
zengyawen 已提交
180

Z
zhangxingxia 已提交
181
**参数:**
Z
zhangxingxia 已提交
182

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

Z
zhangxingxia 已提交
187
**返回值:**
H
HelloCrease 已提交
188 189
  | 类型          | 说明       |
  | ----------- | -------- |
Z
zengyawen 已提交
190
  | [Dir](#dir) | 返回Dir对象。 |
Z
zengyawen 已提交
191

Z
zhangxingxia 已提交
192
**示例:**
Z
zhangxingxia 已提交
193
  ```js
Z
zengyawen 已提交
194 195 196
  let dir = fileio.opendirSync(path);
  // example code in Dir struct
  // use read/readSync/close
Z
zengyawen 已提交
197 198 199
  ```


Z
zengyawen 已提交
200
## fileio.access
Z
zengyawen 已提交
201

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

Z
zengyawen 已提交
204
以异步方法检查当前进程是否可访问某文件,使用promise形式返回结果。
Z
zengyawen 已提交
205

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

Z
zhangxingxia 已提交
208
**参数:**
Z
zhangxingxia 已提交
209

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

Z
zhangxingxia 已提交
215
**返回值:**
H
HelloCrease 已提交
216 217
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
218
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |
Z
zengyawen 已提交
219

Z
zhangxingxia 已提交
220
**示例:**
Z
zhangxingxia 已提交
221 222 223 224 225
  ```js
  fileio.access(path).then(function() {
      console.info("access successfully");
  }).catch(function(err){
      console.info("access failed with error:"+ err);
Z
zengyawen 已提交
226
  });
Z
zengyawen 已提交
227 228 229
  ```


Z
zengyawen 已提交
230
## fileio.access
Z
zengyawen 已提交
231

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

Z
zengyawen 已提交
234
以异步方法检查当前进程是否可访问某文件,使用callback形式返回结果。
Z
zengyawen 已提交
235

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

Z
zhangxingxia 已提交
238
**参数:**
Z
zengyawen 已提交
239 240 241 242 243
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
244

Z
zhangxingxia 已提交
245
**示例:**
Z
zhangxingxia 已提交
246
  ```js
Z
zengyawen 已提交
247
  fileio.access(path, function (err) {
Z
zhangxingxia 已提交
248
      // do something
Z
zengyawen 已提交
249
  });
Z
zengyawen 已提交
250 251 252
  ```


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

Z
zengyawen 已提交
255 256 257
accessSync(path: string, mode?: number): void

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

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

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

Z
zhangxingxia 已提交
267
**示例:**
Z
zhangxingxia 已提交
268
  ```js
Z
zengyawen 已提交
269 270
  try {
      fileio.accessSync(path);
Z
zhangxingxia 已提交
271 272
  } catch(err) {
      console.info("accessSync failed with error:"+ err);
Z
zengyawen 已提交
273
  }
Z
zengyawen 已提交
274 275 276
  ```


Z
zengyawen 已提交
277
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
278

Z
zengyawen 已提交
279
close(fd: number):Promise&lt;void&gt;
Z
zengyawen 已提交
280

Z
zengyawen 已提交
281
以异步方法关闭文件,使用promise形式返回结果。
Z
zengyawen 已提交
282

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

Z
zhangxingxia 已提交
285
**参数:**
H
HelloCrease 已提交
286 287 288
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待关闭文件的文件描述符。 |
Z
zengyawen 已提交
289

Z
zhangxingxia 已提交
290
**返回值:**
H
HelloCrease 已提交
291 292
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
293
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |
Z
zengyawen 已提交
294

Z
zhangxingxia 已提交
295
**示例:**
Z
zhangxingxia 已提交
296
  ```js
Z
zengyawen 已提交
297
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
298 299 300 301 302
  fileio.close(fd).then(function(){
      console.info("close file successfully");
  }).catch(function(err){
      console.info("close file failed with error:"+ err);
  });
Z
zengyawen 已提交
303 304 305
  ```


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

Z
zengyawen 已提交
308
close(fd: number, callback:AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
309

Z
zengyawen 已提交
310
以异步方法关闭文件,使用callback形式返回结果。
Z
zengyawen 已提交
311

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

Z
zhangxingxia 已提交
314
**参数:**
H
HelloCrease 已提交
315 316 317 318
  | 参数名      | 类型                        | 必填   | 说明           |
  | -------- | ------------------------- | ---- | ------------ |
  | fd       | number                    | 是    | 待关闭文件的文件描述符。 |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步关闭文件之后的回调。 |
Z
zengyawen 已提交
319

Z
zhangxingxia 已提交
320
**示例:**
Z
zhangxingxia 已提交
321
  ```js
Z
zengyawen 已提交
322
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
323 324
  fileio.close(fd, function (err) {
      // do something
Z
zengyawen 已提交
325
  });
Z
zengyawen 已提交
326 327 328
  ```


Z
zengyawen 已提交
329
## fileio.closeSync
Z
zengyawen 已提交
330

Z
zengyawen 已提交
331
closeSync(fd: number): void
Z
zengyawen 已提交
332

Z
zengyawen 已提交
333
以同步方法关闭文件。
Z
zengyawen 已提交
334

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

Z
zhangxingxia 已提交
337
**参数:**
H
HelloCrease 已提交
338 339 340
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待关闭文件的文件描述符。 |
Z
zengyawen 已提交
341

Z
zhangxingxia 已提交
342
**示例:**
Z
zhangxingxia 已提交
343
  ```js
Z
zengyawen 已提交
344
  fileio.closeSync(fd);
Z
zengyawen 已提交
345 346 347
  ```


Z
zengyawen 已提交
348
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
349

Z
zengyawen 已提交
350
close(): Promise&lt;void&gt;
Z
zengyawen 已提交
351

Z
zengyawen 已提交
352
以异步方法关闭文件流,使用promise形式返回结果。
Z
zengyawen 已提交
353

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

Z
zhangxingxia 已提交
356
**返回值:**
H
HelloCrease 已提交
357 358
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
359
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |
Z
zengyawen 已提交
360

Z
zhangxingxia 已提交
361
**示例:**
Z
zhangxingxia 已提交
362 363 364 365 366 367
  ```js
  fileio.close().then(function(){
      console.info("close file stream successfully");
  }).catch(function(err){
      console.info("close file stream failed with error:"+ err);
  });
Z
zengyawen 已提交
368 369 370
  ```


Z
zengyawen 已提交
371
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
372

Z
zengyawen 已提交
373
close(callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
374

Z
zengyawen 已提交
375
以异步方法关闭文件流,使用callback形式返回结果。
Z
zengyawen 已提交
376

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

Z
zhangxingxia 已提交
379
**参数:**
H
HelloCrease 已提交
380 381 382
  | 参数名      | 类型                        | 必填   | 说明            |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步关闭文件流之后的回调。 |
Z
zengyawen 已提交
383

Z
zhangxingxia 已提交
384
**示例:**
Z
zhangxingxia 已提交
385 386 387
  ```js
  fileio.close(function(err){
      // do something
Z
zengyawen 已提交
388
  });
Z
zengyawen 已提交
389 390 391
  ```


Z
zengyawen 已提交
392
## fileio.copyFile
Z
zengyawen 已提交
393

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

Z
zengyawen 已提交
396
以异步方法复制文件,使用promise形式返回结果。
Z
zengyawen 已提交
397

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

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

Z
zhangxingxia 已提交
407
**返回值:**
H
HelloCrease 已提交
408 409
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
410
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |
Z
zengyawen 已提交
411

Z
zhangxingxia 已提交
412
**示例:**
Z
zhangxingxia 已提交
413 414 415 416 417 418
  ```js
  fileio.copyFile(src, dest).then(function(){
      console.info("copyFile successfully");
  }).catch(function(err){
      console.info("copyFile failed with error:"+ err);
  });
Z
zengyawen 已提交
419 420 421
  ```


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

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

Z
zengyawen 已提交
426
以异步方法复制文件,使用callback形式返回结果。
Z
zengyawen 已提交
427

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

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

Z
zhangxingxia 已提交
438
**示例:**
Z
zhangxingxia 已提交
439 440 441
  ```js
  fileio.copyFile(src, dest, function (err) {
      // do something
Z
zengyawen 已提交
442
  });
Z
zengyawen 已提交
443 444 445
  ```


Z
zengyawen 已提交
446
## fileio.copyFileSync
Z
zengyawen 已提交
447

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

Z
zengyawen 已提交
450
以同步方法复制文件。
Z
zengyawen 已提交
451

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

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

Z
zhangxingxia 已提交
461
**示例:**
Z
zhangxingxia 已提交
462
  ```js
Z
zengyawen 已提交
463
  fileio.copyFileSync(src, dest);
Z
zengyawen 已提交
464 465 466
  ```


Z
zengyawen 已提交
467
## fileio.mkdir
Z
zengyawen 已提交
468

Z
zengyawen 已提交
469 470 471
mkdir(path:string, mode?: number): Promise&lt;void&gt;

以异步方法创建目录,使用promise形式返回结果。
Z
zengyawen 已提交
472

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

Z
zhangxingxia 已提交
475
**参数:**
Z
zengyawen 已提交
476 477 478 479
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
480

Z
zhangxingxia 已提交
481
**返回值:**
H
HelloCrease 已提交
482 483
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
484
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |
Z
zengyawen 已提交
485

Z
zhangxingxia 已提交
486
**示例:**
Z
zhangxingxia 已提交
487 488 489 490 491 492
  ```js
  fileio.mkdir(path).then(function() {
      console.info("mkdir successfully");
  }).catch(function (error){
      console.info("mkdir failed with error:"+ error);
  });
Z
zengyawen 已提交
493 494 495
  ```


Z
zengyawen 已提交
496
## fileio.mkdir
Z
zengyawen 已提交
497

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

Z
zengyawen 已提交
500
以异步方法创建目录,使用callback形式返回结果。
Z
zengyawen 已提交
501

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

Z
zhangxingxia 已提交
504
**参数:**
Z
zengyawen 已提交
505 506 507 508 509
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
510

Z
zhangxingxia 已提交
511
**示例:**
Z
zhangxingxia 已提交
512 513
  ```js
  fileio.mkdir(path, function(err) {
Z
zhangxingxia 已提交
514
    console.info("mkdir successfully");
Z
zengyawen 已提交
515
  });
Z
zengyawen 已提交
516 517 518
  ```


Z
zengyawen 已提交
519
## fileio.mkdirSync
Z
zengyawen 已提交
520

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

Z
zengyawen 已提交
523
以同步方法创建目录。
Z
zengyawen 已提交
524

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

Z
zhangxingxia 已提交
527
**参数:**
Z
zengyawen 已提交
528 529 530 531
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
532

Z
zhangxingxia 已提交
533
**示例:**
Z
zhangxingxia 已提交
534
  ```js
Z
zengyawen 已提交
535 536 537 538 539 540 541 542 543 544
  fileio.mkdirSync(path);
  ```


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

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

以异步的方法打开文件,使用promise形式返回结果。

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

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

Z
zhangxingxia 已提交
554
**返回值:**
H
HelloCrease 已提交
555 556
  | 类型                    | 说明          |
  | --------------------- | ----------- |
Z
zengyawen 已提交
557
  | Promise&lt;number&gt; | 打开文件的文件描述符。 |
Z
zengyawen 已提交
558

Z
zhangxingxia 已提交
559
**示例:**
Z
zhangxingxia 已提交
560 561 562 563 564 565
  ```js
  fileio.open(path, 0o1, 0o0200).then(function(number){
      console.info("open file successfully");
  }).catch(function(error){
      console.info("open file failed with error:"+ err);
  });
Z
zengyawen 已提交
566 567 568
  ```


Z
zengyawen 已提交
569
## fileio.open<sup>7+</sup>
Z
zengyawen 已提交
570

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

Z
zengyawen 已提交
573
以异步的方法打开文件,使用callback形式返回结果。
Z
zengyawen 已提交
574

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

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

Z
zhangxingxia 已提交
585
**示例:**
Z
zhangxingxia 已提交
586 587 588
  ```js
  fileio.open(path, 0, function(err, fd) {
      // do something
Z
zengyawen 已提交
589
  });
Z
zengyawen 已提交
590 591 592
  ```


Z
zengyawen 已提交
593
## fileio.openSync
Z
zengyawen 已提交
594

Z
zengyawen 已提交
595
openSync(path:string, flags?:number, mode?:number): number
Z
zengyawen 已提交
596

Z
zengyawen 已提交
597
以同步方法打开文件。
Z
zengyawen 已提交
598

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

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

Z
zhangxingxia 已提交
608
**返回值:**
H
HelloCrease 已提交
609 610
  | 类型     | 说明          |
  | ------ | ----------- |
Z
zengyawen 已提交
611
  | number | 打开文件的文件描述符。 |
Z
zengyawen 已提交
612

Z
zhangxingxia 已提交
613
**示例:**
Z
zhangxingxia 已提交
614
  ```js
Z
zengyawen 已提交
615
  let fd = fileio.openSync(path);
Z
zengyawen 已提交
616 617 618
  ```


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

621 622 623 624 625
read(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
626

Z
zengyawen 已提交
627
以异步方法从文件读取数据,使用promise形式返回结果。
Z
zengyawen 已提交
628

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

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

Z
zhangxingxia 已提交
638
**返回值:**
Z
zhangxingxia 已提交
639

H
HelloCrease 已提交
640 641
  | 类型                                 | 说明     |
  | ---------------------------------- | ------ |
642
  | Promise&lt;[ReadOut](#readout)&gt; | 读取的结果。 |
Z
zengyawen 已提交
643

Z
zhangxingxia 已提交
644
**示例:**
Z
zhangxingxia 已提交
645
  ```js
Z
zengyawen 已提交
646 647
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
648
  fileio.read(fd, buf).then(function(readout){
Z
zhangxingxia 已提交
649 650
      console.info("read file data successfully");
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
651 652 653
  }).catch(function(error){
      console.info("read file data failed with error:"+ error);
  });
Z
zengyawen 已提交
654 655 656
  ```


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

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

Z
zengyawen 已提交
665
以异步方法从文件读取数据,使用callback形式返回结果。
Z
zengyawen 已提交
666

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

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

Z
zhangxingxia 已提交
677
**示例:**
Z
zhangxingxia 已提交
678
  ```js
Z
zengyawen 已提交
679 680
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
681
  fileio.read(fd, buf, function (err, readOut) {
Z
zhangxingxia 已提交
682 683 684
      if (readOut) {
          console.info("read file data successfully");
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
685 686
      }
  });
Z
zengyawen 已提交
687
  ```
Z
zengyawen 已提交
688 689


Z
zengyawen 已提交
690
## fileio.readSync
Z
zengyawen 已提交
691

692 693 694 695 696
readSync(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): number
Z
zengyawen 已提交
697 698 699

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

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

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

Z
zhangxingxia 已提交
709
**返回值:**
H
HelloCrease 已提交
710 711
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
712 713
  | number | 实际读取的长度。 |

Z
zhangxingxia 已提交
714
**示例:**
Z
zhangxingxia 已提交
715
  ```js
Z
zengyawen 已提交
716 717 718 719 720 721 722 723 724 725 726 727
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
  let num = fileio.readSync(fd, buf);
  ```


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

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

以异步方法删除目录,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
730
**参数:**
Z
zengyawen 已提交
731 732 733
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除目录的应用沙箱路径。 |
Z
zengyawen 已提交
734

Z
zhangxingxia 已提交
735
**返回值:**
H
HelloCrease 已提交
736 737
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
738 739
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |

Z
zhangxingxia 已提交
740
**示例:**
Z
zhangxingxia 已提交
741 742 743 744 745
  ```js
  fileio.rmdir(path).then(function() {
      console.info("rmdir successfully");
  }).catch(function(err){
      console.info("rmdir failed with error:"+ err);
Z
zengyawen 已提交
746 747 748 749 750 751 752 753 754 755
  });
  ```


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

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

以异步方法删除目录,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
758
**参数:**
Z
zengyawen 已提交
759 760 761 762
| 参数名   | 类型                      | 必填 | 说明                       |
| -------- | ------------------------- | ---- | -------------------------- |
| path     | string                    | 是   | 待删除目录的应用沙箱路径。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步删除目录之后的回调。   |
Z
zengyawen 已提交
763

Z
zhangxingxia 已提交
764
**示例:**
Z
zhangxingxia 已提交
765 766 767
  ```js
  fileio.rmdir(path, function(err){
      // do something
Z
zhangxingxia 已提交
768
      console.info("rmdir successfully");
Z
zengyawen 已提交
769 770 771 772
  });
  ```


773
## fileio.rmdirSync<sup>7+</sup>
Z
zengyawen 已提交
774

775
rmdirSync(path: string): void
Z
zengyawen 已提交
776 777 778

以同步方法删除目录。

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

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

Z
zhangxingxia 已提交
786
**示例:**
Z
zhangxingxia 已提交
787
  ```js
Z
zengyawen 已提交
788 789 790 791 792 793 794 795 796 797
  fileio.rmdirSync(path);
  ```


## fileio.unlink

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

以异步方法删除文件,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
800
**参数:**
Z
zengyawen 已提交
801 802 803
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除文件的应用沙箱路径。 |
Z
zengyawen 已提交
804

Z
zhangxingxia 已提交
805
**返回值:**
H
HelloCrease 已提交
806 807
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
808 809
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |

Z
zhangxingxia 已提交
810
**示例:**
Z
zhangxingxia 已提交
811 812 813 814 815 816
  ```js
  fileio.unlink(path).then(function(){
      console.info("remove file successfully");
  }).catch(function(error){
      console.info("remove file failed with error:"+ error);
  });
Z
zengyawen 已提交
817 818 819 820 821 822 823 824 825
  ```


## fileio.unlink

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

以异步方法删除文件,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
828
**参数:**
Z
zengyawen 已提交
829 830 831 832
| 参数名   | 类型                      | 必填 | 说明                       |
| -------- | ------------------------- | ---- | -------------------------- |
| path     | string                    | 是   | 待删除文件的应用沙箱路径。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步删除文件之后的回调。   |
Z
zengyawen 已提交
833

Z
zhangxingxia 已提交
834
**示例:**
Z
zhangxingxia 已提交
835 836
  ```js
  fileio.unlink(path, function(err) {
Z
zhangxingxia 已提交
837
      console.info("remove file successfully");
Z
zengyawen 已提交
838 839 840 841 842 843 844 845 846 847
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void

以同步方法删除文件。

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

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

Z
zhangxingxia 已提交
855
**示例:**
Z
zhangxingxia 已提交
856
  ```js
Z
zengyawen 已提交
857 858 859 860 861 862
  fileio.unlinkSync(path);
  ```


## fileio.write

863 864 865 866 867 868
write(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
869 870 871

以异步方法将数据写入文件,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
874
**参数:**
H
HelloCrease 已提交
875 876 877 878
  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd      | number                          | 是    | 待写入文件的文件描述符。                             |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
Z
zhangxingxia 已提交
879
  | 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 已提交
880

Z
zhangxingxia 已提交
881
**返回值:**
H
HelloCrease 已提交
882 883
  | 类型                    | 说明       |
  | --------------------- | -------- |
Z
zengyawen 已提交
884 885
  | Promise&lt;number&gt; | 实际写入的长度。 |

Z
zhangxingxia 已提交
886
**示例:**
Z
zhangxingxia 已提交
887
  ```js
Z
zengyawen 已提交
888
  let fd = fileio.openSync(fpath, 0o100 | 0o2, 0o666);
Z
zhangxingxia 已提交
889
  fileio.write(fd, "hello, world").then(function(number){
Z
zhangxingxia 已提交
890
       console.info("write data to file successfully and size is:"+ number);
Z
zhangxingxia 已提交
891 892 893
  }).catch(function(err){
      console.info("write data to file failed with error:"+ err);
  });
Z
zengyawen 已提交
894 895 896 897 898
  ```


## fileio.write

899 900 901 902 903 904
write(fd: number, buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
905 906 907

以异步方法将数据写入文件,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
910
**参数:**
H
HelloCrease 已提交
911 912 913 914
  | 参数名      | 类型                              | 必填   | 说明                                       |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | fd       | number                          | 是    | 待写入文件的文件描述符。                             |
  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
Z
zhangxingxia 已提交
915
  | 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。 |
H
HelloCrease 已提交
916
  | callback | AsyncCallback&lt;number&gt;     | 是    | 异步将数据写入完成后执行的回调函数。                       |
Z
zengyawen 已提交
917

Z
zhangxingxia 已提交
918
**示例:**
Z
zhangxingxia 已提交
919
  ```js
Z
zengyawen 已提交
920 921
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
Z
zhangxingxia 已提交
922 923
      if (bytesWritten) {
         console.info("write data to file successfully and size is:"+ bytesWritten);
Z
zengyawen 已提交
924 925 926 927 928 929 930
      }
  });
  ```


## fileio.writeSync

931 932 933 934 935 936
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
937 938 939

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

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

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

Z
zhangxingxia 已提交
949
**返回值:**
H
HelloCrease 已提交
950 951
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
952 953
  | number | 实际写入的长度。 |

Z
zhangxingxia 已提交
954
**示例:**
Z
zhangxingxia 已提交
955
  ```js
Z
zengyawen 已提交
956 957 958 959 960 961 962 963 964 965 966
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  let num = fileio.writeSync(fd, "hello, world");
  ```


## fileio.hash

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

以异步方法计算文件的哈希值,使用promise形式返回结果。

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

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

Z
zhangxingxia 已提交
975
**返回值:**
H
HelloCrease 已提交
976 977
  | 类型                    | 说明                         |
  | --------------------- | -------------------------- |
Z
zengyawen 已提交
978 979
  | Promise&lt;string&gt; | 文件的哈希值。表示为十六进制数字串,所有字母均大写。 |

Z
zhangxingxia 已提交
980
**示例:**
Z
zhangxingxia 已提交
981 982 983 984 985 986
  ```js
  fileio.hash(path, "sha256").then(function(str){
      console.info("calculate file hash successfully:"+ str);
  }).catch(function(error){
      console.info("calculate file hash failed with error:"+ err);
  });
Z
zengyawen 已提交
987 988 989 990 991
  ```


## fileio.hash

992
hash(path: string, algorithm: string, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
993 994 995

以异步方法计算文件的哈希值,使用callback形式返回结果。

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

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

Z
zhangxingxia 已提交
1005
**示例:**
Z
zhangxingxia 已提交
1006
  ```js
Z
zengyawen 已提交
1007
  fileio.hash(fpath, "sha256", function(err, hashStr) {
Z
zhangxingxia 已提交
1008 1009
      if (hashStr) {
          console.info("calculate file hash successfully:"+ hashStr);
Z
zengyawen 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
      }
  });
  ```


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

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

以异步方法基于文件路径改变文件权限,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1023
**参数:**
Z
zengyawen 已提交
1024 1025 1026 1027
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待改变文件权限的应用沙箱路径。                               |
| 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 已提交
1028

Z
zhangxingxia 已提交
1029
**返回值:**
H
HelloCrease 已提交
1030 1031
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
1032 1033
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |

Z
zhangxingxia 已提交
1034
**示例:**
Z
zhangxingxia 已提交
1035 1036 1037 1038 1039
  ```js
  fileio.chmod(path, mode).then(function() {
      console.info("chmod successfully");
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
  });
  ```


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

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

以异步方法基于文件路径改变文件权限,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
1052
**参数:**
Z
zengyawen 已提交
1053 1054 1055 1056 1057
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| path     | string                    | 是   | 待改变文件权限的应用沙箱路径。                               |
| 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 已提交
1058

Z
zhangxingxia 已提交
1059
**示例:**
Z
zhangxingxia 已提交
1060 1061 1062
  ```js
  fileio.chmod(path, mode, function (err) {
      // do something
Z
zengyawen 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
  });
  ```


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

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

以同步方法基于文件路径改变文件权限。

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

Z
zhangxingxia 已提交
1075
**参数:**
Z
zengyawen 已提交
1076 1077 1078 1079
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待改变文件权限的应用沙箱路径。                               |
| 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 已提交
1080

Z
zhangxingxia 已提交
1081
**示例:**
Z
zhangxingxia 已提交
1082
  ```js
Z
zengyawen 已提交
1083 1084 1085 1086 1087 1088 1089 1090
  fileio.chmodSync(fpath, mode);
  ```


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

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

1091
以异步方法基于文件描述符获取文件信息,使用promise形式返回结果。
Z
zengyawen 已提交
1092

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

Z
zhangxingxia 已提交
1095
**参数:**
H
HelloCrease 已提交
1096 1097 1098
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待获取文件的文件描述符。 |
Z
zengyawen 已提交
1099

Z
zhangxingxia 已提交
1100
**返回值:**
H
HelloCrease 已提交
1101 1102
  | 类型                           | 说明         |
  | ---------------------------- | ---------- |
Z
zengyawen 已提交
1103 1104
  | Promise&lt;[Stat](#stat)&gt; | 表示文件的具体信息。 |

Z
zhangxingxia 已提交
1105
**示例:**
Z
zhangxingxia 已提交
1106 1107 1108 1109 1110 1111
  ```js
  fileio.fstat(fd).then(function(stat){
      console.info("fstat successfully:"+ JSON.stringify(stat));
  }).catch(function(err){
      console.info("fstat failed with error:"+ err);
  });
Z
zengyawen 已提交
1112 1113 1114 1115 1116 1117 1118
  ```


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

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

1119
以异步方法基于文件描述符获取文件信息,使用callback形式返回结果。
Z
zengyawen 已提交
1120

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

Z
zhangxingxia 已提交
1123
**参数:**
H
HelloCrease 已提交
1124 1125 1126
  | 参数名      | 类型                                 | 必填   | 说明               |
  | -------- | ---------------------------------- | ---- | ---------------- |
  | fd       | number                             | 是    | 待获取文件的文件描述符。     |
1127
  | callback | AsyncCallback&lt;[Stat](#stat)&gt; | 是    | 异步获取文件信息之后的回调。 |
Z
zengyawen 已提交
1128

Z
zhangxingxia 已提交
1129
**示例:**
Z
zhangxingxia 已提交
1130
  ```js
Z
zengyawen 已提交
1131
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1132 1133
  fileio.fstat(fd, function (err) {
      // do something
Z
zengyawen 已提交
1134 1135 1136 1137 1138 1139 1140 1141
  });
  ```


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

fstatSync(fd: number): Stat

1142
以同步方法基于文件描述符获取文件信息。
Z
zengyawen 已提交
1143

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

Z
zhangxingxia 已提交
1146
**参数:**
H
HelloCrease 已提交
1147 1148 1149
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待获取文件的文件描述符。 |
Z
zengyawen 已提交
1150

Z
zhangxingxia 已提交
1151
**返回值:**
H
HelloCrease 已提交
1152 1153
  | 类型            | 说明         |
  | ------------- | ---------- |
Z
zengyawen 已提交
1154 1155
  | [Stat](#stat) | 表示文件的具体信息。 |

Z
zhangxingxia 已提交
1156
**示例:**
Z
zhangxingxia 已提交
1157
  ```js
Z
zengyawen 已提交
1158 1159 1160 1161 1162 1163 1164
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```


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

1165
ftruncate(fd: number, len?: number): Promise&lt;void&gt;
Z
zengyawen 已提交
1166 1167 1168

以异步方法基于文件描述符截断文件,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1171
**参数:**
H
HelloCrease 已提交
1172 1173 1174 1175
  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
  | len  | number | 是    | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1176

Z
zhangxingxia 已提交
1177
**返回值:**
H
HelloCrease 已提交
1178 1179
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
1180 1181
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |

Z
zhangxingxia 已提交
1182
**示例:**
Z
zhangxingxia 已提交
1183
  ```js
Z
zengyawen 已提交
1184
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1185 1186 1187 1188
  fileio.ftruncate(fd, 5).then(function(err) {    
      console.info("truncate file successfully");
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
  });
  ```


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

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

以异步方法基于文件描述符截断文件,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
1201
**参数:**
H
HelloCrease 已提交
1202 1203 1204 1205 1206
  | 参数名      | 类型                        | 必填   | 说明               |
  | -------- | ------------------------- | ---- | ---------------- |
  | fd       | number                    | 是    | 待截断文件的文件描述符。     |
  | len      | number                    | 是    | 文件截断后的长度,以字节为单位。 |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步截断文件的信息之后的回调。  |
Z
zengyawen 已提交
1207

Z
zhangxingxia 已提交
1208
**示例:**
Z
zhangxingxia 已提交
1209 1210 1211
  ```js
  fileio.ftruncate(fd, len, function(err){
      // do something
Z
zengyawen 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1224
**参数:**
H
HelloCrease 已提交
1225 1226 1227 1228
  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
  | len  | number | 否    | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1229

Z
zhangxingxia 已提交
1230
**示例:**
Z
zhangxingxia 已提交
1231
  ```js
Z
zhangxingxia 已提交
1232
  fileio.ftruncateSync(fd, len);
Z
zengyawen 已提交
1233 1234 1235 1236 1237
  ```


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

1238
truncate(path: string, len?: number): Promise&lt;void&gt;
Z
zengyawen 已提交
1239 1240 1241

以异步方法基于文件路径截断文件,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1244
**参数:**
Z
zengyawen 已提交
1245 1246 1247 1248
| 参数名 | 类型   | 必填 | 说明                             |
| ------ | ------ | ---- | -------------------------------- |
| path   | string | 是   | 待截断文件的应用沙箱路径。       |
| len    | number | 是   | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1249

Z
zhangxingxia 已提交
1250
**返回值:**
H
HelloCrease 已提交
1251 1252
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
1253 1254
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |

Z
zhangxingxia 已提交
1255
**示例:**
Z
zhangxingxia 已提交
1256 1257 1258 1259 1260
  ```js
  fileio.truncate(path, len).then(function(){
      console.info("truncate file successfully");
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
  });
  ```


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

truncate(path: string, len: number, callback:AsyncCallback&lt;void&gt;): void

以异步方法基于文件路径截断文件,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
1273
**参数:**
Z
zengyawen 已提交
1274 1275 1276 1277 1278
| 参数名   | 类型                      | 必填 | 说明                             |
| -------- | ------------------------- | ---- | -------------------------------- |
| path     | string                    | 是   | 待截断文件的应用沙箱路径。       |
| len      | number                    | 是   | 文件截断后的长度,以字节为单位。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步截断文件的信息之后的回调。   |
Z
zengyawen 已提交
1279

Z
zhangxingxia 已提交
1280
**示例:**
Z
zhangxingxia 已提交
1281 1282 1283
  ```js
  fileio.truncate(path, len, function(err){
      // do something
Z
zengyawen 已提交
1284 1285 1286 1287 1288 1289
  });
  ```


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

1290
truncateSync(path: string, len?: number): void
Z
zengyawen 已提交
1291 1292 1293

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

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

Z
zhangxingxia 已提交
1296
**参数:**
Z
zengyawen 已提交
1297 1298 1299 1300
| 参数名 | 类型   | 必填 | 说明                             |
| ------ | ------ | ---- | -------------------------------- |
| path   | string | 是   | 待截断文件的应用沙箱路径。       |
| len    | number | 否   | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1301

Z
zhangxingxia 已提交
1302
**示例:**
Z
zhangxingxia 已提交
1303
  ```js
Z
zhangxingxia 已提交
1304
  fileio.truncateSync(path, len);
Z
zengyawen 已提交
1305 1306 1307 1308 1309
  ```


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

1310 1311 1312 1313 1314
readText(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): Promise&lt;string&gt;
Z
zengyawen 已提交
1315 1316 1317

以异步方法基于文本方式读取文件(即直接读取文件的文本内容),使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1320
**参数:**
Z
zengyawen 已提交
1321 1322 1323 1324
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1325

Z
zhangxingxia 已提交
1326
**返回值:**
H
HelloCrease 已提交
1327 1328
  | 类型                    | 说明         |
  | --------------------- | ---------- |
Z
zengyawen 已提交
1329 1330
  | Promise&lt;string&gt; | 返回读取文件的内容。 |

Z
zhangxingxia 已提交
1331
**示例:**
Z
zhangxingxia 已提交
1332 1333 1334 1335 1336
  ```js
  fileio.readText(path).then(function(str) {
      console.info("readText successfully:"+ str);
  }).catch(function(err){
      console.info("readText failed with error:"+ err);
Z
zengyawen 已提交
1337 1338 1339 1340 1341 1342
  });
  ```


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

1343 1344 1345 1346 1347
readText(filePath: string, options: {
    position?: number;
    length?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1348 1349 1350

以异步方法基于文本方式读取文件(即直接读取文件的文本内容),使用callback形式返回结果。

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

Z
zhangxingxia 已提交
1353
**参数:**
Z
zengyawen 已提交
1354 1355 1356 1357 1358
| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| 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'。 |
| callback | AsyncCallback&lt;string&gt; | 是   | 异步通过文本方式读取文件之后的回调。                         |
Z
zengyawen 已提交
1359

Z
zhangxingxia 已提交
1360
**示例:**
Z
zhangxingxia 已提交
1361
  ```js
Z
zengyawen 已提交
1362
  fileio.readText(path, function(err, str){
Z
zhangxingxia 已提交
1363
      // do something
Z
zengyawen 已提交
1364 1365 1366 1367 1368 1369
  });
  ```


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

1370 1371 1372 1373 1374
readTextSync(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): string
Z
zengyawen 已提交
1375 1376 1377

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

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

Z
zhangxingxia 已提交
1380
**参数:**
Z
zengyawen 已提交
1381 1382 1383 1384
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1385

Z
zhangxingxia 已提交
1386
**返回值:**
Z
zhangxingxia 已提交
1387 1388 1389
  | 类型   | 说明                 |
  | ------ | -------------------- |
  | string | 返回读取文件的内容。 |
Z
zengyawen 已提交
1390

Z
zhangxingxia 已提交
1391
**示例:**
Z
zhangxingxia 已提交
1392 1393
  ```js
  let str = fileio.readTextSync(path, {position: 1, length: 3});
Z
zengyawen 已提交
1394 1395 1396 1397 1398 1399 1400
  ```


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

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

1401
以异步方法获取链接信息,使用promise形式返回结果。
Z
zengyawen 已提交
1402

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

Z
zhangxingxia 已提交
1405
**参数:**
Z
zengyawen 已提交
1406 1407
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
1408
| path   | string | 是   | 目标文件的应用沙箱路径,指向链接。 |
Z
zengyawen 已提交
1409

Z
zhangxingxia 已提交
1410
**返回值:**
H
HelloCrease 已提交
1411 1412
  | 类型                           | 说明         |
  | ---------------------------- | ---------- |
Z
zengyawen 已提交
1413 1414
  | Promise&lt;[Stat](#stat)&gt; | 表示文件的具体信息。 |

Z
zhangxingxia 已提交
1415
**示例:**
Z
zhangxingxia 已提交
1416 1417 1418 1419 1420 1421
  ```js
  fileio.lstat(path).then(function(stat){
      console.info("get link status successfully:"+ number);
  }).catch(function(err){
      console.info("get link status failed with error:"+ err);
  });
Z
zengyawen 已提交
1422 1423 1424 1425 1426 1427 1428
  ```


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

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

1429
以异步方法获取链接信息,使用callback形式返回结果。
Z
zengyawen 已提交
1430

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

Z
zhangxingxia 已提交
1433
**参数:**
Z
zengyawen 已提交
1434 1435
| 参数名   | 类型                               | 必填 | 说明                                   |
| -------- | ---------------------------------- | ---- | -------------------------------------- |
1436 1437
| path     | string                             | 是   | 目标文件的应用沙箱路径,指向链接。 |
| callback | AsyncCallback&lt;[Stat](#stat)&gt; | 是   | 异步获取链接信息之后的回调。       |
Z
zengyawen 已提交
1438

Z
zhangxingxia 已提交
1439
**示例:**
Z
zhangxingxia 已提交
1440 1441 1442
  ```js
  fileio.lstat(path, function (err, stat) {
      // do something
Z
zengyawen 已提交
1443
  });
Z
zengyawen 已提交
1444 1445 1446 1447 1448 1449 1450
  ```


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

lstatSync(path:string): Stat

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

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

Z
zhangxingxia 已提交
1455
**参数:**
Z
zengyawen 已提交
1456 1457
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
1458
| path   | string | 是   | 目标文件的应用沙箱路径,指向链接。 |
Z
zengyawen 已提交
1459

Z
zhangxingxia 已提交
1460
**返回值:**
H
HelloCrease 已提交
1461 1462
  | 类型            | 说明         |
  | ------------- | ---------- |
Z
zengyawen 已提交
1463 1464
  | [Stat](#stat) | 表示文件的具体信息。 |

Z
zhangxingxia 已提交
1465
**示例:**
Z
zhangxingxia 已提交
1466
  ```js
Z
zengyawen 已提交
1467 1468 1469 1470 1471 1472
  let stat = fileio.lstatSync(path);
  ```


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

1473 1474 1475 1476 1477
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
1478 1479 1480

以异步方法从文件读取数据,使用promise形式返回结果。

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

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

Z
zhangxingxia 已提交
1489
**返回值:**
H
HelloCrease 已提交
1490 1491
  | 类型                                 | 说明     |
  | ---------------------------------- | ------ |
1492
  | Promise&lt;[ReadOut](#readout)&gt; | 读取的结果。 |
Z
zengyawen 已提交
1493

Z
zhangxingxia 已提交
1494
**示例:**
Z
zhangxingxia 已提交
1495 1496
  ```js
  fileio.read(new ArrayBuffer(4096)).then(function(readout){
Z
zhangxingxia 已提交
1497 1498
      console.info("read file data successfully");
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
1499 1500 1501
  }).catch(function(err){
      console.info("read file data failed with error:"+ err);
  });
Z
zengyawen 已提交
1502 1503 1504 1505 1506
  ```


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

1507 1508 1509 1510 1511
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
1512 1513 1514

异步方法从文件读取数据,使用callback形式返回结果。

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

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

Z
zhangxingxia 已提交
1524
**示例:**
Z
zhangxingxia 已提交
1525
  ```js
Z
zengyawen 已提交
1526 1527
  let buf = new ArrayBuffer(4096);
  fileio.read(buf, function (err, readOut) {
Z
zhangxingxia 已提交
1528 1529 1530
      if (readOut) {
          console.info("read file data successfully");
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
      }
  });
  ```


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

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

以异步方法重命名文件,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1544
**参数:**
Z
zengyawen 已提交
1545 1546 1547 1548
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1549

Z
zhangxingxia 已提交
1550
**返回值:**
H
HelloCrease 已提交
1551 1552
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
1553 1554
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |

Z
zhangxingxia 已提交
1555
**示例:**
Z
zhangxingxia 已提交
1556
  ```js
Z
zhangxingxia 已提交
1557
  fileio.rename(oldPath, newPath).then(function() {
Z
zhangxingxia 已提交
1558 1559 1560
      console.info("rename successfully");
  }).catch(function(err){
      console.info("rename failed with error:"+ err);
Z
zengyawen 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
  });
  ```


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

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

以异步方法重命名文件,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
1573
**参数:**
Z
zengyawen 已提交
1574 1575 1576 1577 1578
| 参数名   | 类型                      | 必填 | 说明                         |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | 是   | 目标文件的当前应用沙箱路径。 |
| newPath  | String                    | 是   | 目标文件的新应用沙箱路径。   |
| Callback | AsyncCallback&lt;void&gt; | 是   | 异步重命名文件之后的回调。   |
Z
zengyawen 已提交
1579

Z
zhangxingxia 已提交
1580
**示例:**
Z
zhangxingxia 已提交
1581
  ```js
Z
zhangxingxia 已提交
1582
  fileio.rename(oldPath, newPath, function(err){
Z
zengyawen 已提交
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
  });
  ```


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

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

以同步方法重命名文件。

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

Z
zhangxingxia 已提交
1595
**参数:**
Z
zengyawen 已提交
1596 1597 1598 1599
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1600

Z
zhangxingxia 已提交
1601
**示例:**
Z
zhangxingxia 已提交
1602
  ```js
Z
zhangxingxia 已提交
1603
  fileio.renameSync(oldPath, newPath);
Z
zengyawen 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612
  ```


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

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

以异步方法同步文件数据,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1615
**参数:**
H
HelloCrease 已提交
1616 1617 1618
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1619

Z
zhangxingxia 已提交
1620
**返回值:**
H
HelloCrease 已提交
1621 1622
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
1623 1624
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |

Z
zhangxingxia 已提交
1625
**示例:**
Z
zhangxingxia 已提交
1626 1627 1628 1629 1630 1631
  ```js
  fileio.fsync(fd).then(function(){
      console.info("sync data successfully");
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
  });
Z
zengyawen 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640
  ```


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

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

以异步方法同步文件数据,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
1643
**参数:**
H
HelloCrease 已提交
1644 1645 1646 1647
  | 参数名      | 类型                        | 必填   | 说明              |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | 是    | 待同步文件的文件描述符。    |
  | Callback | AsyncCallback&lt;void&gt; | 是    | 异步将文件数据同步之后的回调。 |
Z
zengyawen 已提交
1648

Z
zhangxingxia 已提交
1649
**示例:**
Z
zhangxingxia 已提交
1650
  ```js
Z
zengyawen 已提交
1651
  fileio.fsync(fd, function(err){
Z
zhangxingxia 已提交
1652
      // do something
Z
zengyawen 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
  });
  ```


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

fsyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1665
**参数:**
H
HelloCrease 已提交
1666 1667 1668
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1669

Z
zhangxingxia 已提交
1670
**示例:**
Z
zhangxingxia 已提交
1671
  ```js
Z
zengyawen 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
  fileio.fyncsSync(fd);
  ```


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

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

以异步方法实现文件内容数据同步,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1684
**参数:**
H
HelloCrease 已提交
1685 1686 1687
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1688

Z
zhangxingxia 已提交
1689
**返回值:**
H
HelloCrease 已提交
1690 1691
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
1692 1693
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果,本调用将返回空值。 |

Z
zhangxingxia 已提交
1694
**示例:**
Z
zhangxingxia 已提交
1695 1696 1697 1698 1699
  ```js
  fileio.fdatasync(fd).then(function(err) {
      console.info("sync data successfully");
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
Z
zengyawen 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
  });
  ```


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

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

以异步方法实现文件内容数据同步,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
1712
**参数:**
H
HelloCrease 已提交
1713 1714 1715 1716
  | 参数名      | 类型                              | 必填   | 说明                |
  | -------- | ------------------------------- | ---- | ----------------- |
  | fd       | number                          | 是    | 待同步文件的文件描述符。      |
  | callback | AsyncCallback&nbsp;&lt;void&gt; | 是    | 异步将文件内容数据同步之后的回调。 |
Z
zengyawen 已提交
1717

Z
zhangxingxia 已提交
1718
**示例:**
Z
zhangxingxia 已提交
1719
  ```js
Z
zengyawen 已提交
1720
  fileio.fdatasync (fd, function (err) {
Z
zhangxingxia 已提交
1721
      // do something
Z
zengyawen 已提交
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
  });
  ```


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

fdatasyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1734
**参数:**
H
HelloCrease 已提交
1735 1736 1737
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1738

Z
zhangxingxia 已提交
1739
**示例:**
Z
zhangxingxia 已提交
1740
  ```js
Z
zengyawen 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

以异步方法基于文件路径创建符号链接,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1753
**参数:**
Z
zengyawen 已提交
1754 1755 1756 1757
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1758

Z
zhangxingxia 已提交
1759
**返回值:**
H
HelloCrease 已提交
1760 1761
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
1762 1763
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果,本调用将返回空值。 |

Z
zhangxingxia 已提交
1764
**示例:**
Z
zhangxingxia 已提交
1765 1766 1767 1768 1769
  ```js
  fileio.symlink(target, srcPath).then(function() {
      console.info("symlink successfully");
  }).catch(function(err){
      console.info("symlink failed with error:"+ err);
Z
zengyawen 已提交
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
  });
  ```


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

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

以异步方法基于文件路径创建符号链接,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
1782
**参数:**
Z
zengyawen 已提交
1783 1784 1785 1786 1787
| 参数名   | 类型                      | 必填 | 说明                             |
| -------- | ------------------------- | ---- | -------------------------------- |
| target   | string                    | 是   | 目标文件的应用沙箱路径。         |
| srcPath  | string                    | 是   | 符号链接文件的应用沙箱路径。     |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步创建符号链接信息之后的回调。 |
Z
zengyawen 已提交
1788

Z
zhangxingxia 已提交
1789
**示例:**
Z
zhangxingxia 已提交
1790 1791 1792
  ```js
  fileio.symlink(target, srcPath, function (err) {
      // do something
Z
zengyawen 已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1805
**参数:**
Z
zengyawen 已提交
1806 1807 1808 1809
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1810

Z
zhangxingxia 已提交
1811
**示例:**
Z
zhangxingxia 已提交
1812
  ```js
Z
zengyawen 已提交
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
  fileio.symlinkSync(target, srcPath);
  ```


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

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

以异步的方法基于文件路径改变文件所有者,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1825
**参数:**
Z
zengyawen 已提交
1826 1827 1828 1829 1830
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID(UserID)。        |
| gid    | number | 是   | 新的GID(GroupID)。       |
Z
zengyawen 已提交
1831

Z
zhangxingxia 已提交
1832
**返回值:**
H
HelloCrease 已提交
1833 1834
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
1835 1836
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果,本调用将返回空值。 |

Z
zhangxingxia 已提交
1837
**示例:**
Z
zhangxingxia 已提交
1838
  ```js
Z
zengyawen 已提交
1839
  let stat = fileio.statSync(path);
Z
zengyawen 已提交
1840
  fileio.chown(path, stat.uid, stat.gid).then(function(){
Z
zhangxingxia 已提交
1841 1842 1843 1844
      console.info("chown successfully");
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
1845 1846 1847 1848 1849 1850 1851 1852 1853
  ```


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

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

以异步的方法基于文件路径改变文件所有者,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
1856
**参数:**
Z
zengyawen 已提交
1857 1858 1859 1860 1861 1862
| 参数名   | 类型                      | 必填 | 说明                           |
| -------- | ------------------------- | ---- | ------------------------------ |
| path     | string                    | 是   | 待改变文件的应用沙箱路径。     |
| uid      | number                    | 是   | 新的UID。                      |
| gid      | number                    | 是   | 新的GID。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
1863

Z
zhangxingxia 已提交
1864
**示例:**
Z
zhangxingxia 已提交
1865
  ```js
Z
zengyawen 已提交
1866 1867
  let stat = fileio.statSync(fpath)
  fileio.chown(path, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
1868
      // do something
Z
zengyawen 已提交
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1881
**参数:**
Z
zengyawen 已提交
1882 1883 1884 1885 1886
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
1887

Z
zhangxingxia 已提交
1888
**示例:**
Z
zhangxingxia 已提交
1889
  ```js
Z
zengyawen 已提交
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
  let stat = fileio.statSync(fpath)
  fileio.chownSync(path, stat.uid, stat.gid);
  ```


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

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

以异步的方法创建临时目录,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1903
**参数:**
H
HelloCrease 已提交
1904 1905 1906
  | 参数名    | 类型     | 必填   | 说明                          |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | 是    | 用随机产生的字符串替换以“XXXXXX”结尾目录路径。 |
Z
zengyawen 已提交
1907

Z
zhangxingxia 已提交
1908
**返回值:**
Z
zhangxingxia 已提交
1909
  | 类型                   | 说明         |
H
HelloCrease 已提交
1910
  | --------------------- | ---------- |
Z
zengyawen 已提交
1911 1912
  | Promise&lt;string&gt; | 生成的唯一目录路径。 |

Z
zhangxingxia 已提交
1913
**示例:**
Z
zhangxingxia 已提交
1914 1915 1916 1917 1918 1919
  ```js
  fileio.mkdtemp(path + "XXXX").then(function(path){
      console.info("mkdtemp successfully:"+ path);
  }).catch(function(err){
      console.info("mkdtemp failed with error:"+ err);
  });
Z
zengyawen 已提交
1920 1921 1922 1923 1924 1925 1926 1927 1928
  ```


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

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

以异步的方法创建临时目录,使用callback形式返回结果。

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

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

Z
zhangxingxia 已提交
1937
**示例:**
Z
zhangxingxia 已提交
1938
  ```js
Z
zengyawen 已提交
1939
  fileio.mkdtemp(path + "XXXX", function (err, res) {
Z
zhangxingxia 已提交
1940
      // do something
Z
zengyawen 已提交
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
  });
  ```


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

mkdtempSync(prefix: string): string

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

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

Z
zhangxingxia 已提交
1953
**参数:**
H
HelloCrease 已提交
1954 1955 1956
  | 参数名    | 类型     | 必填   | 说明                          |
  | ------ | ------ | ---- | --------------------------- |
  | prefix | string | 是    | 用随机产生的字符串替换以“XXXXXX”结尾目录路径。 |
Z
zengyawen 已提交
1957

Z
zhangxingxia 已提交
1958
**返回值:**
Z
zhangxingxia 已提交
1959
  | 类型    | 说明         |
H
HelloCrease 已提交
1960
  | ------ | ---------- |
Z
zengyawen 已提交
1961 1962
  | string | 产生的唯一目录路径。 |

Z
zhangxingxia 已提交
1963
**示例:**
Z
zhangxingxia 已提交
1964
  ```js
Z
zengyawen 已提交
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
  let res = fileio.mkdtempSync(path + "XXXX");
  ```


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

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

以异步方法基于文件描述符改变文件权限,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
1977
**参数:**
H
HelloCrease 已提交
1978 1979 1980 1981
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
1982

Z
zhangxingxia 已提交
1983
**返回值:**
Z
zhangxingxia 已提交
1984
  | 类型                 | 说明                           |
H
HelloCrease 已提交
1985
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
1986 1987
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果,本调用将返回空值。 |

Z
zhangxingxia 已提交
1988
**示例:**
Z
zhangxingxia 已提交
1989 1990 1991 1992 1993
  ```js
  fileio.fchmod(fd, mode).then(function() {
      console.info("chmod successfully");
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
  });
  ```


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

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

以异步方法基于文件描述符改变文件权限,使用callback形式返回结果。

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

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

Z
zhangxingxia 已提交
2013
**示例:**
Z
zhangxingxia 已提交
2014
  ```js
Z
zengyawen 已提交
2015
  fileio.fchmod(fd, mode, function (err) {
Z
zhangxingxia 已提交
2016
      // do something
Z
zengyawen 已提交
2017 2018 2019 2020 2021 2022
  });
  ```


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

2023
fchmodSync(fd: number, mode: number): void
Z
zengyawen 已提交
2024 2025 2026

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

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

Z
zhangxingxia 已提交
2029
**参数:**
H
HelloCrease 已提交
2030 2031 2032 2033
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2034

Z
zhangxingxia 已提交
2035
**示例:**
Z
zhangxingxia 已提交
2036
  ```js
Z
zengyawen 已提交
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
   fileio.fchmodSync(fd, mode);
  ```


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

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

以异步方法基于文件路径打开文件流,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
2049
**参数:**
Z
zengyawen 已提交
2050 2051 2052 2053
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2054

Z
zhangxingxia 已提交
2055
**返回值:**
H
HelloCrease 已提交
2056 2057
  | 类型                                | 说明        |
  | --------------------------------- | --------- |
Z
zengyawen 已提交
2058 2059
  | Promise&lt;[Stream](#stream7)&gt; | 返回文件流的结果。 |

Z
zhangxingxia 已提交
2060
**示例:**
Z
zhangxingxia 已提交
2061 2062 2063 2064 2065 2066
  ```js
  fileio.createStream(path, "r+").then(function(stream){
      console.info("createStream successfully");
  }).catch(function(err){
      console.info("createStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2067 2068 2069 2070 2071 2072 2073 2074 2075
  ```


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

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

以异步方法基于文件路径打开文件流,使用callback形式返回结果。

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

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

Z
zhangxingxia 已提交
2085
**示例:**
Z
zhangxingxia 已提交
2086 2087 2088
  ```js
  fileio.createStream(path, mode, function(err, stream){
      // do something
Z
zengyawen 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2101
**参数:**
Z
zengyawen 已提交
2102 2103 2104 2105
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2106

Z
zhangxingxia 已提交
2107
**返回值:**
Z
zhangxingxia 已提交
2108
  | 类型                | 说明        |
H
HelloCrease 已提交
2109
  | ------------------ | --------- |
Z
zengyawen 已提交
2110 2111
  | [Stream](#stream7) | 返回文件流的结果。 |

Z
zhangxingxia 已提交
2112
**示例:**
Z
zhangxingxia 已提交
2113
  ```js
Z
zengyawen 已提交
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
  let ss = fileio.createStreamSync(path, "r+");
  ```


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

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

以异步方法基于文件描述符打开文件流,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
2126
**参数:**
H
HelloCrease 已提交
2127 2128 2129 2130
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2131

Z
zhangxingxia 已提交
2132
**返回值:**
Z
zhangxingxia 已提交
2133
  | 类型                               | 说明        |
H
HelloCrease 已提交
2134
  | --------------------------------- | --------- |
Z
zengyawen 已提交
2135 2136
  | Promise&lt;[Stream](#stream7)&gt; | 返回文件流的结果。 |

Z
zhangxingxia 已提交
2137
**示例:**
Z
zhangxingxia 已提交
2138 2139
  ```js
  fileio.fdopenStream(fd, mode).then(function(stream){
Z
zengyawen 已提交
2140
      console.info("openStream successfully");
Z
zhangxingxia 已提交
2141 2142 2143
  }).catch(function(err){
      console.info("openStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2144 2145 2146 2147 2148 2149 2150 2151 2152
  ```


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

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

以异步方法基于文件描述符打开文件流,使用callback形式返回结果。

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

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

Z
zhangxingxia 已提交
2162
**示例:**
Z
zhangxingxia 已提交
2163 2164 2165
  ```js
  fileio.fdopenStream(fd, mode, function (err, stream) {
      // do something
Z
zengyawen 已提交
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2178
**参数:**
H
HelloCrease 已提交
2179 2180 2181 2182
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2183

Z
zhangxingxia 已提交
2184
**返回值:**
Z
zhangxingxia 已提交
2185
  | 类型                | 说明        |
H
HelloCrease 已提交
2186
  | ------------------ | --------- |
Z
zengyawen 已提交
2187 2188
  | [Stream](#stream7) | 返回文件流的结果。 |

Z
zhangxingxia 已提交
2189
**示例:**
Z
zhangxingxia 已提交
2190
  ```js
Z
zengyawen 已提交
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
  let ss = fileio.fdopenStreamSync(fd, "r+");
  ```


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

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

以异步方法基于文件描述符改变文件所有者,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
2203
**参数:**
H
HelloCrease 已提交
2204 2205 2206 2207 2208
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2209

Z
zhangxingxia 已提交
2210
**返回值:**
H
HelloCrease 已提交
2211 2212
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
2213 2214
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |

Z
zhangxingxia 已提交
2215
**示例:**
Z
zhangxingxia 已提交
2216
  ```js
Z
zengyawen 已提交
2217
  let stat = fileio.statSync(path);
Z
zhangxingxia 已提交
2218 2219 2220 2221
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
      console.info("chown successfully");
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
Z
zengyawen 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
  });
  ```


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

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

以异步方法基于文件描述符改变文件所有者,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
2234
**参数:**
H
HelloCrease 已提交
2235 2236 2237 2238 2239 2240
  | 参数名      | 类型                        | 必填   | 说明              |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | 是    | 待改变文件的文件描述符。    |
  | uid      | number                    | 是    | 文件所有者的UID。      |
  | gid      | number                    | 是    | 文件所有组的GID。      |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
2241

Z
zhangxingxia 已提交
2242
**示例:**
Z
zhangxingxia 已提交
2243
  ```js
Z
zengyawen 已提交
2244 2245
  let stat = fileio.statSync(fpath);
  fileio.fchown(fd, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2246
      // do something
Z
zengyawen 已提交
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2259
**参数:**
H
HelloCrease 已提交
2260 2261 2262 2263 2264
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2265

Z
zhangxingxia 已提交
2266
**示例:**
Z
zhangxingxia 已提交
2267
  ```js
Z
zengyawen 已提交
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
  let stat = fileio.statSync(fpath);
  fileio.fchownSync(fd, stat.uid, stat.gid);
  ```


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

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

以异步方法基于文件路径改变文件所有者,更改符号链接本身的所有者,而不是符号链接所指向的实际文件,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
2281
**参数:**
Z
zengyawen 已提交
2282 2283 2284 2285 2286
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2287

Z
zhangxingxia 已提交
2288
**返回值:**
H
HelloCrease 已提交
2289 2290
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
Z
zengyawen 已提交
2291 2292
  | Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值。 |

Z
zhangxingxia 已提交
2293
**示例:**
Z
zhangxingxia 已提交
2294
  ```js
Z
zengyawen 已提交
2295
  let stat = fileio.statSync(path);
Z
zhangxingxia 已提交
2296 2297 2298 2299 2300
  fileio.lchown(path, stat.uid, stat.gid).then(function() {
      console.info("chown successfully");
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
2301 2302 2303 2304 2305 2306 2307 2308 2309
  ```


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

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

以异步方法基于文件路径改变文件所有者,更改符号链接本身的所有者,而不是更改符号链接所指向的实际文件,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
2312
**参数:**
Z
zengyawen 已提交
2313 2314 2315 2316 2317 2318
| 参数名   | 类型                      | 必填 | 说明                           |
| -------- | ------------------------- | ---- | ------------------------------ |
| path     | string                    | 是   | 待打开文件的应用沙箱路径。     |
| uid      | number                    | 是   | 新的UID。                      |
| gid      | number                    | 是   | 新的GID。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
2319

Z
zhangxingxia 已提交
2320
**示例:**
Z
zhangxingxia 已提交
2321
  ```js
Z
zengyawen 已提交
2322 2323
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2324
      // do something
Z
zengyawen 已提交
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2337
**参数:**
Z
zengyawen 已提交
2338 2339 2340 2341 2342
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2343

Z
zhangxingxia 已提交
2344
**示例:**
Z
zhangxingxia 已提交
2345
  ```js
Z
zengyawen 已提交
2346 2347 2348 2349 2350 2351 2352 2353 2354
  let stat = fileio.statSync(path);
  fileio.lchownSync(path, stat.uid, stat.gid);
  ```


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

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

Z
zengyawen 已提交
2355
以异步方法监听文件或者目录的变化,使用callback形式返回结果。
Z
zengyawen 已提交
2356

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

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

Z
zhangxingxia 已提交
2366
**返回值:**
Z
zhangxingxia 已提交
2367
  | 类型                  | 说明         |
H
HelloCrease 已提交
2368
  | -------------------- | ---------- |
Z
zengyawen 已提交
2369 2370
  | [Watcher](#watcher7) | 文件变化监听的实例。 |

Z
zhangxingxia 已提交
2371
**示例:**
Z
zhangxingxia 已提交
2372 2373 2374
  ```js
  fileio.createWatcher(filename, events, function(watcher){
      // do something
Z
zengyawen 已提交
2375 2376 2377 2378 2379 2380 2381 2382
  });
  ```


## Readout

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

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

H
HelloCrease 已提交
2385 2386 2387 2388 2389
| 名称        | 参数类型       | 可读   | 可写   | 说明                |
| --------- | ---------- | ---- | ---- | ----------------- |
| bytesRead | number     | 是    | 是    | 实际读取长度。           |
| offset    | number     | 是    | 是    | 读取数据相对于缓冲区首地址的偏移。 |
| buffer    | ArrayBufer | 是    | 是    | 保存读取数据的缓冲区。       |
Z
zengyawen 已提交
2390 2391 2392 2393 2394


## Stat

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

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

Z
zengyawen 已提交
2398
### 属性
Z
zengyawen 已提交
2399

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


Z
zengyawen 已提交
2416
### isBlockDevice
Z
zengyawen 已提交
2417

Z
zengyawen 已提交
2418
isBlockDevice(): boolean
Z
zengyawen 已提交
2419

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

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

Z
zhangxingxia 已提交
2424
**返回值:**
H
HelloCrease 已提交
2425 2426
  | 类型      | 说明               |
  | ------- | ---------------- |
Z
zhangxingxia 已提交
2427
  | boolean | 表示文件是否是块特殊设备。 |
Z
zengyawen 已提交
2428

Z
zhangxingxia 已提交
2429
**示例:**
Z
zhangxingxia 已提交
2430
  ```js
Z
zengyawen 已提交
2431 2432
  let isBLockDevice = fileio.statSync(path).isBlockDevice();
  ```
Z
zengyawen 已提交
2433 2434


Z
zengyawen 已提交
2435
### isCharacterDevice
Z
zengyawen 已提交
2436

Z
zengyawen 已提交
2437
isCharacterDevice(): boolean
Z
zengyawen 已提交
2438

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

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

Z
zhangxingxia 已提交
2443
**返回值:**
H
HelloCrease 已提交
2444 2445
  | 类型      | 说明                |
  | ------- | ----------------- |
Z
zhangxingxia 已提交
2446
  | boolean | 表示文件是否是字符特殊设备。 |
Z
zengyawen 已提交
2447

Z
zhangxingxia 已提交
2448
**示例:**
Z
zhangxingxia 已提交
2449
  ```js
Z
zengyawen 已提交
2450 2451
  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
  ```
Z
zengyawen 已提交
2452 2453


Z
zengyawen 已提交
2454
### isDirectory
Z
zengyawen 已提交
2455

Z
zengyawen 已提交
2456
isDirectory(): boolean
Z
zengyawen 已提交
2457

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

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

Z
zhangxingxia 已提交
2462
**返回值:**
H
HelloCrease 已提交
2463 2464
  | 类型      | 说明            |
  | ------- | ------------- |
Z
zhangxingxia 已提交
2465
  | boolean | 表示文件是否是目录。 |
Z
zengyawen 已提交
2466

Z
zhangxingxia 已提交
2467
**示例:**
Z
zhangxingxia 已提交
2468
  ```js
Z
zengyawen 已提交
2469
  let isDirectory = fileio.statSync(path).isDirectory(); 
Z
zengyawen 已提交
2470
  ```
Z
zengyawen 已提交
2471 2472


Z
zengyawen 已提交
2473
### isFIFO
Z
zengyawen 已提交
2474

Z
zengyawen 已提交
2475
isFIFO(): boolean
Z
zengyawen 已提交
2476

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

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

Z
zhangxingxia 已提交
2481
**返回值:**
H
HelloCrease 已提交
2482 2483
  | 类型      | 说明                    |
  | ------- | --------------------- |
Z
zhangxingxia 已提交
2484
  | boolean | 表示文件是否是&nbsp;FIFO。 |
Z
zengyawen 已提交
2485

Z
zhangxingxia 已提交
2486
**示例:**
Z
zhangxingxia 已提交
2487
  ```js
Z
zengyawen 已提交
2488
  let isFIFO = fileio.statSync(path).isFIFO(); 
Z
zengyawen 已提交
2489
  ```
Z
zengyawen 已提交
2490 2491


Z
zengyawen 已提交
2492
### isFile
Z
zengyawen 已提交
2493

Z
zengyawen 已提交
2494
isFile(): boolean
Z
zengyawen 已提交
2495

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

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

Z
zhangxingxia 已提交
2500
**返回值:**
H
HelloCrease 已提交
2501 2502
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
2503
  | boolean | 表示文件是否是普通文件。 |
Z
zengyawen 已提交
2504

Z
zhangxingxia 已提交
2505
**示例:**
Z
zhangxingxia 已提交
2506
  ```js
Z
zengyawen 已提交
2507
  let isFile = fileio.statSync(fpath).isFile();
Z
zengyawen 已提交
2508
  ```
Z
zengyawen 已提交
2509 2510


Z
zengyawen 已提交
2511
### isSocket
Z
zengyawen 已提交
2512

Z
zengyawen 已提交
2513
isSocket(): boolean
Z
zengyawen 已提交
2514

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

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

Z
zhangxingxia 已提交
2519
**返回值:**
H
HelloCrease 已提交
2520 2521
  | 类型      | 说明             |
  | ------- | -------------- |
Z
zhangxingxia 已提交
2522
  | boolean | 表示文件是否是套接字。 |
Z
zengyawen 已提交
2523

Z
zhangxingxia 已提交
2524
**示例:**
Z
zhangxingxia 已提交
2525
  ```js
Z
zengyawen 已提交
2526 2527
  let isSocket = fileio.statSync(path).isSocket(); 
  ```
Z
zengyawen 已提交
2528 2529


Z
zengyawen 已提交
2530
### isSymbolicLink
Z
zengyawen 已提交
2531

Z
zengyawen 已提交
2532
isSymbolicLink(): boolean
Z
zengyawen 已提交
2533

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

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

Z
zhangxingxia 已提交
2538
**返回值:**
H
HelloCrease 已提交
2539 2540
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
2541
  | boolean | 表示文件是否是符号链接。 |
Z
zengyawen 已提交
2542

Z
zhangxingxia 已提交
2543
**示例:**
Z
zhangxingxia 已提交
2544
  ```js
Z
zengyawen 已提交
2545 2546
  let isSymbolicLink = fileio.statSync(path).isSymbolicLink(); 
  ```
Z
zengyawen 已提交
2547 2548


Z
zengyawen 已提交
2549 2550 2551 2552 2553 2554 2555
## Watcher<sup>7+</sup>

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


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

2556
stop(): Promise&lt;void&gt;
Z
zengyawen 已提交
2557 2558 2559

以异步方法关闭watcher监听,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
2562
**示例:**
Z
zhangxingxia 已提交
2563
  ```js
Z
zengyawen 已提交
2564 2565 2566 2567 2568 2569
  fileio.stop();
  ```


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

2570
stop(callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
2571 2572 2573

以异步方法关闭watcher监听,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
2576
**参数:**
H
HelloCrease 已提交
2577 2578 2579
  | 参数名      | 类型                        | 必填   | 说明                     |
  | -------- | ------------------------- | ---- | ---------------------- |
  | callback | AsyncCallback&lt;void&gt; | 是    | 以异步方法关闭watcher监听之后的回调。 |
Z
zengyawen 已提交
2580

Z
zhangxingxia 已提交
2581
**示例:**
Z
zhangxingxia 已提交
2582
  ```js
Z
zengyawen 已提交
2583
  fileio.stop(function(err){
Z
zhangxingxia 已提交
2584
      // do something
Z
zengyawen 已提交
2585 2586 2587 2588
  });
  ```


Z
zengyawen 已提交
2589
## Stream<sup>7+</sup>
Z
zengyawen 已提交
2590

Z
zengyawen 已提交
2591 2592 2593 2594 2595 2596 2597 2598 2599
文件流,在调用Stream的方法前,需要先通过createStream()方法(同步或异步)来构建一个Stream实例。


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

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

异步关闭文件流,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
2602
**返回值:**
H
HelloCrease 已提交
2603 2604
  | 类型                  | 说明            |
  | ------------------- | ------------- |
Z
zengyawen 已提交
2605 2606
  | Promise&lt;void&gt; | 表示异步关闭文件流的结果。 |

Z
zhangxingxia 已提交
2607
**示例:**
Z
zhangxingxia 已提交
2608
  ```js
Z
zengyawen 已提交
2609
  let ss= fileio.createStreamSync(path);
Z
zhangxingxia 已提交
2610 2611 2612 2613 2614
  ss.close().then(function(){
      console.info("close fileStream successfully");
  }).catch(function(err){
      console.info("close fileStream  failed with error:"+ err);
  });
Z
zengyawen 已提交
2615 2616 2617 2618 2619 2620 2621 2622 2623
  ```


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

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

异步关闭文件流,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
2626
**参数:**
H
HelloCrease 已提交
2627 2628 2629
  | 参数名      | 类型                        | 必填   | 说明            |
  | -------- | ------------------------- | ---- | ------------- |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步关闭文件流之后的回调。 |
Z
zengyawen 已提交
2630

Z
zhangxingxia 已提交
2631
**示例:**
Z
zhangxingxia 已提交
2632
  ```js
Z
zengyawen 已提交
2633 2634
  let ss= fileio.createStreamSync(path);
  ss.close(function (err) {
Z
zhangxingxia 已提交
2635
      // do something
Z
zengyawen 已提交
2636 2637
  });
  ```
Z
zengyawen 已提交
2638 2639


2640
### closeSync
Z
zengyawen 已提交
2641

Z
zengyawen 已提交
2642
closeSync(): void
Z
zengyawen 已提交
2643

Z
zengyawen 已提交
2644
同步关闭文件流。
Z
zengyawen 已提交
2645

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

Z
zhangxingxia 已提交
2648
**示例:**
Z
zhangxingxia 已提交
2649
  ```js
Z
zengyawen 已提交
2650 2651 2652
  let ss= fileio.createStreamSync(path);
  ss.closeSync();
  ```
Z
zengyawen 已提交
2653 2654


Z
zengyawen 已提交
2655 2656 2657 2658 2659 2660
### flush<sup>7+</sup>

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

异步刷新文件流,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
2663
**返回值:**
H
HelloCrease 已提交
2664 2665
  | 类型                  | 说明            |
  | ------------------- | ------------- |
Z
zengyawen 已提交
2666 2667
  | Promise&lt;void&gt; | 表示异步刷新文件流的结果。 |

Z
zhangxingxia 已提交
2668
**示例:**
Z
zhangxingxia 已提交
2669
  ```js
Z
zengyawen 已提交
2670
  let ss= fileio.createStreamSync(path);
Z
zhangxingxia 已提交
2671 2672 2673 2674 2675
  ss.flush().then(function (){
      console.info("flush successfully");
  }).catch(function(err){
      console.info("flush failed with error:"+ err);
  });
Z
zengyawen 已提交
2676 2677 2678 2679 2680 2681 2682 2683 2684
  ```


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

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

异步刷新文件流,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
2687
**参数:**
H
HelloCrease 已提交
2688 2689 2690
  | 参数名      | 类型                        | 必填   | 说明             |
  | -------- | ------------------------- | ---- | -------------- |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步刷新文件流后的回调函数。 |
Z
zengyawen 已提交
2691

Z
zhangxingxia 已提交
2692
**示例:**
Z
zhangxingxia 已提交
2693
  ```js
Z
zengyawen 已提交
2694 2695
  let ss= fileio.createStreamSync(path);
  ss.flush(function (err) {
Z
zhangxingxia 已提交
2696
      // do something
Z
zengyawen 已提交
2697 2698 2699 2700
  });
  ```


Z
zengyawen 已提交
2701
### flushSync<sup>7+</sup>
Z
zengyawen 已提交
2702

Z
zengyawen 已提交
2703
flushSync(): void
Z
zengyawen 已提交
2704

Z
zengyawen 已提交
2705
同步刷新文件流。
Z
zengyawen 已提交
2706

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

Z
zhangxingxia 已提交
2709
**示例:**
Z
zhangxingxia 已提交
2710
  ```js
Z
zengyawen 已提交
2711 2712 2713
  let ss= fileio.createStreamSync(path);
  ss.flushSync();
  ```
Z
zengyawen 已提交
2714 2715


Z
zengyawen 已提交
2716 2717
### write<sup>7+</sup>

2718 2719 2720 2721 2722 2723
write(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
2724 2725 2726

以异步方法将数据写入流文件,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
2729
**参数:**
H
HelloCrease 已提交
2730 2731 2732
  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
Z
zhangxingxia 已提交
2733
  | 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 已提交
2734

Z
zhangxingxia 已提交
2735
**返回值:**
H
HelloCrease 已提交
2736 2737
  | 类型                    | 说明       |
  | --------------------- | -------- |
Z
zengyawen 已提交
2738 2739
  | Promise&lt;number&gt; | 实际写入的长度。 |

Z
zhangxingxia 已提交
2740
**示例:**
Z
zhangxingxia 已提交
2741
  ```js
Z
zengyawen 已提交
2742
  let ss= fileio.createStreamSync(fpath, "r+");
Z
zhangxingxia 已提交
2743
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
Z
zhangxingxia 已提交
2744
      console.info("write successfully and size is:"+ number);
Z
zhangxingxia 已提交
2745 2746 2747
  }).catch(function(err){
      console.info("write failed with error:"+ err);
  });
Z
zengyawen 已提交
2748 2749 2750 2751 2752
  ```


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

2753 2754 2755 2756 2757 2758
write(buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
2759 2760 2761

以异步方法将数据写入流文件,使用callback形式返回结果。

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

Z
zhangxingxia 已提交
2764
**参数:**
Z
update  
zhangxingxia 已提交
2765 2766 2767
  | 参数名   | 类型                            | 必填 | 说明                                                         |
  | -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
  | buffer   | ArrayBuffer&nbsp;\|&nbsp;string | 是   | 待写入文件的数据,可来自缓冲区或字符串。                     |
Z
zhangxingxia 已提交
2768
  | 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
update  
zhangxingxia 已提交
2769
  | callback | AsyncCallback&lt;number&gt;     | 是   | 异步写入完成后执行的回调函数。                               |
Z
zengyawen 已提交
2770

Z
zhangxingxia 已提交
2771
**示例:**
Z
zhangxingxia 已提交
2772
  ```js
Z
zengyawen 已提交
2773 2774
  let ss= fileio.createStreamSync(fpath, "r+");
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
Z
zhangxingxia 已提交
2775
      if (bytesWritten) {
Z
zhangxingxia 已提交
2776
         // do something
Z
zhangxingxia 已提交
2777
         console.info("write successfully and size is:"+ bytesWritten);
Z
zengyawen 已提交
2778 2779 2780 2781 2782
      }
  });
  ```


Z
zengyawen 已提交
2783
### writeSync<sup>7+</sup>
Z
zengyawen 已提交
2784

2785 2786 2787 2788 2789 2790
writeSync(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
2791

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

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

Z
zhangxingxia 已提交
2796
**参数:**
H
HelloCrease 已提交
2797 2798 2799
  | 参数名     | 类型                              | 必填   | 说明                                       |
  | ------- | ------------------------------- | ---- | ---------------------------------------- |
  | buffer  | ArrayBuffer&nbsp;\|&nbsp;string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
Z
zhangxingxia 已提交
2800
  | 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 已提交
2801

Z
zhangxingxia 已提交
2802
**返回值:**
H
HelloCrease 已提交
2803 2804
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
2805
  | number | 实际写入的长度。 |
Z
zengyawen 已提交
2806

Z
zhangxingxia 已提交
2807
**示例:**
Z
zhangxingxia 已提交
2808
  ```js
Z
zengyawen 已提交
2809
  let ss= fileio.createStreamSync(fpath,"r+");
Z
zengyawen 已提交
2810 2811 2812 2813 2814 2815
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


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

2816 2817 2818 2819 2820
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
2821 2822 2823

以异步方法从流文件读取数据,使用promise形式返回结果。

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

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

Z
zhangxingxia 已提交
2832
**返回值:**
H
HelloCrease 已提交
2833 2834
  | 类型                                 | 说明     |
  | ---------------------------------- | ------ |
2835
  | Promise&lt;[ReadOut](#readout)&gt; | 读取的结果。 |
Z
zengyawen 已提交
2836

Z
zhangxingxia 已提交
2837
**示例:**
Z
zhangxingxia 已提交
2838
  ```js
Z
zengyawen 已提交
2839
  let ss = fileio.createStreamSync(fpath, "r+");
Z
zhangxingxia 已提交
2840 2841
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readout){
      console.info("read data successfully");
Z
zhangxingxia 已提交
2842
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
2843 2844 2845
  }).catch(function(err){
      console.info("read data failed with error:"+ err);
  });
Z
zengyawen 已提交
2846 2847 2848 2849 2850
  ```


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

2851 2852 2853 2854 2855
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
2856 2857 2858

以异步方法从流文件读取数据,使用callback形式返回结果。

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

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

Z
zhangxingxia 已提交
2868
**示例:**
Z
zhangxingxia 已提交
2869
  ```js
Z
zengyawen 已提交
2870 2871
  let ss = fileio.createStreamSync(fpath, "r+");
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
Z
zhangxingxia 已提交
2872 2873 2874
      if (readOut) {
          console.info("read data successfully");
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
2875 2876
      }
  });
Z
zengyawen 已提交
2877
  ```
Z
zengyawen 已提交
2878 2879


Z
zengyawen 已提交
2880
### readSync<sup>7+</sup>
Z
zengyawen 已提交
2881

2882 2883 2884 2885 2886
readSync(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): number
Z
zengyawen 已提交
2887 2888 2889

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

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

Z
zhangxingxia 已提交
2892
**参数:**
Z
zhangxingxia 已提交
2893

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

Z
zhangxingxia 已提交
2899
**返回值:**
Z
zhangxingxia 已提交
2900

H
HelloCrease 已提交
2901 2902
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
2903
  | number | 实际读取的长度。 |
Z
zhangxingxia 已提交
2904

Z
zhangxingxia 已提交
2905
**示例:**
Z
zhangxingxia 已提交
2906
  ```js
Z
zengyawen 已提交
2907
  let ss = fileio.createStreamSync(fpath, "r+");
Z
zengyawen 已提交
2908
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
Z
zengyawen 已提交
2909
  ```
Z
zengyawen 已提交
2910 2911


Z
zengyawen 已提交
2912
## Dir
Z
zengyawen 已提交
2913

Z
zengyawen 已提交
2914 2915 2916 2917 2918 2919 2920 2921 2922
管理目录,在调用Dir的方法前,需要先通过[opendir()](#fileioopendir)方法(同步或异步)来构建一个Dir实例。


### read

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

异步读取下一个目录项,使用promise形式返回结果。

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

Z
zhangxingxia 已提交
2925
**返回值:**
H
HelloCrease 已提交
2926 2927
  | 类型                               | 说明            |
  | -------------------------------- | ------------- |
Z
zengyawen 已提交
2928 2929
  | Promise&lt;[Dirent](#dirent)&gt; | 表示异步读取目录项的结果。 |

Z
zhangxingxia 已提交
2930
**示例:**
Z
zhangxingxia 已提交
2931
  ```js
H
HelloCrease 已提交
2932
  let dir = fileio.opendirSync(path);
Z
zhangxingxia 已提交
2933
  dir.read().then(function (dirent){
Z
zhangxingxia 已提交
2934
      console.log("read successfully:"+JSON.stringify(dirent));
Z
zhangxingxia 已提交
2935 2936 2937
  }).catch(function(err){
      console.info("read failed with error:"+ err);
  });
Z
zengyawen 已提交
2938 2939 2940 2941 2942 2943 2944 2945 2946
  ```


### read

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

异步读取下一个目录项,使用callback形式返回结果。

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

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

Z
zhangxingxia 已提交
2954
**示例:**
Z
zhangxingxia 已提交
2955
  ```js
H
HelloCrease 已提交
2956
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2957
  dir.read(function (err, dirent) {
Z
zhangxingxia 已提交
2958
      if (dirent) {
Z
zhangxingxia 已提交
2959
          // do something
Z
zhangxingxia 已提交
2960
          console.log("read successfully:"+JSON.stringify(dirent));
Z
zengyawen 已提交
2961 2962 2963
      }
  });
  ```
Z
zengyawen 已提交
2964 2965


Z
zengyawen 已提交
2966
### readSync
Z
zengyawen 已提交
2967

Z
zengyawen 已提交
2968
readSync(): Dirent
Z
zengyawen 已提交
2969

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

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

Z
zhangxingxia 已提交
2974
**返回值:**
H
HelloCrease 已提交
2975 2976
  | 类型                | 说明       |
  | ----------------- | -------- |
Z
zengyawen 已提交
2977
  | [Dirent](#dirent) | 表示一个目录项。 |
Z
zengyawen 已提交
2978

Z
zhangxingxia 已提交
2979
**示例:**
Z
zhangxingxia 已提交
2980
  ```js
H
HelloCrease 已提交
2981
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2982 2983
  let dirent = dir.readSync();
  ```
Z
zengyawen 已提交
2984 2985


Z
zengyawen 已提交
2986
### closeSync
Z
zengyawen 已提交
2987

Z
zengyawen 已提交
2988
closeSync(): void
Z
zengyawen 已提交
2989

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

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

Z
zhangxingxia 已提交
2994
**示例:**
Z
zhangxingxia 已提交
2995
  ```js
H
HelloCrease 已提交
2996
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2997 2998
  dir.closeSync();
  ```
Z
zengyawen 已提交
2999 3000


Z
zengyawen 已提交
3001
## Dirent
Z
zengyawen 已提交
3002

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

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

Z
zengyawen 已提交
3007
### 属性
Z
zengyawen 已提交
3008

H
HelloCrease 已提交
3009 3010 3011
| 名称   | 参数类型   | 可读   | 可写   | 说明      |
| ---- | ------ | ---- | ---- | ------- |
| name | string | 是    | 否    | 目录项的名称。 |
Z
zengyawen 已提交
3012 3013


Z
zengyawen 已提交
3014
### isBlockDevice
Z
zengyawen 已提交
3015

Z
zengyawen 已提交
3016
isBlockDevice(): boolean
Z
zengyawen 已提交
3017

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

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

Z
zhangxingxia 已提交
3022
**返回值:**
H
HelloCrease 已提交
3023 3024
  | 类型      | 说明               |
  | ------- | ---------------- |
Z
zhangxingxia 已提交
3025
  | boolean | 表示当前目录项是否是块特殊设备。 |
Z
zengyawen 已提交
3026

Z
zhangxingxia 已提交
3027
**示例:**
Z
zhangxingxia 已提交
3028
  ```js
H
HelloCrease 已提交
3029
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3030 3031
  let isBLockDevice = dir.readSync().isBlockDevice();
  ```
Z
zengyawen 已提交
3032 3033


Z
zengyawen 已提交
3034
### isCharacterDevice
Z
zengyawen 已提交
3035

Z
zengyawen 已提交
3036
isCharacterDevice(): boolean
Z
zengyawen 已提交
3037

Z
zhangxingxia 已提交
3038
用于判断当前目录项是否是字符特殊设备。一个字符特殊设备可进行随机访问,且访问的时候不带缓存。
Z
zengyawen 已提交
3039

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

Z
zhangxingxia 已提交
3042
**返回值:**
H
HelloCrease 已提交
3043 3044
  | 类型      | 说明                |
  | ------- | ----------------- |
Z
zhangxingxia 已提交
3045
  | boolean | 表示当前目录项是否是字符特殊设备。 |
Z
zengyawen 已提交
3046

Z
zhangxingxia 已提交
3047
**示例:**
Z
zhangxingxia 已提交
3048
  ```js
H
HelloCrease 已提交
3049
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3050 3051
  let isCharacterDevice = dir.readSync().isCharacterDevice(); 
  ```
Z
zengyawen 已提交
3052 3053


Z
zengyawen 已提交
3054
### isDirectory
Z
zengyawen 已提交
3055

Z
zengyawen 已提交
3056
isDirectory(): boolean
Z
zengyawen 已提交
3057

Z
zhangxingxia 已提交
3058
用于判断当前目录项是否是目录。
Z
zengyawen 已提交
3059

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

Z
zhangxingxia 已提交
3062
**返回值:**
H
HelloCrease 已提交
3063 3064
  | 类型      | 说明            |
  | ------- | ------------- |
Z
zhangxingxia 已提交
3065
  | boolean | 表示当前目录项是否是目录。 |
Z
zengyawen 已提交
3066

Z
zhangxingxia 已提交
3067
**示例:**
Z
zhangxingxia 已提交
3068
  ```js
H
HelloCrease 已提交
3069
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3070 3071
  let isDirectory = dir.readSync().isDirectory(); 
  ```
Z
zengyawen 已提交
3072 3073


Z
zengyawen 已提交
3074
### isFIFO
Z
zengyawen 已提交
3075

Z
zengyawen 已提交
3076
isFIFO(): boolean
Z
zengyawen 已提交
3077

Z
zhangxingxia 已提交
3078
用于判断当前目录项是否是命名管道(有时也称为FIFO)。命名管道通常用于进程间通信。
Z
zengyawen 已提交
3079

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

Z
zhangxingxia 已提交
3082
**返回值:**
H
HelloCrease 已提交
3083 3084
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
3085
  | boolean | 表示当前目录项是否是FIFO。 |
Z
zengyawen 已提交
3086

Z
zhangxingxia 已提交
3087
**示例:**
Z
zhangxingxia 已提交
3088
  ```js
H
HelloCrease 已提交
3089
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3090 3091
  let isFIFO = dir.readSync().isFIFO(); 
  ```
Z
zengyawen 已提交
3092 3093


Z
zengyawen 已提交
3094
### isFile
Z
zengyawen 已提交
3095

Z
zengyawen 已提交
3096
isFile(): boolean
Z
zengyawen 已提交
3097

Z
zhangxingxia 已提交
3098
用于判断当前目录项是否是普通文件。
Z
zengyawen 已提交
3099

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

Z
zhangxingxia 已提交
3102
**返回值:**
H
HelloCrease 已提交
3103 3104
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
3105
  | boolean | 表示当前目录项是否是普通文件。 |
Z
zengyawen 已提交
3106

Z
zhangxingxia 已提交
3107
**示例:**
Z
zhangxingxia 已提交
3108
  ```js
H
HelloCrease 已提交
3109
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3110 3111
  let isFile = dir.readSync().isFile(); 
  ```
Z
zengyawen 已提交
3112 3113


Z
zengyawen 已提交
3114
### isSocket
Z
zengyawen 已提交
3115

Z
zengyawen 已提交
3116
isSocket(): boolean
Z
zengyawen 已提交
3117

Z
zhangxingxia 已提交
3118
用于判断当前目录项是否是套接字。
Z
zengyawen 已提交
3119

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

Z
zhangxingxia 已提交
3122
**返回值:**
H
HelloCrease 已提交
3123 3124
  | 类型      | 说明             |
  | ------- | -------------- |
Z
zhangxingxia 已提交
3125
  | boolean | 表示当前目录项是否是套接字。 |
Z
zengyawen 已提交
3126

Z
zhangxingxia 已提交
3127
**示例:**
Z
zhangxingxia 已提交
3128
  ```js
Z
zengyawen 已提交
3129 3130 3131
  let dir = fileio.opendirSync(dpath);
  let isSocket = dir.readSync().isSocket(); 
  ```
Z
zengyawen 已提交
3132 3133


Z
zengyawen 已提交
3134
### isSymbolicLink
Z
zengyawen 已提交
3135

Z
zengyawen 已提交
3136
isSymbolicLink(): boolean
Z
zengyawen 已提交
3137

Z
zhangxingxia 已提交
3138
用于判断当前目录项是否是符号链接。
Z
zengyawen 已提交
3139

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

Z
zhangxingxia 已提交
3142
**返回值:**
H
HelloCrease 已提交
3143 3144
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
3145
  | boolean | 表示当前目录项是否是符号链接。 |
Z
zengyawen 已提交
3146

Z
zhangxingxia 已提交
3147
**示例:**
Z
zhangxingxia 已提交
3148
  ```js
H
HelloCrease 已提交
3149
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3150 3151
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```