js-apis-fileio.md 126.5 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

W
wangbo 已提交
6
该模块提供文件存储管理能力,包括文件基本管理、文件目录管理、文件信息统计、文件流式读写等常用功能。
Z
zhangxingxia 已提交
7

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

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

Z
zengyawen 已提交
14 15

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

W
wangbo 已提交
17 18 19 20
使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
 let context = featureAbility.getContext();
R
raoxian 已提交
21 22 23 24
 let path = '';
 context.getFilesDir().then((data) => {
      path = data;
 })
Z
zengyawen 已提交
25 26 27
```


Z
zengyawen 已提交
28 29 30 31
## fileio.stat

stat(path: string): Promise<Stat>

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

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

Z
zhangxingxia 已提交
36 37
**参数:**

Z
zengyawen 已提交
38 39 40
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待获取文件的应用沙箱路径。 |
Z
zengyawen 已提交
41

Z
zhangxingxia 已提交
42 43
**返回值:**

H
HelloCrease 已提交
44 45
  | 类型                           | 说明         |
  | ---------------------------- | ---------- |
H
haonan_7 已提交
46
  | Promise<[Stat](#stat)> | Promise对象。返回文件的具体信息。 |
Z
zengyawen 已提交
47

Z
zhangxingxia 已提交
48
**示例:**
Z
zhangxingxia 已提交
49 50
  ```js
  fileio.stat(path).then(function(stat){
H
haonan_7 已提交
51
      console.info("getFileInfo succeed:"+ JSON.stringify(stat));
Z
zhangxingxia 已提交
52 53 54
  }).catch(function(err){
      console.info("getFileInfo failed with error:"+ err);
  });
Z
zengyawen 已提交
55 56 57 58 59 60 61
  ```


## fileio.stat

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

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

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

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

Z
zhangxingxia 已提交
72
**示例:**
Z
zhangxingxia 已提交
73
  ```js
Z
zengyawen 已提交
74
  fileio.stat(path, function (err, stat) {
Z
zhangxingxia 已提交
75
      // example code in Stat
Z
zengyawen 已提交
76 77 78 79
  });
  ```


Z
zengyawen 已提交
80 81 82
## fileio.statSync

statSync(path:string): Stat
Z
zengyawen 已提交
83 84 85

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

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

Z
zhangxingxia 已提交
88
**参数:**
Z
zengyawen 已提交
89 90 91
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待获取文件的应用沙箱路径。 |
Z
zengyawen 已提交
92 93


Z
zhangxingxia 已提交
94
**返回值:**
H
HelloCrease 已提交
95 96
  | 类型            | 说明         |
  | ------------- | ---------- |
Z
zengyawen 已提交
97 98
  | [Stat](#stat) | 表示文件的具体信息。 |

Z
zhangxingxia 已提交
99
**示例:**
Z
zhangxingxia 已提交
100
  ```js
Z
zengyawen 已提交
101
  let stat = fileio.statSync(path);
Z
zengyawen 已提交
102
  // example code in Stat
Z
zengyawen 已提交
103 104 105
  ```


Z
zengyawen 已提交
106
## fileio.opendir
Z
zengyawen 已提交
107

Z
zengyawen 已提交
108
opendir(path: string): Promise<Dir>
Z
zengyawen 已提交
109

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

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

Z
zhangxingxia 已提交
114
**参数:**
Z
zengyawen 已提交
115 116 117
| 参数名 | 类型   | 必填 | 说明                           |
| ------ | ------ | ---- | ------------------------------ |
| path   | string | 是   | 待打开文件目录的应用沙箱路径。 |
Z
zengyawen 已提交
118

Z
zhangxingxia 已提交
119
**返回值:**
H
HelloCrease 已提交
120 121
  | 类型                         | 说明       |
  | -------------------------- | -------- |
H
haonan_7 已提交
122
  | Promise<[Dir](#dir)> | Promise对象。返回Dir对象。 |
Z
zengyawen 已提交
123

Z
zhangxingxia 已提交
124
**示例:**
Z
zhangxingxia 已提交
125 126
  ```js
  fileio.opendir(path).then(function(dir){
H
haonan_7 已提交
127
      console.info("opendir succeed:"+ JSON.stringify(dir));
Z
zhangxingxia 已提交
128 129 130
  }).catch(function(err){
      console.info("opendir failed with error:"+ err);
  });
Z
zengyawen 已提交
131 132 133
  ```


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

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

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

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

Z
zhangxingxia 已提交
142 143
**参数:**

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

Z
zhangxingxia 已提交
149
**示例:**
Z
zhangxingxia 已提交
150
  ```js
Z
zengyawen 已提交
151
  fileio.opendir(path, function (err, dir) { 
Z
zhangxingxia 已提交
152 153
      // example code in Dir struct
      // use read/readSync/close
Z
zengyawen 已提交
154
  });
Z
zengyawen 已提交
155 156 157
  ```


Z
zengyawen 已提交
158
## fileio.opendirSync
Z
zengyawen 已提交
159

Z
zengyawen 已提交
160 161 162
opendirSync(path: string): Dir

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

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

Z
zengyawen 已提交
166

Z
zhangxingxia 已提交
167 168
**参数:**

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

Z
zhangxingxia 已提交
173
**返回值:**
H
HelloCrease 已提交
174 175
  | 类型          | 说明       |
  | ----------- | -------- |
Z
zengyawen 已提交
176
  | [Dir](#dir) | 返回Dir对象。 |
Z
zengyawen 已提交
177

Z
zhangxingxia 已提交
178
**示例:**
Z
zhangxingxia 已提交
179
  ```js
Z
zengyawen 已提交
180 181 182
  let dir = fileio.opendirSync(path);
  // example code in Dir struct
  // use read/readSync/close
Z
zengyawen 已提交
183 184 185
  ```


Z
zengyawen 已提交
186
## fileio.access
Z
zengyawen 已提交
187

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

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

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

Z
zhangxingxia 已提交
194 195
**参数:**

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

Z
zhangxingxia 已提交
201
**返回值:**
H
HelloCrease 已提交
202 203
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
204
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
205

Z
zhangxingxia 已提交
206
**示例:**
Z
zhangxingxia 已提交
207 208
  ```js
  fileio.access(path).then(function() {
H
haonan_7 已提交
209
      console.info("access succeed");
Z
zhangxingxia 已提交
210 211
  }).catch(function(err){
      console.info("access failed with error:"+ err);
Z
zengyawen 已提交
212
  });
Z
zengyawen 已提交
213 214 215
  ```


Z
zengyawen 已提交
216
## fileio.access
Z
zengyawen 已提交
217

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

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

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

Z
zhangxingxia 已提交
224
**参数:**
Z
zengyawen 已提交
225 226 227 228 229
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
230

Z
zhangxingxia 已提交
231
**示例:**
Z
zhangxingxia 已提交
232
  ```js
Z
zengyawen 已提交
233
  fileio.access(path, function (err) {
Z
zhangxingxia 已提交
234
      // do something
Z
zengyawen 已提交
235
  });
Z
zengyawen 已提交
236 237 238
  ```


Z
zengyawen 已提交
239
## fileio.accessSync
Z
zengyawen 已提交
240

Z
zengyawen 已提交
241 242 243
accessSync(path: string, mode?: number): void

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

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

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

Z
zhangxingxia 已提交
253
**示例:**
Z
zhangxingxia 已提交
254
  ```js
Z
zengyawen 已提交
255 256
  try {
      fileio.accessSync(path);
Z
zhangxingxia 已提交
257 258
  } catch(err) {
      console.info("accessSync failed with error:"+ err);
Z
zengyawen 已提交
259
  }
Z
zengyawen 已提交
260 261 262
  ```


Z
zengyawen 已提交
263
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
264

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

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

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

Z
zhangxingxia 已提交
271
**参数:**
H
HelloCrease 已提交
272 273 274
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待关闭文件的文件描述符。 |
Z
zengyawen 已提交
275

Z
zhangxingxia 已提交
276
**返回值:**
H
HelloCrease 已提交
277 278
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
279
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
280

Z
zhangxingxia 已提交
281
**示例:**
Z
zhangxingxia 已提交
282
  ```js
Z
zengyawen 已提交
283
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
284
  fileio.close(fd).then(function(){
H
haonan_7 已提交
285
      console.info("close file succeed");
Z
zhangxingxia 已提交
286 287 288
  }).catch(function(err){
      console.info("close file failed with error:"+ err);
  });
Z
zengyawen 已提交
289 290 291
  ```


Z
zengyawen 已提交
292
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
293

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

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

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

Z
zhangxingxia 已提交
300
**参数:**
H
HelloCrease 已提交
301 302 303 304
  | 参数名      | 类型                        | 必填   | 说明           |
  | -------- | ------------------------- | ---- | ------------ |
  | fd       | number                    | 是    | 待关闭文件的文件描述符。 |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步关闭文件之后的回调。 |
Z
zengyawen 已提交
305

Z
zhangxingxia 已提交
306
**示例:**
Z
zhangxingxia 已提交
307
  ```js
Z
zengyawen 已提交
308
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
309 310
  fileio.close(fd, function (err) {
      // do something
Z
zengyawen 已提交
311
  });
Z
zengyawen 已提交
312 313 314
  ```


Z
zengyawen 已提交
315
## fileio.closeSync
Z
zengyawen 已提交
316

Z
zengyawen 已提交
317
closeSync(fd: number): void
Z
zengyawen 已提交
318

Z
zengyawen 已提交
319
以同步方法关闭文件。
Z
zengyawen 已提交
320

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

Z
zhangxingxia 已提交
323
**参数:**
H
HelloCrease 已提交
324 325 326
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待关闭文件的文件描述符。 |
Z
zengyawen 已提交
327

Z
zhangxingxia 已提交
328
**示例:**
Z
zhangxingxia 已提交
329
  ```js
Z
zengyawen 已提交
330
  fileio.closeSync(fd);
Z
zengyawen 已提交
331 332 333
  ```


Z
zengyawen 已提交
334
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
335

Z
zengyawen 已提交
336
close(): Promise&lt;void&gt;
Z
zengyawen 已提交
337

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

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

Z
zhangxingxia 已提交
342
**返回值:**
H
HelloCrease 已提交
343 344
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
345
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
346

Z
zhangxingxia 已提交
347
**示例:**
Z
zhangxingxia 已提交
348 349
  ```js
  fileio.close().then(function(){
H
haonan_7 已提交
350
      console.info("close file stream succeed");
Z
zhangxingxia 已提交
351 352 353
  }).catch(function(err){
      console.info("close file stream failed with error:"+ err);
  });
