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




Z
zengyawen 已提交
334
## fileio.copyFile
Z
zengyawen 已提交
335

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

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

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

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

Z
zhangxingxia 已提交
349
**返回值:**
H
HelloCrease 已提交
350 351
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
352
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
353

Z
zhangxingxia 已提交
354
**示例:**
Z
zhangxingxia 已提交
355
  ```js
W
wangbo 已提交
356 357
  let dest = "";
  fileio.copyFile(path, dest).then(function(){
H
haonan_7 已提交
358
      console.info("copyFile succeed");
Z
zhangxingxia 已提交
359 360 361
  }).catch(function(err){
      console.info("copyFile failed with error:"+ err);
  });
Z
zengyawen 已提交
362 363 364
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
381
**示例:**
Z
zhangxingxia 已提交
382
  ```js
W
wangbo 已提交
383 384
  let dest = "";
  fileio.copyFile(path, dest, function (err) {
Z
zhangxingxia 已提交
385
      // do something
Z
zengyawen 已提交
386
  });
Z
zengyawen 已提交
387 388 389
  ```


Z
zengyawen 已提交
390
## fileio.copyFileSync
Z
zengyawen 已提交
391

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

Z
zengyawen 已提交
394
以同步方法复制文件。
Z
zengyawen 已提交
395

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

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

Z
zhangxingxia 已提交
405
**示例:**
Z
zhangxingxia 已提交
406
  ```js
W
wangbo 已提交
407 408
  let dest = "";
  fileio.copyFileSync(path, dest);
Z
zengyawen 已提交
409 410 411
  ```


Z
zengyawen 已提交
412
## fileio.mkdir
Z
zengyawen 已提交
413

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

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

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

Z
zhangxingxia 已提交
420
**参数:**
Z
zengyawen 已提交
421 422 423 424
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
425

Z
zhangxingxia 已提交
426
**返回值:**
H
HelloCrease 已提交
427 428
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
429
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
430

Z
zhangxingxia 已提交
431
**示例:**
Z
zhangxingxia 已提交
432 433
  ```js
  fileio.mkdir(path).then(function() {
H
haonan_7 已提交
434
      console.info("mkdir succeed");
Z
zhangxingxia 已提交
435 436 437
  }).catch(function (error){
      console.info("mkdir failed with error:"+ error);
  });
Z
zengyawen 已提交
438 439 440
  ```


Z
zengyawen 已提交
441
## fileio.mkdir
Z
zengyawen 已提交
442

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

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

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

Z
zhangxingxia 已提交
449
**参数:**
Z
zengyawen 已提交
450 451 452 453 454
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
455

Z
zhangxingxia 已提交
456
**示例:**
Z
zhangxingxia 已提交
457 458
  ```js
  fileio.mkdir(path, function(err) {
H
haonan_7 已提交
459
    console.info("mkdir succeed");
Z
zengyawen 已提交
460
  });
Z
zengyawen 已提交
461 462 463
  ```


Z
zengyawen 已提交
464
## fileio.mkdirSync
Z
zengyawen 已提交
465

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

Z
zengyawen 已提交
468
以同步方法创建目录。
Z
zengyawen 已提交
469

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

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

Z
zhangxingxia 已提交
478
**示例:**
Z
zhangxingxia 已提交
479
  ```js
Z
zengyawen 已提交
480 481 482 483 484 485 486 487
  fileio.mkdirSync(path);
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
499
**返回值:**
H
HelloCrease 已提交
500 501
  | 类型                    | 说明          |
  | --------------------- | ----------- |
H
haonan_7 已提交
502
  | Promise&lt;number&gt; | Promise对象。返回打开文件的文件描述符。 |
Z
zengyawen 已提交
503

Z
zhangxingxia 已提交
504
**示例:**
Z
zhangxingxia 已提交
505 506
  ```js
  fileio.open(path, 0o1, 0o0200).then(function(number){
H
haonan_7 已提交
507
      console.info("open file succeed");
Z
zhangxingxia 已提交
508 509 510
  }).catch(function(error){
      console.info("open file failed with error:"+ err);
  });
Z
zengyawen 已提交
511 512 513
  ```


Z
zengyawen 已提交
514
## fileio.open<sup>7+</sup>
Z
zengyawen 已提交
515

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

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

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

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

Z
zhangxingxia 已提交
530
**示例:**
Z
zhangxingxia 已提交
531 532 533
  ```js
  fileio.open(path, 0, function(err, fd) {
      // do something
Z
zengyawen 已提交
534
  });
Z
zengyawen 已提交
535 536 537
  ```


Z
zengyawen 已提交
538
## fileio.openSync
Z
zengyawen 已提交
539

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

Z
zengyawen 已提交
542
以同步方法打开文件。
Z
zengyawen 已提交
543

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

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

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

Z
zhangxingxia 已提交
558
**示例:**
Z
zhangxingxia 已提交
559
  ```js
W
wangbo 已提交
560
  let fd = fileio.openSync(path);
R
raoxian 已提交
561
  ```
Z
zengyawen 已提交
562 563


Z
zengyawen 已提交
564
## fileio.read
Z
zengyawen 已提交
565

