js-apis-fileio.md 125.3 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 21
使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:
 ```js
 import featureAbility from '@ohos.ability.featureAbility';
 let context = featureAbility.getContext();
 let path = context.getFilesDir();
Z
zengyawen 已提交
22 23 24
```


Z
zengyawen 已提交
25 26 27 28
## fileio.stat

stat(path: string): Promise<Stat>

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

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

Z
zhangxingxia 已提交
33
**参数:**
Z
zhangxingxia 已提交
34

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

Z
zhangxingxia 已提交
39
**返回值:**
Z
zhangxingxia 已提交
40

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

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


## fileio.stat

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

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

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

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

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


Z
zengyawen 已提交
77 78 79
## fileio.statSync

statSync(path:string): Stat
Z
zengyawen 已提交
80 81 82

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

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

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


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

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


Z
zengyawen 已提交
103
## fileio.opendir
Z
zengyawen 已提交
104

Z
zengyawen 已提交
105
opendir(path: string): Promise<Dir>
Z
zengyawen 已提交
106

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

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

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

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

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


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

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

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

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

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

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

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


Z
zengyawen 已提交
155
## fileio.opendirSync
Z
zengyawen 已提交
156

Z
zengyawen 已提交
157 158 159
opendirSync(path: string): Dir

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

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

Z
zengyawen 已提交
163

Z
zhangxingxia 已提交
164
**参数:**
Z
zhangxingxia 已提交
165

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

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

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


Z
zengyawen 已提交
183
## fileio.access
Z
zengyawen 已提交
184

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

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

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

Z
zhangxingxia 已提交
191
**参数:**
Z
zhangxingxia 已提交
192

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

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

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


Z
zengyawen 已提交
213
## fileio.access
Z
zengyawen 已提交
214

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

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

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

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

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


Z
zengyawen 已提交
236
## fileio.accessSync
Z
zengyawen 已提交
237

Z
zengyawen 已提交
238 239 240
accessSync(path: string, mode?: number): void

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

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

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

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


Z
zengyawen 已提交
260
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
261

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

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

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

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

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

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


Z
zengyawen 已提交
289
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
290

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

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

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

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

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


Z
zengyawen 已提交
312
## fileio.closeSync
Z
zengyawen 已提交
313

Z
zengyawen 已提交
314
closeSync(fd: number): void
Z
zengyawen 已提交
315

Z
zengyawen 已提交
316
以同步方法关闭文件。
Z
zengyawen 已提交
317

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

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

Z
zhangxingxia 已提交
325
**示例:**
Z
zhangxingxia 已提交
326
  ```js
Z
zengyawen 已提交
327
  fileio.closeSync(fd);
Z
zengyawen 已提交
328 329 330
  ```


Z
zengyawen 已提交
331
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
332

Z
zengyawen 已提交
333
close(): Promise&lt;void&gt;
Z
zengyawen 已提交
334

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

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

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

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


Z
zengyawen 已提交
354
## fileio.close<sup>7+</sup>
Z
zengyawen 已提交
355

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

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

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

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

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


Z
zengyawen 已提交
375
## fileio.copyFile
Z
zengyawen 已提交
376

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

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

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

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

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

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


Z
zengyawen 已提交
405
## fileio.copyFile
Z
zengyawen 已提交
406

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

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

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

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

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


Z
zengyawen 已提交
429
## fileio.copyFileSync
Z
zengyawen 已提交
430

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

Z
zengyawen 已提交
433
以同步方法复制文件。
Z
zengyawen 已提交
434

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

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

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


Z
zengyawen 已提交
450
## fileio.mkdir
Z
zengyawen 已提交
451

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

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

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

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

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

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


Z
zengyawen 已提交
479
## fileio.mkdir
Z
zengyawen 已提交
480

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

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

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

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

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


Z
zengyawen 已提交
502
## fileio.mkdirSync
Z
zengyawen 已提交
503

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

Z
zengyawen 已提交
506
以同步方法创建目录。
Z
zengyawen 已提交
507

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

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

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


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

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

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

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

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

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

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


Z
zengyawen 已提交
552
## fileio.open<sup>7+</sup>
Z
zengyawen 已提交
553

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

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

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

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

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


Z
zengyawen 已提交
576
## fileio.openSync
Z
zengyawen 已提交
577

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

Z
zengyawen 已提交
580
以同步方法打开文件。
Z
zengyawen 已提交
581

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

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

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

Z
zhangxingxia 已提交
596
**示例:**
Z
zhangxingxia 已提交
597
  ```js
R
raoxian 已提交
598
  let fd = fileio.openSync(path, 0o102, 0o640);
Z
zengyawen 已提交
599 600 601
  ```


Z
zengyawen 已提交
602
## fileio.read
Z
zengyawen 已提交
603