Z
zengyawen 已提交
354 355 356
  ```


Z
zengyawen 已提交
357
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
358

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

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

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

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

Z
zhangxingxia 已提交
370
**示例:**
Z
zhangxingxia 已提交
371 372 373
  ```js
  fileio.close(function(err){
      // do something
Z
zengyawen 已提交
374
  });
Z
zengyawen 已提交
375 376 377
  ```


Z
zengyawen 已提交
378
## fileio.copyFile
Z
zengyawen 已提交
379

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

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

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

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

Z
zhangxingxia 已提交
393
**返回值:**
H
HelloCrease 已提交
394 395
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
396
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
397

Z
zhangxingxia 已提交
398
**示例:**
Z
zhangxingxia 已提交
399 400
  ```js
  fileio.copyFile(src, dest).then(function(){
H
haonan_7 已提交
401
      console.info("copyFile succeed");
Z
zhangxingxia 已提交
402 403 404
  }).catch(function(err){
      console.info("copyFile failed with error:"+ err);
  });
Z
zengyawen 已提交
405 406 407
  ```


Z
zengyawen 已提交
408
## fileio.copyFile
Z
zengyawen 已提交
409

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

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

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

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

Z
zhangxingxia 已提交
424
**示例:**
Z
zhangxingxia 已提交
425 426 427
  ```js
  fileio.copyFile(src, dest, function (err) {
      // do something
Z
zengyawen 已提交
428
  });
Z
zengyawen 已提交
429 430 431
  ```


Z
zengyawen 已提交
432
## fileio.copyFileSync
Z
zengyawen 已提交
433

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

Z
zengyawen 已提交
436
以同步方法复制文件。
Z
zengyawen 已提交
437

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

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

Z
zhangxingxia 已提交
447
**示例:**
Z
zhangxingxia 已提交
448
  ```js
Z
zengyawen 已提交
449
  fileio.copyFileSync(src, dest);
Z
zengyawen 已提交
450 451 452
  ```


Z
zengyawen 已提交
453
## fileio.mkdir
Z
zengyawen 已提交
454

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

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

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

Z
zhangxingxia 已提交
461
**参数:**
Z
zengyawen 已提交
462 463 464 465
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
466

Z
zhangxingxia 已提交
467
**返回值:**
H
HelloCrease 已提交
468 469
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
470
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
471

Z
zhangxingxia 已提交
472
**示例:**
Z
zhangxingxia 已提交
473 474
  ```js
  fileio.mkdir(path).then(function() {
H
haonan_7 已提交
475
      console.info("mkdir succeed");
Z
zhangxingxia 已提交
476 477 478
  }).catch(function (error){
      console.info("mkdir failed with error:"+ error);
  });
Z
zengyawen 已提交
479 480 481
  ```


Z
zengyawen 已提交
482
## fileio.mkdir
Z
zengyawen 已提交
483

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

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

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

Z
zhangxingxia 已提交
490
**参数:**
Z
zengyawen 已提交
491 492 493 494 495
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
496

Z
zhangxingxia 已提交
497
**示例:**
Z
zhangxingxia 已提交
498 499
  ```js
  fileio.mkdir(path, function(err) {
H
haonan_7 已提交
500
    console.info("mkdir succeed");
Z
zengyawen 已提交
501
  });
Z
zengyawen 已提交
502 503 504
  ```


Z
zengyawen 已提交
505
## fileio.mkdirSync
Z
zengyawen 已提交
506

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

Z
zengyawen 已提交
509
以同步方法创建目录。
Z
zengyawen 已提交
510

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

Z
zhangxingxia 已提交
513
**参数:**
Z
zengyawen 已提交
514 515 516 517
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
518

Z
zhangxingxia 已提交
519
**示例:**
Z
zhangxingxia 已提交
520
  ```js
Z
zengyawen 已提交
521 522 523 524 525 526 527 528
  fileio.mkdirSync(path);
  ```


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

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

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

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

Z
zhangxingxia 已提交
533
**参数:**
Z
zengyawen 已提交
534 535 536
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待打开文件的应用沙箱路径。                                   |
537
| flags  | number | 否   | 打开文件的选项,必须指定如下选项中的一个,默认以只读方式打开:<br/>-&nbsp;0o0:只读打开。<br/>-&nbsp;0o1:只写打开。<br/>-&nbsp;0o2:读写打开。<br/>同时,也可给定如下选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;0o100:若文件不存在,则创建文件。使用该选项时必须指定第三个参数&nbsp;mode。<br/>-&nbsp;0o200:如果追加了0o100选项,且文件已经存在,则出错。<br/>-&nbsp;0o1000:如果文件存在且以只写或读写的方式打开文件,则将其长度裁剪为零。<br/>-&nbsp;0o2000:以追加方式打开,后续写将追加到文件末尾。<br/>-&nbsp;0o4000:如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;0o200000:如果path不指向目录,则出错。<br/>-&nbsp;0o400000:如果path指向符号链接,则出错。<br/>-&nbsp;0o4010000:以同步IO的方式打开文件。 |
Z
zengyawen 已提交
538
| 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 已提交
539

Z
zhangxingxia 已提交
540
**返回值:**
H
HelloCrease 已提交
541 542
  | 类型                    | 说明          |
  | --------------------- | ----------- |
H
haonan_7 已提交
543
  | Promise&lt;number&gt; | Promise对象。返回打开文件的文件描述符。 |
Z
zengyawen 已提交
544

Z
zhangxingxia 已提交
545
**示例:**
Z
zhangxingxia 已提交
546 547
  ```js
  fileio.open(path, 0o1, 0o0200).then(function(number){
H
haonan_7 已提交
548
      console.info("open file succeed");
Z
zhangxingxia 已提交
549 550 551
  }).catch(function(error){
      console.info("open file failed with error:"+ err);
  });
Z
zengyawen 已提交
552 553 554
  ```


Z
zengyawen 已提交
555
## fileio.open<sup>7+</sup>
Z
zengyawen 已提交
556

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

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

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

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

Z
zhangxingxia 已提交
571
**示例:**
Z
zhangxingxia 已提交
572 573 574
  ```js
  fileio.open(path, 0, function(err, fd) {
      // do something
Z
zengyawen 已提交
575
  });
Z
zengyawen 已提交
576 577 578
  ```


Z
zengyawen 已提交
579
## fileio.openSync
Z
zengyawen 已提交
580

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

Z
zengyawen 已提交
583
以同步方法打开文件。
Z
zengyawen 已提交
584

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

Z
zhangxingxia 已提交
587
**参数:**
Z
zengyawen 已提交
588 589 590
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| path   | string | 是   | 待打开文件的应用沙箱路径。                                   |
591
| 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的方式打开文件。 |
R
raoxian 已提交
592
| mode   | number | 否   | 若创建文件,则指定文件的权限,可给定如下权限,以按位或的方式追加权限,默认给定0o666。<br/>-&nbsp;0o666:所有者具有读、写权限,所有用户组具有读、写权限,其余用户具有读、写权限。<br/>-&nbsp;0o640:所有者具有读、写权限,所有用户组具有读权限。<br/>-&nbsp;0o700:所有者具有读、写及可执行权限。<br/>-&nbsp;0o400:所有者具有读权限。<br/>-&nbsp;0o200:所有者具有写权限。<br/>-&nbsp;0o100:所有者具有可执行权限。<br/>-&nbsp;0o070:所有用户组具有读、写及可执行权限。<br/>-&nbsp;0o040:所有用户组具有读权限。<br/>-&nbsp;0o020:所有用户组具有写权限。<br/>-&nbsp;0o010:所有用户组具有可执行权限。<br/>-&nbsp;0o007:其余用户具有读、写及可执行权限。<br/>-&nbsp;0o004:其余用户具有读权限。<br/>-&nbsp;0o002:其余用户具有写权限。<br/>-&nbsp;0o001:其余用户具有可执行权限。<br/>创建出的文件权限受umask影响,umask随进程启动确定,其修改当前不开放。 |
Z
zengyawen 已提交
593

Z
zhangxingxia 已提交
594
**返回值:**
H
HelloCrease 已提交
595 596
  | 类型     | 说明          |
  | ------ | ----------- |
Z
zengyawen 已提交
597
  | number | 打开文件的文件描述符。 |
Z
zengyawen 已提交
598

Z
zhangxingxia 已提交
599
**示例:**
Z
zhangxingxia 已提交
600
  ```js
601
  let fd = fileio.openSync(path, 0o102, 0o640);
Z
zengyawen 已提交
602
  ```
R
raoxian 已提交
603 604 605
  ```js
  let fd = fileio.openSync(path, 0o102, 0o666);
  fileio.writeSync(fd, 'hello world');
R
raoxian 已提交
606 607 608
  let fd1 = fileio.openSync(path, 0o2002);
  fileio.writeSync(fd1, 'hello world');
  let num = fileio.readSync(fd1, new ArrayBuffer(4096), {position: 0});
R
raoxian 已提交
609 610
  console.info("num == " + num);
  ```
Z
zengyawen 已提交
611 612


Z
zengyawen 已提交
613
## fileio.read
Z
zengyawen 已提交
614

615 616 617 618 619
read(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
620

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

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

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

**返回值:**
Z
zengyawen 已提交
633

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

Z
zhangxingxia 已提交
638
**示例:**
Z
zhangxingxia 已提交
639
  ```js
Z
zengyawen 已提交
640 641
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
642
  fileio.read(fd, buf).then(function(readout){
H
haonan_7 已提交
643
      console.info("read file data succeed");
644
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
645 646 647
  }).catch(function(error){
      console.info("read file data failed with error:"+ error);
  });
Z
zengyawen 已提交
648 649 650
  ```


Z
zengyawen 已提交
651
## fileio.read
Z
zengyawen 已提交
652

653 654 655 656 657
read(fd: number, buffer: ArrayBuffer, options: {
    offset?: number;
    length?: number;
    position?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
658

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

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

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

Z
zhangxingxia 已提交
671
**示例:**
Z
zhangxingxia 已提交
672
  ```js
Z
zengyawen 已提交
673 674
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
675
  fileio.read(fd, buf, function (err, readOut) {
676
      if (readOut) {
H
haonan_7 已提交
677
          console.info("read file data succeed");
678
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
679 680
      }
  });