566 567 568 569 570
read(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
571

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

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

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

Z
zhangxingxia 已提交
583
**返回值:**
Z
zhangxingxia 已提交
584

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

Z
zhangxingxia 已提交
589
**示例:**
Z
zhangxingxia 已提交
590
  ```js
Z
zengyawen 已提交
591 592
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
593
  fileio.read(fd, buf).then(function(readout){
H
haonan_7 已提交
594
      console.info("read file data succeed");
Z
zhangxingxia 已提交
595
      console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zhangxingxia 已提交
596 597 598
  }).catch(function(error){
      console.info("read file data failed with error:"+ error);
  });
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;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
609

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

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

Z
zhangxingxia 已提交
614
**参数:**
H
HelloCrease 已提交
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。  |
H
HelloCrease 已提交
620
  | callback | AsyncCallback&lt;[ReadOut](#readout)&gt; | 是    | 异步读取数据之后的回调。                             |
Z
zengyawen 已提交
621

Z
zhangxingxia 已提交
622
**示例:**
Z
zhangxingxia 已提交
623
  ```js
Z
zengyawen 已提交
624 625
  let fd = fileio.openSync(path, 0o2);
  let buf = new ArrayBuffer(4096);
Z
zhangxingxia 已提交
626
  fileio.read(fd, buf, function (err, readOut) {
Z
zhangxingxia 已提交
627
      if (readOut) {
H
haonan_7 已提交
628
          console.info("read file data succeed");
Z
zhangxingxia 已提交
629
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
630 631
      }
  });
Z
zengyawen 已提交
632
  ```
Z
zengyawen 已提交
633 634


Z
zengyawen 已提交
635
## fileio.readSync
Z
zengyawen 已提交
636

637 638 639 640 641
readSync(fd: number, buffer: ArrayBuffer, options?: {
    offset?: number;
    length?: number;
    position?: number;
}): number
Z
zengyawen 已提交
642 643 644

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

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

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

Z
zhangxingxia 已提交
654
**返回值:**
H
HelloCrease 已提交
655 656
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
657 658
  | number | 实际读取的长度。 |

Z
zhangxingxia 已提交
659
**示例:**
Z
zhangxingxia 已提交
660
  ```js
Z
zengyawen 已提交
661 662 663 664 665 666 667 668 669 670
  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 已提交
671
删除目录,使用Promise异步回调。
Z
zengyawen 已提交
672

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

Z
zhangxingxia 已提交
675
**参数:**
Z
zengyawen 已提交
676 677 678
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除目录的应用沙箱路径。 |
Z
zengyawen 已提交
679

Z
zhangxingxia 已提交
680
**返回值:**
H
HelloCrease 已提交
681 682
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
683
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
684

Z
zhangxingxia 已提交
685
**示例:**
Z
zhangxingxia 已提交
686 687
  ```js
  fileio.rmdir(path).then(function() {
H
haonan_7 已提交
688
      console.info("rmdir succeed");
Z
zhangxingxia 已提交
689 690
  }).catch(function(err){
      console.info("rmdir failed with error:"+ err);
Z
zengyawen 已提交
691 692 693 694 695 696 697 698
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
709
**示例:**
Z
zhangxingxia 已提交
710 711 712
  ```js
  fileio.rmdir(path, function(err){
      // do something
H
haonan_7 已提交
713
      console.info("rmdir succeed");
Z
zengyawen 已提交
714 715 716 717
  });
  ```


718
## fileio.rmdirSync<sup>7+</sup>
Z
zengyawen 已提交
719

720
rmdirSync(path: string): void
Z
zengyawen 已提交
721 722 723

以同步方法删除目录。

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

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

Z
zhangxingxia 已提交
731
**示例:**
Z
zhangxingxia 已提交
732
  ```js
Z
zengyawen 已提交
733 734 735 736 737 738 739 740
  fileio.rmdirSync(path);
  ```


## fileio.unlink

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

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

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

Z
zhangxingxia 已提交
745
**参数:**
Z
zengyawen 已提交
746 747 748
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待删除文件的应用沙箱路径。 |
Z
zengyawen 已提交
749

Z
zhangxingxia 已提交
750
**返回值:**
H
HelloCrease 已提交
751 752
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
753
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
754

Z
zhangxingxia 已提交
755
**示例:**
Z
zhangxingxia 已提交
756 757
  ```js
  fileio.unlink(path).then(function(){
H
haonan_7 已提交
758
      console.info("remove file succeed");
Z
zhangxingxia 已提交
759 760 761
  }).catch(function(error){
      console.info("remove file failed with error:"+ error);
  });
Z
zengyawen 已提交
762 763 764 765 766 767 768
  ```


## fileio.unlink

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

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

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

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

Z
zhangxingxia 已提交
779
**示例:**
Z
zhangxingxia 已提交
780 781
  ```js
  fileio.unlink(path, function(err) {
H
haonan_7 已提交
782
      console.info("remove file succeed");
Z
zengyawen 已提交
783 784 785 786 787 788 789 790 791 792
  });
  ```


## fileio.unlinkSync

unlinkSync(path: string): void

以同步方法删除文件。

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

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

Z
zhangxingxia 已提交
800
**示例:**
Z
zhangxingxia 已提交
801
  ```js
Z
zengyawen 已提交
802 803 804 805 806 807
  fileio.unlinkSync(path);
  ```


## fileio.write

808 809 810 811 812 813
write(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
814

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

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

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

Z
zhangxingxia 已提交
826
**返回值:**
H
HelloCrease 已提交
827 828
  | 类型                    | 说明       |
  | --------------------- | -------- |
H
haonan_7 已提交
829
  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
Z
zengyawen 已提交
830

Z
zhangxingxia 已提交
831
**示例:**
Z
zhangxingxia 已提交
832
  ```js
W
wangbo 已提交
833
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
Z
zhangxingxia 已提交
834
  fileio.write(fd, "hello, world").then(function(number){
H
haonan_7 已提交
835
       console.info("write data to file succeed and size is:"+ number);
Z
zhangxingxia 已提交
836 837 838
  }).catch(function(err){
      console.info("write data to file failed with error:"+ err);
  });
Z
zengyawen 已提交
839 840 841 842 843
  ```


## fileio.write

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

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

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

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

Z
zhangxingxia 已提交
863
**示例:**
Z
zhangxingxia 已提交
864
  ```js
Z
zengyawen 已提交
865 866
  let fd = fileio.openSync(path, 0o100 | 0o2, 0o666);
  fileio.write(fd, "hello, world", function (err, bytesWritten) {
Z
zhangxingxia 已提交
867
      if (bytesWritten) {
H
haonan_7 已提交
868
         console.info("write data to file succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
869 870 871 872 873 874 875
      }
  });
  ```


## fileio.writeSync

876 877 878 879 880 881
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
882 883 884

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

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

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

Z
zhangxingxia 已提交
894
**返回值:**
H
HelloCrease 已提交
895 896
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
897 898
  | number | 实际写入的长度。 |

Z
zhangxingxia 已提交
899
**示例:**
Z
zhangxingxia 已提交
900
  ```js
Z
zengyawen 已提交
901 902 903 904 905 906 907 908 909
  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 已提交
910
计算文件的哈希值,使用Promise异步回调。
Z
zengyawen 已提交
911

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

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

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

Z
zhangxingxia 已提交
925
**示例:**
Z
zhangxingxia 已提交
926 927
  ```js
  fileio.hash(path, "sha256").then(function(str){
H
haonan_7 已提交
928
      console.info("calculate file hash succeed:"+ str);
Z
zhangxingxia 已提交
929
  }).catch(function(error){
W
wangbo 已提交
930
      console.info("calculate file hash failed with error:"+ error);
Z
zhangxingxia 已提交
931
  });
Z
zengyawen 已提交
932 933 934 935 936
  ```


## fileio.hash

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

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

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

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

Z
zhangxingxia 已提交
950
**示例:**
Z
zhangxingxia 已提交
951
  ```js
W
wangbo 已提交
952
  fileio.hash(path, "sha256", function(err, hashStr) {
Z
zhangxingxia 已提交
953
      if (hashStr) {
H
haonan_7 已提交
954
          console.info("calculate file hash succeed:"+ hashStr);
Z
zengyawen 已提交
955 956 957 958 959 960 961 962 963
      }
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
968
**参数:**
Z
zengyawen 已提交
969 970
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
971
| path   | string | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
972
| 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 已提交
973

Z
zhangxingxia 已提交
974
**返回值:**
H
HelloCrease 已提交
975 976
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
977
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
978

Z
zhangxingxia 已提交
979
**示例:**
Z
zhangxingxia 已提交
980
  ```js
W
wangbo 已提交
981
  fileio.chmod(path, 0o400).then(function() {
H
haonan_7 已提交
982
      console.info("chmod succeed");
Z
zhangxingxia 已提交
983 984
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
985 986 987 988 989 990 991 992
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
997
**参数:**
Z
zengyawen 已提交
998 999
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
H
haonan_7 已提交
1000
| path     | string                    | 是   | 所需变更权限的文件的应用沙箱路径。                               |
Z
zengyawen 已提交
1001 1002
| 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 已提交
1003

Z
zhangxingxia 已提交
1004
**示例:**
Z
zhangxingxia 已提交
1005
  ```js
W
wangbo 已提交
1006
  fileio.chmod(path, 0o400, function (err) {
Z
zhangxingxia 已提交
1007
      // do something
Z
zengyawen 已提交
1008 1009 1010 1011 1012 1013 1014 1015
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1026
**示例:**
Z
zhangxingxia 已提交
1027
  ```js
W
wangbo 已提交
1028
  fileio.chmodSync(path, 0o400);
Z
zengyawen 已提交
1029 1030 1031 1032 1033 1034 1035
  ```


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

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

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

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

Z
zhangxingxia 已提交
1040
**参数:**
H
HelloCrease 已提交
1041 1042
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
H
haonan_7 已提交
1043
  | fd   | number | 是    | 待获取文件状态的文件描述符。 |
Z
zengyawen 已提交
1044

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

Z
zhangxingxia 已提交
1050
**示例:**
Z
zhangxingxia 已提交
1051
  ```js
W
wangbo 已提交
1052
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1053
  fileio.fstat(fd).then(function(stat){
H
haonan_7 已提交
1054
      console.info("fstat succeed:"+ JSON.stringify(stat));
Z
zhangxingxia 已提交
1055 1056 1057
  }).catch(function(err){
      console.info("fstat failed with error:"+ err);
  });
Z
zengyawen 已提交
1058 1059 1060 1061 1062 1063 1064
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1075
**示例:**
Z
zhangxingxia 已提交
1076
  ```js
Z
zengyawen 已提交
1077
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1078 1079
  fileio.fstat(fd, function (err) {
      // do something
Z
zengyawen 已提交
1080 1081 1082 1083 1084 1085 1086 1087
  });
  ```


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

fstatSync(fd: number): Stat

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

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

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

Z
zhangxingxia 已提交
1097
**返回值:**
H
HelloCrease 已提交
1098 1099
  | 类型            | 说明         |
  | ------------- | ---------- |
H
haonan_7 已提交
1100
  | [Stat](#stat) | 表示文件状态的具体信息。 |
Z
zengyawen 已提交
1101

Z
zhangxingxia 已提交
1102
**示例:**
Z
zhangxingxia 已提交
1103
  ```js
Z
zengyawen 已提交
1104 1105 1106 1107 1108 1109 1110
  let fd = fileio.openSync(path);
  let stat = fileio.fstatSync(fd);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1117
**参数:**
H
HelloCrease 已提交
1118 1119 1120
  | 参数名  | 类型     | 必填   | 说明               |
  | ---- | ------ | ---- | ---------------- |
  | fd   | number | 是    | 待截断文件的文件描述符。     |
H
haonan_7 已提交
1121
  | len  | number | 否    | 文件截断后的长度,以字节为单位。 |
Z
zengyawen 已提交
1122

Z
zhangxingxia 已提交
1123
**返回值:**
H
HelloCrease 已提交
1124 1125
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1126
  | Promise&lt;void&gt; | Promise对象。无返回值。|
Z
zengyawen 已提交
1127

Z
zhangxingxia 已提交
1128
**示例:**
Z
zhangxingxia 已提交
1129
  ```js
Z
zengyawen 已提交
1130
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1131
  fileio.ftruncate(fd, 5).then(function(err) {    
H
haonan_7 已提交
1132
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1133 1134
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1135 1136 1137 1138 1139 1140 1141 1142
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1154
**示例:**
Z
zhangxingxia 已提交
1155
  ```js
W
wangbo 已提交
1156 1157
  let fd = fileio.openSync(path);
  let len = 5;
Z
zhangxingxia 已提交
1158 1159
  fileio.ftruncate(fd, len, function(err){
      // do something
Z
zengyawen 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1178
**示例:**
Z
zhangxingxia 已提交
1179
  ```js
W
wangbo 已提交
1180 1181
  let fd = fileio.openSync(path);
  let len = 5;
Z
zhangxingxia 已提交
1182
  fileio.ftruncateSync(fd, len);
Z
zengyawen 已提交
1183 1184 1185 1186 1187
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1200
**返回值:**
H
HelloCrease 已提交
1201 1202
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1203
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1204

Z
zhangxingxia 已提交
1205
**示例:**
Z
zhangxingxia 已提交
1206
  ```js
W
wangbo 已提交
1207
  let len = 5;
Z
zhangxingxia 已提交
1208
  fileio.truncate(path, len).then(function(){
H
haonan_7 已提交
1209
      console.info("truncate file succeed");
Z
zhangxingxia 已提交
1210 1211
  }).catch(function(err){
      console.info("truncate file failed with error:"+ err);
Z
zengyawen 已提交
1212 1213 1214 1215 1216 1217 1218 1219
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1231
**示例:**
Z
zhangxingxia 已提交
1232
  ```js
W
wangbo 已提交
1233
  let len = 5;
Z
zhangxingxia 已提交
1234 1235
  fileio.truncate(path, len, function(err){
      // do something
Z
zengyawen 已提交
1236 1237 1238 1239 1240 1241
  });
  ```


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

1242
truncateSync(path: string, len?: number): void
Z
zengyawen 已提交
1243 1244 1245

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

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

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

Z
zhangxingxia 已提交
1254
**示例:**
Z
zhangxingxia 已提交
1255
  ```js
W
wangbo 已提交
1256
  let len = 5;
Z
zhangxingxia 已提交
1257
  fileio.truncateSync(path, len);
Z
zengyawen 已提交
1258 1259 1260 1261 1262
  ```


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

1263 1264 1265 1266 1267
readText(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): Promise&lt;string&gt;
Z
zengyawen 已提交
1268

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

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

Z
zhangxingxia 已提交
1273
**参数:**
Z
zengyawen 已提交
1274 1275 1276 1277
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1278

Z
zhangxingxia 已提交
1279
**返回值:**
H
HelloCrease 已提交
1280 1281
  | 类型                    | 说明         |
  | --------------------- | ---------- |
H
haonan_7 已提交
1282
  | Promise&lt;string&gt; | Promise对象。返回读取文件的内容。 |
Z
zengyawen 已提交
1283

Z
zhangxingxia 已提交
1284
**示例:**
Z
zhangxingxia 已提交
1285 1286
  ```js
  fileio.readText(path).then(function(str) {
H
haonan_7 已提交
1287
      console.info("readText succeed:"+ str);
Z
zhangxingxia 已提交
1288 1289
  }).catch(function(err){
      console.info("readText failed with error:"+ err);
Z
zengyawen 已提交
1290 1291 1292 1293 1294 1295
  });
  ```


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

1296 1297 1298 1299 1300
readText(filePath: string, options: {
    position?: number;
    length?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1301

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

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

Z
zhangxingxia 已提交
1306
**参数:**
Z
zengyawen 已提交
1307 1308 1309
| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| filePath | string                      | 是   | 待读取文件的应用沙箱路径。                                   |
W
wangbo 已提交
1310 1311
| 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 已提交
1312

Z
zhangxingxia 已提交
1313
**示例:**
Z
zhangxingxia 已提交
1314
  ```js
W
wangbo 已提交
1315
  fileio.readText(path, { position: pos, length: len, encoding: 'UTF-8' }, function (err, str){
Z
zhangxingxia 已提交
1316
      // do something
Z
zengyawen 已提交
1317 1318 1319 1320 1321 1322
  });
  ```


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

1323 1324 1325 1326 1327
readTextSync(filePath: string, options?: {
    position?: number;
    length?: number;
    encoding?: string;
}): string
Z
zengyawen 已提交
1328 1329 1330

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

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

Z
zhangxingxia 已提交
1333
**参数:**
Z
zengyawen 已提交
1334 1335 1336 1337
| 参数名   | 类型   | 必填 | 说明                                                         |
| -------- | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1338

Z
zhangxingxia 已提交
1339
**返回值:**
Z
zhangxingxia 已提交
1340 1341 1342
  | 类型   | 说明                 |
  | ------ | -------------------- |
  | string | 返回读取文件的内容。 |
Z
zengyawen 已提交
1343

Z
zhangxingxia 已提交
1344
**示例:**
Z
zhangxingxia 已提交
1345 1346
  ```js
  let str = fileio.readTextSync(path, {position: 1, length: 3});
Z
zengyawen 已提交
1347 1348 1349 1350 1351 1352 1353
  ```


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

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

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

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

Z
zhangxingxia 已提交
1358
**参数:**
Z
zengyawen 已提交
1359 1360
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
W
wangbo 已提交
1361
| path   | string | 是   | 目标文件的应用沙箱路径。 |
Z
zengyawen 已提交
1362

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

Z
zhangxingxia 已提交
1368
**示例:**
Z
zhangxingxia 已提交
1369 1370
  ```js
  fileio.lstat(path).then(function(stat){
W
wangbo 已提交
1371
      console.info("get link status succeed:"+ stat.rdev);
Z
zhangxingxia 已提交
1372 1373 1374
  }).catch(function(err){
      console.info("get link status failed with error:"+ err);
  });
Z
zengyawen 已提交
1375 1376 1377 1378 1379 1380 1381
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1392
**示例:**
Z
zhangxingxia 已提交
1393 1394 1395
  ```js
  fileio.lstat(path, function (err, stat) {
      // do something
Z
zengyawen 已提交
1396
  });
Z
zengyawen 已提交
1397 1398 1399 1400 1401 1402 1403
  ```


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

lstatSync(path:string): Stat

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

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

Z
zhangxingxia 已提交
1408
**参数:**
Z
zengyawen 已提交
1409 1410
| 参数名 | 类型   | 必填 | 说明                                   |
| ------ | ------ | ---- | -------------------------------------- |
W
wangbo 已提交
1411
| path   | string | 是   | 目标文件的应用沙箱路径。 |
Z
zengyawen 已提交
1412

Z
zhangxingxia 已提交
1413
**返回值:**
H
HelloCrease 已提交
1414 1415
  | 类型            | 说明         |
  | ------------- | ---------- |
Z
zengyawen 已提交
1416 1417
  | [Stat](#stat) | 表示文件的具体信息。 |

Z
zhangxingxia 已提交
1418
**示例:**
Z
zhangxingxia 已提交
1419
  ```js
Z
zengyawen 已提交
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
  let stat = fileio.lstatSync(path);
  ```




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

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

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

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

Z
zhangxingxia 已提交
1434
**参数:**
Z
zengyawen 已提交
1435 1436 1437 1438
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1439

Z
zhangxingxia 已提交
1440
**返回值:**
H
HelloCrease 已提交
1441 1442
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1443
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1444

Z
zhangxingxia 已提交
1445
**示例:**
Z
zhangxingxia 已提交
1446
  ```js
W
wangbo 已提交
1447 1448
  let newPath = path +"123";
  fileio.rename(path, newPath).then(function() {
H
haonan_7 已提交
1449
      console.info("rename succeed");
Z
zhangxingxia 已提交
1450 1451
  }).catch(function(err){
      console.info("rename failed with error:"+ err);
Z
zengyawen 已提交
1452 1453 1454 1455 1456 1457 1458 1459
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1464
**参数:**
Z
zengyawen 已提交
1465 1466 1467 1468 1469
| 参数名   | 类型                      | 必填 | 说明                         |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath  | string                    | 是   | 目标文件的当前应用沙箱路径。 |
| newPath  | String                    | 是   | 目标文件的新应用沙箱路径。   |
| Callback | AsyncCallback&lt;void&gt; | 是   | 异步重命名文件之后的回调。   |
Z
zengyawen 已提交
1470

Z
zhangxingxia 已提交
1471
**示例:**
Z
zhangxingxia 已提交
1472
  ```js
W
wangbo 已提交
1473 1474
  let newPath = path +"123";
  fileio.rename(path, newPath, function(err){
Z
zengyawen 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
  });
  ```


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

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

以同步方法重命名文件。

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

Z
zhangxingxia 已提交
1487
**参数:**
Z
zengyawen 已提交
1488 1489 1490 1491
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | 是   | 目标文件的当前应用沙箱路径。 |
| newPath | String | 是   | 目标文件的新应用沙箱路径。   |
Z
zengyawen 已提交
1492

Z
zhangxingxia 已提交
1493
**示例:**
Z
zhangxingxia 已提交
1494
  ```js
W
wangbo 已提交
1495 1496
  let newPath = path +"123";
  fileio.renameSync(path, newPath);
Z
zengyawen 已提交
1497 1498 1499 1500 1501 1502 1503
  ```


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

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

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

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

Z
zhangxingxia 已提交
1508
**参数:**
H
HelloCrease 已提交
1509 1510 1511
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1512

Z
zhangxingxia 已提交
1513
**返回值:**
H
HelloCrease 已提交
1514 1515
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1516
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1517

Z
zhangxingxia 已提交
1518
**示例:**
Z
zhangxingxia 已提交
1519
  ```js
W
wangbo 已提交
1520
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1521
  fileio.fsync(fd).then(function(){
H
haonan_7 已提交
1522
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1523 1524 1525
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
  });
Z
zengyawen 已提交
1526 1527 1528 1529 1530 1531 1532
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1543
**示例:**
W
wangbo 已提交
1544 1545
  ```js  
  let fd = fileio.openSync(path);
Z
zengyawen 已提交
1546
  fileio.fsync(fd, function(err){
Z
zhangxingxia 已提交
1547
      // do something
Z
zengyawen 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
  });
  ```


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

fsyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1560
**参数:**
H
HelloCrease 已提交
1561 1562 1563
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1564

Z
zhangxingxia 已提交
1565
**示例:**
Z
zhangxingxia 已提交
1566
  ```js
W
wangbo 已提交
1567
  let fd = fileio.openSync(path);
Z
zengyawen 已提交
1568 1569 1570 1571 1572 1573 1574 1575
  fileio.fyncsSync(fd);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1580
**参数:**
H
HelloCrease 已提交
1581 1582 1583
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1584

Z
zhangxingxia 已提交
1585
**返回值:**
H
HelloCrease 已提交
1586 1587
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1588
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1589

Z
zhangxingxia 已提交
1590
**示例:**
W
wangbo 已提交
1591 1592
  ```js  
  let fd = fileio.openSync(path);
Z
zhangxingxia 已提交
1593
  fileio.fdatasync(fd).then(function(err) {
H
haonan_7 已提交
1594
      console.info("sync data succeed");
Z
zhangxingxia 已提交
1595 1596
  }).catch(function(err){
      console.info("sync data failed with error:"+ err);
Z
zengyawen 已提交
1597 1598 1599 1600 1601 1602 1603 1604
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1615
**示例:**
Z
zhangxingxia 已提交
1616
  ```js
W
wangbo 已提交
1617
  let fd = fileio.openSync(path);
Z
zengyawen 已提交
1618
  fileio.fdatasync (fd, function (err) {
Z
zhangxingxia 已提交
1619
      // do something
Z
zengyawen 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
  });
  ```


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

fdatasyncSync(fd: number): void

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

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

Z
zhangxingxia 已提交
1632
**参数:**
H
HelloCrease 已提交
1633 1634 1635
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待同步文件的文件描述符。 |
Z
zengyawen 已提交
1636

Z
zhangxingxia 已提交
1637
**示例:**
Z
zhangxingxia 已提交
1638
  ```js
W
wangbo 已提交
1639
  let fd = fileio.openSync(path);
Z
zengyawen 已提交
1640 1641 1642 1643 1644 1645 1646 1647
  let stat = fileio.fdatasyncSync(fd);
  ```


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

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

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

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

Z
zhangxingxia 已提交
1652
**参数:**
Z
zengyawen 已提交
1653 1654 1655 1656
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1657

Z
zhangxingxia 已提交
1658
**返回值:**
H
HelloCrease 已提交
1659 1660
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1661
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1662

Z
zhangxingxia 已提交
1663
**示例:**
Z
zhangxingxia 已提交
1664
  ```js
W
wangbo 已提交
1665 1666
  let srcPath = "";
  fileio.symlink(path, srcPath).then(function() {
H
haonan_7 已提交
1667
      console.info("symlink succeed");
Z
zhangxingxia 已提交
1668 1669
  }).catch(function(err){
      console.info("symlink failed with error:"+ err);
Z
zengyawen 已提交
1670 1671 1672 1673 1674 1675 1676 1677
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1689
**示例:**
Z
zhangxingxia 已提交
1690
  ```js
W
wangbo 已提交
1691 1692
  let srcPath = "";
  fileio.symlink(path, srcPath, function (err) {
Z
zhangxingxia 已提交
1693
      // do something
Z
zengyawen 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1706
**参数:**
Z
zengyawen 已提交
1707 1708 1709 1710
| 参数名  | 类型   | 必填 | 说明                         |
| ------- | ------ | ---- | ---------------------------- |
| target  | string | 是   | 目标文件的应用沙箱路径。     |
| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
Z
zengyawen 已提交
1711

Z
zhangxingxia 已提交
1712
**示例:**
Z
zhangxingxia 已提交
1713
  ```js
W
wangbo 已提交
1714 1715
  let srcPath = "";
  fileio.symlinkSync(path, srcPath);
Z
zengyawen 已提交
1716 1717 1718 1719 1720 1721 1722
  ```


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

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

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

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

Z
zhangxingxia 已提交
1727
**参数:**
Z
zengyawen 已提交
1728 1729 1730 1731 1732
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID(UserID)。        |
| gid    | number | 是   | 新的GID(GroupID)。       |
Z
zengyawen 已提交
1733

Z
zhangxingxia 已提交
1734
**返回值:**
H
HelloCrease 已提交
1735 1736
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1737
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1738

Z
zhangxingxia 已提交
1739
**示例:**
Z
zhangxingxia 已提交
1740
  ```js
Z
zengyawen 已提交
1741
  let stat = fileio.statSync(path);
Z
zengyawen 已提交
1742
  fileio.chown(path, stat.uid, stat.gid).then(function(){
H
haonan_7 已提交
1743
      console.info("chown succeed");
Z
zhangxingxia 已提交
1744 1745 1746
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
  });
Z
zengyawen 已提交
1747 1748 1749 1750 1751 1752 1753
  ```


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

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

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

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

Z
zhangxingxia 已提交
1758
**参数:**
Z
zengyawen 已提交
1759 1760 1761 1762 1763 1764
| 参数名   | 类型                      | 必填 | 说明                           |
| -------- | ------------------------- | ---- | ------------------------------ |
| path     | string                    | 是   | 待改变文件的应用沙箱路径。     |
| uid      | number                    | 是   | 新的UID。                      |
| gid      | number                    | 是   | 新的GID。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
1765

Z
zhangxingxia 已提交
1766
**示例:**
Z
zhangxingxia 已提交
1767
  ```js
W
wangbo 已提交
1768
  let stat = fileio.statSync(path)
Z
zengyawen 已提交
1769
  fileio.chown(path, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
1770
      // do something
Z
zengyawen 已提交
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1783
**参数:**
Z
zengyawen 已提交
1784 1785 1786 1787 1788
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待改变文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
1789

Z
zhangxingxia 已提交
1790
**示例:**
Z
zhangxingxia 已提交
1791
  ```js
W
wangbo 已提交
1792
  let stat = fileio.statSync(path)
Z
zengyawen 已提交
1793 1794 1795 1796 1797 1798 1799 1800
  fileio.chownSync(path, stat.uid, stat.gid);
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1810
**返回值:**
Z
zhangxingxia 已提交
1811
  | 类型                   | 说明         |
H
HelloCrease 已提交
1812
  | --------------------- | ---------- |
H
haonan_7 已提交
1813
  | Promise&lt;string&gt; | Promise对象。返回生成的唯一目录路径。 |
Z
zengyawen 已提交
1814

Z
zhangxingxia 已提交
1815
**示例:**
Z
zhangxingxia 已提交
1816 1817
  ```js
  fileio.mkdtemp(path + "XXXX").then(function(path){
H
haonan_7 已提交
1818
      console.info("mkdtemp succeed:"+ path);
Z
zhangxingxia 已提交
1819 1820 1821
  }).catch(function(err){
      console.info("mkdtemp failed with error:"+ err);
  });
Z
zengyawen 已提交
1822 1823 1824 1825 1826 1827 1828
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
1839
**示例:**
Z
zhangxingxia 已提交
1840
  ```js
Z
zengyawen 已提交
1841
  fileio.mkdtemp(path + "XXXX", function (err, res) {
Z
zhangxingxia 已提交
1842
      // do something
Z
zengyawen 已提交
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
  });
  ```


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

mkdtempSync(prefix: string): string

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

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

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

Z
zhangxingxia 已提交
1860
**返回值:**
Z
zhangxingxia 已提交
1861
  | 类型    | 说明         |
H
HelloCrease 已提交
1862
  | ------ | ---------- |
Z
zengyawen 已提交
1863 1864
  | string | 产生的唯一目录路径。 |

Z
zhangxingxia 已提交
1865
**示例:**
Z
zhangxingxia 已提交
1866
  ```js
Z
zengyawen 已提交
1867 1868 1869 1870 1871 1872 1873 1874
  let res = fileio.mkdtempSync(path + "XXXX");
  ```


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

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

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

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

Z
zhangxingxia 已提交
1879
**参数:**
H
HelloCrease 已提交
1880 1881 1882 1883
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
1884

Z
zhangxingxia 已提交
1885
**返回值:**
Z
zhangxingxia 已提交
1886
  | 类型                 | 说明                           |
H
HelloCrease 已提交
1887
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
1888
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
1889

Z
zhangxingxia 已提交
1890
**示例:**
Z
zhangxingxia 已提交
1891
  ```js
W
wangbo 已提交
1892 1893
  let fd = fileio.openSync(path);
  let mode = 0o400;
Z
zhangxingxia 已提交
1894
  fileio.fchmod(fd, mode).then(function() {
H
haonan_7 已提交
1895
      console.info("chmod succeed");
Z
zhangxingxia 已提交
1896 1897
  }).catch(function(err){
      console.info("chmod failed with error:"+ err);
Z
zengyawen 已提交
1898 1899 1900 1901 1902 1903 1904 1905
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
1910
**参数:**
H
HelloCrease 已提交
1911 1912 1913 1914 1915
  | 参数名      | 类型                              | 必填   | 说明                                       |
  | -------- | ------------------------------- | ---- | ---------------------------------------- |
  | 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 已提交
1916

Z
zhangxingxia 已提交
1917
**示例:**
Z
zhangxingxia 已提交
1918
  ```js
W
wangbo 已提交
1919 1920
  let fd = fileio.openSync(path);
  let mode = 0o400;
Z
zengyawen 已提交
1921
  fileio.fchmod(fd, mode, function (err) {
Z
zhangxingxia 已提交
1922
      // do something
Z
zengyawen 已提交
1923 1924 1925 1926 1927 1928
  });
  ```


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

1929
fchmodSync(fd: number, mode: number): void
Z
zengyawen 已提交
1930 1931 1932

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

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

Z
zhangxingxia 已提交
1935
**参数:**
H
HelloCrease 已提交
1936 1937 1938 1939
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
1940

Z
zhangxingxia 已提交
1941
**示例:**
Z
zhangxingxia 已提交
1942
  ```js
W
wangbo 已提交
1943 1944 1945
  let fd = fileio.openSync(path);
  let mode = 0o400;
  fileio.fchmodSync(fd, mode);
Z
zengyawen 已提交
1946 1947 1948 1949 1950 1951 1952
  ```


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

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

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

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

Z
zhangxingxia 已提交
1957
**参数:**
Z
zengyawen 已提交
1958 1959 1960 1961
| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
1962

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

Z
zhangxingxia 已提交
1968
**示例:**
Z
zhangxingxia 已提交
1969 1970
  ```js
  fileio.createStream(path, "r+").then(function(stream){
H
haonan_7 已提交
1971
      console.info("createStream succeed");
Z
zhangxingxia 已提交
1972 1973 1974
  }).catch(function(err){
      console.info("createStream failed with error:"+ err);
  });
Z
zengyawen 已提交
1975 1976 1977 1978 1979 1980 1981
  ```


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

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

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

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

Z
zhangxingxia 已提交
1986
**参数:**
Z
zengyawen 已提交
1987 1988 1989 1990 1991
| 参数名   | 类型                                    | 必填 | 说明                                                         |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| 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 已提交
1992

Z
zhangxingxia 已提交
1993
**示例:**
Z
zhangxingxia 已提交
1994
  ```js
W
wangbo 已提交
1995
  let mode = 0o400;
Z
zhangxingxia 已提交
1996 1997
  fileio.createStream(path, mode, function(err, stream){
      // do something
Z
zengyawen 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
  });
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2016
**返回值:**
Z
zhangxingxia 已提交
2017
  | 类型                | 说明        |
H
HelloCrease 已提交
2018
  | ------------------ | --------- |
Z
zengyawen 已提交
2019 2020
  | [Stream](#stream7) | 返回文件流的结果。 |

Z
zhangxingxia 已提交
2021
**示例:**
Z
zhangxingxia 已提交
2022
  ```js
Z
zengyawen 已提交
2023 2024 2025 2026 2027 2028 2029 2030
  let ss = fileio.createStreamSync(path, "r+");
  ```


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

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

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

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

Z
zhangxingxia 已提交
2035
**参数:**
H
HelloCrease 已提交
2036 2037 2038 2039
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2040

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

Z
zhangxingxia 已提交
2046
**示例:**
Z
zhangxingxia 已提交
2047
  ```js
W
wangbo 已提交
2048 2049
  let fd = fileio.openSync(path);
  let mode = 0o400;
Z
zhangxingxia 已提交
2050
  fileio.fdopenStream(fd, mode).then(function(stream){
H
haonan_7 已提交
2051
      console.info("openStream succeed");
Z
zhangxingxia 已提交
2052 2053 2054
  }).catch(function(err){
      console.info("openStream failed with error:"+ err);
  });
Z
zengyawen 已提交
2055 2056 2057 2058 2059 2060 2061
  ```


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

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

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

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

Z
zhangxingxia 已提交
2066
**参数:**
H
HelloCrease 已提交
2067 2068 2069 2070 2071
  | 参数名      | 类型                                       | 必填   | 说明                                       |
  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
  | 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 已提交
2072

Z
zhangxingxia 已提交
2073
**示例:**
Z
zhangxingxia 已提交
2074
  ```js
W
wangbo 已提交
2075 2076
  let fd = fileio.openSync(path);
  let mode = 0o400;
Z
zhangxingxia 已提交
2077 2078
  fileio.fdopenStream(fd, mode, function (err, stream) {
      // do something
Z
zengyawen 已提交
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2091
**参数:**
H
HelloCrease 已提交
2092 2093 2094 2095
  | 参数名  | 类型     | 必填   | 说明                                       |
  | ---- | ------ | ---- | ---------------------------------------- |
  | 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 已提交
2096

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

Z
zhangxingxia 已提交
2102
**示例:**
Z
zhangxingxia 已提交
2103
  ```js
W
wangbo 已提交
2104
  let fd = fileio.openSync(path);
Z
zengyawen 已提交
2105 2106 2107 2108 2109 2110 2111 2112
  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 已提交
2113
基于文件描述符改变文件所有者,使用Promise异步回调。
Z
zengyawen 已提交
2114

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

Z
zhangxingxia 已提交
2117
**参数:**
H
HelloCrease 已提交
2118 2119 2120 2121 2122
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2123

Z
zhangxingxia 已提交
2124
**返回值:**
H
HelloCrease 已提交
2125 2126
  | 类型                  | 说明                           |
  | ------------------- | ---------------------------- |
H
haonan_7 已提交
2127
  | Promise&lt;void&gt; | Promise对象。无返回值。 |
Z
zengyawen 已提交
2128

Z
zhangxingxia 已提交
2129
**示例:**
Z
zhangxingxia 已提交
2130
  ```js
W
wangbo 已提交
2131
  let fd = fileio.openSync(path);
Z
zengyawen 已提交
2132
  let stat = fileio.statSync(path);
Z
zhangxingxia 已提交
2133
  fileio.fchown(fd, stat.uid, stat.gid).then(function() {
H
haonan_7 已提交
2134
      console.info("chown succeed");
Z
zhangxingxia 已提交
2135 2136
  }).catch(function(err){
      console.info("chown failed with error:"+ err);
Z
zengyawen 已提交
2137 2138 2139 2140 2141 2142 2143 2144
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2149
**参数:**
H
HelloCrease 已提交
2150 2151 2152 2153 2154 2155
  | 参数名      | 类型                        | 必填   | 说明              |
  | -------- | ------------------------- | ---- | --------------- |
  | fd       | number                    | 是    | 待改变文件的文件描述符。    |
  | uid      | number                    | 是    | 文件所有者的UID。      |
  | gid      | number                    | 是    | 文件所有组的GID。      |
  | callback | AsyncCallback&lt;void&gt; | 是    | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
2156

Z
zhangxingxia 已提交
2157
**示例:**
Z
zhangxingxia 已提交
2158
  ```js
W
wangbo 已提交
2159 2160
  let fd = fileio.openSync(path);
  let stat = fileio.statSync(path);
Z
zengyawen 已提交
2161
  fileio.fchown(fd, stat.uid, stat.gid, function (err){
Z
zhangxingxia 已提交
2162
      // do something
Z
zengyawen 已提交
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
  });
  ```


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

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

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

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

Z
zhangxingxia 已提交
2175
**参数:**
H
HelloCrease 已提交
2176 2177 2178 2179 2180
  | 参数名  | 类型     | 必填   | 说明           |
  | ---- | ------ | ---- | ------------ |
  | fd   | number | 是    | 待改变文件的文件描述符。 |
  | uid  | number | 是    | 文件所有者的UID。   |
  | gid  | number | 是    | 文件所有组的GID。   |
Z
zengyawen 已提交
2181

Z
zhangxingxia 已提交
2182
**示例:**
Z
zhangxingxia 已提交
2183
  ```js
W
wangbo 已提交
2184 2185
  let fd = fileio.openSync(path);
  let stat = fileio.statSync(path);
Z
zengyawen 已提交
2186 2187 2188 2189 2190 2191 2192 2193
  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 已提交
2194
基于文件路径改变文件所有者,更改符号链接本身的所有者,而不是符号链接所指向的实际文件,使用Promise异步回调。
Z
zengyawen 已提交
2195

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

Z
zhangxingxia 已提交
2198
**参数:**
Z
zengyawen 已提交
2199 2200 2201 2202 2203
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2204

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

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


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

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

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

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

Z
zhangxingxia 已提交
2229
**参数:**
Z
zengyawen 已提交
2230 2231 2232 2233 2234 2235
| 参数名   | 类型                      | 必填 | 说明                           |
| -------- | ------------------------- | ---- | ------------------------------ |
| path     | string                    | 是   | 待打开文件的应用沙箱路径。     |
| uid      | number                    | 是   | 新的UID。                      |
| gid      | number                    | 是   | 新的GID。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 异步改变文件所有者之后的回调。 |
Z
zengyawen 已提交
2236

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


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

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

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

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

Z
zhangxingxia 已提交
2254
**参数:**
Z
zengyawen 已提交
2255 2256 2257 2258 2259
| 参数名 | 类型   | 必填 | 说明                       |
| ------ | ------ | ---- | -------------------------- |
| path   | string | 是   | 待打开文件的应用沙箱路径。 |
| uid    | number | 是   | 新的UID。                  |
| gid    | number | 是   | 新的GID。                  |
Z
zengyawen 已提交
2260

Z
zhangxingxia 已提交
2261
**示例:**
Z
zhangxingxia 已提交
2262
  ```js
Z
zengyawen 已提交
2263 2264 2265 2266 2267 2268 2269 2270 2271
  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 已提交
2272
监听文件或者目录的变化,使用callback异步回调。
Z
zengyawen 已提交
2273

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

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

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

Z
zhangxingxia 已提交
2288
**示例:**
Z
zhangxingxia 已提交
2289
  ```js
W
wangbo 已提交
2290 2291
  let filename = path +"/test.txt";
  let events = 1;
Z
zhangxingxia 已提交
2292 2293
  fileio.createWatcher(filename, events, function(watcher){
      // do something
Z
zengyawen 已提交
2294 2295 2296 2297 2298 2299 2300 2301
  });
  ```


## Readout

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

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

H
HelloCrease 已提交
2304 2305 2306 2307 2308
| 名称        | 参数类型       | 可读   | 可写   | 说明                |
| --------- | ---------- | ---- | ---- | ----------------- |
| bytesRead | number     | 是    | 是    | 实际读取长度。           |
| offset    | number     | 是    | 是    | 读取数据相对于缓冲区首地址的偏移。 |
| buffer    | ArrayBufer | 是    | 是    | 保存读取数据的缓冲区。       |
Z
zengyawen 已提交
2309 2310 2311 2312 2313


## Stat

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

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

Z
zengyawen 已提交
2317
### 属性
Z
zengyawen 已提交
2318

H
HelloCrease 已提交
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
| 名称     | 参数类型   | 可读   | 可写   | 说明                                       |
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| 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 已提交
2333 2334


Z
zengyawen 已提交
2335
### isBlockDevice
Z
zengyawen 已提交
2336

Z
zengyawen 已提交
2337
isBlockDevice(): boolean
Z
zengyawen 已提交
2338

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

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

Z
zhangxingxia 已提交
2343
**返回值:**
H
HelloCrease 已提交
2344 2345
  | 类型      | 说明               |
  | ------- | ---------------- |
Z
zhangxingxia 已提交
2346
  | boolean | 表示文件是否是块特殊设备。 |
Z
zengyawen 已提交
2347

Z
zhangxingxia 已提交
2348
**示例:**
Z
zhangxingxia 已提交
2349
  ```js
Z
zengyawen 已提交
2350 2351
  let isBLockDevice = fileio.statSync(path).isBlockDevice();
  ```
Z
zengyawen 已提交
2352 2353


Z
zengyawen 已提交
2354
### isCharacterDevice
Z
zengyawen 已提交
2355

Z
zengyawen 已提交
2356
isCharacterDevice(): boolean
Z
zengyawen 已提交
2357

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

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

Z
zhangxingxia 已提交
2362
**返回值:**
H
HelloCrease 已提交
2363 2364
  | 类型      | 说明                |
  | ------- | ----------------- |
Z
zhangxingxia 已提交
2365
  | boolean | 表示文件是否是字符特殊设备。 |
Z
zengyawen 已提交
2366

Z
zhangxingxia 已提交
2367
**示例:**
Z
zhangxingxia 已提交
2368
  ```js
Z
zengyawen 已提交
2369 2370
  let isCharacterDevice = fileio.statSync(path).isCharacterDevice();
  ```
Z
zengyawen 已提交
2371 2372


Z
zengyawen 已提交
2373
### isDirectory
Z
zengyawen 已提交
2374

Z
zengyawen 已提交
2375
isDirectory(): boolean
Z
zengyawen 已提交
2376

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

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

Z
zhangxingxia 已提交
2381
**返回值:**
H
HelloCrease 已提交
2382 2383
  | 类型      | 说明            |
  | ------- | ------------- |
Z
zhangxingxia 已提交
2384
  | boolean | 表示文件是否是目录。 |
Z
zengyawen 已提交
2385

Z
zhangxingxia 已提交
2386
**示例:**
Z
zhangxingxia 已提交
2387
  ```js
Z
zengyawen 已提交
2388
  let isDirectory = fileio.statSync(path).isDirectory(); 
Z
zengyawen 已提交
2389
  ```
Z
zengyawen 已提交
2390 2391


Z
zengyawen 已提交
2392
### isFIFO
Z
zengyawen 已提交
2393

Z
zengyawen 已提交
2394
isFIFO(): boolean
Z
zengyawen 已提交
2395

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

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

Z
zhangxingxia 已提交
2400
**返回值:**
H
HelloCrease 已提交
2401 2402
  | 类型      | 说明                    |
  | ------- | --------------------- |
Z
zhangxingxia 已提交
2403
  | boolean | 表示文件是否是&nbsp;FIFO。 |
Z
zengyawen 已提交
2404

Z
zhangxingxia 已提交
2405
**示例:**
Z
zhangxingxia 已提交
2406
  ```js
Z
zengyawen 已提交
2407
  let isFIFO = fileio.statSync(path).isFIFO(); 
Z
zengyawen 已提交
2408
  ```
Z
zengyawen 已提交
2409 2410


Z
zengyawen 已提交
2411
### isFile
Z
zengyawen 已提交
2412

Z
zengyawen 已提交
2413
isFile(): boolean
Z
zengyawen 已提交
2414

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

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

Z
zhangxingxia 已提交
2419
**返回值:**
H
HelloCrease 已提交
2420 2421
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
2422
  | boolean | 表示文件是否是普通文件。 |
Z
zengyawen 已提交
2423

Z
zhangxingxia 已提交
2424
**示例:**
Z
zhangxingxia 已提交
2425
  ```js
W
wangbo 已提交
2426
  let isFile = fileio.statSync(path).isFile();
Z
zengyawen 已提交
2427
  ```
Z
zengyawen 已提交
2428 2429


Z
zengyawen 已提交
2430
### isSocket
Z
zengyawen 已提交
2431

Z
zengyawen 已提交
2432
isSocket(): boolean
Z
zengyawen 已提交
2433

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

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

Z
zhangxingxia 已提交
2438
**返回值:**
H
HelloCrease 已提交
2439 2440
  | 类型      | 说明             |
  | ------- | -------------- |
Z
zhangxingxia 已提交
2441
  | boolean | 表示文件是否是套接字。 |
Z
zengyawen 已提交
2442

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


Z
zengyawen 已提交
2449
### isSymbolicLink
Z
zengyawen 已提交
2450

Z
zengyawen 已提交
2451
isSymbolicLink(): boolean
Z
zengyawen 已提交
2452

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

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

Z
zhangxingxia 已提交
2457
**返回值:**
H
HelloCrease 已提交
2458 2459
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
2460
  | boolean | 表示文件是否是符号链接。 |
Z
zengyawen 已提交
2461

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


Z
zengyawen 已提交
2468 2469 2470 2471 2472 2473 2474
## Watcher<sup>7+</sup>

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


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

2475
stop(): Promise&lt;void&gt;
Z
zengyawen 已提交
2476

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

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

Z
zhangxingxia 已提交
2481
**示例:**
Z
zhangxingxia 已提交
2482
  ```js
Z
zengyawen 已提交
2483 2484 2485 2486 2487 2488
  fileio.stop();
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2500
**示例:**
Z
zhangxingxia 已提交
2501
  ```js
Z
zengyawen 已提交
2502
  fileio.stop(function(err){
Z
zhangxingxia 已提交
2503
      // do something
Z
zengyawen 已提交
2504 2505 2506 2507
  });
  ```


Z
zengyawen 已提交
2508
## Stream<sup>7+</sup>
Z
zengyawen 已提交
2509

Z
zengyawen 已提交
2510 2511 2512 2513 2514 2515 2516
文件流,在调用Stream的方法前,需要先通过createStream()方法(同步或异步)来构建一个Stream实例。


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

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

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

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

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

Z
zhangxingxia 已提交
2526
**示例:**
Z
zhangxingxia 已提交
2527
  ```js
W
wangbo 已提交
2528
  let ss= fileio.createStreamSync(path,"r+");
Z
zhangxingxia 已提交
2529
  ss.close().then(function(){
H
haonan_7 已提交
2530
      console.info("close fileStream succeed");
Z
zhangxingxia 已提交
2531 2532 2533
  }).catch(function(err){
      console.info("close fileStream  failed with error:"+ err);
  });
Z
zengyawen 已提交
2534 2535 2536 2537 2538 2539 2540
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2550
**示例:**
Z
zhangxingxia 已提交
2551
  ```js
Z
zengyawen 已提交
2552 2553
  let ss= fileio.createStreamSync(path);
  ss.close(function (err) {
Z
zhangxingxia 已提交
2554
      // do something
Z
zengyawen 已提交
2555 2556
  });
  ```
Z
zengyawen 已提交
2557 2558


2559
### closeSync
Z
zengyawen 已提交
2560

Z
zengyawen 已提交
2561
closeSync(): void
Z
zengyawen 已提交
2562

Z
zengyawen 已提交
2563
同步关闭文件流。
Z
zengyawen 已提交
2564

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

Z
zhangxingxia 已提交
2567
**示例:**
Z
zhangxingxia 已提交
2568
  ```js
W
wangbo 已提交
2569
  let ss= fileio.createStreamSync(path,"r+");
Z
zengyawen 已提交
2570 2571
  ss.closeSync();
  ```
Z
zengyawen 已提交
2572 2573


Z
zengyawen 已提交
2574 2575 2576 2577
### flush<sup>7+</sup>

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

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

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

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

Z
zhangxingxia 已提交
2587
**示例:**
Z
zhangxingxia 已提交
2588
  ```js
W
wangbo 已提交
2589
  let ss= fileio.createStreamSync(path,"r+");
Z
zhangxingxia 已提交
2590
  ss.flush().then(function (){
H
haonan_7 已提交
2591
      console.info("flush succeed");
Z
zhangxingxia 已提交
2592 2593 2594
  }).catch(function(err){
      console.info("flush failed with error:"+ err);
  });
Z
zengyawen 已提交
2595 2596 2597 2598 2599 2600 2601
  ```


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

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

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

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

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

Z
zhangxingxia 已提交
2611
**示例:**
Z
zhangxingxia 已提交
2612
  ```js
W
wangbo 已提交
2613
  let ss= fileio.createStreamSync(path,"r+");
Z
zengyawen 已提交
2614
  ss.flush(function (err) {
Z
zhangxingxia 已提交
2615
      // do something
Z
zengyawen 已提交
2616 2617 2618 2619
  });
  ```


Z
zengyawen 已提交
2620
### flushSync<sup>7+</sup>
Z
zengyawen 已提交
2621

Z
zengyawen 已提交
2622
flushSync(): void
Z
zengyawen 已提交
2623

Z
zengyawen 已提交
2624
同步刷新文件流。
Z
zengyawen 已提交
2625

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

Z
zhangxingxia 已提交
2628
**示例:**
Z
zhangxingxia 已提交
2629
  ```js
W
wangbo 已提交
2630
  let ss= fileio.createStreamSync(path,"r+");
Z
zengyawen 已提交
2631 2632
  ss.flushSync();
  ```
Z
zengyawen 已提交
2633 2634


Z
zengyawen 已提交
2635 2636
### write<sup>7+</sup>

2637 2638 2639 2640 2641 2642
write(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): Promise&lt;number&gt;
Z
zengyawen 已提交
2643

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

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

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

Z
zhangxingxia 已提交
2654
**返回值:**
H
HelloCrease 已提交
2655 2656
  | 类型                    | 说明       |
  | --------------------- | -------- |
H
haonan_7 已提交
2657
  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
Z
zengyawen 已提交
2658

Z
zhangxingxia 已提交
2659
**示例:**
Z
zhangxingxia 已提交
2660
  ```js
W
wangbo 已提交
2661
  let ss= fileio.createStreamSync(path, "r+");
Z
zhangxingxia 已提交
2662
  ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
H
haonan_7 已提交
2663
      console.info("write succeed and size is:"+ number);
Z
zhangxingxia 已提交
2664 2665 2666
  }).catch(function(err){
      console.info("write failed with error:"+ err);
  });
Z
zengyawen 已提交
2667 2668 2669 2670 2671
  ```


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

2672 2673 2674 2675 2676 2677
write(buffer: ArrayBuffer | string, options: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
2678

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

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

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

Z
zhangxingxia 已提交
2690
**示例:**
Z
zhangxingxia 已提交
2691
  ```js
W
wangbo 已提交
2692
  let ss= fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2693
  ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
Z
zhangxingxia 已提交
2694
      if (bytesWritten) {
Z
zhangxingxia 已提交
2695
         // do something
H
haonan_7 已提交
2696
         console.info("write succeed and size is:"+ bytesWritten);
Z
zengyawen 已提交
2697 2698 2699 2700 2701
      }
  });
  ```


Z
zengyawen 已提交
2702
### writeSync<sup>7+</sup>
Z
zengyawen 已提交
2703

2704 2705 2706 2707 2708 2709
writeSync(buffer: ArrayBuffer | string, options?: {
    offset?: number;
    length?: number;
    position?: number;
    encoding?: string;
}): number
Z
zengyawen 已提交
2710

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

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

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

Z
zhangxingxia 已提交
2721
**返回值:**
H
HelloCrease 已提交
2722 2723
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
2724
  | number | 实际写入的长度。 |
Z
zengyawen 已提交
2725

Z
zhangxingxia 已提交
2726
**示例:**
Z
zhangxingxia 已提交
2727
  ```js
W
wangbo 已提交
2728
  let ss= fileio.createStreamSync(path,"r+");
Z
zengyawen 已提交
2729 2730 2731 2732 2733 2734
  let num = ss.writeSync("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'});
  ```


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

2735 2736 2737 2738 2739
read(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): Promise&lt;ReadOut&gt;
Z
zengyawen 已提交
2740

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

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

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

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

Z
zhangxingxia 已提交
2756
**示例:**
Z
zhangxingxia 已提交
2757
  ```js
W
wangbo 已提交
2758
  let ss = fileio.createStreamSync(path, "r+");
Z
zhangxingxia 已提交
2759
  ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readout){
H
haonan_7 已提交
2760
      console.info("read data succeed");
W
wangbo 已提交
2761
      console.log(String.fromCharCode.apply(null, new Uint8Array(readout.buffer)));
Z
zhangxingxia 已提交
2762 2763 2764
  }).catch(function(err){
      console.info("read data failed with error:"+ err);
  });
Z
zengyawen 已提交
2765 2766 2767 2768 2769
  ```


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

2770 2771 2772 2773 2774
read(buffer: ArrayBuffer, options: {
    position?: number;
    offset?: number;
    length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
Z
zengyawen 已提交
2775

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

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

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

Z
zhangxingxia 已提交
2787
**示例:**
Z
zhangxingxia 已提交
2788
  ```js
W
wangbo 已提交
2789
  let ss = fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2790
  ss.read(new ArrayBuffer(4096),{offset: 1, length: 5, position: 5},function (err, readOut) {
Z
zhangxingxia 已提交
2791
      if (readOut) {
H
haonan_7 已提交
2792
          console.info("read data succeed");
Z
zhangxingxia 已提交
2793
          console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
Z
zengyawen 已提交
2794 2795
      }
  });
Z
zengyawen 已提交
2796
  ```
Z
zengyawen 已提交
2797 2798


Z
zengyawen 已提交
2799
### readSync<sup>7+</sup>
Z
zengyawen 已提交
2800

2801 2802 2803 2804 2805
readSync(buffer: ArrayBuffer, options?: {
    position?: number;
    offset?: number;
    length?: number;
}): number
Z
zengyawen 已提交
2806 2807 2808

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

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

Z
zhangxingxia 已提交
2811
**参数:**
Z
zhangxingxia 已提交
2812

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

Z
zhangxingxia 已提交
2818
**返回值:**
Z
zhangxingxia 已提交
2819

H
HelloCrease 已提交
2820 2821
  | 类型     | 说明       |
  | ------ | -------- |
Z
zengyawen 已提交
2822
  | number | 实际读取的长度。 |
Z
zhangxingxia 已提交
2823

Z
zhangxingxia 已提交
2824
**示例:**
Z
zhangxingxia 已提交
2825
  ```js
W
wangbo 已提交
2826
  let ss = fileio.createStreamSync(path, "r+");
Z
zengyawen 已提交
2827
  let num = ss.readSync(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5});
Z
zengyawen 已提交
2828
  ```
Z
zengyawen 已提交
2829 2830


Z
zengyawen 已提交
2831
## Dir
Z
zengyawen 已提交
2832

Z
zengyawen 已提交
2833 2834 2835 2836 2837 2838 2839
管理目录,在调用Dir的方法前,需要先通过[opendir()](#fileioopendir)方法(同步或异步)来构建一个Dir实例。


### read

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

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

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

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

Z
zhangxingxia 已提交
2849
**示例:**
Z
zhangxingxia 已提交
2850
  ```js
W
wangbo 已提交
2851
  dir = fileio.opendirSync(path);
Z
zhangxingxia 已提交
2852
  dir.read().then(function (dirent){
H
haonan_7 已提交
2853
      console.log("read succeed:"+JSON.stringify(dirent));
Z
zhangxingxia 已提交
2854 2855 2856
  }).catch(function(err){
      console.info("read failed with error:"+ err);
  });
Z
zengyawen 已提交
2857 2858 2859 2860 2861 2862 2863
  ```


### read

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

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

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

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

Z
zhangxingxia 已提交
2873
**示例:**
Z
zhangxingxia 已提交
2874
  ```js
W
wangbo 已提交
2875
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2876
  dir.read(function (err, dirent) {
Z
zhangxingxia 已提交
2877
      if (dirent) {
Z
zhangxingxia 已提交
2878
          // do something
H
haonan_7 已提交
2879
          console.log("read succeed:"+JSON.stringify(dirent));
Z
zengyawen 已提交
2880 2881 2882
      }
  });
  ```
Z
zengyawen 已提交
2883 2884


Z
zengyawen 已提交
2885
### readSync
Z
zengyawen 已提交
2886

Z
zengyawen 已提交
2887
readSync(): Dirent
Z
zengyawen 已提交
2888

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

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

Z
zhangxingxia 已提交
2893
**返回值:**
H
HelloCrease 已提交
2894 2895
  | 类型                | 说明       |
  | ----------------- | -------- |
Z
zengyawen 已提交
2896
  | [Dirent](#dirent) | 表示一个目录项。 |
Z
zengyawen 已提交
2897

Z
zhangxingxia 已提交
2898
**示例:**
Z
zhangxingxia 已提交
2899
  ```js
W
wangbo 已提交
2900
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2901 2902
  let dirent = dir.readSync();
  ```
Z
zengyawen 已提交
2903 2904


Z
zengyawen 已提交
2905
### closeSync
Z
zengyawen 已提交
2906

Z
zengyawen 已提交
2907
closeSync(): void
Z
zengyawen 已提交
2908

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

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

Z
zhangxingxia 已提交
2913
**示例:**
Z
zhangxingxia 已提交
2914
  ```js
W
wangbo 已提交
2915
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2916 2917
  dir.closeSync();
  ```
Z
zengyawen 已提交
2918 2919


Z
zengyawen 已提交
2920
## Dirent
Z
zengyawen 已提交
2921

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

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

Z
zengyawen 已提交
2926
### 属性
Z
zengyawen 已提交
2927

H
HelloCrease 已提交
2928 2929 2930
| 名称   | 参数类型   | 可读   | 可写   | 说明      |
| ---- | ------ | ---- | ---- | ------- |
| name | string | 是    | 否    | 目录项的名称。 |
Z
zengyawen 已提交
2931 2932


Z
zengyawen 已提交
2933
### isBlockDevice
Z
zengyawen 已提交
2934

Z
zengyawen 已提交
2935
isBlockDevice(): boolean
Z
zengyawen 已提交
2936

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

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

Z
zhangxingxia 已提交
2941
**返回值:**
H
HelloCrease 已提交
2942 2943
  | 类型      | 说明               |
  | ------- | ---------------- |
Z
zhangxingxia 已提交
2944
  | boolean | 表示当前目录项是否是块特殊设备。 |
Z
zengyawen 已提交
2945

Z
zhangxingxia 已提交
2946
**示例:**
Z
zhangxingxia 已提交
2947
  ```js
W
wangbo 已提交
2948
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2949 2950
  let isBLockDevice = dir.readSync().isBlockDevice();
  ```
Z
zengyawen 已提交
2951 2952


Z
zengyawen 已提交
2953
### isCharacterDevice
Z
zengyawen 已提交
2954

Z
zengyawen 已提交
2955
isCharacterDevice(): boolean
Z
zengyawen 已提交
2956

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

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

Z
zhangxingxia 已提交
2961
**返回值:**
H
HelloCrease 已提交
2962 2963
  | 类型      | 说明                |
  | ------- | ----------------- |
Z
zhangxingxia 已提交
2964
  | boolean | 表示当前目录项是否是字符特殊设备。 |
Z
zengyawen 已提交
2965

Z
zhangxingxia 已提交
2966
**示例:**
Z
zhangxingxia 已提交
2967
  ```js
W
wangbo 已提交
2968
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2969 2970
  let isCharacterDevice = dir.readSync().isCharacterDevice(); 
  ```
Z
zengyawen 已提交
2971 2972


Z
zengyawen 已提交
2973
### isDirectory
Z
zengyawen 已提交
2974

Z
zengyawen 已提交
2975
isDirectory(): boolean
Z
zengyawen 已提交
2976

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

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

Z
zhangxingxia 已提交
2981
**返回值:**
H
HelloCrease 已提交
2982 2983
  | 类型      | 说明            |
  | ------- | ------------- |
Z
zhangxingxia 已提交
2984
  | boolean | 表示当前目录项是否是目录。 |
Z
zengyawen 已提交
2985

Z
zhangxingxia 已提交
2986
**示例:**
Z
zhangxingxia 已提交
2987
  ```js
W
wangbo 已提交
2988
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
2989 2990
  let isDirectory = dir.readSync().isDirectory(); 
  ```
Z
zengyawen 已提交
2991 2992


Z
zengyawen 已提交
2993
### isFIFO
Z
zengyawen 已提交
2994

Z
zengyawen 已提交
2995
isFIFO(): boolean
Z
zengyawen 已提交
2996

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

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

Z
zhangxingxia 已提交
3001
**返回值:**
H
HelloCrease 已提交
3002 3003
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
3004
  | boolean | 表示当前目录项是否是FIFO。 |
Z
zengyawen 已提交
3005

Z
zhangxingxia 已提交
3006
**示例:**
Z
zhangxingxia 已提交
3007
  ```js
W
wangbo 已提交
3008
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3009 3010
  let isFIFO = dir.readSync().isFIFO(); 
  ```
Z
zengyawen 已提交
3011 3012


Z
zengyawen 已提交
3013
### isFile
Z
zengyawen 已提交
3014

Z
zengyawen 已提交
3015
isFile(): boolean
Z
zengyawen 已提交
3016

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

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

Z
zhangxingxia 已提交
3021
**返回值:**
H
HelloCrease 已提交
3022 3023
  | 类型      | 说明              |
  | ------- | --------------- |
Z
zhangxingxia 已提交
3024
  | boolean | 表示当前目录项是否是普通文件。 |
Z
zengyawen 已提交
3025

Z
zhangxingxia 已提交
3026
**示例:**
Z
zhangxingxia 已提交
3027
  ```js
W
wangbo 已提交
3028
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3029 3030
  let isFile = dir.readSync().isFile(); 
  ```
Z
zengyawen 已提交
3031 3032


Z
zengyawen 已提交
3033
### isSocket
Z
zengyawen 已提交
3034

Z
zengyawen 已提交
3035
isSocket(): boolean
Z
zengyawen 已提交
3036

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

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

Z
zhangxingxia 已提交
3041
**返回值:**
H
HelloCrease 已提交
3042 3043
  | 类型      | 说明             |
  | ------- | -------------- |
Z
zhangxingxia 已提交
3044
  | boolean | 表示当前目录项是否是套接字。 |
Z
zengyawen 已提交
3045

Z
zhangxingxia 已提交
3046
**示例:**
Z
zhangxingxia 已提交
3047
  ```js
W
wangbo 已提交
3048
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3049 3050
  let isSocket = dir.readSync().isSocket(); 
  ```
Z
zengyawen 已提交
3051 3052


Z
zengyawen 已提交
3053
### isSymbolicLink
Z
zengyawen 已提交
3054

Z
zengyawen 已提交
3055
isSymbolicLink(): boolean
Z
zengyawen 已提交
3056

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

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

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

Z
zhangxingxia 已提交
3066
**示例:**
Z
zhangxingxia 已提交
3067
  ```js
W
wangbo 已提交
3068
  dir = fileio.opendirSync(path);
Z
zengyawen 已提交
3069 3070
  let isSymbolicLink = dir.readSync().isSymbolicLink();
  ```