604 605 606 607 608
read(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
609

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

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

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

Z
zhangxingxia 已提交
621
**返回值:**
Z
zhangxingxia 已提交
622

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

Z
zhangxingxia 已提交
627
**示例:**
Z
zhangxingxia 已提交
628
  ```js
Z
zengyawen 已提交
629 630
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
631
  fileio.read(fd, buf).then(function(readout){
H
haonan_7 已提交
632
      console.info("read file data succeed");
Z
zhangxingxia 已提交
633
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
634 635 636
  }).catch(function(error){
      console.info("read file data failed with error:"+ error);
  });
Z
zengyawen 已提交
637 638 639
  ```


Z
zengyawen 已提交
640
## fileio.read
Z
zengyawen 已提交
641

642 643 644 645 646
read(fd: number, buffer: ArrayBuffer, options: {
    offset?: number;
    length?: number;
    position?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
647

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

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

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

Z
zhangxingxia 已提交
660
**示例:**
Z
zhangxingxia 已提交
661
  ```js
Z
zengyawen 已提交
662 663
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
664
  fileio.read(fd, buf, function (err, readOut) {
Z
zhangxingxia 已提交
665
      if (readOut) {
H
haonan_7 已提交
666
          console.info("read file data succeed");
Z
zhangxingxia 已提交
667
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
668 669
      }
  });
Z
zengyawen 已提交
670
  ```
Z
zengyawen 已提交
671 672


Z
zengyawen 已提交
673
## fileio.readSync
Z
zengyawen 已提交
674

675 676 677 678 679
readSync(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): number
Z
zengyawen 已提交
680 681 682

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

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

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

Z
zhangxingxia 已提交
692
**返回值:**
H
HelloCrease 已提交
693 694
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
695 696
  | number | 实际读取的长度。 |

Z
zhangxingxia 已提交
697
**示例:**
Z
zhangxingxia 已提交
698
  ```js
Z
zengyawen 已提交
699 700 701 702 703 704 705 706 707 708
  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 已提交
709
删除目录,使用Promise异步回调。
Z
zengyawen 已提交
710

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

Z
zhangxingxia 已提交
713
**参数:**
Z
zengyawen 已提交
714 715 716
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除目录的应用沙箱路径。 |
Z
zengyawen 已提交
717

Z
zhangxingxia 已提交
718
**返回值:**
H
HelloCrease 已提交
719 720
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
721
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
722

Z
zhangxingxia 已提交
723
**示例:**
Z
zhangxingxia 已提交
724 725
  ```js
  fileio.rmdir(path).then(function() {
H
haonan_7 已提交
726
      console.info("rmdir succeed");
Z
zhangxingxia 已提交
727 728
  }).catch(function(err){
      console.info("rmdir failed with error:"+ err);
Z
zengyawen 已提交
729 730 731 732 733 734 735 736
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
747
**示例:**
Z
zhangxingxia 已提交
748 749 750
  ```js
  fileio.rmdir(path, function(err){
      // do something
H
haonan_7 已提交
751
      console.info("rmdir succeed");
Z
zengyawen 已提交
752 753 754 755
  });
  ```


756
## fileio.rmdirSync<sup>7+</sup>
Z
zengyawen 已提交
757

758
rmdirSync(path: string): void
Z
zengyawen 已提交
759 760 761

以同步方法删除目录。

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

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

Z
zhangxingxia 已提交
769
**示例:**
Z
zhangxingxia 已提交
770
  ```js
Z
zengyawen 已提交
771 772 773 774 775 776 777 778
  fileio.rmdirSync(path);
  ```


## fileio.unlink

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

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

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

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

Z
zhangxingxia 已提交
788
**返回值:**
H
HelloCrease 已提交
789 790
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
791
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
792

Z
zhangxingxia 已提交
793
**示例:**
Z
zhangxingxia 已提交
794 795
  ```js
  fileio.unlink(path).then(function(){
H
haonan_7 已提交
796
      console.info("remove file succeed");
Z
zhangxingxia 已提交
797 798 799
  }).catch(function(error){
      console.info("remove file failed with error:"+ error);
  });
Z
zengyawen 已提交
800 801 802 803 804 805 806
  ```


## fileio.unlink

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

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

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

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

Z
zhangxingxia 已提交
817
**示例:**
Z
zhangxingxia 已提交
818 819
  ```js
  fileio.unlink(path, function(err) {
H
haonan_7 已提交
820
      console.info("remove file succeed");
Z
zengyawen 已提交
821 822 823 824 825 826 827 828 829 830
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void

以同步方法删除文件。

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

Z
zhangxingxia 已提交
833
**参数:**
Z
zengyawen 已提交
834 835 836
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除文件的应用沙箱路径。 |
Z
zengyawen 已提交
837

Z
zhangxingxia 已提交
838
**示例:**
Z
zhangxingxia 已提交
839
  ```js
Z
zengyawen 已提交
840 841 842 843 844 845
  fileio.unlinkSync(path);
  ```


## fileio.write

846 847 848 849 850 851
write(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
852

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

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

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

Z
zhangxingxia 已提交
864
**返回值:**
H
HelloCrease 已提交
865 866
  | 类型                    | 说明       |
  | --------------------- | -------- |
H
haonan_7 已提交
867
  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
Z
zengyawen 已提交
868

Z
zhangxingxia 已提交
869
**示例:**
Z
zhangxingxia 已提交
870
  ```js
Z
zengyawen 已提交
871
  let fd = fileio.openSync(fpath, 0o100 | 0o2, 0o666);
Z
zhangxingxia 已提交
872
  fileio.write(fd, "hello, world").then(function(number){
H
haonan_7 已提交
873
       console.info("write data to file succeed and size is:"+ number);
Z
zhangxingxia 已提交
874 875 876
  }).catch(function(err){
      console.info("write data to file failed with error:"+ err);
  });
Z
zengyawen 已提交
877 878 879 880 881
  ```


## fileio.write

882 883 884 885 886 887
write(fd: number, buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
888

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

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

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

Z
zhangxingxia 已提交
901
**示例:**
Z
zhangxingxia 已提交
902
  ```js
Z
zengyawen 已提交
903 904
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
Z
zhangxingxia 已提交
905
      if (bytesWritten) {
H
haonan_7 已提交
906
         console.info("write data to file succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
907 908 909 910 911 912 913
      }
  });
  ```


## fileio.writeSync

914 915 916 917 918 919
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
920 921 922

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

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

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

Z
zhangxingxia 已提交
932
**返回值:**
H
HelloCrease 已提交
933 934
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
935 936
  | number | 实际写入的长度。 |

Z
zhangxingxia 已提交
937
**示例:**
Z
zhangxingxia 已提交
938
  ```js
Z
zengyawen 已提交
939 940 941 942 943 944 945 946 947
  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 已提交
948
计算文件的哈希值,使用Promise异步回调。
Z
zengyawen 已提交
949

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

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

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

Z
zhangxingxia 已提交
963
**示例:**
Z
zhangxingxia 已提交
964 965
  ```js
  fileio.hash(path, "sha256").then(function(str){
H
haonan_7 已提交
966
      console.info("calculate file hash succeed:"+ str);
Z
zhangxingxia 已提交
967 968 969
  }).catch(function(error){
      console.info("calculate file hash failed with error:"+ err);
  });
Z
zengyawen 已提交
970 971 972 973 974
  ```


## fileio.hash

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

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

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

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

Z
zhangxingxia 已提交
988
**示例:**
Z
zhangxingxia 已提交
989
  ```js
Z
zengyawen 已提交
990
  fileio.hash(fpath, "sha256", function(err, hashStr) {
Z
zhangxingxia 已提交
991
      if (hashStr) {
H
haonan_7 已提交
992
          console.info("calculate file hash succeed:"+ hashStr);
Z
zengyawen 已提交
993 994 995 996 997 998 999 1000 1001
      }
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1006
**参数:**
Z
zengyawen 已提交
1007 1008
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1009
| path   | string | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1010
| 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 已提交
1011

Z
zhangxingxia 已提交
1012
**返回值:**
H
HelloCrease 已提交
1013 1014
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1015
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1016

Z
zhangxingxia 已提交
1017
**示例:**
Z
zhangxingxia 已提交
1018 1019
  ```js
  fileio.chmod(path, mode).then(function() {
H
haonan_7 已提交
1020
      console.info("chmod succeed");
Z
zhangxingxia 已提交
1021 1022
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
1023 1024 1025 1026 1027 1028 1029 1030
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1035
**参数:**
Z
zengyawen 已提交
1036 1037
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1038
| path     | string                    | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1039 1040
| 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 已提交
1041

Z
zhangxingxia 已提交
1042
**示例:**
Z
zhangxingxia 已提交
1043 1044 1045
  ```js
  fileio.chmod(path, mode, function (err) {
      // do something
Z
zengyawen 已提交
1046 1047 1048 1049 1050 1051 1052 1053
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1058
**参数:**
Z
zengyawen 已提交
1059 1060
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1061
| path   | string | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1062
| 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 已提交
1063

Z
zhangxingxia 已提交
1064
**示例:**
Z
zhangxingxia 已提交
1065
  ```js
Z
zengyawen 已提交
1066 1067 1068 1069 1070 1071 1072 1073
  fileio.chmodSync(fpath, mode);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1078
**参数:**
H
HelloCrease 已提交
1079 1080
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
H
haonan_7 已提交
1081
  | fd   | number | 是    | 待获取文件状态的文件描述符。 |
Z
zengyawen 已提交
1082

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

Z
zhangxingxia 已提交
1088
**示例:**
Z
zhangxingxia 已提交
1089 1090
  ```js
  fileio.fstat(fd).then(function(stat){
H
haonan_7 已提交
1091
      console.info("fstat succeed:"+ JSON.stringify(stat));
Z
zhangxingxia 已提交
1092 1093 1094
  }).catch(function(err){
      console.info("fstat failed with error:"+ err);
  });
Z
zengyawen 已提交
1095 1096 1097 1098 1099 1100 1101
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1112
**示例:**
Z
zhangxingxia 已提交
1113
  ```js
Z
zengyawen 已提交
1114
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1115 1116
  fileio.fstat(fd, function (err) {
      // do something
Z
zengyawen 已提交
1117 1118 1119 1120 1121 1122 1123 1124
  });
  ```


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

fstatSync(fd: number): Stat

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

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

Z
zhangxingxia 已提交
1129
**参数:**
H
HelloCrease 已提交
1130 1131
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
H
haonan_7 已提交
1132
  | fd   | number | 是    | 待获取文件状态的文件描述符。 |
Z
zengyawen 已提交
1133

Z
zhangxingxia 已提交
1134
**返回值:**
H
HelloCrease 已提交
1135 1136
  | 类型            | 说明         |
  | ------------- | ---------- |
H
haonan_7 已提交
1137
  | [Stat](#stat) | 表示文件状态的具体信息。 |
Z
zengyawen 已提交
1138

Z
zhangxingxia 已提交
1139
**示例:**
Z
zhangxingxia 已提交
1140
  ```js
Z
zengyawen 已提交
1141 1142 1143 1144 1145 1146 1147
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1154
**参数:**
H
HelloCrease 已提交
1155 1156 1157
  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
H
haonan_7 已提交
1158
  | len  | number | 否    | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1159

Z
zhangxingxia 已提交
1160
**返回值:**
H
HelloCrease 已提交
1161 1162
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1163
  | Promise&lt;void&gt; | Promise对象。无返回值。|
Z
zengyawen 已提交
1164

Z
zhangxingxia 已提交
1165
**示例:**
Z
zhangxingxia 已提交
1166
  ```js
Z
zengyawen 已提交
1167
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1168
  fileio.ftruncate(fd, 5).then(function(err) {    
H
haonan_7 已提交
1169
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1170 1171
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1172 1173 1174 1175 1176 1177 1178 1179
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1191
**示例:**
Z
zhangxingxia 已提交
1192 1193 1194
  ```js
  fileio.ftruncate(fd, len, function(err){
      // do something
Z
zengyawen 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1207
**参数:**
H
HelloCrease 已提交
1208 1209 1210 1211
  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
  | len  | number | 否    | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1212

Z
zhangxingxia 已提交
1213
**示例:**
Z
zhangxingxia 已提交
1214
  ```js
Z
zhangxingxia 已提交
1215
  fileio.ftruncateSync(fd, len);
Z
zengyawen 已提交
1216 1217 1218 1219 1220
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1233
**返回值:**
H
HelloCrease 已提交
1234 1235
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1236
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1237

Z
zhangxingxia 已提交
1238
**示例:**
Z
zhangxingxia 已提交
1239 1240
  ```js
  fileio.truncate(path, len).then(function(){
H
haonan_7 已提交
1241
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1242 1243
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1244 1245 1246 1247 1248 1249 1250 1251
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1263
**示例:**
Z
zhangxingxia 已提交
1264 1265 1266
  ```js
  fileio.truncate(path, len, function(err){
      // do something
Z
zengyawen 已提交
1267 1268 1269 1270 1271 1272
  });
  ```


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

1273
truncateSync(path: string, len?: number): void
Z
zengyawen 已提交
1274 1275 1276

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

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

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

Z
zhangxingxia 已提交
1285
**示例:**
Z
zhangxingxia 已提交
1286
  ```js
Z
zhangxingxia 已提交
1287
  fileio.truncateSync(path, len);
Z
zengyawen 已提交
1288 1289 1290 1291 1292
  ```


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

1293 1294 1295 1296 1297
readText(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): Promise&lt;string&gt;
Z
zengyawen 已提交
1298

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

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

Z
zhangxingxia 已提交
1303
**参数:**
Z
zengyawen 已提交
1304 1305 1306 1307
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1308

Z
zhangxingxia 已提交
1309
**返回值:**
H
HelloCrease 已提交
1310 1311
  | 类型                    | 说明         |
  | --------------------- | ---------- |
H
haonan_7 已提交
1312
  | Promise&lt;string&gt; | Promise对象。返回读取文件的内容。 |
Z
zengyawen 已提交
1313

Z
zhangxingxia 已提交
1314
**示例:**
Z
zhangxingxia 已提交
1315 1316
  ```js
  fileio.readText(path).then(function(str) {
H
haonan_7 已提交
1317
      console.info("readText succeed:"+ str);
Z
zhangxingxia 已提交
1318 1319
  }).catch(function(err){
      console.info("readText failed with error:"+ err);
Z
zengyawen 已提交
1320 1321 1322 1323 1324 1325
  });
  ```


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

1326 1327 1328 1329 1330
readText(filePath: string, options: {
    position?: number;
    length?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1331

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

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

Z
zhangxingxia 已提交
1336
**参数:**
Z
zengyawen 已提交
1337 1338 1339
| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| filePath | string                      | 是   | 待读取文件的应用沙箱路径。                                   |
W
wangbo 已提交
1340 1341
| 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 已提交
1342

Z
zhangxingxia 已提交
1343
**示例:**
Z
zhangxingxia 已提交
1344
  ```js
Z
zengyawen 已提交
1345
  fileio.readText(path, function(err, str){
Z
zhangxingxia 已提交
1346
      // do something
Z
zengyawen 已提交
1347 1348 1349 1350 1351 1352
  });
  ```


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

1353 1354 1355 1356 1357
readTextSync(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): string
Z
zengyawen 已提交
1358 1359 1360

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

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

Z
zhangxingxia 已提交
1363
**参数:**
Z
zengyawen 已提交
1364 1365 1366 1367
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1368

Z
zhangxingxia 已提交
1369
**返回值:**
Z
zhangxingxia 已提交
1370 1371 1372
  | 类型   | 说明                 |
  | ------ | -------------------- |
  | string | 返回读取文件的内容。 |
Z
zengyawen 已提交
1373

Z
zhangxingxia 已提交
1374
**示例:**
Z
zhangxingxia 已提交
1375 1376
  ```js
  let str = fileio.readTextSync(path, {position: 1, length: 3});
Z
zengyawen 已提交
1377 1378 1379 1380 1381 1382 1383
  ```


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

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

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

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

Z
zhangxingxia 已提交
1388
**参数:**
Z
zengyawen 已提交
1389 1390
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
W
wangbo 已提交
1391
| path   | string | 是   | 目标文件的应用沙箱路径。 |
Z
zengyawen 已提交
1392

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

Z
zhangxingxia 已提交
1398
**示例:**
Z
zhangxingxia 已提交
1399 1400
  ```js
  fileio.lstat(path).then(function(stat){
H
haonan_7 已提交
1401
      console.info("get link status succeed:"+ number);
Z
zhangxingxia 已提交
1402 1403 1404
  }).catch(function(err){
      console.info("get link status failed with error:"+ err);
  });
Z
zengyawen 已提交
1405 1406 1407 1408 1409 1410 1411
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1422
**示例:**
Z
zhangxingxia 已提交
1423 1424 1425
  ```js
  fileio.lstat(path, function (err, stat) {
      // do something
Z
zengyawen 已提交
1426
  });
Z
zengyawen 已提交
1427 1428 1429 1430 1431 1432 1433
  ```


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

lstatSync(path:string): Stat

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

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

Z
zhangxingxia 已提交
1438
**参数:**
Z
zengyawen 已提交
1439 1440
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
W
wangbo 已提交
1441
| path   | string | 是   | 目标文件的应用沙箱路径。 |
Z
zengyawen 已提交
1442

Z
zhangxingxia 已提交
1443
**返回值:**
H
HelloCrease 已提交
1444 1445
  | 类型            | 说明         |
  | ------------- | ---------- |
Z
zengyawen 已提交
1446 1447
  | [Stat](#stat) | 表示文件的具体信息。 |

Z
zhangxingxia 已提交
1448
**示例:**
Z
zhangxingxia 已提交
1449
  ```js
Z
zengyawen 已提交
1450 1451 1452 1453 1454 1455
  let stat = fileio.lstatSync(path);
  ```


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

1456 1457 1458 1459 1460
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
1461

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

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

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

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

Z
zhangxingxia 已提交
1477
**示例:**
Z
zhangxingxia 已提交
1478 1479
  ```js
  fileio.read(new ArrayBuffer(4096)).then(function(readout){
H
haonan_7 已提交
1480
      console.info("read file data succeed");
Z
zhangxingxia 已提交
1481
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
1482 1483 1484
  }).catch(function(err){
      console.info("read file data failed with error:"+ err);
  });
Z
zengyawen 已提交
1485 1486 1487 1488 1489
  ```


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

1490 1491 1492 1493 1494
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
1495

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

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

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

Z
zhangxingxia 已提交
1507
**示例:**
Z
zhangxingxia 已提交
1508
  ```js
Z
zengyawen 已提交
1509 1510
  let buf = new ArrayBuffer(4096);
  fileio.read(buf, function (err, readOut) {
Z
zhangxingxia 已提交
1511
      if (readOut) {
H
haonan_7 已提交
1512
          console.info("read file data succeed");
Z
zhangxingxia 已提交
1513
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
1514 1515 1516 1517 1518 1519 1520 1521 1522
      }
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1527
**参数:**
Z
zengyawen 已提交
1528 1529 1530 1531
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1532

Z
zhangxingxia 已提交
1533
**返回值:**
H
HelloCrease 已提交
1534 1535
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1536
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1537

Z
zhangxingxia 已提交
1538
**示例:**
Z
zhangxingxia 已提交
1539
  ```js
Z
zhangxingxia 已提交
1540
  fileio.rename(oldPath, newPath).then(function() {
H
haonan_7 已提交
1541
      console.info("rename succeed");
Z
zhangxingxia 已提交
1542 1543
  }).catch(function(err){
      console.info("rename failed with error:"+ err);
Z
zengyawen 已提交
1544 1545 1546 1547 1548 1549 1550 1551
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1556
**参数:**
Z
zengyawen 已提交
1557 1558 1559 1560 1561
| 参数名   | 类型                      | 必填 | 说明                         |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | 是   | 目标文件的当前应用沙箱路径。 |
| newPath  | String                    | 是   | 目标文件的新应用沙箱路径。   |
| Callback | AsyncCallback&lt;void&gt; | 是   | 异步重命名文件之后的回调。   |
Z
zengyawen 已提交
1562

Z
zhangxingxia 已提交
1563
**示例:**
Z
zhangxingxia 已提交
1564
  ```js
Z
zhangxingxia 已提交
1565
  fileio.rename(oldPath, newPath, function(err){
Z
zengyawen 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
  });
  ```


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

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

以同步方法重命名文件。

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

Z
zhangxingxia 已提交
1578
**参数:**
Z
zengyawen 已提交
1579 1580 1581 1582
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1583

Z
zhangxingxia 已提交
1584
**示例:**
Z
zhangxingxia 已提交
1585
  ```js
Z
zhangxingxia 已提交
1586
  fileio.renameSync(oldPath, newPath);
Z
zengyawen 已提交
1587 1588 1589 1590 1591 1592 1593
  ```


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

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

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

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

Z
zhangxingxia 已提交
1598
**参数:**
H
HelloCrease 已提交
1599 1600 1601
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1602

Z
zhangxingxia 已提交
1603
**返回值:**
H
HelloCrease 已提交
1604 1605
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1606
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1607

Z
zhangxingxia 已提交
1608
**示例:**
Z
zhangxingxia 已提交
1609 1610
  ```js
  fileio.fsync(fd).then(function(){
H
haonan_7 已提交
1611
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1612 1613 1614
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
  });
Z
zengyawen 已提交
1615 1616 1617 1618 1619 1620 1621
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1632
**示例:**
Z
zhangxingxia 已提交
1633
  ```js
Z
zengyawen 已提交
1634
  fileio.fsync(fd, function(err){
Z
zhangxingxia 已提交
1635
      // do something
Z
zengyawen 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
  });
  ```


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

fsyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1648
**参数:**
H
HelloCrease 已提交
1649 1650 1651
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1652

Z
zhangxingxia 已提交
1653
**示例:**
Z
zhangxingxia 已提交
1654
  ```js
Z
zengyawen 已提交
1655 1656 1657 1658 1659 1660 1661 1662
  fileio.fyncsSync(fd);
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1672
**返回值:**
H
HelloCrease 已提交
1673 1674
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1675
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1676

Z
zhangxingxia 已提交
1677
**示例:**
Z
zhangxingxia 已提交
1678 1679
  ```js
  fileio.fdatasync(fd).then(function(err) {
H
haonan_7 已提交
1680
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1681 1682
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
Z
zengyawen 已提交
1683 1684 1685 1686 1687 1688 1689 1690
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1701
**示例:**
Z
zhangxingxia 已提交
1702
  ```js
Z
zengyawen 已提交
1703
  fileio.fdatasync (fd, function (err) {
Z
zhangxingxia 已提交
1704
      // do something
Z
zengyawen 已提交
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
  });
  ```


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

fdatasyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1717
**参数:**
H
HelloCrease 已提交
1718 1719 1720
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1721

Z
zhangxingxia 已提交
1722
**示例:**
Z
zhangxingxia 已提交
1723
  ```js
Z
zengyawen 已提交
1724 1725 1726 1727 1728 1729 1730 1731
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1736
**参数:**
Z
zengyawen 已提交
1737 1738 1739 1740
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1741

Z
zhangxingxia 已提交
1742
**返回值:**
H
HelloCrease 已提交
1743 1744
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1745
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1746

Z
zhangxingxia 已提交
1747
**示例:**
Z
zhangxingxia 已提交
1748 1749
  ```js
  fileio.symlink(target, srcPath).then(function() {
H
haonan_7 已提交
1750
      console.info("symlink succeed");
Z
zhangxingxia 已提交
1751 1752
  }).catch(function(err){
      console.info("symlink failed with error:"+ err);
Z
zengyawen 已提交
1753 1754 1755 1756 1757 1758 1759 1760
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1772
**示例:**
Z
zhangxingxia 已提交
1773 1774 1775
  ```js
  fileio.symlink(target, srcPath, function (err) {
      // do something
Z
zengyawen 已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1788
**参数:**
Z
zengyawen 已提交
1789 1790 1791 1792
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1793

Z
zhangxingxia 已提交
1794
**示例:**
Z
zhangxingxia 已提交
1795
  ```js
Z
zengyawen 已提交
1796 1797 1798 1799 1800 1801 1802 1803
  fileio.symlinkSync(target, srcPath);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1808
**参数:**
Z
zengyawen 已提交
1809 1810 1811 1812 1813
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID(UserID)。        |
| gid    | number | 是   | 新的GID(GroupID)。       |
Z
zengyawen 已提交
1814

Z
zhangxingxia 已提交
1815
**返回值:**
H
HelloCrease 已提交
1816 1817
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1818
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1819

Z
zhangxingxia 已提交
1820
**示例:**
Z
zhangxingxia 已提交
1821
  ```js
Z
zengyawen 已提交
1822
  let stat = fileio.statSync(path);
Z
zengyawen 已提交
1823
  fileio.chown(path, stat.uid, stat.gid).then(function(){
H
haonan_7 已提交
1824
      console.info("chown succeed");
Z
zhangxingxia 已提交
1825 1826 1827
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
1828 1829 1830 1831 1832 1833 1834
  ```


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

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

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

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

Z
zhangxingxia 已提交
1839
**参数:**
Z
zengyawen 已提交
1840 1841 1842 1843 1844 1845
| 参数名   | 类型                      | 必填 | 说明                           |
| -------- | ------------------------- | ---- | ------------------------------ |
| path     | string                    | 是   | 待改变文件的应用沙箱路径。     |
| uid      | number                    | 是   | 新的UID。                      |
| gid      | number                    | 是   | 新的GID。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
1846

Z
zhangxingxia 已提交
1847
**示例:**
Z
zhangxingxia 已提交
1848
  ```js
Z
zengyawen 已提交
1849 1850
  let stat = fileio.statSync(fpath)
  fileio.chown(path, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
1851
      // do something
Z
zengyawen 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1864
**参数:**
Z
zengyawen 已提交
1865 1866 1867 1868 1869
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
1870

Z
zhangxingxia 已提交
1871
**示例:**
Z
zhangxingxia 已提交
1872
  ```js
Z
zengyawen 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881
  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 已提交
1882
创建临时目录,使用Promise异步回调。
Z
zengyawen 已提交
1883

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

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

Z
zhangxingxia 已提交
1891
**返回值:**
Z
zhangxingxia 已提交
1892
  | 类型                   | 说明         |
H
HelloCrease 已提交
1893
  | --------------------- | ---------- |
H
haonan_7 已提交
1894
  | Promise&lt;string&gt; | Promise对象。返回生成的唯一目录路径。 |
Z
zengyawen 已提交
1895

Z
zhangxingxia 已提交
1896
**示例:**
Z
zhangxingxia 已提交
1897 1898
  ```js
  fileio.mkdtemp(path + "XXXX").then(function(path){
H
haonan_7 已提交
1899
      console.info("mkdtemp succeed:"+ path);
Z
zhangxingxia 已提交
1900 1901 1902
  }).catch(function(err){
      console.info("mkdtemp failed with error:"+ err);
  });
Z
zengyawen 已提交
1903 1904 1905 1906 1907 1908 1909
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1920
**示例:**
Z
zhangxingxia 已提交
1921
  ```js
Z
zengyawen 已提交
1922
  fileio.mkdtemp(path + "XXXX", function (err, res) {
Z
zhangxingxia 已提交
1923
      // do something
Z
zengyawen 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
  });
  ```


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

mkdtempSync(prefix: string): string

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

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

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

Z
zhangxingxia 已提交
1941
**返回值:**
Z
zhangxingxia 已提交
1942
  | 类型    | 说明         |
H
HelloCrease 已提交
1943
  | ------ | ---------- |
Z
zengyawen 已提交
1944 1945
  | string | 产生的唯一目录路径。 |

Z
zhangxingxia 已提交
1946
**示例:**
Z
zhangxingxia 已提交
1947
  ```js
Z
zengyawen 已提交
1948 1949 1950 1951 1952 1953 1954 1955
  let res = fileio.mkdtempSync(path + "XXXX");
  ```


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

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

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

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

Z
zhangxingxia 已提交
1960
**参数:**
H
HelloCrease 已提交
1961 1962 1963 1964
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
1965

Z
zhangxingxia 已提交
1966
**返回值:**
Z
zhangxingxia 已提交
1967
  | 类型                 | 说明                           |
H
HelloCrease 已提交
1968
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1969
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1970

Z
zhangxingxia 已提交
1971
**示例:**
Z
zhangxingxia 已提交
1972 1973
  ```js
  fileio.fchmod(fd, mode).then(function() {
H
haonan_7 已提交
1974
      console.info("chmod succeed");
Z
zhangxingxia 已提交
1975 1976
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
1977 1978 1979 1980 1981 1982 1983 1984
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1989
**参数:**
H
HelloCrease 已提交
1990 1991 1992 1993 1994
  | 参数名      | 类型                              | 必填   | 说明                                       |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | 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 已提交
1995

Z
zhangxingxia 已提交
1996
**示例:**
Z
zhangxingxia 已提交
1997
  ```js
Z
zengyawen 已提交
1998
  fileio.fchmod(fd, mode, function (err) {
Z
zhangxingxia 已提交
1999
      // do something
Z
zengyawen 已提交
2000 2001 2002 2003 2004 2005
  });
  ```


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

2006
fchmodSync(fd: number, mode: number): void
Z
zengyawen 已提交
2007 2008 2009

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

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

Z
zhangxingxia 已提交
2012
**参数:**
H
HelloCrease 已提交
2013 2014 2015 2016
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2017

Z
zhangxingxia 已提交
2018
**示例:**
Z
zhangxingxia 已提交
2019
  ```js
Z
zengyawen 已提交
2020 2021 2022 2023 2024 2025 2026 2027
   fileio.fchmodSync(fd, mode);
  ```


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

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

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

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

Z
zhangxingxia 已提交
2032
**参数:**
Z
zengyawen 已提交
2033 2034 2035 2036
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2037

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

Z
zhangxingxia 已提交
2043
**示例:**
Z
zhangxingxia 已提交
2044 2045
  ```js
  fileio.createStream(path, "r+").then(function(stream){
H
haonan_7 已提交
2046
      console.info("createStream succeed");
Z
zhangxingxia 已提交
2047 2048 2049
  }).catch(function(err){
      console.info("createStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2050 2051 2052 2053 2054 2055 2056
  ```


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

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

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

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

Z
zhangxingxia 已提交
2061
**参数:**
Z
zengyawen 已提交
2062 2063 2064 2065 2066
| 参数名   | 类型                                    | 必填 | 说明                                                         |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
2067

Z
zhangxingxia 已提交
2068
**示例:**
Z
zhangxingxia 已提交
2069 2070 2071
  ```js
  fileio.createStream(path, mode, function(err, stream){
      // do something
Z
zengyawen 已提交
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2084
**参数:**
Z
zengyawen 已提交
2085 2086 2087 2088
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2089

Z
zhangxingxia 已提交
2090
**返回值:**
Z
zhangxingxia 已提交
2091
  | 类型                | 说明        |
H
HelloCrease 已提交
2092
  | ------------------ | --------- |
Z
zengyawen 已提交
2093 2094
  | [Stream](#stream7) | 返回文件流的结果。 |

Z
zhangxingxia 已提交
2095
**示例:**
Z
zhangxingxia 已提交
2096
  ```js
Z
zengyawen 已提交
2097 2098 2099 2100 2101 2102 2103 2104
  let ss = fileio.createStreamSync(path, "r+");
  ```


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

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

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

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

Z
zhangxingxia 已提交
2109
**参数:**
H
HelloCrease 已提交
2110 2111 2112 2113
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2114

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

Z
zhangxingxia 已提交
2120
**示例:**
Z
zhangxingxia 已提交
2121 2122
  ```js
  fileio.fdopenStream(fd, mode).then(function(stream){
H
haonan_7 已提交
2123
      console.info("openStream succeed");
Z
zhangxingxia 已提交
2124 2125 2126
  }).catch(function(err){
      console.info("openStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2127 2128 2129 2130 2131 2132 2133
  ```


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

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

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

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

Z
zhangxingxia 已提交
2138
**参数:**
H
HelloCrease 已提交
2139 2140 2141 2142 2143
  | 参数名      | 类型                                       | 必填   | 说明                                       |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | 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 已提交
2144

Z
zhangxingxia 已提交
2145
**示例:**
Z
zhangxingxia 已提交
2146 2147 2148
  ```js
  fileio.fdopenStream(fd, mode, function (err, stream) {
      // do something
Z
zengyawen 已提交
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2161
**参数:**
H
HelloCrease 已提交
2162 2163 2164 2165
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2166

Z
zhangxingxia 已提交
2167
**返回值:**
Z
zhangxingxia 已提交
2168
  | 类型                | 说明        |
H
HelloCrease 已提交
2169
  | ------------------ | --------- |
Z
zengyawen 已提交
2170 2171
  | [Stream](#stream7) | 返回文件流的结果。 |

Z
zhangxingxia 已提交
2172
**示例:**
Z
zhangxingxia 已提交
2173
  ```js
Z
zengyawen 已提交
2174 2175 2176 2177 2178 2179 2180 2181
  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 已提交
2182
基于文件描述符改变文件所有者,使用Promise异步回调。
Z
zengyawen 已提交
2183

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

Z
zhangxingxia 已提交
2186
**参数:**
H
HelloCrease 已提交
2187 2188 2189 2190 2191
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2192

Z
zhangxingxia 已提交
2193
**返回值:**
H
HelloCrease 已提交
2194 2195
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
2196
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
2197

Z
zhangxingxia 已提交
2198
**示例:**
Z
zhangxingxia 已提交
2199
  ```js
Z
zengyawen 已提交
2200
  let stat = fileio.statSync(path);
Z
zhangxingxia 已提交
2201
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
H
haonan_7 已提交
2202
      console.info("chown succeed");
Z
zhangxingxia 已提交
2203 2204
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
Z
zengyawen 已提交
2205 2206 2207 2208 2209 2210 2211 2212
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2217
**参数:**
H
HelloCrease 已提交
2218 2219 2220 2221 2222 2223
  | 参数名      | 类型                        | 必填   | 说明              |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | 是    | 待改变文件的文件描述符。    |
  | uid      | number                    | 是    | 文件所有者的UID。      |
  | gid      | number                    | 是    | 文件所有组的GID。      |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
2224

Z
zhangxingxia 已提交
2225
**示例:**
Z
zhangxingxia 已提交
2226
  ```js
Z
zengyawen 已提交
2227 2228
  let stat = fileio.statSync(fpath);
  fileio.fchown(fd, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2229
      // do something
Z
zengyawen 已提交
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2242
**参数:**
H
HelloCrease 已提交
2243 2244 2245 2246 2247
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2248

Z
zhangxingxia 已提交
2249
**示例:**
Z
zhangxingxia 已提交
2250
  ```js
Z
zengyawen 已提交
2251 2252 2253 2254 2255 2256 2257 2258 2259
  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 已提交
2260
基于文件路径改变文件所有者,更改符号链接本身的所有者,而不是符号链接所指向的实际文件,使用Promise异步回调。
Z
zengyawen 已提交
2261

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

Z
zhangxingxia 已提交
2264
**参数:**
Z
zengyawen 已提交
2265 2266 2267 2268 2269
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2270

Z
zhangxingxia 已提交
2271
**返回值:**
H
HelloCrease 已提交
2272 2273
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
2274
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
2275

Z
zhangxingxia 已提交
2276
**示例:**
Z
zhangxingxia 已提交
2277
  ```js
Z
zengyawen 已提交
2278
  let stat = fileio.statSync(path);
Z
zhangxingxia 已提交
2279
  fileio.lchown(path, stat.uid, stat.gid).then(function() {
H
haonan_7 已提交
2280
      console.info("chown succeed");
Z
zhangxingxia 已提交
2281 2282 2283
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
2284 2285 2286 2287 2288 2289 2290
  ```


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

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

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

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

Z
zhangxingxia 已提交
2295
**参数:**
Z
zengyawen 已提交
2296 2297 2298 2299 2300 2301
| 参数名   | 类型                      | 必填 | 说明                           |
| -------- | ------------------------- | ---- | ------------------------------ |
| path     | string                    | 是   | 待打开文件的应用沙箱路径。     |
| uid      | number                    | 是   | 新的UID。                      |
| gid      | number                    | 是   | 新的GID。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
2302

Z
zhangxingxia 已提交
2303
**示例:**
Z
zhangxingxia 已提交
2304
  ```js
Z
zengyawen 已提交
2305 2306
  let stat = fileio.statSync(path);
  fileio.lchown(path, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2307
      // do something
Z
zengyawen 已提交
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2320
**参数:**
Z
zengyawen 已提交
2321 2322 2323 2324 2325
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2326

Z
zhangxingxia 已提交
2327
**示例:**
Z
zhangxingxia 已提交
2328
  ```js
Z
zengyawen 已提交
2329 2330 2331 2332 2333 2334 2335 2336 2337
  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 已提交
2338
监听文件或者目录的变化,使用callback异步回调。
Z
zengyawen 已提交
2339

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

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

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

Z
zhangxingxia 已提交
2354
**示例:**
Z
zhangxingxia 已提交
2355 2356 2357
  ```js
  fileio.createWatcher(filename, events, function(watcher){
      // do something
Z
zengyawen 已提交
2358 2359 2360 2361 2362 2363 2364 2365
  });
  ```


## Readout

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

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

H
HelloCrease 已提交
2368 2369 2370 2371 2372
| 名称        | 参数类型       | 可读   | 可写   | 说明                |
| --------- | ---------- | ---- | ---- | ----------------- |
| bytesRead | number     | 是    | 是    | 实际读取长度。           |
| offset    | number     | 是    | 是    | 读取数据相对于缓冲区首地址的偏移。 |
| buffer    | ArrayBufer | 是    | 是    | 保存读取数据的缓冲区。       |
Z
zengyawen 已提交
2373 2374 2375 2376 2377


## Stat

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

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

Z
zengyawen 已提交
2381
### 属性
Z
zengyawen 已提交
2382

H
HelloCrease 已提交
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
| 名称     | 参数类型   | 可读   | 可写   | 说明                                       |
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| 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 已提交
2397 2398


Z
zengyawen 已提交
2399
### isBlockDevice
Z
zengyawen 已提交
2400

Z
zengyawen 已提交
2401
isBlockDevice(): boolean
Z
zengyawen 已提交
2402

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

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

Z
zhangxingxia 已提交
2407
**返回值:**
H
HelloCrease 已提交
2408 2409
  | 类型      | 说明               |
  | ------- | ---------------- |
Z
zhangxingxia 已提交
2410
  | boolean | 表示文件是否是块特殊设备。 |
Z
zengyawen 已提交
2411

Z
zhangxingxia 已提交
2412
**示例:**
Z
zhangxingxia 已提交
2413
  ```js
Z
zengyawen 已提交
2414 2415
  let isBLockDevice = fileio.statSync(path).isBlockDevice();
  ```
Z
zengyawen 已提交
2416 2417


Z
zengyawen 已提交
2418
### isCharacterDevice
Z
zengyawen 已提交
2419

Z
zengyawen 已提交
2420
isCharacterDevice(): boolean
Z
zengyawen 已提交
2421

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

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

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

Z
zhangxingxia 已提交
2431
**示例:**
Z
zhangxingxia 已提交
2432
  ```js
Z
zengyawen 已提交
2433 2434
  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
  ```
Z
zengyawen 已提交
2435 2436


Z
zengyawen 已提交
2437
### isDirectory
Z
zengyawen 已提交
2438

Z
zengyawen 已提交
2439
isDirectory(): boolean
Z
zengyawen 已提交
2440

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

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

Z
zhangxingxia 已提交
2445
**返回值:**
H
HelloCrease 已提交
2446 2447
  | 类型      | 说明            |
  | ------- | ------------- |
Z
zhangxingxia 已提交
2448
  | boolean | 表示文件是否是目录。 |
Z
zengyawen 已提交
2449

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


Z
zengyawen 已提交
2456
### isFIFO
Z
zengyawen 已提交
2457

Z
zengyawen 已提交
2458
isFIFO(): boolean
Z
zengyawen 已提交
2459

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

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

Z
zhangxingxia 已提交
2464
**返回值:**
H
HelloCrease 已提交
2465 2466
  | 类型      | 说明                    |
  | ------- | --------------------- |
Z
zhangxingxia 已提交
2467
  | boolean | 表示文件是否是&nbsp;FIFO。 |
Z
zengyawen 已提交
2468

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


Z
zengyawen 已提交
2475
### isFile
Z
zengyawen 已提交
2476

Z
zengyawen 已提交
2477
isFile(): boolean
Z
zengyawen 已提交
2478

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

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

Z
zhangxingxia 已提交
2483
**返回值:**
H
HelloCrease 已提交
2484 2485
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
2486
  | boolean | 表示文件是否是普通文件。 |
Z
zengyawen 已提交
2487

Z
zhangxingxia 已提交
2488
**示例:**
Z
zhangxingxia 已提交
2489
  ```js
Z
zengyawen 已提交
2490
  let isFile = fileio.statSync(fpath).isFile();
Z
zengyawen 已提交
2491
  ```
Z
zengyawen 已提交
2492 2493


Z
zengyawen 已提交
2494
### isSocket
Z
zengyawen 已提交
2495

Z
zengyawen 已提交
2496
isSocket(): boolean
Z
zengyawen 已提交
2497

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

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

Z
zhangxingxia 已提交
2502
**返回值:**
H
HelloCrease 已提交
2503 2504
  | 类型      | 说明             |
  | ------- | -------------- |
Z
zhangxingxia 已提交
2505
  | boolean | 表示文件是否是套接字。 |
Z
zengyawen 已提交
2506

Z
zhangxingxia 已提交
2507
**示例:**
Z
zhangxingxia 已提交
2508
  ```js
Z
zengyawen 已提交
2509 2510
  let isSocket = fileio.statSync(path).isSocket(); 
  ```
Z
zengyawen 已提交
2511 2512


Z
zengyawen 已提交
2513
### isSymbolicLink
Z
zengyawen 已提交
2514

Z
zengyawen 已提交
2515
isSymbolicLink(): boolean
Z
zengyawen 已提交
2516

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

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

Z
zhangxingxia 已提交
2521
**返回值:**
H
HelloCrease 已提交
2522 2523
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
2524
  | boolean | 表示文件是否是符号链接。 |
Z
zengyawen 已提交
2525

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


Z
zengyawen 已提交
2532 2533 2534 2535 2536 2537 2538
## Watcher<sup>7+</sup>

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


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

2539
stop(): Promise&lt;void&gt;
Z
zengyawen 已提交
2540

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

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

Z
zhangxingxia 已提交
2545
**示例:**
Z
zhangxingxia 已提交
2546
  ```js
Z
zengyawen 已提交
2547 2548 2549 2550 2551 2552
  fileio.stop();
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2564
**示例:**
Z
zhangxingxia 已提交
2565
  ```js
Z
zengyawen 已提交
2566
  fileio.stop(function(err){
Z
zhangxingxia 已提交
2567
      // do something
Z
zengyawen 已提交
2568 2569 2570 2571
  });
  ```


Z
zengyawen 已提交
2572
## Stream<sup>7+</sup>
Z
zengyawen 已提交
2573

Z
zengyawen 已提交
2574 2575 2576 2577 2578 2579 2580
文件流,在调用Stream的方法前,需要先通过createStream()方法(同步或异步)来构建一个Stream实例。


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

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

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

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

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

Z
zhangxingxia 已提交
2590
**示例:**
Z
zhangxingxia 已提交
2591
  ```js
Z
zengyawen 已提交
2592
  let ss= fileio.createStreamSync(path);
Z
zhangxingxia 已提交
2593
  ss.close().then(function(){
H
haonan_7 已提交
2594
      console.info("close fileStream succeed");
Z
zhangxingxia 已提交
2595 2596 2597
  }).catch(function(err){
      console.info("close fileStream  failed with error:"+ err);
  });
Z
zengyawen 已提交
2598 2599 2600 2601 2602 2603 2604
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2614
**示例:**
Z
zhangxingxia 已提交
2615
  ```js
Z
zengyawen 已提交
2616 2617
  let ss= fileio.createStreamSync(path);
  ss.close(function (err) {
Z
zhangxingxia 已提交
2618
      // do something
Z
zengyawen 已提交
2619 2620
  });
  ```
Z
zengyawen 已提交
2621 2622


2623
### closeSync
Z
zengyawen 已提交
2624

Z
zengyawen 已提交
2625
closeSync(): void
Z
zengyawen 已提交
2626

Z
zengyawen 已提交
2627
同步关闭文件流。
Z
zengyawen 已提交
2628

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

Z
zhangxingxia 已提交
2631
**示例:**
Z
zhangxingxia 已提交
2632
  ```js
Z
zengyawen 已提交
2633 2634 2635
  let ss= fileio.createStreamSync(path);
  ss.closeSync();
  ```
Z
zengyawen 已提交
2636 2637


Z
zengyawen 已提交
2638 2639 2640 2641
### flush<sup>7+</sup>

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

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

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

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

Z
zhangxingxia 已提交
2651
**示例:**
Z
zhangxingxia 已提交
2652
  ```js
Z
zengyawen 已提交
2653
  let ss= fileio.createStreamSync(path);
Z
zhangxingxia 已提交
2654
  ss.flush().then(function (){
H
haonan_7 已提交
2655
      console.info("flush succeed");
Z
zhangxingxia 已提交
2656 2657 2658
  }).catch(function(err){
      console.info("flush failed with error:"+ err);
  });
Z
zengyawen 已提交
2659 2660 2661 2662 2663 2664 2665
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2675
**示例:**
Z
zhangxingxia 已提交
2676
  ```js
Z
zengyawen 已提交
2677 2678
  let ss= fileio.createStreamSync(path);
  ss.flush(function (err) {
Z
zhangxingxia 已提交
2679
      // do something
Z
zengyawen 已提交
2680 2681 2682 2683
  });
  ```


Z
zengyawen 已提交
2684
### flushSync<sup>7+</sup>
Z
zengyawen 已提交
2685

Z
zengyawen 已提交
2686
flushSync(): void
Z
zengyawen 已提交
2687

Z
zengyawen 已提交
2688
同步刷新文件流。
Z
zengyawen 已提交
2689

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

Z
zhangxingxia 已提交
2692
**示例:**
Z
zhangxingxia 已提交
2693
  ```js
Z
zengyawen 已提交
2694 2695 2696
  let ss= fileio.createStreamSync(path);
  ss.flushSync();
  ```
Z
zengyawen 已提交
2697 2698


Z
zengyawen 已提交
2699 2700
### write<sup>7+</sup>

2701 2702 2703 2704 2705 2706
write(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
2707

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

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

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

Z
zhangxingxia 已提交
2718
**返回值:**
H
HelloCrease 已提交
2719 2720
  | 类型                    | 说明       |
  | --------------------- | -------- |
H
haonan_7 已提交
2721
  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
Z
zengyawen 已提交
2722

Z
zhangxingxia 已提交
2723
**示例:**
Z
zhangxingxia 已提交
2724
  ```js
Z
zengyawen 已提交
2725
  let ss= fileio.createStreamSync(fpath, "r+");
Z
zhangxingxia 已提交
2726
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
H
haonan_7 已提交
2727
      console.info("write succeed and size is:"+ number);
Z
zhangxingxia 已提交
2728 2729 2730
  }).catch(function(err){
      console.info("write failed with error:"+ err);
  });
Z
zengyawen 已提交
2731 2732 2733 2734 2735
  ```


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

2736 2737 2738 2739 2740 2741
write(buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
2742

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

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

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

Z
zhangxingxia 已提交
2754
**示例:**
Z
zhangxingxia 已提交
2755
  ```js
Z
zengyawen 已提交
2756 2757
  let ss= fileio.createStreamSync(fpath, "r+");
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
Z
zhangxingxia 已提交
2758
      if (bytesWritten) {
Z
zhangxingxia 已提交
2759
         // do something
H
haonan_7 已提交
2760
         console.info("write succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
2761 2762 2763 2764 2765
      }
  });
  ```


Z
zengyawen 已提交
2766
### writeSync<sup>7+</sup>
Z
zengyawen 已提交
2767

2768 2769 2770 2771 2772 2773
writeSync(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
2774

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

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

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

Z
zhangxingxia 已提交
2785
**返回值:**
H
HelloCrease 已提交
2786 2787
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
2788
  | number | 实际写入的长度。 |
Z
zengyawen 已提交
2789

Z
zhangxingxia 已提交
2790
**示例:**
Z
zhangxingxia 已提交
2791
  ```js
Z
zengyawen 已提交
2792
  let ss= fileio.createStreamSync(fpath,"r+");
Z
zengyawen 已提交
2793 2794 2795 2796 2797 2798
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


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

2799 2800 2801 2802 2803
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
2804

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

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

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

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

Z
zhangxingxia 已提交
2820
**示例:**
Z
zhangxingxia 已提交
2821
  ```js
Z
zengyawen 已提交
2822
  let ss = fileio.createStreamSync(fpath, "r+");
Z
zhangxingxia 已提交
2823
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readout){
H
haonan_7 已提交
2824
      console.info("read data succeed");
Z
zhangxingxia 已提交
2825
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
2826 2827 2828
  }).catch(function(err){
      console.info("read data failed with error:"+ err);
  });
Z
zengyawen 已提交
2829 2830 2831 2832 2833
  ```


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

2834 2835 2836 2837 2838
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
2839

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

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

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

Z
zhangxingxia 已提交
2851
**示例:**
Z
zhangxingxia 已提交
2852
  ```js
Z
zengyawen 已提交
2853 2854
  let ss = fileio.createStreamSync(fpath, "r+");
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
Z
zhangxingxia 已提交
2855
      if (readOut) {
H
haonan_7 已提交
2856
          console.info("read data succeed");
Z
zhangxingxia 已提交
2857
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
2858 2859
      }
  });
Z
zengyawen 已提交
2860
  ```
Z
zengyawen 已提交
2861 2862


Z
zengyawen 已提交
2863
### readSync<sup>7+</sup>
Z
zengyawen 已提交
2864

2865 2866 2867 2868 2869
readSync(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): number
Z
zengyawen 已提交
2870 2871 2872

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

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

Z
zhangxingxia 已提交
2875
**参数:**
Z
zhangxingxia 已提交
2876

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

Z
zhangxingxia 已提交
2882
**返回值:**
Z
zhangxingxia 已提交
2883

H
HelloCrease 已提交
2884 2885
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
2886
  | number | 实际读取的长度。 |
Z
zhangxingxia 已提交
2887

Z
zhangxingxia 已提交
2888
**示例:**
Z
zhangxingxia 已提交
2889
  ```js
Z
zengyawen 已提交
2890
  let ss = fileio.createStreamSync(fpath, "r+");
Z
zengyawen 已提交
2891
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
Z
zengyawen 已提交
2892
  ```
Z
zengyawen 已提交
2893 2894


Z
zengyawen 已提交
2895
## Dir
Z
zengyawen 已提交
2896

Z
zengyawen 已提交
2897 2898 2899 2900 2901 2902 2903
管理目录,在调用Dir的方法前,需要先通过[opendir()](#fileioopendir)方法(同步或异步)来构建一个Dir实例。


### read

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

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

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

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

Z
zhangxingxia 已提交
2913
**示例:**
Z
zhangxingxia 已提交
2914
  ```js
H
HelloCrease 已提交
2915
  let dir = fileio.opendirSync(path);
Z
zhangxingxia 已提交
2916
  dir.read().then(function (dirent){
H
haonan_7 已提交
2917
      console.log("read succeed:"+JSON.stringify(dirent));
Z
zhangxingxia 已提交
2918 2919 2920
  }).catch(function(err){
      console.info("read failed with error:"+ err);
  });
Z
zengyawen 已提交
2921 2922 2923 2924 2925 2926 2927
  ```


### read

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

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

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

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

Z
zhangxingxia 已提交
2937
**示例:**
Z
zhangxingxia 已提交
2938
  ```js
H
HelloCrease 已提交
2939
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2940
  dir.read(function (err, dirent) {
Z
zhangxingxia 已提交
2941
      if (dirent) {
Z
zhangxingxia 已提交
2942
          // do something
H
haonan_7 已提交
2943
          console.log("read succeed:"+JSON.stringify(dirent));
Z
zengyawen 已提交
2944 2945 2946
      }
  });
  ```
Z
zengyawen 已提交
2947 2948


Z
zengyawen 已提交
2949
### readSync
Z
zengyawen 已提交
2950

Z
zengyawen 已提交
2951
readSync(): Dirent
Z
zengyawen 已提交
2952

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

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

Z
zhangxingxia 已提交
2957
**返回值:**
H
HelloCrease 已提交
2958 2959
  | 类型                | 说明       |
  | ----------------- | -------- |
Z
zengyawen 已提交
2960
  | [Dirent](#dirent) | 表示一个目录项。 |
Z
zengyawen 已提交
2961

Z
zhangxingxia 已提交
2962
**示例:**
Z
zhangxingxia 已提交
2963
  ```js
H
HelloCrease 已提交
2964
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2965 2966
  let dirent = dir.readSync();
  ```
Z
zengyawen 已提交
2967 2968


Z
zengyawen 已提交
2969
### closeSync
Z
zengyawen 已提交
2970

Z
zengyawen 已提交
2971
closeSync(): void
Z
zengyawen 已提交
2972

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

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

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


Z
zengyawen 已提交
2984
## Dirent
Z
zengyawen 已提交
2985

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

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

Z
zengyawen 已提交
2990
### 属性
Z
zengyawen 已提交
2991

H
HelloCrease 已提交
2992 2993 2994
| 名称   | 参数类型   | 可读   | 可写   | 说明      |
| ---- | ------ | ---- | ---- | ------- |
| name | string | 是    | 否    | 目录项的名称。 |
Z
zengyawen 已提交
2995 2996


Z
zengyawen 已提交
2997
### isBlockDevice
Z
zengyawen 已提交
2998

Z
zengyawen 已提交
2999
isBlockDevice(): boolean
Z
zengyawen 已提交
3000

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

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

Z
zhangxingxia 已提交
3005
**返回值:**
H
HelloCrease 已提交
3006 3007
  | 类型      | 说明               |
  | ------- | ---------------- |
Z
zhangxingxia 已提交
3008
  | boolean | 表示当前目录项是否是块特殊设备。 |
Z
zengyawen 已提交
3009

Z
zhangxingxia 已提交
3010
**示例:**
Z
zhangxingxia 已提交
3011
  ```js
H
HelloCrease 已提交
3012
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3013 3014
  let isBLockDevice = dir.readSync().isBlockDevice();
  ```
Z
zengyawen 已提交
3015 3016


Z
zengyawen 已提交
3017
### isCharacterDevice
Z
zengyawen 已提交
3018

Z
zengyawen 已提交
3019
isCharacterDevice(): boolean
Z
zengyawen 已提交
3020

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

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

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

Z
zhangxingxia 已提交
3030
**示例:**
Z
zhangxingxia 已提交
3031
  ```js
H
HelloCrease 已提交
3032
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3033 3034
  let isCharacterDevice = dir.readSync().isCharacterDevice(); 
  ```
Z
zengyawen 已提交
3035 3036


Z
zengyawen 已提交
3037
### isDirectory
Z
zengyawen 已提交
3038

Z
zengyawen 已提交
3039
isDirectory(): boolean
Z
zengyawen 已提交
3040

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

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

Z
zhangxingxia 已提交
3045
**返回值:**
H
HelloCrease 已提交
3046 3047
  | 类型      | 说明            |
  | ------- | ------------- |
Z
zhangxingxia 已提交
3048
  | boolean | 表示当前目录项是否是目录。 |
Z
zengyawen 已提交
3049

Z
zhangxingxia 已提交
3050
**示例:**
Z
zhangxingxia 已提交
3051
  ```js
H
HelloCrease 已提交
3052
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3053 3054
  let isDirectory = dir.readSync().isDirectory(); 
  ```
Z
zengyawen 已提交
3055 3056


Z
zengyawen 已提交
3057
### isFIFO
Z
zengyawen 已提交
3058

Z
zengyawen 已提交
3059
isFIFO(): boolean
Z
zengyawen 已提交
3060

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

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

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

Z
zhangxingxia 已提交
3070
**示例:**
Z
zhangxingxia 已提交
3071
  ```js
H
HelloCrease 已提交
3072
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3073 3074
  let isFIFO = dir.readSync().isFIFO(); 
  ```
Z
zengyawen 已提交
3075 3076


Z
zengyawen 已提交
3077
### isFile
Z
zengyawen 已提交
3078

Z
zengyawen 已提交
3079
isFile(): boolean
Z
zengyawen 已提交
3080

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

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

Z
zhangxingxia 已提交
3085
**返回值:**
H
HelloCrease 已提交
3086 3087
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
3088
  | boolean | 表示当前目录项是否是普通文件。 |
Z
zengyawen 已提交
3089

Z
zhangxingxia 已提交
3090
**示例:**
Z
zhangxingxia 已提交
3091
  ```js
H
HelloCrease 已提交
3092
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3093 3094
  let isFile = dir.readSync().isFile(); 
  ```
Z
zengyawen 已提交
3095 3096


Z
zengyawen 已提交
3097
### isSocket
Z
zengyawen 已提交
3098

Z
zengyawen 已提交
3099
isSocket(): boolean
Z
zengyawen 已提交
3100

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

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

Z
zhangxingxia 已提交
3105
**返回值:**
H
HelloCrease 已提交
3106 3107
  | 类型      | 说明             |
  | ------- | -------------- |
Z
zhangxingxia 已提交
3108
  | boolean | 表示当前目录项是否是套接字。 |
Z
zengyawen 已提交
3109

Z
zhangxingxia 已提交
3110
**示例:**
Z
zhangxingxia 已提交
3111
  ```js
Z
zengyawen 已提交
3112 3113 3114
  let dir = fileio.opendirSync(dpath);
  let isSocket = dir.readSync().isSocket(); 
  ```
Z
zengyawen 已提交
3115 3116


Z
zengyawen 已提交
3117
### isSymbolicLink
Z
zengyawen 已提交
3118

Z
zengyawen 已提交
3119
isSymbolicLink(): boolean
Z
zengyawen 已提交
3120

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

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

Z
zhangxingxia 已提交
3125
**返回值:**
H
HelloCrease 已提交
3126 3127
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
3128
  | boolean | 表示当前目录项是否是符号链接。 |
Z
zengyawen 已提交
3129

Z
zhangxingxia 已提交
3130
**示例:**
Z
zhangxingxia 已提交
3131
  ```js
H
HelloCrease 已提交
3132
  let dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3133 3134
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```