Z
zengyawen 已提交
681
  ```
Z
zengyawen 已提交
682 683


Z
zengyawen 已提交
684
## fileio.readSync
Z
zengyawen 已提交
685

686 687 688 689 690
readSync(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): number
Z
zengyawen 已提交
691 692 693

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

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

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

Z
zhangxingxia 已提交
703
**返回值:**
H
HelloCrease 已提交
704 705
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
706 707
  | number | 实际读取的长度。 |

Z
zhangxingxia 已提交
708
**示例:**
Z
zhangxingxia 已提交
709
  ```js
Z
zengyawen 已提交
710 711 712 713 714 715 716 717 718 719
  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;

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

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

Z
zhangxingxia 已提交
724
**参数:**
Z
zengyawen 已提交
725 726 727
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除目录的应用沙箱路径。 |
Z
zengyawen 已提交
728

Z
zhangxingxia 已提交
729
**返回值:**
H
HelloCrease 已提交
730 731
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
732
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
733

Z
zhangxingxia 已提交
734
**示例:**
Z
zhangxingxia 已提交
735 736
  ```js
  fileio.rmdir(path).then(function() {
H
haonan_7 已提交
737
      console.info("rmdir succeed");
Z
zhangxingxia 已提交
738 739
  }).catch(function(err){
      console.info("rmdir failed with error:"+ err);
Z
zengyawen 已提交
740 741 742 743 744 745 746 747
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
758
**示例:**
Z
zhangxingxia 已提交
759 760 761
  ```js
  fileio.rmdir(path, function(err){
      // do something
H
haonan_7 已提交
762
      console.info("rmdir succeed");
Z
zengyawen 已提交
763 764 765 766
  });
  ```


767
## fileio.rmdirSync<sup>7+</sup>
Z
zengyawen 已提交
768

769
rmdirSync(path: string): void
Z
zengyawen 已提交
770 771 772

以同步方法删除目录。

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

Z
zhangxingxia 已提交
775
**参数:**
Z
zengyawen 已提交
776 777 778
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除目录的应用沙箱路径。 |
Z
zengyawen 已提交
779

Z
zhangxingxia 已提交
780
**示例:**
Z
zhangxingxia 已提交
781
  ```js
Z
zengyawen 已提交
782 783 784 785 786 787 788 789
  fileio.rmdirSync(path);
  ```


## fileio.unlink

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

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

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

Z
zhangxingxia 已提交
794
**参数:**
Z
zengyawen 已提交
795 796 797
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除文件的应用沙箱路径。 |
Z
zengyawen 已提交
798

Z
zhangxingxia 已提交
799
**返回值:**
H
HelloCrease 已提交
800 801
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
802
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
803

Z
zhangxingxia 已提交
804
**示例:**
Z
zhangxingxia 已提交
805 806
  ```js
  fileio.unlink(path).then(function(){
H
haonan_7 已提交
807
      console.info("remove file succeed");
Z
zhangxingxia 已提交
808 809 810
  }).catch(function(error){
      console.info("remove file failed with error:"+ error);
  });
Z
zengyawen 已提交
811 812 813 814 815 816 817
  ```


## fileio.unlink

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

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

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

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

Z
zhangxingxia 已提交
828
**示例:**
Z
zhangxingxia 已提交
829 830
  ```js
  fileio.unlink(path, function(err) {
H
haonan_7 已提交
831
      console.info("remove file succeed");
Z
zengyawen 已提交
832 833 834 835 836 837 838 839 840 841
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void

以同步方法删除文件。

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

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

Z
zhangxingxia 已提交
849
**示例:**
Z
zhangxingxia 已提交
850
  ```js
Z
zengyawen 已提交
851 852 853 854 855 856
  fileio.unlinkSync(path);
  ```


## fileio.write

857 858 859 860 861 862
write(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
863

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

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

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

Z
zhangxingxia 已提交
875
**返回值:**
H
HelloCrease 已提交
876 877
  | 类型                    | 说明       |
  | --------------------- | -------- |
H
haonan_7 已提交
878
  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
Z
zengyawen 已提交
879

Z
zhangxingxia 已提交
880
**示例:**
Z
zhangxingxia 已提交
881
  ```js
Z
zengyawen 已提交
882
  let fd = fileio.openSync(fpath, 0o100 | 0o2, 0o666);
Z
zhangxingxia 已提交
883
  fileio.write(fd, "hello, world").then(function(number){
H
haonan_7 已提交
884
       console.info("write data to file succeed and size is:"+ number);
Z
zhangxingxia 已提交
885 886 887
  }).catch(function(err){
      console.info("write data to file failed with error:"+ err);
  });
Z
zengyawen 已提交
888 889 890 891 892
  ```


## fileio.write

893 894 895 896 897 898
write(fd: number, buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
899

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

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

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

Z
zhangxingxia 已提交
912
**示例:**
Z
zhangxingxia 已提交
913
  ```js
Z
zengyawen 已提交
914 915
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
916
      if (bytesWritten) {
H
haonan_7 已提交
917
         console.info("write data to file succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
918 919 920 921 922 923 924
      }
  });
  ```


## fileio.writeSync

925 926 927 928 929 930
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
931 932 933

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

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

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

Z
zhangxingxia 已提交
943
**返回值:**
H
HelloCrease 已提交
944 945
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
946 947
  | number | 实际写入的长度。 |

Z
zhangxingxia 已提交
948
**示例:**
Z
zhangxingxia 已提交
949
  ```js
Z
zengyawen 已提交
950 951 952 953 954 955 956 957 958
  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;

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

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

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

Z
zhangxingxia 已提交
969
**返回值:**
H
HelloCrease 已提交
970 971
  | 类型                    | 说明                         |
  | --------------------- | -------------------------- |
H
haonan_7 已提交
972
  | Promise&lt;string&gt; | Promise对象。返回文件的哈希值。表示为十六进制数字串,所有字母均大写。 |
Z
zengyawen 已提交
973

Z
zhangxingxia 已提交
974
**示例:**
Z
zhangxingxia 已提交
975 976
  ```js
  fileio.hash(path, "sha256").then(function(str){
H
haonan_7 已提交
977
      console.info("calculate file hash succeed:"+ str);
Z
zhangxingxia 已提交
978 979 980
  }).catch(function(error){
      console.info("calculate file hash failed with error:"+ err);
  });
Z
zengyawen 已提交
981 982 983 984 985
  ```


## fileio.hash

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

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

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

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

Z
zhangxingxia 已提交
999
**示例:**
Z
zhangxingxia 已提交
1000
  ```js
Z
zengyawen 已提交
1001
  fileio.hash(fpath, "sha256", function(err, hashStr) {
1002
      if (hashStr) {
H
haonan_7 已提交
1003
          console.info("calculate file hash succeed:"+ hashStr);
Z
zengyawen 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012
      }
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1017
**参数:**
Z
zengyawen 已提交
1018 1019
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1020
| path   | string | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1021
| 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 已提交
1022

Z
zhangxingxia 已提交
1023
**返回值:**
H
HelloCrease 已提交
1024 1025
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1026
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1027

Z
zhangxingxia 已提交
1028
**示例:**
Z
zhangxingxia 已提交
1029 1030
  ```js
  fileio.chmod(path, mode).then(function() {
H
haonan_7 已提交
1031
      console.info("chmod succeed");
Z
zhangxingxia 已提交
1032 1033
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
1034 1035 1036 1037 1038 1039 1040 1041
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1046
**参数:**
Z
zengyawen 已提交
1047 1048
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1049
| path     | string                    | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1050 1051
| 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 已提交
1052

Z
zhangxingxia 已提交
1053
**示例:**
Z
zhangxingxia 已提交
1054 1055 1056
  ```js
  fileio.chmod(path, mode, function (err) {
      // do something
Z
zengyawen 已提交
1057 1058 1059 1060 1061 1062 1063 1064
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1069
**参数:**
Z
zengyawen 已提交
1070 1071
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1072
| path   | string | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1073
| 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 已提交
1074

Z
zhangxingxia 已提交
1075
**示例:**
Z
zhangxingxia 已提交
1076
  ```js
Z
zengyawen 已提交
1077 1078 1079 1080 1081 1082 1083 1084
  fileio.chmodSync(fpath, mode);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1089
**参数:**
H
HelloCrease 已提交
1090 1091
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
H
haonan_7 已提交
1092
  | fd   | number | 是    | 待获取文件状态的文件描述符。 |
Z
zengyawen 已提交
1093

Z
zhangxingxia 已提交
1094
**返回值:**
H
HelloCrease 已提交
1095 1096
  | 类型                           | 说明         |
  | ---------------------------- | ---------- |
H
haonan_7 已提交
1097
  | Promise&lt;[Stat](#stat)&gt; | Promise对象。返回表示文件状态的具体信息。 |
Z
zengyawen 已提交
1098

Z
zhangxingxia 已提交
1099
**示例:**
Z
zhangxingxia 已提交
1100 1101
  ```js
  fileio.fstat(fd).then(function(stat){
H
haonan_7 已提交
1102
      console.info("fstat succeed:"+ JSON.stringify(stat));
Z
zhangxingxia 已提交
1103 1104 1105
  }).catch(function(err){
      console.info("fstat failed with error:"+ err);
  });
Z
zengyawen 已提交
1106 1107 1108 1109 1110 1111 1112
  ```


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

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

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

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

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

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


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

fstatSync(fd: number): Stat

以同步方法基于文件描述符获取文件状态信息。

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

Z
zhangxingxia 已提交
1140
**参数:**
H
HelloCrease 已提交
1141 1142
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
H
haonan_7 已提交
1143
  | fd   | number | 是    | 待获取文件状态的文件描述符。 |
Z
zengyawen 已提交
1144

Z
zhangxingxia 已提交
1145
**返回值:**
H
HelloCrease 已提交
1146 1147
  | 类型            | 说明         |
  | ------------- | ---------- |
H
haonan_7 已提交
1148
  | [Stat](#stat) | 表示文件状态的具体信息。 |
Z
zengyawen 已提交
1149

Z
zhangxingxia 已提交
1150
**示例:**
Z
zhangxingxia 已提交
1151
  ```js
Z
zengyawen 已提交
1152 1153 1154 1155 1156 1157 1158
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1165
**参数:**
H
HelloCrease 已提交
1166 1167 1168
  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
H
haonan_7 已提交
1169
  | len  | number | 否    | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1170

Z
zhangxingxia 已提交
1171
**返回值:**
H
HelloCrease 已提交
1172 1173
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1174
  | Promise&lt;void&gt; | Promise对象。无返回值。|
Z
zengyawen 已提交
1175

Z
zhangxingxia 已提交
1176
**示例:**
Z
zhangxingxia 已提交
1177
  ```js
Z
zengyawen 已提交
1178
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1179
  fileio.ftruncate(fd, 5).then(function(err) {    
H
haonan_7 已提交
1180
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1181 1182
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1183 1184 1185 1186 1187 1188 1189 1190
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1195
**参数:**
H
HelloCrease 已提交
1196 1197 1198 1199
  | 参数名      | 类型                        | 必填   | 说明               |
  | -------- | ------------------------- | ---- | ---------------- |
  | fd       | number                    | 是    | 待截断文件的文件描述符。     |
  | len      | number                    | 是    | 文件截断后的长度,以字节为单位。 |
W
wangbo 已提交
1200
  | callback | AsyncCallback&lt;void&gt; | 是    | 回调函数,本调用无返回值。  |
Z
zengyawen 已提交
1201

Z
zhangxingxia 已提交
1202
**示例:**
Z
zhangxingxia 已提交
1203 1204 1205
  ```js
  fileio.ftruncate(fd, len, function(err){
      // do something
Z
zengyawen 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1218
**参数:**
H
HelloCrease 已提交
1219 1220 1221 1222
  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
  | len  | number | 否    | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1223

Z
zhangxingxia 已提交
1224
**示例:**
Z
zhangxingxia 已提交
1225
  ```js
Z
zhangxingxia 已提交
1226
  fileio.ftruncateSync(fd, len);
Z
zengyawen 已提交
1227 1228 1229 1230 1231
  ```


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

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

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

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

Z
zhangxingxia 已提交
1238
**参数:**
Z
zengyawen 已提交
1239 1240 1241
| 参数名 | 类型   | 必填 | 说明                             |
| ------ | ------ | ---- | -------------------------------- |
| path   | string | 是   | 待截断文件的应用沙箱路径。       |
H
haonan_7 已提交
1242
| len    | number | 否   | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1243

Z
zhangxingxia 已提交
1244
**返回值:**
H
HelloCrease 已提交
1245 1246
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1247
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1248

Z
zhangxingxia 已提交
1249
**示例:**
Z
zhangxingxia 已提交
1250 1251
  ```js
  fileio.truncate(path, len).then(function(){
H
haonan_7 已提交
1252
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1253 1254
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1255 1256 1257 1258 1259 1260 1261 1262
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1267
**参数:**
Z
zengyawen 已提交
1268 1269 1270 1271
| 参数名   | 类型                      | 必填 | 说明                             |
| -------- | ------------------------- | ---- | -------------------------------- |
| path     | string                    | 是   | 待截断文件的应用沙箱路径。       |
| len      | number                    | 是   | 文件截断后的长度,以字节为单位。 |
W
wangbo 已提交
1272
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数,本调用无返回值。   |
Z
zengyawen 已提交
1273

Z
zhangxingxia 已提交
1274
**示例:**
Z
zhangxingxia 已提交
1275 1276 1277
  ```js
  fileio.truncate(path, len, function(err){
      // do something
Z
zengyawen 已提交
1278 1279 1280 1281 1282 1283
  });
  ```


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

1284
truncateSync(path: string, len?: number): void
Z
zengyawen 已提交
1285 1286 1287

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

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

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

Z
zhangxingxia 已提交
1296
**示例:**
Z
zhangxingxia 已提交
1297
  ```js
Z
zhangxingxia 已提交
1298
  fileio.truncateSync(path, len);
Z
zengyawen 已提交
1299 1300 1301 1302 1303
  ```


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

1304 1305 1306 1307 1308
readText(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): Promise&lt;string&gt;
Z
zengyawen 已提交
1309

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

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

Z
zhangxingxia 已提交
1314
**参数:**
Z
zengyawen 已提交
1315 1316 1317 1318
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1319

Z
zhangxingxia 已提交
1320
**返回值:**
H
HelloCrease 已提交
1321 1322
  | 类型                    | 说明         |
  | --------------------- | ---------- |
H
haonan_7 已提交
1323
  | Promise&lt;string&gt; | Promise对象。返回读取文件的内容。 |
Z
zengyawen 已提交
1324

Z
zhangxingxia 已提交
1325
**示例:**
Z
zhangxingxia 已提交
1326 1327
  ```js
  fileio.readText(path).then(function(str) {
H
haonan_7 已提交
1328
      console.info("readText succeed:"+ str);
Z
zhangxingxia 已提交
1329 1330
  }).catch(function(err){
      console.info("readText failed with error:"+ err);
Z
zengyawen 已提交
1331 1332 1333 1334 1335 1336
  });
  ```


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

1337 1338 1339 1340 1341
readText(filePath: string, options: {
    position?: number;
    length?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1342

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

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

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

Z
zhangxingxia 已提交
1354
**示例:**
Z
zhangxingxia 已提交
1355
  ```js
Z
zengyawen 已提交
1356
  fileio.readText(path, function(err, str){
Z
zhangxingxia 已提交
1357
      // do something
Z
zengyawen 已提交
1358 1359 1360 1361 1362 1363
  });
  ```


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

1364 1365 1366 1367 1368
readTextSync(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): string
Z
zengyawen 已提交
1369 1370 1371

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

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

Z
zhangxingxia 已提交
1374
**参数:**
Z
zengyawen 已提交
1375 1376 1377 1378
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1379

Z
zhangxingxia 已提交
1380
**返回值:**
Z
zhangxingxia 已提交
1381 1382 1383
  | 类型   | 说明                 |
  | ------ | -------------------- |
  | string | 返回读取文件的内容。 |
Z
zengyawen 已提交
1384

Z
zhangxingxia 已提交
1385
**示例:**
Z
zhangxingxia 已提交
1386 1387
  ```js
  let str = fileio.readTextSync(path, {position: 1, length: 3});
Z
zengyawen 已提交
1388 1389 1390 1391 1392 1393 1394
  ```


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

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

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

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

Z
zhangxingxia 已提交
1399
**参数:**
Z
zengyawen 已提交
1400 1401
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
W
wangbo 已提交
1402
| path   | string | 是   | 目标文件的应用沙箱路径。 |
Z
zengyawen 已提交
1403

Z
zhangxingxia 已提交
1404
**返回值:**
H
HelloCrease 已提交
1405 1406
  | 类型                           | 说明         |
  | ---------------------------- | ---------- |
W
wangbo 已提交
1407
  | Promise&lt;[Stat](#stat)&gt; | promise对象,返回文件对象,表示文件的具体信息,详情见stat。 |
Z
zengyawen 已提交
1408

Z
zhangxingxia 已提交
1409
**示例:**
Z
zhangxingxia 已提交
1410 1411
  ```js
  fileio.lstat(path).then(function(stat){
H
haonan_7 已提交
1412
      console.info("get link status succeed:"+ number);
Z
zhangxingxia 已提交
1413 1414 1415
  }).catch(function(err){
      console.info("get link status failed with error:"+ err);
  });
Z
zengyawen 已提交
1416 1417 1418 1419 1420 1421 1422
  ```


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

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

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

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

Z
zhangxingxia 已提交
1427
**参数:**
Z
zengyawen 已提交
1428 1429
| 参数名   | 类型                               | 必填 | 说明                                   |
| -------- | ---------------------------------- | ---- | -------------------------------------- |
W
wangbo 已提交
1430 1431
| path     | string                             | 是   | 目标文件的应用沙箱路径。 |
| callback | AsyncCallback&lt;[Stat](#stat)&gt; | 是   | 回调函数,返回文件的具体信息。       |
Z
zengyawen 已提交
1432

Z
zhangxingxia 已提交
1433
**示例:**
Z
zhangxingxia 已提交
1434 1435 1436
  ```js
  fileio.lstat(path, function (err, stat) {
      // do something
Z
zengyawen 已提交
1437
  });
Z
zengyawen 已提交
1438 1439 1440 1441 1442 1443 1444
  ```


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

lstatSync(path:string): Stat

H
haonan_7 已提交
1445
以同步方法获取链接信息。
Z
zengyawen 已提交
1446

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

Z
zhangxingxia 已提交
1449
**参数:**
Z
zengyawen 已提交
1450 1451
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
W
wangbo 已提交
1452
| path   | string | 是   | 目标文件的应用沙箱路径。 |
Z
zengyawen 已提交
1453

Z
zhangxingxia 已提交
1454
**返回值:**
H
HelloCrease 已提交
1455 1456
  | 类型            | 说明         |
  | ------------- | ---------- |
Z
zengyawen 已提交
1457 1458
  | [Stat](#stat) | 表示文件的具体信息。 |

Z
zhangxingxia 已提交
1459
**示例:**
Z
zhangxingxia 已提交
1460
  ```js
Z
zengyawen 已提交
1461 1462 1463 1464 1465 1466
  let stat = fileio.lstatSync(path);
  ```


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

1467 1468 1469 1470 1471
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
1472

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

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

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

Z
zhangxingxia 已提交
1483
**返回值:**
H
HelloCrease 已提交
1484 1485
  | 类型                                 | 说明     |
  | ---------------------------------- | ------ |
H
haonan_7 已提交
1486
  | Promise&lt;[ReadOut](#readout)&gt; | Promise对象。返回读取的结果。 |
Z
zengyawen 已提交
1487

Z
zhangxingxia 已提交
1488
**示例:**
Z
zhangxingxia 已提交
1489 1490
  ```js
  fileio.read(new ArrayBuffer(4096)).then(function(readout){
H
haonan_7 已提交
1491
      console.info("read file data succeed");
1492
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
1493 1494 1495
  }).catch(function(err){
      console.info("read file data failed with error:"+ err);
  });
Z
zengyawen 已提交
1496 1497 1498 1499 1500
  ```


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

1501 1502 1503 1504 1505
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
1506

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

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

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

Z
zhangxingxia 已提交
1518
**示例:**
Z
zhangxingxia 已提交
1519
  ```js
Z
zengyawen 已提交
1520 1521
  let buf = new ArrayBuffer(4096);
  fileio.read(buf, function (err, readOut) {
1522
      if (readOut) {
H
haonan_7 已提交
1523
          console.info("read file data succeed");
1524
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533
      }
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1538
**参数:**
Z
zengyawen 已提交
1539 1540 1541 1542
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1543

Z
zhangxingxia 已提交
1544
**返回值:**
H
HelloCrease 已提交
1545 1546
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1547
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1548

Z
zhangxingxia 已提交
1549
**示例:**
Z
zhangxingxia 已提交
1550
  ```js
Z
zhangxingxia 已提交
1551
  fileio.rename(oldPath, newPath).then(function() {
H
haonan_7 已提交
1552
      console.info("rename succeed");
Z
zhangxingxia 已提交
1553 1554
  }).catch(function(err){
      console.info("rename failed with error:"+ err);
Z
zengyawen 已提交
1555 1556 1557 1558 1559 1560 1561 1562
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1574
**示例:**
Z
zhangxingxia 已提交
1575
  ```js
Z
zhangxingxia 已提交
1576
  fileio.rename(oldPath, newPath, function(err){
Z
zengyawen 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
  });
  ```


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

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

以同步方法重命名文件。

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

Z
zhangxingxia 已提交
1589
**参数:**
Z
zengyawen 已提交
1590 1591 1592 1593
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1594

Z
zhangxingxia 已提交
1595
**示例:**
Z
zhangxingxia 已提交
1596
  ```js
Z
zhangxingxia 已提交
1597
  fileio.renameSync(oldPath, newPath);
Z
zengyawen 已提交
1598 1599 1600 1601 1602 1603 1604
  ```


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

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

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

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

Z
zhangxingxia 已提交
1609
**参数:**
H
HelloCrease 已提交
1610 1611 1612
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1613

Z
zhangxingxia 已提交
1614
**返回值:**
H
HelloCrease 已提交
1615 1616
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1617
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1618

Z
zhangxingxia 已提交
1619
**示例:**
Z
zhangxingxia 已提交
1620 1621
  ```js
  fileio.fsync(fd).then(function(){
H
haonan_7 已提交
1622
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1623 1624 1625
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
  });
Z
zengyawen 已提交
1626 1627 1628 1629 1630 1631 1632
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1643
**示例:**
Z
zhangxingxia 已提交
1644
  ```js
Z
zengyawen 已提交
1645
  fileio.fsync(fd, function(err){
Z
zhangxingxia 已提交
1646
      // do something
Z
zengyawen 已提交
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
  });
  ```


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

fsyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1659
**参数:**
H
HelloCrease 已提交
1660 1661 1662
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1663

Z
zhangxingxia 已提交
1664
**示例:**
Z
zhangxingxia 已提交
1665
  ```js
Z
zengyawen 已提交
1666 1667 1668 1669 1670 1671 1672 1673
  fileio.fyncsSync(fd);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1678
**参数:**
H
HelloCrease 已提交
1679 1680 1681
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1682

Z
zhangxingxia 已提交
1683
**返回值:**
H
HelloCrease 已提交
1684 1685
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1686
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1687

Z
zhangxingxia 已提交
1688
**示例:**
Z
zhangxingxia 已提交
1689 1690
  ```js
  fileio.fdatasync(fd).then(function(err) {
H
haonan_7 已提交
1691
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1692 1693
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
Z
zengyawen 已提交
1694 1695 1696 1697 1698 1699 1700 1701
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1712
**示例:**
Z
zhangxingxia 已提交
1713
  ```js
Z
zengyawen 已提交
1714
  fileio.fdatasync (fd, function (err) {
Z
zhangxingxia 已提交
1715
      // do something
Z
zengyawen 已提交
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
  });
  ```


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

fdatasyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1728
**参数:**
H
HelloCrease 已提交
1729 1730 1731
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1732

Z
zhangxingxia 已提交
1733
**示例:**
Z
zhangxingxia 已提交
1734
  ```js
Z
zengyawen 已提交
1735 1736 1737 1738 1739 1740 1741 1742
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1747
**参数:**
Z
zengyawen 已提交
1748 1749 1750 1751
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1752

Z
zhangxingxia 已提交
1753
**返回值:**
H
HelloCrease 已提交
1754 1755
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1756
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1757

Z
zhangxingxia 已提交
1758
**示例:**
Z
zhangxingxia 已提交
1759 1760
  ```js
  fileio.symlink(target, srcPath).then(function() {
H
haonan_7 已提交
1761
      console.info("symlink succeed");
Z
zhangxingxia 已提交
1762 1763
  }).catch(function(err){
      console.info("symlink failed with error:"+ err);
Z
zengyawen 已提交
1764 1765 1766 1767 1768 1769 1770 1771
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1783
**示例:**
Z
zhangxingxia 已提交
1784 1785 1786
  ```js
  fileio.symlink(target, srcPath, function (err) {
      // do something
Z
zengyawen 已提交
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1799
**参数:**
Z
zengyawen 已提交
1800 1801 1802 1803
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1804

Z
zhangxingxia 已提交
1805
**示例:**
Z
zhangxingxia 已提交
1806
  ```js
Z
zengyawen 已提交
1807 1808 1809 1810 1811 1812 1813 1814
  fileio.symlinkSync(target, srcPath);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1819
**参数:**
Z
zengyawen 已提交
1820 1821 1822 1823 1824
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID(UserID)。        |
| gid    | number | 是   | 新的GID(GroupID)。       |
Z
zengyawen 已提交
1825

Z
zhangxingxia 已提交
1826
**返回值:**
H
HelloCrease 已提交
1827 1828
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1829
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1830

Z
zhangxingxia 已提交
1831
**示例:**
Z
zhangxingxia 已提交
1832
  ```js
Z
zengyawen 已提交
1833
  let stat = fileio.statSync(path);
Z
zengyawen 已提交
1834
  fileio.chown(path, stat.uid, stat.gid).then(function(){
H
haonan_7 已提交
1835
      console.info("chown succeed");
Z
zhangxingxia 已提交
1836 1837 1838
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
1839 1840 1841 1842 1843 1844 1845
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1858
**示例:**
Z
zhangxingxia 已提交
1859
  ```js
Z
zengyawen 已提交
1860 1861
  let stat = fileio.statSync(fpath)
  fileio.chown(path, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
1862
      // do something
Z
zengyawen 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1875
**参数:**
Z
zengyawen 已提交
1876 1877 1878 1879 1880
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
1881

Z
zhangxingxia 已提交
1882
**示例:**
Z
zhangxingxia 已提交
1883
  ```js
Z
zengyawen 已提交
1884 1885 1886 1887 1888 1889 1890 1891 1892
  let stat = fileio.statSync(fpath)
  fileio.chownSync(path, stat.uid, stat.gid);
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1902
**返回值:**
Z
zhangxingxia 已提交
1903
  | 类型                   | 说明         |
H
HelloCrease 已提交
1904
  | --------------------- | ---------- |
H
haonan_7 已提交
1905
  | Promise&lt;string&gt; | Promise对象。返回生成的唯一目录路径。 |
Z
zengyawen 已提交
1906

Z
zhangxingxia 已提交
1907
**示例:**
Z
zhangxingxia 已提交
1908 1909
  ```js
  fileio.mkdtemp(path + "XXXX").then(function(path){
H
haonan_7 已提交
1910
      console.info("mkdtemp succeed:"+ path);
Z
zhangxingxia 已提交
1911 1912 1913
  }).catch(function(err){
      console.info("mkdtemp failed with error:"+ err);
  });
Z
zengyawen 已提交
1914 1915 1916 1917 1918 1919 1920
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1931
**示例:**
Z
zhangxingxia 已提交
1932
  ```js
Z
zengyawen 已提交
1933
  fileio.mkdtemp(path + "XXXX", function (err, res) {
Z
zhangxingxia 已提交
1934
      // do something
Z
zengyawen 已提交
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
  });
  ```


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

mkdtempSync(prefix: string): string

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

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

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

Z
zhangxingxia 已提交
1952
**返回值:**
Z
zhangxingxia 已提交
1953
  | 类型    | 说明         |
H
HelloCrease 已提交
1954
  | ------ | ---------- |
Z
zengyawen 已提交
1955 1956
  | string | 产生的唯一目录路径。 |

Z
zhangxingxia 已提交
1957
**示例:**
Z
zhangxingxia 已提交
1958
  ```js
Z
zengyawen 已提交
1959 1960 1961 1962 1963 1964 1965 1966
  let res = fileio.mkdtempSync(path + "XXXX");
  ```


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

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

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

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

Z
zhangxingxia 已提交
1971
**参数:**
H
HelloCrease 已提交
1972 1973 1974 1975
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
1976

Z
zhangxingxia 已提交
1977
**返回值:**
Z
zhangxingxia 已提交
1978
  | 类型                 | 说明                           |
H
HelloCrease 已提交
1979
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1980
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1981

Z
zhangxingxia 已提交
1982
**示例:**
Z
zhangxingxia 已提交
1983 1984
  ```js
  fileio.fchmod(fd, mode).then(function() {
H
haonan_7 已提交
1985
      console.info("chmod succeed");
Z
zhangxingxia 已提交
1986 1987
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
1988 1989 1990 1991 1992 1993 1994 1995
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2000
**参数:**
H
HelloCrease 已提交
2001 2002 2003 2004 2005
  | 参数名      | 类型                              | 必填   | 说明                                       |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | 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 已提交
2006

Z
zhangxingxia 已提交
2007
**示例:**
Z
zhangxingxia 已提交
2008
  ```js
Z
zengyawen 已提交
2009
  fileio.fchmod(fd, mode, function (err) {
Z
zhangxingxia 已提交
2010
      // do something
Z
zengyawen 已提交
2011 2012 2013 2014 2015 2016
  });
  ```


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

2017
fchmodSync(fd: number, mode: number): void
Z
zengyawen 已提交
2018 2019 2020

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

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

Z
zhangxingxia 已提交
2023
**参数:**
H
HelloCrease 已提交
2024 2025 2026 2027
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2028

Z
zhangxingxia 已提交
2029
**示例:**
Z
zhangxingxia 已提交
2030
  ```js
Z
zengyawen 已提交
2031 2032 2033 2034 2035 2036 2037 2038
   fileio.fchmodSync(fd, mode);
  ```


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

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

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

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

Z
zhangxingxia 已提交
2043
**参数:**
Z
zengyawen 已提交
2044 2045 2046 2047
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2048

Z
zhangxingxia 已提交
2049
**返回值:**
H
HelloCrease 已提交
2050 2051
  | 类型                                | 说明        |
  | --------------------------------- | --------- |
H
haonan_7 已提交
2052
  | Promise&lt;[Stream](#stream7)&gt; | Promise对象。返回文件流的结果。 |
Z
zengyawen 已提交
2053

Z
zhangxingxia 已提交
2054
**示例:**
Z
zhangxingxia 已提交
2055 2056
  ```js
  fileio.createStream(path, "r+").then(function(stream){
H
haonan_7 已提交
2057
      console.info("createStream succeed");
Z
zhangxingxia 已提交
2058 2059 2060
  }).catch(function(err){
      console.info("createStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2061 2062 2063 2064 2065 2066 2067
  ```


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

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

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

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

Z
zhangxingxia 已提交
2072
**参数:**
Z
zengyawen 已提交
2073 2074 2075 2076 2077
| 参数名   | 类型                                    | 必填 | 说明                                                         |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
2078

Z
zhangxingxia 已提交
2079
**示例:**
Z
zhangxingxia 已提交
2080 2081 2082
  ```js
  fileio.createStream(path, mode, function(err, stream){
      // do something
Z
zengyawen 已提交
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2095
**参数:**
Z
zengyawen 已提交
2096 2097 2098 2099
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2100

Z
zhangxingxia 已提交
2101
**返回值:**
Z
zhangxingxia 已提交
2102
  | 类型                | 说明        |
H
HelloCrease 已提交
2103
  | ------------------ | --------- |
Z
zengyawen 已提交
2104 2105
  | [Stream](#stream7) | 返回文件流的结果。 |

Z
zhangxingxia 已提交
2106
**示例:**
Z
zhangxingxia 已提交
2107
  ```js
Z
zengyawen 已提交
2108 2109 2110 2111 2112 2113 2114 2115
  let ss = fileio.createStreamSync(path, "r+");
  ```


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

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

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

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

Z
zhangxingxia 已提交
2120
**参数:**
H
HelloCrease 已提交
2121 2122 2123 2124
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2125

Z
zhangxingxia 已提交
2126
**返回值:**
Z
zhangxingxia 已提交
2127
  | 类型                               | 说明        |
H
HelloCrease 已提交
2128
  | --------------------------------- | --------- |
H
haonan_7 已提交
2129
  | Promise&lt;[Stream](#stream7)&gt; | Promise对象。返回文件流的结果。 |
Z
zengyawen 已提交
2130

Z
zhangxingxia 已提交
2131
**示例:**
Z
zhangxingxia 已提交
2132 2133
  ```js
  fileio.fdopenStream(fd, mode).then(function(stream){
H
haonan_7 已提交
2134
      console.info("openStream succeed");
Z
zhangxingxia 已提交
2135 2136 2137
  }).catch(function(err){
      console.info("openStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2138 2139 2140 2141 2142 2143 2144
  ```


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

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

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

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

Z
zhangxingxia 已提交
2149
**参数:**
H
HelloCrease 已提交
2150 2151 2152 2153 2154
  | 参数名      | 类型                                       | 必填   | 说明                                       |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | 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 已提交
2155

Z
zhangxingxia 已提交
2156
**示例:**
Z
zhangxingxia 已提交
2157 2158 2159
  ```js
  fileio.fdopenStream(fd, mode, function (err, stream) {
      // do something
Z
zengyawen 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2172
**参数:**
H
HelloCrease 已提交
2173 2174 2175 2176
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2177

Z
zhangxingxia 已提交
2178
**返回值:**
Z
zhangxingxia 已提交
2179
  | 类型                | 说明        |
H
HelloCrease 已提交
2180
  | ------------------ | --------- |
Z
zengyawen 已提交
2181 2182
  | [Stream](#stream7) | 返回文件流的结果。 |

Z
zhangxingxia 已提交
2183
**示例:**
Z
zhangxingxia 已提交
2184
  ```js
Z
zengyawen 已提交
2185 2186 2187 2188 2189 2190 2191 2192
  let ss = fileio.fdopenStreamSync(fd, "r+");
  ```


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

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

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

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

Z
zhangxingxia 已提交
2197
**参数:**
H
HelloCrease 已提交
2198 2199 2200 2201 2202
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2203

Z
zhangxingxia 已提交
2204
**返回值:**
H
HelloCrease 已提交
2205 2206
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
2207
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
2208

Z
zhangxingxia 已提交
2209
**示例:**
Z
zhangxingxia 已提交
2210
  ```js
Z
zengyawen 已提交
2211
  let stat = fileio.statSync(path);
Z
zhangxingxia 已提交
2212
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
H
haonan_7 已提交
2213
      console.info("chown succeed");
Z
zhangxingxia 已提交
2214 2215
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
Z
zengyawen 已提交
2216 2217 2218 2219 2220 2221 2222 2223
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2236
**示例:**
Z
zhangxingxia 已提交
2237
  ```js
Z
zengyawen 已提交
2238 2239
  let stat = fileio.statSync(fpath);
  fileio.fchown(fd, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2240
      // do something
Z
zengyawen 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2253
**参数:**
H
HelloCrease 已提交
2254 2255 2256 2257 2258
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2259

Z
zhangxingxia 已提交
2260
**示例:**
Z
zhangxingxia 已提交
2261
  ```js
Z
zengyawen 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270
  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;

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

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

Z
zhangxingxia 已提交
2275
**参数:**
Z
zengyawen 已提交
2276 2277 2278 2279 2280
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2281

Z
zhangxingxia 已提交
2282
**返回值:**
H
HelloCrease 已提交
2283 2284
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
2285
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
2286

Z
zhangxingxia 已提交
2287
**示例:**
Z
zhangxingxia 已提交
2288
  ```js
Z
zengyawen 已提交
2289
  let stat = fileio.statSync(path);
Z
zhangxingxia 已提交
2290
  fileio.lchown(path, stat.uid, stat.gid).then(function() {
H
haonan_7 已提交
2291
      console.info("chown succeed");
Z
zhangxingxia 已提交
2292 2293 2294
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
2295 2296 2297 2298 2299 2300 2301
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2314
**示例:**
Z
zhangxingxia 已提交
2315
  ```js
Z
zengyawen 已提交
2316 2317
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2318
      // do something
Z
zengyawen 已提交
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2331
**参数:**
Z
zengyawen 已提交
2332 2333 2334 2335 2336
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2337

Z
zhangxingxia 已提交
2338
**示例:**
Z
zhangxingxia 已提交
2339
  ```js
Z
zengyawen 已提交
2340 2341 2342 2343 2344 2345 2346 2347 2348
  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

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

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

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

Z
zhangxingxia 已提交
2360
**返回值:**
Z
zhangxingxia 已提交
2361
  | 类型                  | 说明         |
H
HelloCrease 已提交
2362
  | -------------------- | ---------- |
H
haonan_7 已提交
2363
  | [Watcher](#watcher7) | Promise对象。返回文件变化监听的实例。 |
Z
zengyawen 已提交
2364

Z
zhangxingxia 已提交
2365
**示例:**
Z
zhangxingxia 已提交
2366 2367 2368
  ```js
  fileio.createWatcher(filename, events, function(watcher){
      // do something
Z
zengyawen 已提交
2369 2370 2371 2372 2373 2374 2375 2376
  });
  ```


## Readout

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

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

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


## Stat

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

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

Z
zengyawen 已提交
2392
### 属性
Z
zengyawen 已提交
2393

H
HelloCrease 已提交
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
| 名称     | 参数类型   | 可读   | 可写   | 说明                                       |
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| 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 已提交
2408 2409


Z
zengyawen 已提交
2410
### isBlockDevice
Z
zengyawen 已提交
2411

Z
zengyawen 已提交
2412
isBlockDevice(): boolean
Z
zengyawen 已提交
2413

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

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

Z
zhangxingxia 已提交
2418
**返回值:**
Z
zhangxingxia 已提交
2419 2420
  | 类型      | 说明               |
  | ------- | ---------------- |
Z
zhangxingxia 已提交
2421
  | boolean | 表示文件是否是块特殊设备。 |
Z
zengyawen 已提交
2422

Z
zhangxingxia 已提交
2423
**示例:**
Z
zhangxingxia 已提交
2424
  ```js
Z
zengyawen 已提交
2425 2426
  let isBLockDevice = fileio.statSync(path).isBlockDevice();
  ```
Z
zengyawen 已提交
2427 2428


Z
zengyawen 已提交
2429
### isCharacterDevice
Z
zengyawen 已提交
2430

Z
zengyawen 已提交
2431
isCharacterDevice(): boolean
Z
zengyawen 已提交
2432

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

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

Z
zhangxingxia 已提交
2437
**返回值:**
Z
zhangxingxia 已提交
2438 2439
  | 类型      | 说明                |
  | ------- | ----------------- |
Z
zhangxingxia 已提交
2440
  | boolean | 表示文件是否是字符特殊设备。 |
Z
zengyawen 已提交
2441

Z
zhangxingxia 已提交
2442
**示例:**
Z
zhangxingxia 已提交
2443
  ```js
Z
zengyawen 已提交
2444 2445
  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
  ```
Z
zengyawen 已提交
2446 2447


Z
zengyawen 已提交
2448
### isDirectory
Z
zengyawen 已提交
2449

Z
zengyawen 已提交
2450
isDirectory(): boolean
Z
zengyawen 已提交
2451

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

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

Z
zhangxingxia 已提交
2456
**返回值:**
Z
zhangxingxia 已提交
2457 2458
  | 类型      | 说明            |
  | ------- | ------------- |
Z
zhangxingxia 已提交
2459
  | boolean | 表示文件是否是目录。 |
Z
zengyawen 已提交
2460

Z
zhangxingxia 已提交
2461
**示例:**
Z
zhangxingxia 已提交
2462
  ```js
Z
zengyawen 已提交
2463
  let isDirectory = fileio.statSync(path).isDirectory(); 
Z
zengyawen 已提交
2464
  ```
Z
zengyawen 已提交
2465 2466


Z
zengyawen 已提交
2467
### isFIFO
Z
zengyawen 已提交
2468

Z
zengyawen 已提交
2469
isFIFO(): boolean
Z
zengyawen 已提交
2470

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

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

Z
zhangxingxia 已提交
2475
**返回值:**
Z
zhangxingxia 已提交
2476 2477
  | 类型      | 说明                    |
  | ------- | --------------------- |
Z
zhangxingxia 已提交
2478
  | boolean | 表示文件是否是&nbsp;FIFO。 |
Z
zengyawen 已提交
2479

Z
zhangxingxia 已提交
2480
**示例:**
Z
zhangxingxia 已提交
2481
  ```js
Z
zengyawen 已提交
2482
  let isFIFO = fileio.statSync(path).isFIFO(); 
Z
zengyawen 已提交
2483
  ```
Z
zengyawen 已提交
2484 2485


Z
zengyawen 已提交
2486
### isFile
Z
zengyawen 已提交
2487

Z
zengyawen 已提交
2488
isFile(): boolean
Z
zengyawen 已提交
2489

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

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

Z
zhangxingxia 已提交
2494
**返回值:**
Z
zhangxingxia 已提交
2495 2496
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
2497
  | boolean | 表示文件是否是普通文件。 |
Z
zengyawen 已提交
2498

Z
zhangxingxia 已提交
2499
**示例:**
Z
zhangxingxia 已提交
2500
  ```js
Z
zengyawen 已提交
2501
  let isFile = fileio.statSync(fpath).isFile();
Z
zengyawen 已提交
2502
  ```
Z
zengyawen 已提交
2503 2504


Z
zengyawen 已提交
2505
### isSocket
Z
zengyawen 已提交
2506

Z
zengyawen 已提交
2507
isSocket(): boolean
Z
zengyawen 已提交
2508

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

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

Z
zhangxingxia 已提交
2513
**返回值:**
Z
zhangxingxia 已提交
2514 2515
  | 类型      | 说明             |
  | ------- | -------------- |
Z
zhangxingxia 已提交
2516
  | boolean | 表示文件是否是套接字。 |
Z
zengyawen 已提交
2517

Z
zhangxingxia 已提交
2518
**示例:**
Z
zhangxingxia 已提交
2519
  ```js
Z
zengyawen 已提交
2520 2521
  let isSocket = fileio.statSync(path).isSocket(); 
  ```
Z
zengyawen 已提交
2522 2523


Z
zengyawen 已提交
2524
### isSymbolicLink
Z
zengyawen 已提交
2525

Z
zengyawen 已提交
2526
isSymbolicLink(): boolean
Z
zengyawen 已提交
2527

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

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

Z
zhangxingxia 已提交
2532
**返回值:**
Z
zhangxingxia 已提交
2533 2534
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
2535
  | boolean | 表示文件是否是符号链接。 |
Z
zengyawen 已提交
2536

Z
zhangxingxia 已提交
2537
**示例:**
Z
zhangxingxia 已提交
2538
  ```js
Z
zengyawen 已提交
2539 2540
  let isSymbolicLink = fileio.statSync(path).isSymbolicLink(); 
  ```
Z
zengyawen 已提交
2541 2542


Z
zengyawen 已提交
2543 2544 2545 2546 2547 2548 2549
## Watcher<sup>7+</sup>

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


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

2550
stop(): Promise&lt;void&gt;
Z
zengyawen 已提交
2551

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

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

Z
zhangxingxia 已提交
2556
**示例:**
Z
zhangxingxia 已提交
2557
  ```js
Z
zengyawen 已提交
2558 2559 2560 2561 2562 2563
  fileio.stop();
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2575
**示例:**
Z
zhangxingxia 已提交
2576
  ```js
Z
zengyawen 已提交
2577
  fileio.stop(function(err){
Z
zhangxingxia 已提交
2578
      // do something
Z
zengyawen 已提交
2579 2580 2581 2582
  });
  ```


Z
zengyawen 已提交
2583
## Stream<sup>7+</sup>
Z
zengyawen 已提交
2584

Z
zengyawen 已提交
2585 2586 2587 2588 2589 2590 2591
文件流,在调用Stream的方法前,需要先通过createStream()方法(同步或异步)来构建一个Stream实例。


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

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

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

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

Z
zhangxingxia 已提交
2596
**返回值:**
H
HelloCrease 已提交
2597 2598
  | 类型                  | 说明            |
  | ------------------- | ------------- |
H
haonan_7 已提交
2599
  | Promise&lt;void&gt; | Promise对象。返回表示异步关闭文件流的结果。 |
Z
zengyawen 已提交
2600

Z
zhangxingxia 已提交
2601
**示例:**
Z
zhangxingxia 已提交
2602
  ```js
Z
zengyawen 已提交
2603
  let ss= fileio.createStreamSync(path);
Z
zhangxingxia 已提交
2604
  ss.close().then(function(){
H
haonan_7 已提交
2605
      console.info("close fileStream succeed");
Z
zhangxingxia 已提交
2606 2607 2608
  }).catch(function(err){
      console.info("close fileStream  failed with error:"+ err);
  });
Z
zengyawen 已提交
2609 2610 2611 2612 2613 2614 2615
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2625
**示例:**
Z
zhangxingxia 已提交
2626
  ```js
Z
zengyawen 已提交
2627 2628
  let ss= fileio.createStreamSync(path);
  ss.close(function (err) {
Z
zhangxingxia 已提交
2629
      // do something
Z
zengyawen 已提交
2630 2631
  });
  ```
Z
zengyawen 已提交
2632 2633


2634
### closeSync
Z
zengyawen 已提交
2635

Z
zengyawen 已提交
2636
closeSync(): void
Z
zengyawen 已提交
2637

Z
zengyawen 已提交
2638
同步关闭文件流。
Z
zengyawen 已提交
2639

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

Z
zhangxingxia 已提交
2642
**示例:**
Z
zhangxingxia 已提交
2643
  ```js
Z
zengyawen 已提交
2644 2645 2646
  let ss= fileio.createStreamSync(path);
  ss.closeSync();
  ```
Z
zengyawen 已提交
2647 2648


Z
zengyawen 已提交
2649 2650 2651 2652
### flush<sup>7+</sup>

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

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

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

Z
zhangxingxia 已提交
2657
**返回值:**
H
HelloCrease 已提交
2658 2659
  | 类型                  | 说明            |
  | ------------------- | ------------- |
H
haonan_7 已提交
2660
  | Promise&lt;void&gt; | Promise对象。返回表示异步刷新文件流的结果。 |
Z
zengyawen 已提交
2661

Z
zhangxingxia 已提交
2662
**示例:**
Z
zhangxingxia 已提交
2663
  ```js
Z
zengyawen 已提交
2664
  let ss= fileio.createStreamSync(path);
Z
zhangxingxia 已提交
2665
  ss.flush().then(function (){
H
haonan_7 已提交
2666
      console.info("flush succeed");
Z
zhangxingxia 已提交
2667 2668 2669
  }).catch(function(err){
      console.info("flush failed with error:"+ err);
  });
Z
zengyawen 已提交
2670 2671 2672 2673 2674 2675 2676
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2686
**示例:**
Z
zhangxingxia 已提交
2687
  ```js
Z
zengyawen 已提交
2688 2689
  let ss= fileio.createStreamSync(path);
  ss.flush(function (err) {
Z
zhangxingxia 已提交
2690
      // do something
Z
zengyawen 已提交
2691 2692 2693 2694
  });
  ```


Z
zengyawen 已提交
2695
### flushSync<sup>7+</sup>
Z
zengyawen 已提交
2696

Z
zengyawen 已提交
2697
flushSync(): void
Z
zengyawen 已提交
2698

Z
zengyawen 已提交
2699
同步刷新文件流。
Z
zengyawen 已提交
2700

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

Z
zhangxingxia 已提交
2703
**示例:**
Z
zhangxingxia 已提交
2704
  ```js
Z
zengyawen 已提交
2705 2706 2707
  let ss= fileio.createStreamSync(path);
  ss.flushSync();
  ```
Z
zengyawen 已提交
2708 2709


Z
zengyawen 已提交
2710 2711
### write<sup>7+</sup>

2712 2713 2714 2715 2716 2717
write(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
2718

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

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

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

Z
zhangxingxia 已提交
2729
**返回值:**
H
HelloCrease 已提交
2730 2731
  | 类型                    | 说明       |
  | --------------------- | -------- |
H
haonan_7 已提交
2732
  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
Z
zengyawen 已提交
2733

Z
zhangxingxia 已提交
2734
**示例:**
Z
zhangxingxia 已提交
2735
  ```js
Z
zengyawen 已提交
2736
  let ss= fileio.createStreamSync(fpath, "r+");
Z
zhangxingxia 已提交
2737
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
H
haonan_7 已提交
2738
      console.info("write succeed and size is:"+ number);
Z
zhangxingxia 已提交
2739 2740 2741
  }).catch(function(err){
      console.info("write failed with error:"+ err);
  });
Z
zengyawen 已提交
2742 2743 2744 2745 2746
  ```


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

2747 2748 2749 2750 2751 2752
write(buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
2753

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

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

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

Z
zhangxingxia 已提交
2765
**示例:**
Z
zhangxingxia 已提交
2766
  ```js
Z
zengyawen 已提交
2767 2768
  let ss= fileio.createStreamSync(fpath, "r+");
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
2769
      if (bytesWritten) {
Z
zhangxingxia 已提交
2770
         // do something
H
haonan_7 已提交
2771
         console.info("write succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
2772 2773 2774 2775 2776
      }
  });
  ```


Z
zengyawen 已提交
2777
### writeSync<sup>7+</sup>
Z
zengyawen 已提交
2778

2779 2780 2781 2782 2783 2784
writeSync(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
2785

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

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

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

Z
zhangxingxia 已提交
2796
**返回值:**
H
HelloCrease 已提交
2797 2798
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
2799
  | number | 实际写入的长度。 |
Z
zengyawen 已提交
2800

Z
zhangxingxia 已提交
2801
**示例:**
Z
zhangxingxia 已提交
2802
  ```js
Z
zengyawen 已提交
2803
  let ss= fileio.createStreamSync(fpath,"r+");
Z
zengyawen 已提交
2804 2805 2806 2807 2808 2809
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


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

2810 2811 2812 2813 2814
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
2815

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

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

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

Z
zhangxingxia 已提交
2826
**返回值:**
H
HelloCrease 已提交
2827 2828
  | 类型                                 | 说明     |
  | ---------------------------------- | ------ |
H
haonan_7 已提交
2829
  | Promise&lt;[ReadOut](#readout)&gt; | Promise对象。返回读取的结果。 |
Z
zengyawen 已提交
2830

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


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

2845 2846 2847 2848 2849
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
2850

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

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

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

Z
zhangxingxia 已提交
2862
**示例:**
Z
zhangxingxia 已提交
2863
  ```js
Z
zengyawen 已提交
2864 2865
  let ss = fileio.createStreamSync(fpath, "r+");
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
2866
      if (readOut) {
H
haonan_7 已提交
2867
          console.info("read data succeed");
2868
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
2869 2870
      }
  });
Z
zengyawen 已提交
2871
  ```
Z
zengyawen 已提交
2872 2873


Z
zengyawen 已提交
2874
### readSync<sup>7+</sup>
Z
zengyawen 已提交
2875

2876 2877 2878 2879 2880
readSync(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): number
Z
zengyawen 已提交
2881 2882 2883

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

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

Z
zhangxingxia 已提交
2886 2887
**参数:**

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

**返回值:**
Z
zengyawen 已提交
2894

H
HelloCrease 已提交
2895 2896
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
2897
  | number | 实际读取的长度。 |
Z
zengyawen 已提交
2898

Z
zhangxingxia 已提交
2899
**示例:**
Z
zhangxingxia 已提交
2900
  ```js
Z
zengyawen 已提交
2901
  let ss = fileio.createStreamSync(fpath, "r+");
Z
zengyawen 已提交
2902
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
Z
zengyawen 已提交
2903
  ```
Z
zengyawen 已提交
2904 2905


Z
zengyawen 已提交
2906
## Dir
Z
zengyawen 已提交
2907

Z
zengyawen 已提交
2908 2909 2910 2911 2912 2913 2914
管理目录,在调用Dir的方法前,需要先通过[opendir()](#fileioopendir)方法(同步或异步)来构建一个Dir实例。


### read

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

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

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

Z
zhangxingxia 已提交
2919
**返回值:**
H
HelloCrease 已提交
2920 2921
  | 类型                               | 说明            |
  | -------------------------------- | ------------- |
H
haonan_7 已提交
2922
  | Promise&lt;[Dirent](#dirent)&gt; | Promise对象。返回表示异步读取目录项的结果。 |
Z
zengyawen 已提交
2923

Z
zhangxingxia 已提交
2924
**示例:**
Z
zhangxingxia 已提交
2925
  ```js
H
HelloCrease 已提交
2926
  let dir = fileio.opendirSync(path);
Z
zhangxingxia 已提交
2927
  dir.read().then(function (dirent){
H
haonan_7 已提交
2928
      console.log("read succeed:"+JSON.stringify(dirent));
Z
zhangxingxia 已提交
2929 2930 2931
  }).catch(function(err){
      console.info("read failed with error:"+ err);
  });
Z
zengyawen 已提交
2932 2933 2934 2935 2936 2937 2938
  ```


### read

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

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

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

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

Z
zhangxingxia 已提交
2948
**示例:**
Z
zhangxingxia 已提交
2949
  ```js
H
HelloCrease 已提交
2950
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2951
  dir.read(function (err, dirent) {
2952
      if (dirent) {
Z
zhangxingxia 已提交
2953
          // do something
H
haonan_7 已提交
2954
          console.log("read succeed:"+JSON.stringify(dirent));
Z
zengyawen 已提交
2955 2956 2957
      }
  });
  ```
Z
zengyawen 已提交
2958 2959


Z
zengyawen 已提交
2960
### readSync
Z
zengyawen 已提交
2961

Z
zengyawen 已提交
2962
readSync(): Dirent
Z
zengyawen 已提交
2963

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

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

Z
zhangxingxia 已提交
2968
**返回值:**
H
HelloCrease 已提交
2969 2970
  | 类型                | 说明       |
  | ----------------- | -------- |
Z
zengyawen 已提交
2971
  | [Dirent](#dirent) | 表示一个目录项。 |
Z
zengyawen 已提交
2972

Z
zhangxingxia 已提交
2973
**示例:**
Z
zhangxingxia 已提交
2974
  ```js
H
HelloCrease 已提交
2975
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2976 2977
  let dirent = dir.readSync();
  ```
Z
zengyawen 已提交
2978 2979


2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
### close

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

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

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

**示例:**
  ```js
  let dir = fileio.opendirSync(path);
  dir.close().then(function(err){
      console.info("close dir successfully");
  });
  ```


  ### close

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

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

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

**示例:**
  ```js
  let dir = fileio.opendirSync(path);
  dir.close(function(err){
      console.info("close dir successfully");
  });
  ```


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

Z
zengyawen 已提交
3016
closeSync(): void
Z
zengyawen 已提交
3017

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

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

Z
zhangxingxia 已提交
3022
**示例:**
Z
zhangxingxia 已提交
3023
  ```js
H
HelloCrease 已提交
3024
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3025 3026
  dir.closeSync();
  ```
Z
zengyawen 已提交
3027 3028


Z
zengyawen 已提交
3029
## Dirent
Z
zengyawen 已提交
3030

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

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

Z
zengyawen 已提交
3035
### 属性
Z
zengyawen 已提交
3036

H
HelloCrease 已提交
3037 3038 3039
| 名称   | 参数类型   | 可读   | 可写   | 说明      |
| ---- | ------ | ---- | ---- | ------- |
| name | string | 是    | 否    | 目录项的名称。 |
Z
zengyawen 已提交
3040 3041


Z
zengyawen 已提交
3042
### isBlockDevice
Z
zengyawen 已提交
3043

Z
zengyawen 已提交
3044
isBlockDevice(): boolean
Z
zengyawen 已提交
3045

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

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

Z
zhangxingxia 已提交
3050
**返回值:**
H
HelloCrease 已提交
3051 3052
  | 类型      | 说明               |
  | ------- | ---------------- |
Z
zengyawen 已提交
3053
  | boolean | 表示当前目录项是否是块特殊设备。 |
Z
zengyawen 已提交
3054

Z
zhangxingxia 已提交
3055
**示例:**
Z
zhangxingxia 已提交
3056
  ```js
H
HelloCrease 已提交
3057
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3058 3059
  let isBLockDevice = dir.readSync().isBlockDevice();
  ```
Z
zengyawen 已提交
3060 3061


Z
zengyawen 已提交
3062
### isCharacterDevice
Z
zengyawen 已提交
3063

Z
zengyawen 已提交
3064
isCharacterDevice(): boolean
Z
zengyawen 已提交
3065

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

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

Z
zhangxingxia 已提交
3070
**返回值:**
H
HelloCrease 已提交
3071 3072
  | 类型      | 说明                |
  | ------- | ----------------- |
Z
zengyawen 已提交
3073
  | boolean | 表示当前目录项是否是字符特殊设备。 |
Z
zengyawen 已提交
3074

Z
zhangxingxia 已提交
3075
**示例:**
Z
zhangxingxia 已提交
3076
  ```js
H
HelloCrease 已提交
3077
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3078 3079
  let isCharacterDevice = dir.readSync().isCharacterDevice(); 
  ```
Z
zengyawen 已提交
3080 3081


Z
zengyawen 已提交
3082
### isDirectory
Z
zengyawen 已提交
3083

Z
zengyawen 已提交
3084
isDirectory(): boolean
Z
zengyawen 已提交
3085

Z
zengyawen 已提交
3086
用于判断当前目录项是否是目录。
Z
zengyawen 已提交
3087

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

Z
zhangxingxia 已提交
3090
**返回值:**
H
HelloCrease 已提交
3091 3092
  | 类型      | 说明            |
  | ------- | ------------- |
Z
zengyawen 已提交
3093
  | boolean | 表示当前目录项是否是目录。 |
Z
zengyawen 已提交
3094

Z
zhangxingxia 已提交
3095
**示例:**
Z
zhangxingxia 已提交
3096
  ```js
H
HelloCrease 已提交
3097
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3098 3099
  let isDirectory = dir.readSync().isDirectory(); 
  ```
Z
zengyawen 已提交
3100 3101


Z
zengyawen 已提交
3102
### isFIFO
Z
zengyawen 已提交
3103

Z
zengyawen 已提交
3104
isFIFO(): boolean
Z
zengyawen 已提交
3105

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

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

Z
zhangxingxia 已提交
3110
**返回值:**
H
HelloCrease 已提交
3111 3112
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zengyawen 已提交
3113
  | boolean | 表示当前目录项是否是FIFO。 |
Z
zengyawen 已提交
3114

Z
zhangxingxia 已提交
3115
**示例:**
Z
zhangxingxia 已提交
3116
  ```js
H
HelloCrease 已提交
3117
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3118 3119
  let isFIFO = dir.readSync().isFIFO(); 
  ```
Z
zengyawen 已提交
3120 3121


Z
zengyawen 已提交
3122
### isFile
Z
zengyawen 已提交
3123

Z
zengyawen 已提交
3124
isFile(): boolean
Z
zengyawen 已提交
3125

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

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

Z
zhangxingxia 已提交
3130
**返回值:**
H
HelloCrease 已提交
3131 3132
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zengyawen 已提交
3133
  | boolean | 表示当前目录项是否是普通文件。 |
Z
zengyawen 已提交
3134

Z
zhangxingxia 已提交
3135
**示例:**
Z
zhangxingxia 已提交
3136
  ```js
H
HelloCrease 已提交
3137
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3138 3139
  let isFile = dir.readSync().isFile(); 
  ```
Z
zengyawen 已提交
3140 3141


Z
zengyawen 已提交
3142
### isSocket
Z
zengyawen 已提交
3143

Z
zengyawen 已提交
3144
isSocket(): boolean
Z
zengyawen 已提交
3145

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

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

Z
zhangxingxia 已提交
3150
**返回值:**
H
HelloCrease 已提交
3151 3152
  | 类型      | 说明             |
  | ------- | -------------- |
Z
zengyawen 已提交
3153
  | boolean | 表示当前目录项是否是套接字。 |
Z
zengyawen 已提交
3154

Z
zhangxingxia 已提交
3155
**示例:**
Z
zhangxingxia 已提交
3156
  ```js
Z
zengyawen 已提交
3157 3158 3159
  let dir = fileio.opendirSync(dpath);
  let isSocket = dir.readSync().isSocket(); 
  ```
Z
zengyawen 已提交
3160 3161


Z
zengyawen 已提交
3162
### isSymbolicLink
Z
zengyawen 已提交
3163

Z
zengyawen 已提交
3164
isSymbolicLink(): boolean
Z
zengyawen 已提交
3165

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

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

Z
zhangxingxia 已提交
3170
**返回值:**
H
HelloCrease 已提交
3171 3172
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zengyawen 已提交
3173
  | boolean | 表示当前目录项是否是符号链接。 |
Z
zengyawen 已提交
3174

Z
zhangxingxia 已提交
3175
**示例:**
Z
zhangxingxia 已提交
3176
  ```js
H
HelloCrease 已提交
3177
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3178 3179
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```