1e41f4b71Sopenharmony_ci# @ohos.file.fs (文件管理)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci该模块为基础文件操作API,提供基础文件操作能力,包括文件基本管理、文件目录管理、文件信息统计、文件流式读写等常用功能。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## 导入模块
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```ts
12e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## 使用说明
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci  ```ts
20e41f4b71Sopenharmony_ci  import { UIAbility } from '@kit.AbilityKit';
21e41f4b71Sopenharmony_ci  import { window } from '@kit.ArkUI';
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci  export default class EntryAbility extends UIAbility {
24e41f4b71Sopenharmony_ci    onWindowStageCreate(windowStage: window.WindowStage) {
25e41f4b71Sopenharmony_ci      let context = this.context;
26e41f4b71Sopenharmony_ci      let pathDir = context.filesDir;
27e41f4b71Sopenharmony_ci    }
28e41f4b71Sopenharmony_ci  }
29e41f4b71Sopenharmony_ci  ```
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径。表示沙箱路径的字符串称为path,获取方式及其接口用法请参考:[应用上下文Context-获取应用文件路径](../../application-models/application-context-stage.md#获取应用文件路径)。将指向资源的字符串称为URI。对于只支持path作为入参的接口可使用构造fileUri对象并获取其path属性的方式将URI转换为path,然后使用文件接口。URI定义解及其转换方式请参考:[文件URI](../../../application-dev/reference/apis-core-file-kit/js-apis-file-fileuri.md)
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci## fs.stat
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_cistat(file: string | number): Promise<Stat>
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci获取文件详细属性信息,使用Promise异步返回。
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci**参数:**
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                       |
46e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- |
47e41f4b71Sopenharmony_ci| file   | string \| number | 是   | 文件应用沙箱路径path或已打开的文件描述符fd。 |
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci**返回值:**
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci  | 类型                           | 说明         |
52e41f4b71Sopenharmony_ci  | ---------------------------- | ---------- |
53e41f4b71Sopenharmony_ci  | Promise<[Stat](#stat)> | Promise对象。返回文件的具体信息。 |
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci**错误码:**
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci**示例:**
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci  ```ts
62e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
63e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
64e41f4b71Sopenharmony_ci  fs.stat(filePath).then((stat: fs.Stat) => {
65e41f4b71Sopenharmony_ci    console.info("get file info succeed, the size of file is " + stat.size);
66e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
67e41f4b71Sopenharmony_ci    console.error("get file info failed with error message: " + err.message + ", error code: " + err.code);
68e41f4b71Sopenharmony_ci  });
69e41f4b71Sopenharmony_ci  ```
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci## fs.stat
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_cistat(file: string | number, callback: AsyncCallback<Stat>): void
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci获取文件详细属性信息,使用callback异步回调。
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci**参数:**
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci| 参数名   | 类型                               | 必填 | 说明                           |
84e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | ---- | ------------------------------ |
85e41f4b71Sopenharmony_ci| file     | string \| number                            | 是   | 文件应用沙箱路径path或已打开的文件描述符fd。     |
86e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Stat](#stat)> | 是   | 异步获取文件的信息之后的回调。 |
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**错误码:**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci**示例:**
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci  ```ts
95e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
96e41f4b71Sopenharmony_ci  fs.stat(pathDir, (err: BusinessError, stat: fs.Stat) => {
97e41f4b71Sopenharmony_ci    if (err) {
98e41f4b71Sopenharmony_ci      console.error("get file info failed with error message: " + err.message + ", error code: " + err.code);
99e41f4b71Sopenharmony_ci    } else {
100e41f4b71Sopenharmony_ci      console.info("get file info succeed, the size of file is " + stat.size);
101e41f4b71Sopenharmony_ci    }
102e41f4b71Sopenharmony_ci  });
103e41f4b71Sopenharmony_ci  ```
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci## fs.statSync
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_cistatSync(file: string | number): Stat
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci以同步方法获取文件详细属性信息。
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci**参数:**
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                       |
118e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- |
119e41f4b71Sopenharmony_ci| file   | string \| number | 是   | 文件应用沙箱路径path或已打开的文件描述符fd。 |
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci**返回值:**
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci  | 类型            | 说明         |
124e41f4b71Sopenharmony_ci  | ------------- | ---------- |
125e41f4b71Sopenharmony_ci  | [Stat](#stat) | 表示文件的具体信息。 |
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci**错误码:**
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**示例:**
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci  ```ts
134e41f4b71Sopenharmony_ci  let stat = fs.statSync(pathDir);
135e41f4b71Sopenharmony_ci  console.info("get file info succeed, the size of file is " + stat.size);
136e41f4b71Sopenharmony_ci  ```
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci## fs.access
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ciaccess(path: string, mode?: AccessModeType): Promise<boolean>
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci检查文件是否存在,使用Promise异步返回。
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci**参数:**
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
151e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
152e41f4b71Sopenharmony_ci| path   | string | 是   | 文件应用沙箱路径。                                   |
153e41f4b71Sopenharmony_ci| mode<sup>12+</sup>   | [AccessModeType](#accessmodetype12) | 否   | 文件校验的权限。                                   |
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci**返回值:**
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
158e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
159e41f4b71Sopenharmony_ci  | Promise&lt;boolean&gt; | Promise对象。返回布尔值,表示文件是否存在。 |
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci**错误码:**
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci| 错误码ID                     | 错误信息        |
166e41f4b71Sopenharmony_ci| ---------------------------- | ---------- |
167e41f4b71Sopenharmony_ci| 401 | 1. 未指定必须的参数 2. 参数类型与接口定义不匹配 |
168e41f4b71Sopenharmony_ci| 13900020 | 非法参数值 |
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci**示例:**
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci  ```ts
173e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
174e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
175e41f4b71Sopenharmony_ci  fs.access(filePath).then((res: boolean) => {
176e41f4b71Sopenharmony_ci    if (res) {
177e41f4b71Sopenharmony_ci      console.info("file exists");
178e41f4b71Sopenharmony_ci    } else {
179e41f4b71Sopenharmony_ci      console.info("file not exists");
180e41f4b71Sopenharmony_ci    }
181e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
182e41f4b71Sopenharmony_ci    console.error("access failed with error message: " + err.message + ", error code: " + err.code);
183e41f4b71Sopenharmony_ci  });
184e41f4b71Sopenharmony_ci  ```
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci## fs.access
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ciaccess(path: string, mode: AccessModeType, flag: AccessFlagType): Promise&lt;boolean&gt;
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci检查文件是否在本地,或校验相关权限,使用Promise异步返回。如果文件在云端,或者其它分布式设备上,返回false。
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci**参数:**
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
197e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
198e41f4b71Sopenharmony_ci| path   | string | 是   | 文件应用沙箱路径。                                   |
199e41f4b71Sopenharmony_ci| mode<sup>12+</sup>   | [AccessModeType](#accessmodetype12) | 是   | 文件校验的权限。|
200e41f4b71Sopenharmony_ci| flag<sup>12+</sup>  | [AccessFlagType](#accessflagtype12) | 是| 文件校验的位置。 |
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci**返回值:**
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
205e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
206e41f4b71Sopenharmony_ci  | Promise&lt;boolean&gt; | Promise对象。返回布尔值,true,表示文件存在本地且具有相关权限;false,表示文件不存在本地或不具有相关权限。 |
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci**错误码:**
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci| 错误码ID                     | 错误信息        |
213e41f4b71Sopenharmony_ci| ---------------------------- | ---------- |
214e41f4b71Sopenharmony_ci| 401 | 1. 未指定必须的参数 2. 参数类型与接口定义不匹配 |
215e41f4b71Sopenharmony_ci| 13900020 | 非法参数值 |
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci**示例:**
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_ci  ```ts
220e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
221e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
222e41f4b71Sopenharmony_ci  fs.access(filePath, fs.AccessModeFlag.EXIST, fs.AccessFlagType.LOCAL).then((res: boolean) => {
223e41f4b71Sopenharmony_ci    if (res) {
224e41f4b71Sopenharmony_ci      console.info("file exists");
225e41f4b71Sopenharmony_ci    } else {
226e41f4b71Sopenharmony_ci      console.info("file not exists");
227e41f4b71Sopenharmony_ci    }
228e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
229e41f4b71Sopenharmony_ci    console.error("access failed with error message: " + err.message + ", error code: " + err.code);
230e41f4b71Sopenharmony_ci  });
231e41f4b71Sopenharmony_ci  ```
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci## fs.access
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ciaccess(path: string, callback: AsyncCallback&lt;boolean&gt;): void
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci检查文件是否存在,使用callback异步回调。
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
240e41f4b71Sopenharmony_ci
241e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci**参数:**
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ci| 参数名   | 类型                      | 必填 | 说明                                                         |
246e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
247e41f4b71Sopenharmony_ci| path     | string                    | 是   | 文件应用沙箱路径。                                   |
248e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;boolean&gt; | 是   | 异步检查文件是否存在的回调,如果存在,回调返回true,否则返回false。 |
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci**错误码:**
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci| 错误码ID                     | 错误信息        |
255e41f4b71Sopenharmony_ci| ---------------------------- | ---------- |
256e41f4b71Sopenharmony_ci| 401 | 1. 未指定必须的参数 2. 参数类型与接口定义不匹配 |
257e41f4b71Sopenharmony_ci| 13900020 | 非法参数值 |
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ci**示例:**
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci  ```ts
262e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
263e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
264e41f4b71Sopenharmony_ci  fs.access(filePath, (err: BusinessError, res: boolean) => {
265e41f4b71Sopenharmony_ci    if (err) {
266e41f4b71Sopenharmony_ci      console.error("access failed with error message: " + err.message + ", error code: " + err.code);
267e41f4b71Sopenharmony_ci    } else {
268e41f4b71Sopenharmony_ci      if (res) {
269e41f4b71Sopenharmony_ci        console.info("file exists");
270e41f4b71Sopenharmony_ci      } else {
271e41f4b71Sopenharmony_ci        console.info("file not exists");
272e41f4b71Sopenharmony_ci      }
273e41f4b71Sopenharmony_ci    }
274e41f4b71Sopenharmony_ci  });
275e41f4b71Sopenharmony_ci  ```
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_ci## fs.accessSync
278e41f4b71Sopenharmony_ci
279e41f4b71Sopenharmony_ciaccessSync(path: string, mode?: AccessModeType): boolean
280e41f4b71Sopenharmony_ci
281e41f4b71Sopenharmony_ci以同步方法检查文件是否存在。
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci**参数:**
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
290e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
291e41f4b71Sopenharmony_ci| path   | string | 是   | 文件应用沙箱路径。                                   |
292e41f4b71Sopenharmony_ci| mode<sup>12+</sup>   | [AccessModeType](#accessmodetype12) | 否   | 文件校验的权限。                                   |
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci**返回值:**
295e41f4b71Sopenharmony_ci
296e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
297e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
298e41f4b71Sopenharmony_ci  | boolean | 返回true,表示文件存在;返回false,表示文件不存在。 |
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci**错误码:**
301e41f4b71Sopenharmony_ci
302e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ci| 错误码ID                     | 错误信息        |
305e41f4b71Sopenharmony_ci| ---------------------------- | ---------- |
306e41f4b71Sopenharmony_ci| 401 | 1. 未指定必须的参数 2. 参数类型与接口定义不匹配 |
307e41f4b71Sopenharmony_ci| 13900020 | 非法参数值 |
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ci**示例:**
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_ci  ```ts
312e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
313e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
314e41f4b71Sopenharmony_ci  try {
315e41f4b71Sopenharmony_ci    let res = fs.accessSync(filePath);
316e41f4b71Sopenharmony_ci    if (res) {
317e41f4b71Sopenharmony_ci      console.info("file exists");
318e41f4b71Sopenharmony_ci    } else {
319e41f4b71Sopenharmony_ci      console.info("file not exists");
320e41f4b71Sopenharmony_ci    }
321e41f4b71Sopenharmony_ci  } catch(error) {
322e41f4b71Sopenharmony_ci    let err: BusinessError = error as BusinessError;
323e41f4b71Sopenharmony_ci    console.error("accessSync failed with error message: " + err.message + ", error code: " + err.code);
324e41f4b71Sopenharmony_ci  }
325e41f4b71Sopenharmony_ci  ```
326e41f4b71Sopenharmony_ci
327e41f4b71Sopenharmony_ci## fs.accessSync
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ciaccessSync(path: string, mode: AccessModeType, flag: AccessFlagType): boolean
330e41f4b71Sopenharmony_ci
331e41f4b71Sopenharmony_ci以同步方法检查文件是否在本地,或校验相关权限。如果文件在云端,或者其它分布式设备上,返回false。
332e41f4b71Sopenharmony_ci
333e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
334e41f4b71Sopenharmony_ci
335e41f4b71Sopenharmony_ci**参数:**
336e41f4b71Sopenharmony_ci
337e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
338e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
339e41f4b71Sopenharmony_ci| path   | string | 是   | 文件应用沙箱路径。                                   |
340e41f4b71Sopenharmony_ci| mode<sup>12+</sup>   | [AccessModeType](#accessmodetype12) | 是   | 文件校验的权限。|
341e41f4b71Sopenharmony_ci| flag<sup>12+</sup>  | [AccessFlagType](#accessflagtype12) | 是   | 文件校验的位置。 |
342e41f4b71Sopenharmony_ci
343e41f4b71Sopenharmony_ci**返回值:**
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
346e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
347e41f4b71Sopenharmony_ci  | boolean | 返回true,表示文件存在本地且具有相关权限;返回false,表示文件不存在本地或不具有相关权限。 |
348e41f4b71Sopenharmony_ci
349e41f4b71Sopenharmony_ci**错误码:**
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
352e41f4b71Sopenharmony_ci
353e41f4b71Sopenharmony_ci| 错误码ID                     | 错误信息        |
354e41f4b71Sopenharmony_ci| ---------------------------- | ---------- |
355e41f4b71Sopenharmony_ci| 401 | 1. 未指定必须的参数 2. 参数类型与接口定义不匹配 |
356e41f4b71Sopenharmony_ci| 13900020 | 非法参数值 |
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_ci**示例:**
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci  ```ts
361e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
362e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
363e41f4b71Sopenharmony_ci  try {
364e41f4b71Sopenharmony_ci    let res = fs.accessSync(filePath, fs.AccessModeFlag.EXIST, fs.AccessFlagType.LOCAL);
365e41f4b71Sopenharmony_ci    if (res) {
366e41f4b71Sopenharmony_ci      console.info("file exists");
367e41f4b71Sopenharmony_ci    } else {
368e41f4b71Sopenharmony_ci      console.info("file not exists");
369e41f4b71Sopenharmony_ci    }
370e41f4b71Sopenharmony_ci  } catch(error) {
371e41f4b71Sopenharmony_ci    let err: BusinessError = error as BusinessError;
372e41f4b71Sopenharmony_ci    console.error("accessSync failed with error message: " + err.message + ", error code: " + err.code);
373e41f4b71Sopenharmony_ci  }
374e41f4b71Sopenharmony_ci  ```
375e41f4b71Sopenharmony_ci
376e41f4b71Sopenharmony_ci## fs.close
377e41f4b71Sopenharmony_ci
378e41f4b71Sopenharmony_ciclose(file: number | File): Promise&lt;void&gt;
379e41f4b71Sopenharmony_ci
380e41f4b71Sopenharmony_ci关闭文件,使用Promise异步返回。
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
383e41f4b71Sopenharmony_ci
384e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci**参数:**
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明           |
389e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ------------ |
390e41f4b71Sopenharmony_ci  | file   | number \| [File](#file) | 是    | 已打开的File对象或已打开的文件描述符fd,关闭后file对象或文件描述符不再具备实际意义,不可再用于进行读写等操作。 |
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci**返回值:**
393e41f4b71Sopenharmony_ci
394e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
395e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
396e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
397e41f4b71Sopenharmony_ci
398e41f4b71Sopenharmony_ci**错误码:**
399e41f4b71Sopenharmony_ci
400e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
401e41f4b71Sopenharmony_ci
402e41f4b71Sopenharmony_ci**示例:**
403e41f4b71Sopenharmony_ci
404e41f4b71Sopenharmony_ci  ```ts
405e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
406e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
407e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
408e41f4b71Sopenharmony_ci  fs.close(file).then(() => {
409e41f4b71Sopenharmony_ci    console.info("close file succeed");
410e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
411e41f4b71Sopenharmony_ci    console.error("close file failed with error message: " + err.message + ", error code: " + err.code);
412e41f4b71Sopenharmony_ci  });
413e41f4b71Sopenharmony_ci  ```
414e41f4b71Sopenharmony_ci
415e41f4b71Sopenharmony_ci## fs.close
416e41f4b71Sopenharmony_ci
417e41f4b71Sopenharmony_ciclose(file: number | File, callback: AsyncCallback&lt;void&gt;): void
418e41f4b71Sopenharmony_ci
419e41f4b71Sopenharmony_ci关闭文件,使用callback异步回调。
420e41f4b71Sopenharmony_ci
421e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
422e41f4b71Sopenharmony_ci
423e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
424e41f4b71Sopenharmony_ci
425e41f4b71Sopenharmony_ci**参数:**
426e41f4b71Sopenharmony_ci
427e41f4b71Sopenharmony_ci  | 参数名      | 类型                        | 必填   | 说明           |
428e41f4b71Sopenharmony_ci  | -------- | ------------------------- | ---- | ------------ |
429e41f4b71Sopenharmony_ci  | file       | number \| [File](#file)                  | 是    | 已打开的File对象或已打开的文件描述符fd,关闭后file对象或文件描述符不再具备实际意义,不可再用于进行读写等操作。 |
430e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void&gt; | 是    | 异步关闭文件之后的回调。 |
431e41f4b71Sopenharmony_ci
432e41f4b71Sopenharmony_ci**错误码:**
433e41f4b71Sopenharmony_ci
434e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
435e41f4b71Sopenharmony_ci
436e41f4b71Sopenharmony_ci**示例:**
437e41f4b71Sopenharmony_ci
438e41f4b71Sopenharmony_ci  ```ts
439e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
440e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
441e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
442e41f4b71Sopenharmony_ci  fs.close(file, (err: BusinessError) => {
443e41f4b71Sopenharmony_ci    if (err) {
444e41f4b71Sopenharmony_ci      console.error("close file failed with error message: " + err.message + ", error code: " + err.code);
445e41f4b71Sopenharmony_ci    } else {
446e41f4b71Sopenharmony_ci      console.info("close file succeed");
447e41f4b71Sopenharmony_ci    }
448e41f4b71Sopenharmony_ci  });
449e41f4b71Sopenharmony_ci  ```
450e41f4b71Sopenharmony_ci
451e41f4b71Sopenharmony_ci## fs.closeSync
452e41f4b71Sopenharmony_ci
453e41f4b71Sopenharmony_cicloseSync(file: number | File): void
454e41f4b71Sopenharmony_ci
455e41f4b71Sopenharmony_ci以同步方法关闭文件。
456e41f4b71Sopenharmony_ci
457e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
458e41f4b71Sopenharmony_ci
459e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
460e41f4b71Sopenharmony_ci
461e41f4b71Sopenharmony_ci**参数:**
462e41f4b71Sopenharmony_ci
463e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明           |
464e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ------------ |
465e41f4b71Sopenharmony_ci  | file   | number \| [File](#file) | 是    | 已打开的File对象或已打开的文件描述符fd,关闭后file对象或文件描述符不再具备实际意义,不可再用于进行读写等操作。 |
466e41f4b71Sopenharmony_ci
467e41f4b71Sopenharmony_ci**错误码:**
468e41f4b71Sopenharmony_ci
469e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
470e41f4b71Sopenharmony_ci
471e41f4b71Sopenharmony_ci**示例:**
472e41f4b71Sopenharmony_ci
473e41f4b71Sopenharmony_ci  ```ts
474e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
475e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
476e41f4b71Sopenharmony_ci  fs.closeSync(file);
477e41f4b71Sopenharmony_ci  ```
478e41f4b71Sopenharmony_ci
479e41f4b71Sopenharmony_ci## fs.copy<sup>11+</sup>
480e41f4b71Sopenharmony_ci
481e41f4b71Sopenharmony_cicopy(srcUri: string, destUri: string, options?: CopyOptions): Promise\<void>
482e41f4b71Sopenharmony_ci
483e41f4b71Sopenharmony_ci拷贝文件或者目录,使用Promise异步返回。
484e41f4b71Sopenharmony_ci
485e41f4b71Sopenharmony_ci支持跨设备拷贝。强制覆盖拷贝。入参支持文件或目录URI。
486e41f4b71Sopenharmony_ci跨端拷贝时,限制同时最多存在10个拷贝任务;单次拷贝的文件数量不得超过500个。
487e41f4b71Sopenharmony_ci
488e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
489e41f4b71Sopenharmony_ci
490e41f4b71Sopenharmony_ci**参数:**
491e41f4b71Sopenharmony_ci
492e41f4b71Sopenharmony_ci  | 参数名  | 类型                         | 必填   | 说明                                       |
493e41f4b71Sopenharmony_ci  | ---- | -------------------------- | ---- | ---------------------------------------- |
494e41f4b71Sopenharmony_ci  | srcUri  | string | 是    | 待复制文件或目录的uri。                      |
495e41f4b71Sopenharmony_ci  | destUri | string | 是    | 目标文件或目录的uri。                          |
496e41f4b71Sopenharmony_ci  | options | [CopyOptions](#copyoptions11)| 否| options中提供拷贝进度回调。|
497e41f4b71Sopenharmony_ci
498e41f4b71Sopenharmony_ci**返回值:**
499e41f4b71Sopenharmony_ci
500e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
501e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
502e41f4b71Sopenharmony_ci  | Promise\<void> | Promise对象。无返回值。 |
503e41f4b71Sopenharmony_ci
504e41f4b71Sopenharmony_ci**错误码:**
505e41f4b71Sopenharmony_ci
506e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
507e41f4b71Sopenharmony_ci
508e41f4b71Sopenharmony_ci**示例:**
509e41f4b71Sopenharmony_ci
510e41f4b71Sopenharmony_ci```ts
511e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit';
512e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
513e41f4b71Sopenharmony_ciimport { fileUri } from '@kit.CoreFileKit';
514e41f4b71Sopenharmony_ci
515e41f4b71Sopenharmony_cilet srcDirPathLocal: string = pathDir + "/src";
516e41f4b71Sopenharmony_cilet dstDirPathLocal: string = pathDir + "/dest";
517e41f4b71Sopenharmony_ci
518e41f4b71Sopenharmony_cilet srcDirUriLocal: string = fileUri.getUriFromPath(srcDirPathLocal);
519e41f4b71Sopenharmony_cilet dstDirUriLocal: string = fileUri.getUriFromPath(dstDirPathLocal);
520e41f4b71Sopenharmony_ci
521e41f4b71Sopenharmony_cilet progressListener: fs.ProgressListener = (progress: fs.Progress) => {
522e41f4b71Sopenharmony_ci  console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`);
523e41f4b71Sopenharmony_ci};
524e41f4b71Sopenharmony_cilet copyoption: fs.CopyOptions = {
525e41f4b71Sopenharmony_ci  "progressListener" : progressListener
526e41f4b71Sopenharmony_ci}
527e41f4b71Sopenharmony_citry {
528e41f4b71Sopenharmony_ci  fs.copy(srcDirUriLocal, dstDirUriLocal, copyoption).then(()=>{
529e41f4b71Sopenharmony_ci    console.info("Succeeded in copying. ");
530e41f4b71Sopenharmony_ci  }).catch((err: BusinessError)=>{
531e41f4b71Sopenharmony_ci    console.error(`Failed to copy: ${JSON.stringify(err)}`);
532e41f4b71Sopenharmony_ci  })
533e41f4b71Sopenharmony_ci} catch(err) {
534e41f4b71Sopenharmony_ci  console.error(`Failed to copy: ${JSON.stringify(err)}`);
535e41f4b71Sopenharmony_ci}
536e41f4b71Sopenharmony_ci```
537e41f4b71Sopenharmony_ci
538e41f4b71Sopenharmony_ci## fs.copy<sup>11+</sup>
539e41f4b71Sopenharmony_ci
540e41f4b71Sopenharmony_cicopy(srcUri: string, destUri: string, callback: AsyncCallback\<void>): void
541e41f4b71Sopenharmony_ci
542e41f4b71Sopenharmony_ci拷贝文件或者目录,使用callback异步回调。
543e41f4b71Sopenharmony_ci
544e41f4b71Sopenharmony_ci支持跨设备拷贝。强制覆盖拷贝。入参支持文件或目录URI。
545e41f4b71Sopenharmony_ci跨端拷贝时,限制同时最多存在10个拷贝任务;单次拷贝的文件数量不得超过500个。
546e41f4b71Sopenharmony_ci
547e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
548e41f4b71Sopenharmony_ci
549e41f4b71Sopenharmony_ci**参数:**
550e41f4b71Sopenharmony_ci
551e41f4b71Sopenharmony_ci  | 参数名  | 类型                         | 必填   | 说明                                       |
552e41f4b71Sopenharmony_ci  | ---- | -------------------------- | ---- | ---------------------------------------- |
553e41f4b71Sopenharmony_ci  | srcUri  | string | 是    | 待复制文件或目录的uri。                      |
554e41f4b71Sopenharmony_ci  | destUri | string | 是    | 目标文件或目录的uri。                          |
555e41f4b71Sopenharmony_ci  | callback | AsyncCallback\<void>| 是| 异步拷贝之后的回调。|
556e41f4b71Sopenharmony_ci
557e41f4b71Sopenharmony_ci**错误码:**
558e41f4b71Sopenharmony_ci
559e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
560e41f4b71Sopenharmony_ci
561e41f4b71Sopenharmony_ci**示例:**
562e41f4b71Sopenharmony_ci
563e41f4b71Sopenharmony_ci```ts
564e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
565e41f4b71Sopenharmony_ciimport { fileUri } from '@kit.CoreFileKit';
566e41f4b71Sopenharmony_ci
567e41f4b71Sopenharmony_cilet srcDirPathLocal: string = pathDir + "/src";
568e41f4b71Sopenharmony_cilet dstDirPathLocal: string = pathDir + "/dest";
569e41f4b71Sopenharmony_ci
570e41f4b71Sopenharmony_cilet srcDirUriLocal: string = fileUri.getUriFromPath(srcDirPathLocal);
571e41f4b71Sopenharmony_cilet dstDirUriLocal: string = fileUri.getUriFromPath(dstDirPathLocal);
572e41f4b71Sopenharmony_ci
573e41f4b71Sopenharmony_citry {
574e41f4b71Sopenharmony_ci  fs.copy(srcDirUriLocal, dstDirUriLocal, (err: BusinessError) => {
575e41f4b71Sopenharmony_ci    if (err) {
576e41f4b71Sopenharmony_ci      console.error(`Failed to copy: ${JSON.stringify(err)}`);
577e41f4b71Sopenharmony_ci      return;
578e41f4b71Sopenharmony_ci    }
579e41f4b71Sopenharmony_ci    console.info("Succeeded in copying. ");
580e41f4b71Sopenharmony_ci  })
581e41f4b71Sopenharmony_ci} catch(err) {
582e41f4b71Sopenharmony_ci  console.error(`Failed to copy: ${JSON.stringify(err)}`);
583e41f4b71Sopenharmony_ci}
584e41f4b71Sopenharmony_ci```
585e41f4b71Sopenharmony_ci
586e41f4b71Sopenharmony_ci## fs.copy<sup>11+</sup>
587e41f4b71Sopenharmony_ci
588e41f4b71Sopenharmony_cicopy(srcUri: string, destUri: string, options: CopyOptions, callback: AsyncCallback\<void>): void
589e41f4b71Sopenharmony_ci
590e41f4b71Sopenharmony_ci拷贝文件或者目录,使用callback异步回调。
591e41f4b71Sopenharmony_ci
592e41f4b71Sopenharmony_ci支持跨设备拷贝。强制覆盖拷贝。入参支持文件或目录URI。
593e41f4b71Sopenharmony_ci跨端拷贝时,限制同时最多存在10个拷贝任务;单次拷贝的文件数量不得超过500个。
594e41f4b71Sopenharmony_ci
595e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
596e41f4b71Sopenharmony_ci
597e41f4b71Sopenharmony_ci**参数:**
598e41f4b71Sopenharmony_ci
599e41f4b71Sopenharmony_ci  | 参数名  | 类型                         | 必填   | 说明                                       |
600e41f4b71Sopenharmony_ci  | ---- | -------------------------- | ---- | ---------------------------------------- |
601e41f4b71Sopenharmony_ci  | srcUri  | string | 是    | 待复制文件或目录的uri。                      |
602e41f4b71Sopenharmony_ci  | destUri | string | 是    | 目标文件或目录的uri。                          |
603e41f4b71Sopenharmony_ci  | options | [CopyOptions](#copyoptions11) |是| 拷贝进度回调。                          |
604e41f4b71Sopenharmony_ci  | callback | AsyncCallback\<void>| 是| 异步拷贝之后的回调。|
605e41f4b71Sopenharmony_ci
606e41f4b71Sopenharmony_ci**错误码:**
607e41f4b71Sopenharmony_ci
608e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
609e41f4b71Sopenharmony_ci
610e41f4b71Sopenharmony_ci**示例:**
611e41f4b71Sopenharmony_ci
612e41f4b71Sopenharmony_ci```ts
613e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit';
614e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
615e41f4b71Sopenharmony_ciimport { fileUri } from '@kit.CoreFileKit';
616e41f4b71Sopenharmony_ci
617e41f4b71Sopenharmony_cilet srcDirPathLocal: string = pathDir + "/src";
618e41f4b71Sopenharmony_cilet dstDirPathLocal: string = pathDir + "/dest";
619e41f4b71Sopenharmony_ci
620e41f4b71Sopenharmony_cilet srcDirUriLocal: string = fileUri.getUriFromPath(srcDirPathLocal);
621e41f4b71Sopenharmony_cilet dstDirUriLocal: string = fileUri.getUriFromPath(dstDirPathLocal);
622e41f4b71Sopenharmony_ci
623e41f4b71Sopenharmony_citry {
624e41f4b71Sopenharmony_ci  let progressListener: fs.ProgressListener = (progress: fs.Progress) => {
625e41f4b71Sopenharmony_ci    console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`);
626e41f4b71Sopenharmony_ci  };
627e41f4b71Sopenharmony_ci  let copyoption: fs.CopyOptions = {
628e41f4b71Sopenharmony_ci    "progressListener" : progressListener
629e41f4b71Sopenharmony_ci  }
630e41f4b71Sopenharmony_ci  fs.copy(srcDirUriLocal, dstDirUriLocal, copyoption, (err: BusinessError) => {
631e41f4b71Sopenharmony_ci    if (err) {
632e41f4b71Sopenharmony_ci      console.error(`Failed to copy: ${JSON.stringify(err)}`);
633e41f4b71Sopenharmony_ci      return;
634e41f4b71Sopenharmony_ci    }
635e41f4b71Sopenharmony_ci    console.info("Succeeded in copying. ");
636e41f4b71Sopenharmony_ci  })
637e41f4b71Sopenharmony_ci} catch(err) {
638e41f4b71Sopenharmony_ci  console.error(`Failed to copy: ${JSON.stringify(err)}`);
639e41f4b71Sopenharmony_ci}
640e41f4b71Sopenharmony_ci```
641e41f4b71Sopenharmony_ci
642e41f4b71Sopenharmony_ci## fs.copyFile
643e41f4b71Sopenharmony_ci
644e41f4b71Sopenharmony_cicopyFile(src: string | number, dest: string | number, mode?: number): Promise&lt;void&gt;
645e41f4b71Sopenharmony_ci
646e41f4b71Sopenharmony_ci复制文件,使用Promise异步返回。
647e41f4b71Sopenharmony_ci
648e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
649e41f4b71Sopenharmony_ci
650e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
651e41f4b71Sopenharmony_ci
652e41f4b71Sopenharmony_ci**参数:**
653e41f4b71Sopenharmony_ci
654e41f4b71Sopenharmony_ci  | 参数名  | 类型                         | 必填   | 说明                                       |
655e41f4b71Sopenharmony_ci  | ---- | -------------------------- | ---- | ---------------------------------------- |
656e41f4b71Sopenharmony_ci  | src  | string \| number | 是    | 待复制文件的路径或待复制文件的文件描述符。                      |
657e41f4b71Sopenharmony_ci  | dest | string \| number | 是    | 目标文件路径或目标文件的文件描述符。                          |
658e41f4b71Sopenharmony_ci  | mode | number                     | 否    | mode提供覆盖文件的选项,当前仅支持0,且默认为0。<br/>0:完全覆盖目标文件,未覆盖部分将被裁切掉。 |
659e41f4b71Sopenharmony_ci
660e41f4b71Sopenharmony_ci**返回值:**
661e41f4b71Sopenharmony_ci
662e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
663e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
664e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
665e41f4b71Sopenharmony_ci
666e41f4b71Sopenharmony_ci**错误码:**
667e41f4b71Sopenharmony_ci
668e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
669e41f4b71Sopenharmony_ci
670e41f4b71Sopenharmony_ci**示例:**
671e41f4b71Sopenharmony_ci
672e41f4b71Sopenharmony_ci  ```ts
673e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
674e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/test.txt";
675e41f4b71Sopenharmony_ci  let dstPath = pathDir + "/dstDir/test.txt";
676e41f4b71Sopenharmony_ci  fs.copyFile(srcPath, dstPath, 0).then(() => {
677e41f4b71Sopenharmony_ci    console.info("copy file succeed");
678e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
679e41f4b71Sopenharmony_ci    console.error("copy file failed with error message: " + err.message + ", error code: " + err.code);
680e41f4b71Sopenharmony_ci  });
681e41f4b71Sopenharmony_ci  ```
682e41f4b71Sopenharmony_ci
683e41f4b71Sopenharmony_ci## fs.copyFile
684e41f4b71Sopenharmony_ci
685e41f4b71Sopenharmony_cicopyFile(src: string | number, dest: string | number, mode: number, callback: AsyncCallback&lt;void&gt;): void
686e41f4b71Sopenharmony_ci
687e41f4b71Sopenharmony_ci复制文件,可设置覆盖文件的方式,使用callback异步回调。
688e41f4b71Sopenharmony_ci
689e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
690e41f4b71Sopenharmony_ci
691e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
692e41f4b71Sopenharmony_ci
693e41f4b71Sopenharmony_ci**参数:**
694e41f4b71Sopenharmony_ci
695e41f4b71Sopenharmony_ci  | 参数名      | 类型                         | 必填   | 说明                                       |
696e41f4b71Sopenharmony_ci  | -------- | -------------------------- | ---- | ---------------------------------------- |
697e41f4b71Sopenharmony_ci  | src      | string \| number | 是    | 待复制文件的路径或待复制文件的文件描述符。                      |
698e41f4b71Sopenharmony_ci  | dest     | string \| number | 是    | 目标文件路径或目标文件的文件描述符。                          |
699e41f4b71Sopenharmony_ci  | mode     | number                     | 是    | mode提供覆盖文件的选项,当前仅支持0,且默认为0。<br/>0:完全覆盖目标文件,未覆盖部分将被裁切掉。 |
700e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void&gt;  | 是    | 异步复制文件之后的回调。                             |
701e41f4b71Sopenharmony_ci
702e41f4b71Sopenharmony_ci**错误码:**
703e41f4b71Sopenharmony_ci
704e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
705e41f4b71Sopenharmony_ci
706e41f4b71Sopenharmony_ci**示例:**
707e41f4b71Sopenharmony_ci
708e41f4b71Sopenharmony_ci  ```ts
709e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
710e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/test.txt";
711e41f4b71Sopenharmony_ci  let dstPath = pathDir + "/dstDir/test.txt";
712e41f4b71Sopenharmony_ci  fs.copyFile(srcPath, dstPath, 0, (err: BusinessError) => {
713e41f4b71Sopenharmony_ci    if (err) {
714e41f4b71Sopenharmony_ci      console.error("copy file failed with error message: " + err.message + ", error code: " + err.code);
715e41f4b71Sopenharmony_ci    } else {
716e41f4b71Sopenharmony_ci      console.info("copy file succeed");
717e41f4b71Sopenharmony_ci    }
718e41f4b71Sopenharmony_ci  });
719e41f4b71Sopenharmony_ci  ```
720e41f4b71Sopenharmony_ci
721e41f4b71Sopenharmony_ci## fs.copyFile
722e41f4b71Sopenharmony_ci
723e41f4b71Sopenharmony_cicopyFile(src: string | number, dest: string | number, callback: AsyncCallback&lt;void&gt;): void
724e41f4b71Sopenharmony_ci
725e41f4b71Sopenharmony_ci复制文件,覆盖方式为完全覆盖目标文件,未覆盖部分将被裁切。使用callback异步回调。
726e41f4b71Sopenharmony_ci
727e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
728e41f4b71Sopenharmony_ci
729e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
730e41f4b71Sopenharmony_ci
731e41f4b71Sopenharmony_ci**参数:**
732e41f4b71Sopenharmony_ci
733e41f4b71Sopenharmony_ci  | 参数名      | 类型                         | 必填   | 说明                                       |
734e41f4b71Sopenharmony_ci  | -------- | -------------------------- | ---- | ---------------------------------------- |
735e41f4b71Sopenharmony_ci  | src      | string \| number | 是    | 待复制文件的路径或待复制文件的文件描述符。                      |
736e41f4b71Sopenharmony_ci  | dest     | string \| number | 是    | 目标文件路径或目标文件的文件描述符。                          |
737e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void&gt;  | 是    | 异步复制文件之后的回调。                             |
738e41f4b71Sopenharmony_ci
739e41f4b71Sopenharmony_ci**错误码:**
740e41f4b71Sopenharmony_ci
741e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
742e41f4b71Sopenharmony_ci
743e41f4b71Sopenharmony_ci**示例:**
744e41f4b71Sopenharmony_ci
745e41f4b71Sopenharmony_ci  ```ts
746e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
747e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/test.txt";
748e41f4b71Sopenharmony_ci  let dstPath = pathDir + "/dstDir/test.txt";
749e41f4b71Sopenharmony_ci  fs.copyFile(srcPath, dstPath, (err: BusinessError) => {
750e41f4b71Sopenharmony_ci    if (err) {
751e41f4b71Sopenharmony_ci      console.error("copy file failed with error message: " + err.message + ", error code: " + err.code);
752e41f4b71Sopenharmony_ci    } else {
753e41f4b71Sopenharmony_ci      console.info("copy file succeed");
754e41f4b71Sopenharmony_ci    }
755e41f4b71Sopenharmony_ci  });
756e41f4b71Sopenharmony_ci  ```
757e41f4b71Sopenharmony_ci
758e41f4b71Sopenharmony_ci
759e41f4b71Sopenharmony_ci## fs.copyFileSync
760e41f4b71Sopenharmony_ci
761e41f4b71Sopenharmony_cicopyFileSync(src: string | number, dest: string | number, mode?: number): void
762e41f4b71Sopenharmony_ci
763e41f4b71Sopenharmony_ci以同步方法复制文件。
764e41f4b71Sopenharmony_ci
765e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
766e41f4b71Sopenharmony_ci
767e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
768e41f4b71Sopenharmony_ci
769e41f4b71Sopenharmony_ci**参数:**
770e41f4b71Sopenharmony_ci
771e41f4b71Sopenharmony_ci  | 参数名  | 类型                         | 必填   | 说明                                       |
772e41f4b71Sopenharmony_ci  | ---- | -------------------------- | ---- | ---------------------------------------- |
773e41f4b71Sopenharmony_ci  | src  | string \| number | 是    | 待复制文件的路径或待复制文件的文件描述符。                      |
774e41f4b71Sopenharmony_ci  | dest | string \| number | 是    | 目标文件路径或目标文件的文件描述符。                          |
775e41f4b71Sopenharmony_ci  | mode | number                     | 否    | mode提供覆盖文件的选项,当前仅支持0,且默认为0。<br/>0:完全覆盖目标文件,未覆盖部分将被裁切掉。 |
776e41f4b71Sopenharmony_ci
777e41f4b71Sopenharmony_ci**错误码:**
778e41f4b71Sopenharmony_ci
779e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
780e41f4b71Sopenharmony_ci
781e41f4b71Sopenharmony_ci**示例:**
782e41f4b71Sopenharmony_ci
783e41f4b71Sopenharmony_ci  ```ts
784e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/test.txt";
785e41f4b71Sopenharmony_ci  let dstPath = pathDir + "/dstDir/test.txt";
786e41f4b71Sopenharmony_ci  fs.copyFileSync(srcPath, dstPath);
787e41f4b71Sopenharmony_ci  ```
788e41f4b71Sopenharmony_ci
789e41f4b71Sopenharmony_ci## fs.copyDir<sup>10+</sup>
790e41f4b71Sopenharmony_ci
791e41f4b71Sopenharmony_cicopyDir(src: string, dest: string, mode?: number): Promise\<void>
792e41f4b71Sopenharmony_ci
793e41f4b71Sopenharmony_ci复制源文件夹至目标路径下,使用Promise异步返回。
794e41f4b71Sopenharmony_ci
795e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
796e41f4b71Sopenharmony_ci
797e41f4b71Sopenharmony_ci**参数:**
798e41f4b71Sopenharmony_ci
799e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
800e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
801e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件夹的应用沙箱路径。 |
802e41f4b71Sopenharmony_ci  | dest | string | 是    | 目标文件夹的应用沙箱路径。 |
803e41f4b71Sopenharmony_ci  | mode | number | 否    | 复制模式。默认mode为0。<br/>-&nbsp; mode为0,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以Array\<[ConflictFiles](#conflictfiles10)>形式提供。<br/>-&nbsp; mode为1,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。|
804e41f4b71Sopenharmony_ci
805e41f4b71Sopenharmony_ci**返回值:**
806e41f4b71Sopenharmony_ci
807e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
808e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
809e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
810e41f4b71Sopenharmony_ci
811e41f4b71Sopenharmony_ci**错误码:**
812e41f4b71Sopenharmony_ci
813e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
814e41f4b71Sopenharmony_ci
815e41f4b71Sopenharmony_ci**示例:**
816e41f4b71Sopenharmony_ci
817e41f4b71Sopenharmony_ci  ```ts
818e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
819e41f4b71Sopenharmony_ci  // copy directory from srcPath to destPath
820e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/";
821e41f4b71Sopenharmony_ci  let destPath = pathDir + "/destDir/";
822e41f4b71Sopenharmony_ci  fs.copyDir(srcPath, destPath, 0).then(() => {
823e41f4b71Sopenharmony_ci    console.info("copy directory succeed");
824e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
825e41f4b71Sopenharmony_ci    console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code);
826e41f4b71Sopenharmony_ci  });
827e41f4b71Sopenharmony_ci  ```
828e41f4b71Sopenharmony_ci
829e41f4b71Sopenharmony_ci## fs.copyDir<sup>10+</sup>
830e41f4b71Sopenharmony_ci
831e41f4b71Sopenharmony_cicopyDir(src: string, dest: string, mode: number, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void
832e41f4b71Sopenharmony_ci
833e41f4b71Sopenharmony_ci复制源文件夹至目标路径下,可设置复制模式。使用callback异步回调。
834e41f4b71Sopenharmony_ci
835e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
836e41f4b71Sopenharmony_ci
837e41f4b71Sopenharmony_ci**参数:**
838e41f4b71Sopenharmony_ci
839e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
840e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
841e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件夹的应用沙箱路径。 |
842e41f4b71Sopenharmony_ci  | dest | string | 是    | 目标文件夹的应用沙箱路径。 |
843e41f4b71Sopenharmony_ci  | mode | number | 是    | 复制模式。默认mode为0。<br/>-&nbsp; mode为0,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以Array\<[ConflictFiles](#conflictfiles10)>形式提供。<br/>-&nbsp; mode为1,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。|
844e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void, Array&lt;[ConflictFiles](#conflictfiles10)&gt;&gt; | 是    | 异步复制文件夹之后的回调。              |
845e41f4b71Sopenharmony_ci
846e41f4b71Sopenharmony_ci**错误码:**
847e41f4b71Sopenharmony_ci
848e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
849e41f4b71Sopenharmony_ci
850e41f4b71Sopenharmony_ci**示例:**
851e41f4b71Sopenharmony_ci
852e41f4b71Sopenharmony_ci  ```ts
853e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
854e41f4b71Sopenharmony_ci  import { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit';
855e41f4b71Sopenharmony_ci  // copy directory from srcPath to destPath
856e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/";
857e41f4b71Sopenharmony_ci  let destPath = pathDir + "/destDir/";
858e41f4b71Sopenharmony_ci  fs.copyDir(srcPath, destPath, 0, (err: BusinessError<Array<ConflictFiles>>) => {
859e41f4b71Sopenharmony_ci    if (err && err.code == 13900015 && err.data?.length !== undefined) {
860e41f4b71Sopenharmony_ci      for (let i = 0; i < err.data.length; i++) {
861e41f4b71Sopenharmony_ci        console.error("copy directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile);
862e41f4b71Sopenharmony_ci      }
863e41f4b71Sopenharmony_ci    } else if (err) {
864e41f4b71Sopenharmony_ci      console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code);
865e41f4b71Sopenharmony_ci    } else {
866e41f4b71Sopenharmony_ci      console.info("copy directory succeed");
867e41f4b71Sopenharmony_ci    }
868e41f4b71Sopenharmony_ci  });
869e41f4b71Sopenharmony_ci  ```
870e41f4b71Sopenharmony_ci
871e41f4b71Sopenharmony_ci## fs.copyDir<sup>10+</sup>
872e41f4b71Sopenharmony_ci
873e41f4b71Sopenharmony_cicopyDir(src: string, dest: string, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void
874e41f4b71Sopenharmony_ci
875e41f4b71Sopenharmony_ci复制源文件夹至目标路径下,使用Callback异步回调。
876e41f4b71Sopenharmony_ci
877e41f4b71Sopenharmony_ci当目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以Array\<[ConflictFiles](#conflictfiles10)>形式提供。
878e41f4b71Sopenharmony_ci
879e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
880e41f4b71Sopenharmony_ci
881e41f4b71Sopenharmony_ci**参数:**
882e41f4b71Sopenharmony_ci
883e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
884e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
885e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件夹的应用沙箱路径。 |
886e41f4b71Sopenharmony_ci  | dest | string | 是    | 目标文件夹的应用沙箱路径。 |
887e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void, Array&lt;[ConflictFiles](#conflictfiles10)&gt;&gt; | 是    | 异步复制文件夹之后的回调。              |
888e41f4b71Sopenharmony_ci
889e41f4b71Sopenharmony_ci**错误码:**
890e41f4b71Sopenharmony_ci
891e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
892e41f4b71Sopenharmony_ci
893e41f4b71Sopenharmony_ci**示例:**
894e41f4b71Sopenharmony_ci
895e41f4b71Sopenharmony_ci  ```ts
896e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
897e41f4b71Sopenharmony_ci  import { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit';
898e41f4b71Sopenharmony_ci  // copy directory from srcPath to destPath
899e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/";
900e41f4b71Sopenharmony_ci  let destPath = pathDir + "/destDir/";
901e41f4b71Sopenharmony_ci  fs.copyDir(srcPath, destPath, (err: BusinessError<Array<ConflictFiles>>) => {
902e41f4b71Sopenharmony_ci    if (err && err.code == 13900015 && err.data?.length !== undefined) {
903e41f4b71Sopenharmony_ci      for (let i = 0; i < err.data.length; i++) {
904e41f4b71Sopenharmony_ci        console.error("copy directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile);
905e41f4b71Sopenharmony_ci      }
906e41f4b71Sopenharmony_ci    } else if (err) {
907e41f4b71Sopenharmony_ci      console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code);
908e41f4b71Sopenharmony_ci    } else {
909e41f4b71Sopenharmony_ci      console.info("copy directory succeed");
910e41f4b71Sopenharmony_ci    }
911e41f4b71Sopenharmony_ci  });
912e41f4b71Sopenharmony_ci  ```
913e41f4b71Sopenharmony_ci
914e41f4b71Sopenharmony_ci## fs.copyDirSync<sup>10+</sup>
915e41f4b71Sopenharmony_ci
916e41f4b71Sopenharmony_cicopyDirSync(src: string, dest: string, mode?: number): void
917e41f4b71Sopenharmony_ci
918e41f4b71Sopenharmony_ci以同步方法复制源文件夹至目标路径下。
919e41f4b71Sopenharmony_ci
920e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
921e41f4b71Sopenharmony_ci
922e41f4b71Sopenharmony_ci**参数:**
923e41f4b71Sopenharmony_ci
924e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
925e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
926e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件夹的应用沙箱路径。 |
927e41f4b71Sopenharmony_ci  | dest | string | 是    | 目标文件夹的应用沙箱路径。 |
928e41f4b71Sopenharmony_ci  | mode | number | 否    | 复制模式。默认mode为0。<br/>-&nbsp; mode为0,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以Array\<[ConflictFiles](#conflictfiles10)>形式提供。<br/>-&nbsp; mode为1,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。|
929e41f4b71Sopenharmony_ci
930e41f4b71Sopenharmony_ci**错误码:**
931e41f4b71Sopenharmony_ci
932e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
933e41f4b71Sopenharmony_ci
934e41f4b71Sopenharmony_ci**示例:**
935e41f4b71Sopenharmony_ci
936e41f4b71Sopenharmony_ci  ```ts
937e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
938e41f4b71Sopenharmony_ci  // copy directory from srcPath to destPath
939e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/";
940e41f4b71Sopenharmony_ci  let destPath = pathDir + "/destDir/";
941e41f4b71Sopenharmony_ci  try {
942e41f4b71Sopenharmony_ci    fs.copyDirSync(srcPath, destPath, 0);
943e41f4b71Sopenharmony_ci    console.info("copy directory succeed");
944e41f4b71Sopenharmony_ci  } catch (error) {
945e41f4b71Sopenharmony_ci    let err: BusinessError = error as BusinessError;
946e41f4b71Sopenharmony_ci    console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code);
947e41f4b71Sopenharmony_ci  }
948e41f4b71Sopenharmony_ci  ```
949e41f4b71Sopenharmony_ci
950e41f4b71Sopenharmony_ci## fs.dup<sup>10+</sup>
951e41f4b71Sopenharmony_ci
952e41f4b71Sopenharmony_cidup(fd: number): File
953e41f4b71Sopenharmony_ci
954e41f4b71Sopenharmony_ci将文件描述符转化为File。
955e41f4b71Sopenharmony_ci
956e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
957e41f4b71Sopenharmony_ci
958e41f4b71Sopenharmony_ci**参数:**
959e41f4b71Sopenharmony_ci
960e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
961e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
962e41f4b71Sopenharmony_ci  | fd | number | 是    | 文件描述符。 |
963e41f4b71Sopenharmony_ci
964e41f4b71Sopenharmony_ci**返回值:**
965e41f4b71Sopenharmony_ci
966e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
967e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
968e41f4b71Sopenharmony_ci  | [File](#file) | 打开的File对象。 |
969e41f4b71Sopenharmony_ci
970e41f4b71Sopenharmony_ci**错误码:**
971e41f4b71Sopenharmony_ci
972e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
973e41f4b71Sopenharmony_ci
974e41f4b71Sopenharmony_ci**示例:**
975e41f4b71Sopenharmony_ci
976e41f4b71Sopenharmony_ci  ```ts
977e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
978e41f4b71Sopenharmony_ci  let file1 = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
979e41f4b71Sopenharmony_ci  let fd: number = file1.fd;
980e41f4b71Sopenharmony_ci  let file2 = fs.dup(fd);
981e41f4b71Sopenharmony_ci  console.info("The name of the file2 is " + file2.name);
982e41f4b71Sopenharmony_ci  fs.closeSync(file1);
983e41f4b71Sopenharmony_ci  fs.closeSync(file2);
984e41f4b71Sopenharmony_ci  ```
985e41f4b71Sopenharmony_ci
986e41f4b71Sopenharmony_ci## fs.connectDfs<sup>12+</sup>
987e41f4b71Sopenharmony_ci
988e41f4b71Sopenharmony_ciconnectDfs(networkId: string, listeners: DfsListeners): Promise&lt;void&gt;
989e41f4b71Sopenharmony_ci
990e41f4b71Sopenharmony_ci业务调用connectDfs接口,触发建链并将对端设备公共文档目录挂载到沙箱路径下,如果对端设备出现异常,业务执行回调DfsListeners内[onStatus](#onstatus12)通知应用。
991e41f4b71Sopenharmony_ci
992e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.DISTRIBUTED_DATASYNC
993e41f4b71Sopenharmony_ci
994e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
995e41f4b71Sopenharmony_ci
996e41f4b71Sopenharmony_ci**参数:**
997e41f4b71Sopenharmony_ci
998e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明                                       |
999e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ---------------------------------------- |
1000e41f4b71Sopenharmony_ci  | networkId   | string | 是    | 设备的网络Id。通过[distributedDeviceManager](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md)接口调用[deviceBasicInfo](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#devicebasicinfo)获得。                             |
1001e41f4b71Sopenharmony_ci  | listeners | [DfsListeners](#fsdfslisteners12) | 是    | 分布式文件系统状态监听器。                |
1002e41f4b71Sopenharmony_ci
1003e41f4b71Sopenharmony_ci**返回值:**
1004e41f4b71Sopenharmony_ci
1005e41f4b71Sopenharmony_ci  | 类型     | 说明                                       |
1006e41f4b71Sopenharmony_ci  | ------ | ---------------------------------------- |
1007e41f4b71Sopenharmony_ci  | Promise&lt;void&gt;| Promise对象。无返回值。                             |
1008e41f4b71Sopenharmony_ci
1009e41f4b71Sopenharmony_ci**错误码:**
1010e41f4b71Sopenharmony_ci
1011e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1012e41f4b71Sopenharmony_ci
1013e41f4b71Sopenharmony_ci**示例:**
1014e41f4b71Sopenharmony_ci
1015e41f4b71Sopenharmony_ci  ```ts
1016e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1017e41f4b71Sopenharmony_ci  import { fileIo as fs } from '@kit.CoreFileKit';
1018e41f4b71Sopenharmony_ci  import { distributedDeviceManager } from '@kit.DistributedServiceKit';
1019e41f4b71Sopenharmony_ci  let dmInstance = distributedDeviceManager.createDeviceManager("com.example.filesync");
1020e41f4b71Sopenharmony_ci  let deviceInfoList: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync();
1021e41f4b71Sopenharmony_ci  let networkId = deviceInfoList[0].networkId;
1022e41f4b71Sopenharmony_ci  let listeners: fs.DfsListeners = {
1023e41f4b71Sopenharmony_ci    onStatus(networkId, status) {
1024e41f4b71Sopenharmony_ci      console.info('onStatus');
1025e41f4b71Sopenharmony_ci    }
1026e41f4b71Sopenharmony_ci  }
1027e41f4b71Sopenharmony_ci  fs.connectDfs(networkId, listeners).then(() => {
1028e41f4b71Sopenharmony_ci    console.info("Success to connectDfs");
1029e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1030e41f4b71Sopenharmony_ci    console.error("connectDfs failed with error message: " + err.message + ", error code: " + err.code);
1031e41f4b71Sopenharmony_ci  });
1032e41f4b71Sopenharmony_ci  ```
1033e41f4b71Sopenharmony_ci
1034e41f4b71Sopenharmony_ci## fs.disconnectDfs<sup>12+</sup>
1035e41f4b71Sopenharmony_ci
1036e41f4b71Sopenharmony_cidisconnectDfs(networkId: string): Promise&lt;void&gt;
1037e41f4b71Sopenharmony_ci
1038e41f4b71Sopenharmony_ci业务调用disconnectDfs接口,传入networkId参数,触发断链并取消公共文档目录挂载。
1039e41f4b71Sopenharmony_ci
1040e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.DISTRIBUTED_DATASYNC
1041e41f4b71Sopenharmony_ci
1042e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1043e41f4b71Sopenharmony_ci
1044e41f4b71Sopenharmony_ci**参数:**
1045e41f4b71Sopenharmony_ci
1046e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明                                       |
1047e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ---------------------------------------- |
1048e41f4b71Sopenharmony_ci  | networkId   | string | 是    | 设备的网络Id。通过[distributedDeviceManager](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md)接口调用[deviceBasicInfo](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#devicebasicinfo)获得。                            |
1049e41f4b71Sopenharmony_ci
1050e41f4b71Sopenharmony_ci**返回值:**
1051e41f4b71Sopenharmony_ci
1052e41f4b71Sopenharmony_ci  | 类型     | 说明                                       |
1053e41f4b71Sopenharmony_ci  | ------ | ---------------------------------------- |
1054e41f4b71Sopenharmony_ci  | Promise&lt;void&gt;| Promise对象。无返回值。                             |
1055e41f4b71Sopenharmony_ci
1056e41f4b71Sopenharmony_ci**错误码:**
1057e41f4b71Sopenharmony_ci
1058e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1059e41f4b71Sopenharmony_ci
1060e41f4b71Sopenharmony_ci**示例:**
1061e41f4b71Sopenharmony_ci
1062e41f4b71Sopenharmony_ci  ```ts
1063e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1064e41f4b71Sopenharmony_ci  import { fileIo as fs } from '@kit.CoreFileKit';
1065e41f4b71Sopenharmony_ci  import { distributedDeviceManager } from '@kit.DistributedServiceKit';
1066e41f4b71Sopenharmony_ci  let dmInstance = distributedDeviceManager.createDeviceManager("com.example.filesync");
1067e41f4b71Sopenharmony_ci  let deviceInfoList: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync();
1068e41f4b71Sopenharmony_ci  let networkId = deviceInfoList[0].networkId;
1069e41f4b71Sopenharmony_ci  fs.disconnectDfs(networkId).then(() => {
1070e41f4b71Sopenharmony_ci    console.info("Success to disconnectDfs");
1071e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1072e41f4b71Sopenharmony_ci    console.error('disconnectDfs failed with error message: ${JSON.stringify(err)}')
1073e41f4b71Sopenharmony_ci  })
1074e41f4b71Sopenharmony_ci  ```
1075e41f4b71Sopenharmony_ci
1076e41f4b71Sopenharmony_ci## fs.setxattr<sup>12+</sup>
1077e41f4b71Sopenharmony_ci
1078e41f4b71Sopenharmony_cisetxattr(path: string, key: string, value: string): Promise&lt;void&gt;
1079e41f4b71Sopenharmony_ci
1080e41f4b71Sopenharmony_ci设置文件的扩展属性。
1081e41f4b71Sopenharmony_ci
1082e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1083e41f4b71Sopenharmony_ci
1084e41f4b71Sopenharmony_ci**参数:**
1085e41f4b71Sopenharmony_ci
1086e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1087e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1088e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。                                   |
1089e41f4b71Sopenharmony_ci| key   | string | 是   | 扩展属性的key。                                   |
1090e41f4b71Sopenharmony_ci| value   | string | 是   | 扩展属性的value。                                   |
1091e41f4b71Sopenharmony_ci
1092e41f4b71Sopenharmony_ci**返回值:**
1093e41f4b71Sopenharmony_ci
1094e41f4b71Sopenharmony_ci  | 类型     | 说明                                       |
1095e41f4b71Sopenharmony_ci  | ------ | ---------------------------------------- |
1096e41f4b71Sopenharmony_ci  | Promise&lt;void&gt;| Promise对象。无返回值。                             |
1097e41f4b71Sopenharmony_ci
1098e41f4b71Sopenharmony_ci**错误码:**
1099e41f4b71Sopenharmony_ci
1100e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1101e41f4b71Sopenharmony_ci
1102e41f4b71Sopenharmony_ci**示例:**
1103e41f4b71Sopenharmony_ci
1104e41f4b71Sopenharmony_ci  ```ts
1105e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1106e41f4b71Sopenharmony_ci  
1107e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1108e41f4b71Sopenharmony_ci  let attrKey = "user.comment";
1109e41f4b71Sopenharmony_ci  let attrValue = "Test file.";
1110e41f4b71Sopenharmony_ci  
1111e41f4b71Sopenharmony_ci  fs.setxattr(filePath, attrKey, attrValue).then(() => {
1112e41f4b71Sopenharmony_ci    console.info("Set extended attribute successfully.");
1113e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1114e41f4b71Sopenharmony_ci    console.error("Failed to set extended attribute with error message: " + err.message + ", error code: " + err.code);
1115e41f4b71Sopenharmony_ci  });
1116e41f4b71Sopenharmony_ci
1117e41f4b71Sopenharmony_ci  ```
1118e41f4b71Sopenharmony_ci## fs.setxattrSync<sup>12+</sup>
1119e41f4b71Sopenharmony_ci
1120e41f4b71Sopenharmony_cisetxattrSync(path: string, key: string, value: string): void;
1121e41f4b71Sopenharmony_ci
1122e41f4b71Sopenharmony_ci设置文件的扩展属性。
1123e41f4b71Sopenharmony_ci
1124e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1125e41f4b71Sopenharmony_ci
1126e41f4b71Sopenharmony_ci**参数:**
1127e41f4b71Sopenharmony_ci
1128e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1129e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1130e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。                                   |
1131e41f4b71Sopenharmony_ci| key   | string | 是   | 扩展属性的key。                                   |
1132e41f4b71Sopenharmony_ci| value   | string | 是   | 扩展属性的value。                                   |
1133e41f4b71Sopenharmony_ci
1134e41f4b71Sopenharmony_ci**错误码:**
1135e41f4b71Sopenharmony_ci
1136e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1137e41f4b71Sopenharmony_ci
1138e41f4b71Sopenharmony_ci**示例:**
1139e41f4b71Sopenharmony_ci
1140e41f4b71Sopenharmony_ci  ```ts
1141e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1142e41f4b71Sopenharmony_ci  
1143e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1144e41f4b71Sopenharmony_ci  let attrKey = "user.comment";
1145e41f4b71Sopenharmony_ci  let attrValue = "Test file.";
1146e41f4b71Sopenharmony_ci
1147e41f4b71Sopenharmony_ci  try {
1148e41f4b71Sopenharmony_ci    fs.setxattrSync(filePath, attrKey, attrValue);
1149e41f4b71Sopenharmony_ci    console.info("Set extended attribute successfully.");
1150e41f4b71Sopenharmony_ci  } catch (err) {
1151e41f4b71Sopenharmony_ci    console.error("Failed to set extended attribute with error message: " + err.message + ", error code: " + err.code);
1152e41f4b71Sopenharmony_ci  }
1153e41f4b71Sopenharmony_ci  
1154e41f4b71Sopenharmony_ci  ```
1155e41f4b71Sopenharmony_ci
1156e41f4b71Sopenharmony_ci## fs.getxattr<sup>12+</sup>
1157e41f4b71Sopenharmony_ci
1158e41f4b71Sopenharmony_cigetxattr(path: string, key: string): Promise&lt;string&gt;
1159e41f4b71Sopenharmony_ci
1160e41f4b71Sopenharmony_ci获取文件的扩展属性。
1161e41f4b71Sopenharmony_ci
1162e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1163e41f4b71Sopenharmony_ci
1164e41f4b71Sopenharmony_ci**参数:**
1165e41f4b71Sopenharmony_ci
1166e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1167e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1168e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。                                   |
1169e41f4b71Sopenharmony_ci| key   | string | 是   | 扩展属性的key。                                   |
1170e41f4b71Sopenharmony_ci
1171e41f4b71Sopenharmony_ci**返回值:**
1172e41f4b71Sopenharmony_ci
1173e41f4b71Sopenharmony_ci  | 类型     | 说明                                       |
1174e41f4b71Sopenharmony_ci  | ------ | ---------------------------------------- |
1175e41f4b71Sopenharmony_ci  | Promise&lt;string&gt;| Promise对象。返回扩展属性的value。    |
1176e41f4b71Sopenharmony_ci
1177e41f4b71Sopenharmony_ci**错误码:**
1178e41f4b71Sopenharmony_ci
1179e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1180e41f4b71Sopenharmony_ci
1181e41f4b71Sopenharmony_ci**示例:**
1182e41f4b71Sopenharmony_ci
1183e41f4b71Sopenharmony_ci  ```ts
1184e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1185e41f4b71Sopenharmony_ci  
1186e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1187e41f4b71Sopenharmony_ci  let attrKey = "user.comment";
1188e41f4b71Sopenharmony_ci  
1189e41f4b71Sopenharmony_ci  fs.getxattr(filePath, attrKey).then((attrValue: string) => {
1190e41f4b71Sopenharmony_ci    console.info("Get extended attribute succeed, the value is: " + attrValue);
1191e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1192e41f4b71Sopenharmony_ci    console.error("Failed to get extended attribute with error message: " + err.message + ", error code: " + err.code);
1193e41f4b71Sopenharmony_ci  });
1194e41f4b71Sopenharmony_ci
1195e41f4b71Sopenharmony_ci  ```
1196e41f4b71Sopenharmony_ci
1197e41f4b71Sopenharmony_ci## fs.getxattrSync<sup>12+</sup>
1198e41f4b71Sopenharmony_ci
1199e41f4b71Sopenharmony_cigetxattrSync(path: string, key: string): string;
1200e41f4b71Sopenharmony_ci
1201e41f4b71Sopenharmony_ci使用同步接口获取文件的扩展属性。
1202e41f4b71Sopenharmony_ci
1203e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1204e41f4b71Sopenharmony_ci
1205e41f4b71Sopenharmony_ci**参数:**
1206e41f4b71Sopenharmony_ci
1207e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1208e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1209e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。                                   |
1210e41f4b71Sopenharmony_ci| key   | string | 是   | 扩展属性的key。                                   |
1211e41f4b71Sopenharmony_ci
1212e41f4b71Sopenharmony_ci**返回值:**
1213e41f4b71Sopenharmony_ci
1214e41f4b71Sopenharmony_ci  | 类型     | 说明                                       |
1215e41f4b71Sopenharmony_ci  | ------ | ---------------------------------------- |
1216e41f4b71Sopenharmony_ci  | key| string对象。返回扩展属性的value。      |
1217e41f4b71Sopenharmony_ci
1218e41f4b71Sopenharmony_ci**错误码:**
1219e41f4b71Sopenharmony_ci
1220e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1221e41f4b71Sopenharmony_ci
1222e41f4b71Sopenharmony_ci**示例:**
1223e41f4b71Sopenharmony_ci
1224e41f4b71Sopenharmony_ci  ```ts
1225e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1226e41f4b71Sopenharmony_ci  
1227e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1228e41f4b71Sopenharmony_ci  let attrKey = "user.comment";
1229e41f4b71Sopenharmony_ci  
1230e41f4b71Sopenharmony_ci  try {
1231e41f4b71Sopenharmony_ci    let attrValue = fs.getxattrSync(filePath, attrKey);
1232e41f4b71Sopenharmony_ci    console.info("Get extended attribute succeed, the value is: " + attrValue);
1233e41f4b71Sopenharmony_ci  } catch (err) {
1234e41f4b71Sopenharmony_ci    console.error("Failed to get extended attribute with error message: " + err.message + ", error code: " + err.code);
1235e41f4b71Sopenharmony_ci    }
1236e41f4b71Sopenharmony_ci
1237e41f4b71Sopenharmony_ci  ```
1238e41f4b71Sopenharmony_ci
1239e41f4b71Sopenharmony_ci## fs.mkdir
1240e41f4b71Sopenharmony_ci
1241e41f4b71Sopenharmony_cimkdir(path: string): Promise&lt;void&gt;
1242e41f4b71Sopenharmony_ci
1243e41f4b71Sopenharmony_ci创建目录,使用Promise异步返回。
1244e41f4b71Sopenharmony_ci
1245e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1246e41f4b71Sopenharmony_ci
1247e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1248e41f4b71Sopenharmony_ci
1249e41f4b71Sopenharmony_ci**参数:**
1250e41f4b71Sopenharmony_ci
1251e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1252e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1253e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。                                   |
1254e41f4b71Sopenharmony_ci
1255e41f4b71Sopenharmony_ci**返回值:**
1256e41f4b71Sopenharmony_ci
1257e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
1258e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
1259e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
1260e41f4b71Sopenharmony_ci
1261e41f4b71Sopenharmony_ci**错误码:**
1262e41f4b71Sopenharmony_ci
1263e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1264e41f4b71Sopenharmony_ci
1265e41f4b71Sopenharmony_ci**示例:**
1266e41f4b71Sopenharmony_ci
1267e41f4b71Sopenharmony_ci  ```ts
1268e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1269e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/testDir";
1270e41f4b71Sopenharmony_ci  fs.mkdir(dirPath).then(() => {
1271e41f4b71Sopenharmony_ci    console.info("mkdir succeed");
1272e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1273e41f4b71Sopenharmony_ci    console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code);
1274e41f4b71Sopenharmony_ci  });
1275e41f4b71Sopenharmony_ci  ```
1276e41f4b71Sopenharmony_ci
1277e41f4b71Sopenharmony_ci## fs.mkdir<sup>11+</sup>
1278e41f4b71Sopenharmony_ci
1279e41f4b71Sopenharmony_cimkdir(path: string, recursion: boolean): Promise\<void>
1280e41f4b71Sopenharmony_ci
1281e41f4b71Sopenharmony_ci创建目录,使用Promise异步返回。当recursion指定为true,可多层级创建目录。
1282e41f4b71Sopenharmony_ci
1283e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1284e41f4b71Sopenharmony_ci
1285e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1286e41f4b71Sopenharmony_ci
1287e41f4b71Sopenharmony_ci**参数:**
1288e41f4b71Sopenharmony_ci
1289e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1290e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1291e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。                                   |
1292e41f4b71Sopenharmony_ci| recursion   | boolean | 是   | 是否多层级创建目录。recursion指定为true时,可多层级创建目录。recursion指定为false时,仅可创建单层目录。   |
1293e41f4b71Sopenharmony_ci
1294e41f4b71Sopenharmony_ci**返回值:**
1295e41f4b71Sopenharmony_ci
1296e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
1297e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
1298e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
1299e41f4b71Sopenharmony_ci
1300e41f4b71Sopenharmony_ci**错误码:**
1301e41f4b71Sopenharmony_ci
1302e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1303e41f4b71Sopenharmony_ci
1304e41f4b71Sopenharmony_ci**示例:**
1305e41f4b71Sopenharmony_ci
1306e41f4b71Sopenharmony_ci  ```ts
1307e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1308e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/testDir1/testDir2/testDir3";
1309e41f4b71Sopenharmony_ci  fs.mkdir(dirPath, true).then(() => {
1310e41f4b71Sopenharmony_ci    console.info("mkdir succeed");
1311e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1312e41f4b71Sopenharmony_ci    console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code);
1313e41f4b71Sopenharmony_ci  });
1314e41f4b71Sopenharmony_ci  ```
1315e41f4b71Sopenharmony_ci
1316e41f4b71Sopenharmony_ci## fs.mkdir
1317e41f4b71Sopenharmony_ci
1318e41f4b71Sopenharmony_cimkdir(path: string, callback: AsyncCallback&lt;void&gt;): void
1319e41f4b71Sopenharmony_ci
1320e41f4b71Sopenharmony_ci创建目录,使用callback异步回调。
1321e41f4b71Sopenharmony_ci
1322e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1323e41f4b71Sopenharmony_ci
1324e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1325e41f4b71Sopenharmony_ci
1326e41f4b71Sopenharmony_ci**参数:**
1327e41f4b71Sopenharmony_ci
1328e41f4b71Sopenharmony_ci| 参数名   | 类型                      | 必填 | 说明                                                         |
1329e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
1330e41f4b71Sopenharmony_ci| path     | string                    | 是   | 目录的应用沙箱路径。                                   |
1331e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | 是   | 异步创建目录操作完成之后的回调。                             |
1332e41f4b71Sopenharmony_ci
1333e41f4b71Sopenharmony_ci**错误码:**
1334e41f4b71Sopenharmony_ci
1335e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1336e41f4b71Sopenharmony_ci
1337e41f4b71Sopenharmony_ci**示例:**
1338e41f4b71Sopenharmony_ci
1339e41f4b71Sopenharmony_ci  ```ts
1340e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1341e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/testDir";
1342e41f4b71Sopenharmony_ci  fs.mkdir(dirPath, (err: BusinessError) => {
1343e41f4b71Sopenharmony_ci    if (err) {
1344e41f4b71Sopenharmony_ci      console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code);
1345e41f4b71Sopenharmony_ci    } else {
1346e41f4b71Sopenharmony_ci      console.info("mkdir succeed");
1347e41f4b71Sopenharmony_ci    }
1348e41f4b71Sopenharmony_ci  });
1349e41f4b71Sopenharmony_ci  ```
1350e41f4b71Sopenharmony_ci
1351e41f4b71Sopenharmony_ci## fs.mkdir<sup>11+</sup>
1352e41f4b71Sopenharmony_ci
1353e41f4b71Sopenharmony_cimkdir(path: string, recursion: boolean, callback: AsyncCallback&lt;void&gt;): void
1354e41f4b71Sopenharmony_ci
1355e41f4b71Sopenharmony_ci创建目录,使用callback异步回调。当recursion指定为true,可多层级创建目录。
1356e41f4b71Sopenharmony_ci
1357e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1358e41f4b71Sopenharmony_ci
1359e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1360e41f4b71Sopenharmony_ci
1361e41f4b71Sopenharmony_ci**参数:**
1362e41f4b71Sopenharmony_ci
1363e41f4b71Sopenharmony_ci| 参数名   | 类型                      | 必填 | 说明                                                         |
1364e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
1365e41f4b71Sopenharmony_ci| path     | string                    | 是   | 目录的应用沙箱路径。                                   |
1366e41f4b71Sopenharmony_ci| recursion   | boolean | 是   | 是否多层级创建目录。recursion指定为true时,可多层级创建目录。recursion指定为false时,仅可创建单层目录。   |
1367e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | 是   | 异步创建目录操作完成之后的回调。                             |
1368e41f4b71Sopenharmony_ci
1369e41f4b71Sopenharmony_ci**错误码:**
1370e41f4b71Sopenharmony_ci
1371e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1372e41f4b71Sopenharmony_ci
1373e41f4b71Sopenharmony_ci**示例:**
1374e41f4b71Sopenharmony_ci
1375e41f4b71Sopenharmony_ci  ```ts
1376e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1377e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/testDir1/testDir2/testDir3";
1378e41f4b71Sopenharmony_ci  fs.mkdir(dirPath, true, (err: BusinessError) => {
1379e41f4b71Sopenharmony_ci    if (err) {
1380e41f4b71Sopenharmony_ci      console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code);
1381e41f4b71Sopenharmony_ci    } else {
1382e41f4b71Sopenharmony_ci      console.info("mkdir succeed");
1383e41f4b71Sopenharmony_ci    }
1384e41f4b71Sopenharmony_ci  });
1385e41f4b71Sopenharmony_ci  ```
1386e41f4b71Sopenharmony_ci
1387e41f4b71Sopenharmony_ci## fs.mkdirSync
1388e41f4b71Sopenharmony_ci
1389e41f4b71Sopenharmony_cimkdirSync(path: string): void
1390e41f4b71Sopenharmony_ci
1391e41f4b71Sopenharmony_ci以同步方法创建目录。
1392e41f4b71Sopenharmony_ci
1393e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1394e41f4b71Sopenharmony_ci
1395e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1396e41f4b71Sopenharmony_ci
1397e41f4b71Sopenharmony_ci**参数:**
1398e41f4b71Sopenharmony_ci
1399e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1400e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1401e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。                                   |
1402e41f4b71Sopenharmony_ci
1403e41f4b71Sopenharmony_ci**错误码:**
1404e41f4b71Sopenharmony_ci
1405e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1406e41f4b71Sopenharmony_ci
1407e41f4b71Sopenharmony_ci**示例:**
1408e41f4b71Sopenharmony_ci
1409e41f4b71Sopenharmony_ci  ```ts
1410e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/testDir";
1411e41f4b71Sopenharmony_ci  fs.mkdirSync(dirPath);
1412e41f4b71Sopenharmony_ci  ```
1413e41f4b71Sopenharmony_ci
1414e41f4b71Sopenharmony_ci## fs.mkdirSync<sup>11+</sup>
1415e41f4b71Sopenharmony_ci
1416e41f4b71Sopenharmony_cimkdirSync(path: string, recursion: boolean): void
1417e41f4b71Sopenharmony_ci
1418e41f4b71Sopenharmony_ci以同步方法创建目录。当recursion指定为true,可多层级创建目录。
1419e41f4b71Sopenharmony_ci
1420e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1421e41f4b71Sopenharmony_ci
1422e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1423e41f4b71Sopenharmony_ci
1424e41f4b71Sopenharmony_ci**参数:**
1425e41f4b71Sopenharmony_ci
1426e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1427e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1428e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。                                   |
1429e41f4b71Sopenharmony_ci| recursion   | boolean | 是   | 是否多层级创建目录。recursion指定为true时,可多层级创建目录。recursion指定为false时,仅可创建单层目录。   |
1430e41f4b71Sopenharmony_ci
1431e41f4b71Sopenharmony_ci**错误码:**
1432e41f4b71Sopenharmony_ci
1433e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1434e41f4b71Sopenharmony_ci
1435e41f4b71Sopenharmony_ci**示例:**
1436e41f4b71Sopenharmony_ci
1437e41f4b71Sopenharmony_ci  ```ts
1438e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/testDir1/testDir2/testDir3";
1439e41f4b71Sopenharmony_ci  fs.mkdirSync(dirPath, true);
1440e41f4b71Sopenharmony_ci  ```
1441e41f4b71Sopenharmony_ci
1442e41f4b71Sopenharmony_ci## fs.open
1443e41f4b71Sopenharmony_ci
1444e41f4b71Sopenharmony_ciopen(path: string, mode?: number): Promise&lt;File&gt;
1445e41f4b71Sopenharmony_ci
1446e41f4b71Sopenharmony_ci打开文件,使用Promise异步返回。支持使用URI打开文件。
1447e41f4b71Sopenharmony_ci
1448e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1449e41f4b71Sopenharmony_ci
1450e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1451e41f4b71Sopenharmony_ci
1452e41f4b71Sopenharmony_ci**参数:**
1453e41f4b71Sopenharmony_ci
1454e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1455e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1456e41f4b71Sopenharmony_ci| path   | string | 是   | 文件的应用沙箱路径或文件URI,使用URI作为入参时,仅支持打开文件。                                   |
1457e41f4b71Sopenharmony_ci| mode  | number | 否   | 打开文件的[选项](#openmode),必须指定如下选项中的一个,默认以只读方式打开:<br/>-&nbsp;OpenMode.READ_ONLY(0o0):只读打开。<br/>-&nbsp;OpenMode.WRITE_ONLY(0o1):只写打开。<br/>-&nbsp;OpenMode.READ_WRITE(0o2):读写打开。<br/>给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;OpenMode.CREATE(0o100):若文件不存在,则创建文件。(path为URI时不支持创建文件)<br/>-&nbsp;OpenMode.TRUNC(0o1000):如果文件存在且文件具有写权限,则将其长度裁剪为零。<br/>-&nbsp;OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到文件末尾。<br/>-&nbsp;OpenMode.NONBLOCK(0o4000):如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。<br/>-&nbsp;OpenMode.NOFOLLOW(0o400000):如果path指向符号链接,则出错。<br/>-&nbsp;OpenMode.SYNC(0o4010000):以同步IO的方式打开文件。 |
1458e41f4b71Sopenharmony_ci
1459e41f4b71Sopenharmony_ci**返回值:**
1460e41f4b71Sopenharmony_ci
1461e41f4b71Sopenharmony_ci  | 类型                    | 说明          |
1462e41f4b71Sopenharmony_ci  | --------------------- | ----------- |
1463e41f4b71Sopenharmony_ci  | Promise&lt;[File](#file)&gt; | Promise对象。返回File对象。 |
1464e41f4b71Sopenharmony_ci
1465e41f4b71Sopenharmony_ci**错误码:**
1466e41f4b71Sopenharmony_ci
1467e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1468e41f4b71Sopenharmony_ci
1469e41f4b71Sopenharmony_ci**示例:**
1470e41f4b71Sopenharmony_ci
1471e41f4b71Sopenharmony_ci  ```ts
1472e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1473e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1474e41f4b71Sopenharmony_ci  fs.open(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).then((file: fs.File) => {
1475e41f4b71Sopenharmony_ci    console.info("file fd: " + file.fd);
1476e41f4b71Sopenharmony_ci    fs.closeSync(file);
1477e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1478e41f4b71Sopenharmony_ci    console.error("open file failed with error message: " + err.message + ", error code: " + err.code);
1479e41f4b71Sopenharmony_ci  });
1480e41f4b71Sopenharmony_ci  ```
1481e41f4b71Sopenharmony_ci
1482e41f4b71Sopenharmony_ci
1483e41f4b71Sopenharmony_ci## fs.open
1484e41f4b71Sopenharmony_ci
1485e41f4b71Sopenharmony_ciopen(path: string, mode: number, callback: AsyncCallback&lt;File&gt;): void
1486e41f4b71Sopenharmony_ci
1487e41f4b71Sopenharmony_ci打开文件,可设置打开文件的选项。使用callback异步回调。
1488e41f4b71Sopenharmony_ci
1489e41f4b71Sopenharmony_ci支持使用URI打开文件。
1490e41f4b71Sopenharmony_ci
1491e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1492e41f4b71Sopenharmony_ci
1493e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1494e41f4b71Sopenharmony_ci
1495e41f4b71Sopenharmony_ci**参数:**
1496e41f4b71Sopenharmony_ci
1497e41f4b71Sopenharmony_ci| 参数名   | 类型                            | 必填 | 说明                                                         |
1498e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
1499e41f4b71Sopenharmony_ci| path     | string                          | 是   | 文件的应用沙箱路径或URI,使用URI作为入参时,仅支持打开文件。                                   |
1500e41f4b71Sopenharmony_ci| mode  | number | 是   | 打开文件的[选项](#openmode),必须指定如下选项中的一个,默认以只读方式打开:<br/>-&nbsp;OpenMode.READ_ONLY(0o0):只读打开。<br/>-&nbsp;OpenMode.WRITE_ONLY(0o1):只写打开。<br/>-&nbsp;OpenMode.READ_WRITE(0o2):读写打开。<br/>给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;OpenMode.CREATE(0o100):若文件不存在,则创建文件。(path为URI时不支持创建文件)<br/>-&nbsp;OpenMode.TRUNC(0o1000):如果文件存在且文件具有写权限,则将其长度裁剪为零。<br/>-&nbsp;OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到文件末尾。<br/>-&nbsp;OpenMode.NONBLOCK(0o4000):如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。<br/>-&nbsp;OpenMode.NOFOLLOW(0o400000):如果path指向符号链接,则出错。<br/>-&nbsp;OpenMode.SYNC(0o4010000):以同步IO的方式打开文件。 |
1501e41f4b71Sopenharmony_ci| callback     | AsyncCallback&lt;void&gt;                          | 是   | 异步打开文件之后的回调。                                   |
1502e41f4b71Sopenharmony_ci
1503e41f4b71Sopenharmony_ci**错误码:**
1504e41f4b71Sopenharmony_ci
1505e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1506e41f4b71Sopenharmony_ci
1507e41f4b71Sopenharmony_ci**示例:**
1508e41f4b71Sopenharmony_ci
1509e41f4b71Sopenharmony_ci  ```ts
1510e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1511e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1512e41f4b71Sopenharmony_ci  fs.open(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE, (err: BusinessError, file: fs.File) => {
1513e41f4b71Sopenharmony_ci    if (err) {
1514e41f4b71Sopenharmony_ci      console.error("open failed with error message: " + err.message + ", error code: " + err.code);
1515e41f4b71Sopenharmony_ci    } else {
1516e41f4b71Sopenharmony_ci      console.info("file fd: " + file.fd);
1517e41f4b71Sopenharmony_ci    }
1518e41f4b71Sopenharmony_ci    fs.closeSync(file);
1519e41f4b71Sopenharmony_ci  });
1520e41f4b71Sopenharmony_ci  ```
1521e41f4b71Sopenharmony_ci
1522e41f4b71Sopenharmony_ci## fs.open
1523e41f4b71Sopenharmony_ci
1524e41f4b71Sopenharmony_ciopen(path: string, callback: AsyncCallback&lt;File&gt;): void
1525e41f4b71Sopenharmony_ci
1526e41f4b71Sopenharmony_ci打开文件,使用callback异步回调。支持使用URI打开文件。
1527e41f4b71Sopenharmony_ci
1528e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1529e41f4b71Sopenharmony_ci
1530e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1531e41f4b71Sopenharmony_ci
1532e41f4b71Sopenharmony_ci**参数:**
1533e41f4b71Sopenharmony_ci
1534e41f4b71Sopenharmony_ci| 参数名   | 类型                            | 必填 | 说明                                                         |
1535e41f4b71Sopenharmony_ci| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
1536e41f4b71Sopenharmony_ci| path     | string                          | 是   | 文件的应用沙箱路径或URI。                                   |
1537e41f4b71Sopenharmony_ci| callback     | AsyncCallback&lt;void&gt;                          | 是   | 异步打开文件之后的回调。                                   |
1538e41f4b71Sopenharmony_ci
1539e41f4b71Sopenharmony_ci**错误码:**
1540e41f4b71Sopenharmony_ci
1541e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1542e41f4b71Sopenharmony_ci
1543e41f4b71Sopenharmony_ci**示例:**
1544e41f4b71Sopenharmony_ci
1545e41f4b71Sopenharmony_ci  ```ts
1546e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1547e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1548e41f4b71Sopenharmony_ci  fs.open(filePath, (err: BusinessError, file: fs.File) => {
1549e41f4b71Sopenharmony_ci    if (err) {
1550e41f4b71Sopenharmony_ci      console.error("open failed with error message: " + err.message + ", error code: " + err.code);
1551e41f4b71Sopenharmony_ci    } else {
1552e41f4b71Sopenharmony_ci      console.info("file fd: " + file.fd);
1553e41f4b71Sopenharmony_ci    }
1554e41f4b71Sopenharmony_ci    fs.closeSync(file);
1555e41f4b71Sopenharmony_ci  });
1556e41f4b71Sopenharmony_ci  ```
1557e41f4b71Sopenharmony_ci
1558e41f4b71Sopenharmony_ci## fs.openSync
1559e41f4b71Sopenharmony_ci
1560e41f4b71Sopenharmony_ciopenSync(path: string, mode?: number): File
1561e41f4b71Sopenharmony_ci
1562e41f4b71Sopenharmony_ci以同步方法打开文件。支持使用URI打开文件。
1563e41f4b71Sopenharmony_ci
1564e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1565e41f4b71Sopenharmony_ci
1566e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1567e41f4b71Sopenharmony_ci
1568e41f4b71Sopenharmony_ci**参数:**
1569e41f4b71Sopenharmony_ci
1570e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
1571e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
1572e41f4b71Sopenharmony_ci| path   | string | 是   | 打开文件的应用沙箱路径或URI,使用URI作为入参时,仅支持打开文件。                                   |
1573e41f4b71Sopenharmony_ci| mode  | number | 否   | 打开文件的[选项](#openmode),必须指定如下选项中的一个,默认以只读方式打开:<br/>-&nbsp;OpenMode.READ_ONLY(0o0):只读打开。<br/>-&nbsp;OpenMode.WRITE_ONLY(0o1):只写打开。<br/>-&nbsp;OpenMode.READ_WRITE(0o2):读写打开。<br/>给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;OpenMode.CREATE(0o100):若文件不存在,则创建文件。(path为URI时不支持创建文件)<br/>-&nbsp;OpenMode.TRUNC(0o1000):如果文件存在且文件具有写权限,则将其长度裁剪为零。<br/>-&nbsp;OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到文件末尾。<br/>-&nbsp;OpenMode.NONBLOCK(0o4000):如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。<br/>-&nbsp;OpenMode.NOFOLLOW(0o400000):如果path指向符号链接,则出错。<br/>-&nbsp;OpenMode.SYNC(0o4010000):以同步IO的方式打开文件。 |
1574e41f4b71Sopenharmony_ci
1575e41f4b71Sopenharmony_ci**返回值:**
1576e41f4b71Sopenharmony_ci
1577e41f4b71Sopenharmony_ci  | 类型     | 说明          |
1578e41f4b71Sopenharmony_ci  | ------ | ----------- |
1579e41f4b71Sopenharmony_ci  | [File](#file) | 打开的File对象。 |
1580e41f4b71Sopenharmony_ci
1581e41f4b71Sopenharmony_ci**错误码:**
1582e41f4b71Sopenharmony_ci
1583e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1584e41f4b71Sopenharmony_ci
1585e41f4b71Sopenharmony_ci**示例:**
1586e41f4b71Sopenharmony_ci
1587e41f4b71Sopenharmony_ci  ```ts
1588e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1589e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
1590e41f4b71Sopenharmony_ci  console.info("file fd: " + file.fd);
1591e41f4b71Sopenharmony_ci  fs.closeSync(file);
1592e41f4b71Sopenharmony_ci  ```
1593e41f4b71Sopenharmony_ci
1594e41f4b71Sopenharmony_ci## fs.read
1595e41f4b71Sopenharmony_ci
1596e41f4b71Sopenharmony_ciread(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise&lt;number&gt;
1597e41f4b71Sopenharmony_ci
1598e41f4b71Sopenharmony_ci从文件读取数据,使用Promise异步返回。
1599e41f4b71Sopenharmony_ci
1600e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1601e41f4b71Sopenharmony_ci
1602e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1603e41f4b71Sopenharmony_ci
1604e41f4b71Sopenharmony_ci**参数:**
1605e41f4b71Sopenharmony_ci
1606e41f4b71Sopenharmony_ci| 参数名  | 类型        | 必填 | 说明                                                         |
1607e41f4b71Sopenharmony_ci| ------- | ----------- | ---- | ------------------------------------------------------------ |
1608e41f4b71Sopenharmony_ci| fd      | number      | 是   | 已打开的文件描述符。                                     |
1609e41f4b71Sopenharmony_ci| buffer  | ArrayBuffer | 是   | 用于保存读取到的文件数据的缓冲区。                           |
1610e41f4b71Sopenharmony_ci| options | [ReadOptions](#readoptions11)      | 否   | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。|
1611e41f4b71Sopenharmony_ci
1612e41f4b71Sopenharmony_ci**返回值:**
1613e41f4b71Sopenharmony_ci
1614e41f4b71Sopenharmony_ci  | 类型                                 | 说明     |
1615e41f4b71Sopenharmony_ci  | ---------------------------------- | ------ |
1616e41f4b71Sopenharmony_ci  | Promise&lt;number&gt; | Promise对象。返回实际读取的数据长度,单位字节。|
1617e41f4b71Sopenharmony_ci
1618e41f4b71Sopenharmony_ci**错误码:**
1619e41f4b71Sopenharmony_ci
1620e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1621e41f4b71Sopenharmony_ci
1622e41f4b71Sopenharmony_ci**示例:**
1623e41f4b71Sopenharmony_ci
1624e41f4b71Sopenharmony_ci  ```ts
1625e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1626e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
1627e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1628e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
1629e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(4096);
1630e41f4b71Sopenharmony_ci  fs.read(file.fd, arrayBuffer).then((readLen: number) => {
1631e41f4b71Sopenharmony_ci    console.info("read file data succeed");
1632e41f4b71Sopenharmony_ci    let buf = buffer.from(arrayBuffer, 0, readLen);
1633e41f4b71Sopenharmony_ci    console.info(`The content of file: ${buf.toString()}`);
1634e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1635e41f4b71Sopenharmony_ci    console.error("read file data failed with error message: " + err.message + ", error code: " + err.code);
1636e41f4b71Sopenharmony_ci  }).finally(() => {
1637e41f4b71Sopenharmony_ci    fs.closeSync(file);
1638e41f4b71Sopenharmony_ci  });
1639e41f4b71Sopenharmony_ci  ```
1640e41f4b71Sopenharmony_ci
1641e41f4b71Sopenharmony_ci## fs.read
1642e41f4b71Sopenharmony_ci
1643e41f4b71Sopenharmony_ciread(fd: number, buffer: ArrayBuffer, options?: ReadOptions, callback: AsyncCallback&lt;number&gt;): void
1644e41f4b71Sopenharmony_ci
1645e41f4b71Sopenharmony_ci从文件读取数据,使用callback异步回调。
1646e41f4b71Sopenharmony_ci
1647e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1648e41f4b71Sopenharmony_ci
1649e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1650e41f4b71Sopenharmony_ci
1651e41f4b71Sopenharmony_ci**参数:**
1652e41f4b71Sopenharmony_ci
1653e41f4b71Sopenharmony_ci  | 参数名      | 类型                                       | 必填   | 说明                                       |
1654e41f4b71Sopenharmony_ci  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
1655e41f4b71Sopenharmony_ci  | fd       | number                                   | 是    | 已打开的文件描述符。                             |
1656e41f4b71Sopenharmony_ci  | buffer   | ArrayBuffer                              | 是    | 用于保存读取到的文件数据的缓冲区。                        |
1657e41f4b71Sopenharmony_ci  | options | [ReadOptions](#readoptions11)      | 否   | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。|
1658e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;number&gt; | 是    | 异步读取数据之后的回调。返回实际读取的数据长度,单位字节。                             |
1659e41f4b71Sopenharmony_ci
1660e41f4b71Sopenharmony_ci**错误码:**
1661e41f4b71Sopenharmony_ci
1662e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1663e41f4b71Sopenharmony_ci
1664e41f4b71Sopenharmony_ci**示例:**
1665e41f4b71Sopenharmony_ci
1666e41f4b71Sopenharmony_ci  ```ts
1667e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1668e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
1669e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1670e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
1671e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(4096);
1672e41f4b71Sopenharmony_ci  fs.read(file.fd, arrayBuffer, (err: BusinessError, readLen: number) => {
1673e41f4b71Sopenharmony_ci    if (err) {
1674e41f4b71Sopenharmony_ci      console.error("read failed with error message: " + err.message + ", error code: " + err.code);
1675e41f4b71Sopenharmony_ci    } else {
1676e41f4b71Sopenharmony_ci      console.info("read file data succeed");
1677e41f4b71Sopenharmony_ci      let buf = buffer.from(arrayBuffer, 0, readLen);
1678e41f4b71Sopenharmony_ci      console.info(`The content of file: ${buf.toString()}`);
1679e41f4b71Sopenharmony_ci    }
1680e41f4b71Sopenharmony_ci    fs.closeSync(file);
1681e41f4b71Sopenharmony_ci  });
1682e41f4b71Sopenharmony_ci  ```
1683e41f4b71Sopenharmony_ci
1684e41f4b71Sopenharmony_ci## fs.readSync
1685e41f4b71Sopenharmony_ci
1686e41f4b71Sopenharmony_cireadSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number
1687e41f4b71Sopenharmony_ci
1688e41f4b71Sopenharmony_ci以同步方法从文件读取数据。
1689e41f4b71Sopenharmony_ci
1690e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1691e41f4b71Sopenharmony_ci
1692e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1693e41f4b71Sopenharmony_ci
1694e41f4b71Sopenharmony_ci**参数:**
1695e41f4b71Sopenharmony_ci
1696e41f4b71Sopenharmony_ci  | 参数名     | 类型          | 必填   | 说明                                       |
1697e41f4b71Sopenharmony_ci  | ------- | ----------- | ---- | ---------------------------------------- |
1698e41f4b71Sopenharmony_ci  | fd      | number      | 是    | 已打开的文件描述符。                             |
1699e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer | 是    | 用于保存读取到的文件数据的缓冲区。                        |
1700e41f4b71Sopenharmony_ci  | options | [ReadOptions](#readoptions11)      | 否   | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。|
1701e41f4b71Sopenharmony_ci
1702e41f4b71Sopenharmony_ci**返回值:**
1703e41f4b71Sopenharmony_ci
1704e41f4b71Sopenharmony_ci  | 类型     | 说明       |
1705e41f4b71Sopenharmony_ci  | ------ | -------- |
1706e41f4b71Sopenharmony_ci  | number | 返回实际读取的数据长度,单位字节。 |
1707e41f4b71Sopenharmony_ci
1708e41f4b71Sopenharmony_ci**错误码:**
1709e41f4b71Sopenharmony_ci
1710e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1711e41f4b71Sopenharmony_ci
1712e41f4b71Sopenharmony_ci**示例:**
1713e41f4b71Sopenharmony_ci
1714e41f4b71Sopenharmony_ci  ```ts
1715e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1716e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
1717e41f4b71Sopenharmony_ci  let buf = new ArrayBuffer(4096);
1718e41f4b71Sopenharmony_ci  fs.readSync(file.fd, buf);
1719e41f4b71Sopenharmony_ci  fs.closeSync(file);
1720e41f4b71Sopenharmony_ci  ```
1721e41f4b71Sopenharmony_ci
1722e41f4b71Sopenharmony_ci## fs.rmdir
1723e41f4b71Sopenharmony_ci
1724e41f4b71Sopenharmony_cirmdir(path: string): Promise&lt;void&gt;
1725e41f4b71Sopenharmony_ci
1726e41f4b71Sopenharmony_ci删除整个目录,使用Promise异步返回。
1727e41f4b71Sopenharmony_ci
1728e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1729e41f4b71Sopenharmony_ci
1730e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1731e41f4b71Sopenharmony_ci
1732e41f4b71Sopenharmony_ci**参数:**
1733e41f4b71Sopenharmony_ci
1734e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                       |
1735e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- |
1736e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。 |
1737e41f4b71Sopenharmony_ci
1738e41f4b71Sopenharmony_ci**返回值:**
1739e41f4b71Sopenharmony_ci
1740e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
1741e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
1742e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
1743e41f4b71Sopenharmony_ci
1744e41f4b71Sopenharmony_ci**错误码:**
1745e41f4b71Sopenharmony_ci
1746e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1747e41f4b71Sopenharmony_ci
1748e41f4b71Sopenharmony_ci**示例:**
1749e41f4b71Sopenharmony_ci
1750e41f4b71Sopenharmony_ci  ```ts
1751e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1752e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/testDir";
1753e41f4b71Sopenharmony_ci  fs.rmdir(dirPath).then(() => {
1754e41f4b71Sopenharmony_ci    console.info("rmdir succeed");
1755e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1756e41f4b71Sopenharmony_ci    console.error("rmdir failed with error message: " + err.message + ", error code: " + err.code);
1757e41f4b71Sopenharmony_ci  });
1758e41f4b71Sopenharmony_ci  ```
1759e41f4b71Sopenharmony_ci
1760e41f4b71Sopenharmony_ci## fs.rmdir
1761e41f4b71Sopenharmony_ci
1762e41f4b71Sopenharmony_cirmdir(path: string, callback: AsyncCallback&lt;void&gt;): void
1763e41f4b71Sopenharmony_ci
1764e41f4b71Sopenharmony_ci删除整个目录,使用callback异步回调。
1765e41f4b71Sopenharmony_ci
1766e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1767e41f4b71Sopenharmony_ci
1768e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1769e41f4b71Sopenharmony_ci
1770e41f4b71Sopenharmony_ci**参数:**
1771e41f4b71Sopenharmony_ci
1772e41f4b71Sopenharmony_ci| 参数名   | 类型                      | 必填 | 说明                       |
1773e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | -------------------------- |
1774e41f4b71Sopenharmony_ci| path     | string                    | 是   | 目录的应用沙箱路径。 |
1775e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | 是   | 异步删除目录之后的回调。   |
1776e41f4b71Sopenharmony_ci
1777e41f4b71Sopenharmony_ci**错误码:**
1778e41f4b71Sopenharmony_ci
1779e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1780e41f4b71Sopenharmony_ci
1781e41f4b71Sopenharmony_ci**示例:**
1782e41f4b71Sopenharmony_ci
1783e41f4b71Sopenharmony_ci  ```ts
1784e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1785e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/testDir";
1786e41f4b71Sopenharmony_ci  fs.rmdir(dirPath, (err: BusinessError) => {
1787e41f4b71Sopenharmony_ci    if (err) {
1788e41f4b71Sopenharmony_ci      console.error("rmdir failed with error message: " + err.message + ", error code: " + err.code);
1789e41f4b71Sopenharmony_ci    } else {
1790e41f4b71Sopenharmony_ci      console.info("rmdir succeed");
1791e41f4b71Sopenharmony_ci    }
1792e41f4b71Sopenharmony_ci  });
1793e41f4b71Sopenharmony_ci  ```
1794e41f4b71Sopenharmony_ci
1795e41f4b71Sopenharmony_ci## fs.rmdirSync
1796e41f4b71Sopenharmony_ci
1797e41f4b71Sopenharmony_cirmdirSync(path: string): void
1798e41f4b71Sopenharmony_ci
1799e41f4b71Sopenharmony_ci以同步方法删除目录。
1800e41f4b71Sopenharmony_ci
1801e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1802e41f4b71Sopenharmony_ci
1803e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1804e41f4b71Sopenharmony_ci
1805e41f4b71Sopenharmony_ci**参数:**
1806e41f4b71Sopenharmony_ci
1807e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                       |
1808e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- |
1809e41f4b71Sopenharmony_ci| path   | string | 是   | 目录的应用沙箱路径。 |
1810e41f4b71Sopenharmony_ci
1811e41f4b71Sopenharmony_ci**错误码:**
1812e41f4b71Sopenharmony_ci
1813e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1814e41f4b71Sopenharmony_ci
1815e41f4b71Sopenharmony_ci**示例:**
1816e41f4b71Sopenharmony_ci
1817e41f4b71Sopenharmony_ci  ```ts
1818e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/testDir";
1819e41f4b71Sopenharmony_ci  fs.rmdirSync(dirPath);
1820e41f4b71Sopenharmony_ci  ```
1821e41f4b71Sopenharmony_ci
1822e41f4b71Sopenharmony_ci## fs.unlink
1823e41f4b71Sopenharmony_ci
1824e41f4b71Sopenharmony_ciunlink(path: string): Promise&lt;void&gt;
1825e41f4b71Sopenharmony_ci
1826e41f4b71Sopenharmony_ci删除单个文件,使用Promise异步返回。
1827e41f4b71Sopenharmony_ci
1828e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1829e41f4b71Sopenharmony_ci
1830e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1831e41f4b71Sopenharmony_ci
1832e41f4b71Sopenharmony_ci**参数:**
1833e41f4b71Sopenharmony_ci
1834e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                       |
1835e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- |
1836e41f4b71Sopenharmony_ci| path   | string | 是   | 文件的应用沙箱路径。 |
1837e41f4b71Sopenharmony_ci
1838e41f4b71Sopenharmony_ci**返回值:**
1839e41f4b71Sopenharmony_ci
1840e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
1841e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
1842e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
1843e41f4b71Sopenharmony_ci
1844e41f4b71Sopenharmony_ci**错误码:**
1845e41f4b71Sopenharmony_ci
1846e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1847e41f4b71Sopenharmony_ci
1848e41f4b71Sopenharmony_ci**示例:**
1849e41f4b71Sopenharmony_ci
1850e41f4b71Sopenharmony_ci  ```ts
1851e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1852e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1853e41f4b71Sopenharmony_ci  fs.unlink(filePath).then(() => {
1854e41f4b71Sopenharmony_ci    console.info("remove file succeed");
1855e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1856e41f4b71Sopenharmony_ci    console.error("remove file failed with error message: " + err.message + ", error code: " + err.code);
1857e41f4b71Sopenharmony_ci  });
1858e41f4b71Sopenharmony_ci  ```
1859e41f4b71Sopenharmony_ci
1860e41f4b71Sopenharmony_ci## fs.unlink
1861e41f4b71Sopenharmony_ci
1862e41f4b71Sopenharmony_ciunlink(path: string, callback: AsyncCallback&lt;void&gt;): void
1863e41f4b71Sopenharmony_ci
1864e41f4b71Sopenharmony_ci删除文件,使用callback异步回调。
1865e41f4b71Sopenharmony_ci
1866e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1867e41f4b71Sopenharmony_ci
1868e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1869e41f4b71Sopenharmony_ci
1870e41f4b71Sopenharmony_ci**参数:**
1871e41f4b71Sopenharmony_ci
1872e41f4b71Sopenharmony_ci| 参数名   | 类型                      | 必填 | 说明                       |
1873e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | -------------------------- |
1874e41f4b71Sopenharmony_ci| path     | string                    | 是   | 文件的应用沙箱路径。 |
1875e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | 是   | 异步删除文件之后的回调。   |
1876e41f4b71Sopenharmony_ci
1877e41f4b71Sopenharmony_ci**错误码:**
1878e41f4b71Sopenharmony_ci
1879e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1880e41f4b71Sopenharmony_ci
1881e41f4b71Sopenharmony_ci**示例:**
1882e41f4b71Sopenharmony_ci
1883e41f4b71Sopenharmony_ci  ```ts
1884e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1885e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1886e41f4b71Sopenharmony_ci  fs.unlink(filePath, (err: BusinessError) => {
1887e41f4b71Sopenharmony_ci    if (err) {
1888e41f4b71Sopenharmony_ci      console.error("remove file failed with error message: " + err.message + ", error code: " + err.code);
1889e41f4b71Sopenharmony_ci    } else {
1890e41f4b71Sopenharmony_ci      console.info("remove file succeed");
1891e41f4b71Sopenharmony_ci    }
1892e41f4b71Sopenharmony_ci  });
1893e41f4b71Sopenharmony_ci  ```
1894e41f4b71Sopenharmony_ci
1895e41f4b71Sopenharmony_ci## fs.unlinkSync
1896e41f4b71Sopenharmony_ci
1897e41f4b71Sopenharmony_ciunlinkSync(path: string): void
1898e41f4b71Sopenharmony_ci
1899e41f4b71Sopenharmony_ci以同步方法删除文件。
1900e41f4b71Sopenharmony_ci
1901e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1902e41f4b71Sopenharmony_ci
1903e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1904e41f4b71Sopenharmony_ci
1905e41f4b71Sopenharmony_ci**参数:**
1906e41f4b71Sopenharmony_ci
1907e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                       |
1908e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- |
1909e41f4b71Sopenharmony_ci| path   | string | 是   | 文件的应用沙箱路径。 |
1910e41f4b71Sopenharmony_ci
1911e41f4b71Sopenharmony_ci**错误码:**
1912e41f4b71Sopenharmony_ci
1913e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1914e41f4b71Sopenharmony_ci
1915e41f4b71Sopenharmony_ci**示例:**
1916e41f4b71Sopenharmony_ci
1917e41f4b71Sopenharmony_ci  ```ts
1918e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1919e41f4b71Sopenharmony_ci  fs.unlinkSync(filePath);
1920e41f4b71Sopenharmony_ci  ```
1921e41f4b71Sopenharmony_ci
1922e41f4b71Sopenharmony_ci
1923e41f4b71Sopenharmony_ci## fs.write
1924e41f4b71Sopenharmony_ci
1925e41f4b71Sopenharmony_ciwrite(fd: number, buffer: ArrayBuffer | string, options?: WriteOptions): Promise&lt;number&gt;
1926e41f4b71Sopenharmony_ci
1927e41f4b71Sopenharmony_ci将数据写入文件,使用Promise异步返回。
1928e41f4b71Sopenharmony_ci
1929e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1930e41f4b71Sopenharmony_ci
1931e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1932e41f4b71Sopenharmony_ci
1933e41f4b71Sopenharmony_ci**参数:**
1934e41f4b71Sopenharmony_ci
1935e41f4b71Sopenharmony_ci  | 参数名     | 类型                              | 必填   | 说明                                       |
1936e41f4b71Sopenharmony_ci  | ------- | ------------------------------- | ---- | ---------------------------------------- |
1937e41f4b71Sopenharmony_ci  | fd      | number                          | 是    | 已打开的文件描述符。                             |
1938e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer \| string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
1939e41f4b71Sopenharmony_ci  | options | [WriteOptions](#writeoptions11)                          | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。当前仅支持&nbsp;'utf-8'。|
1940e41f4b71Sopenharmony_ci
1941e41f4b71Sopenharmony_ci**返回值:**
1942e41f4b71Sopenharmony_ci
1943e41f4b71Sopenharmony_ci  | 类型                    | 说明       |
1944e41f4b71Sopenharmony_ci  | --------------------- | -------- |
1945e41f4b71Sopenharmony_ci  | Promise&lt;number&gt; | Promise对象。返回实际写入的数据长度,单位字节。 |
1946e41f4b71Sopenharmony_ci
1947e41f4b71Sopenharmony_ci**错误码:**
1948e41f4b71Sopenharmony_ci
1949e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1950e41f4b71Sopenharmony_ci
1951e41f4b71Sopenharmony_ci**示例:**
1952e41f4b71Sopenharmony_ci
1953e41f4b71Sopenharmony_ci  ```ts
1954e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1955e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1956e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
1957e41f4b71Sopenharmony_ci  let str: string = "hello, world";
1958e41f4b71Sopenharmony_ci  fs.write(file.fd, str).then((writeLen: number) => {
1959e41f4b71Sopenharmony_ci    console.info("write data to file succeed and size is:" + writeLen);
1960e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
1961e41f4b71Sopenharmony_ci    console.error("write data to file failed with error message: " + err.message + ", error code: " + err.code);
1962e41f4b71Sopenharmony_ci  }).finally(() => {
1963e41f4b71Sopenharmony_ci    fs.closeSync(file);
1964e41f4b71Sopenharmony_ci  });
1965e41f4b71Sopenharmony_ci  ```
1966e41f4b71Sopenharmony_ci
1967e41f4b71Sopenharmony_ci## fs.write
1968e41f4b71Sopenharmony_ci
1969e41f4b71Sopenharmony_ciwrite(fd: number, buffer: ArrayBuffer | string, options?: WriteOptions, callback: AsyncCallback&lt;number&gt;): void
1970e41f4b71Sopenharmony_ci
1971e41f4b71Sopenharmony_ci将数据写入文件,使用callback异步回调。
1972e41f4b71Sopenharmony_ci
1973e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
1974e41f4b71Sopenharmony_ci
1975e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
1976e41f4b71Sopenharmony_ci
1977e41f4b71Sopenharmony_ci**参数:**
1978e41f4b71Sopenharmony_ci
1979e41f4b71Sopenharmony_ci  | 参数名      | 类型                              | 必填   | 说明                                       |
1980e41f4b71Sopenharmony_ci  | -------- | ------------------------------- | ---- | ---------------------------------------- |
1981e41f4b71Sopenharmony_ci  | fd       | number                          | 是    | 已打开的文件描述符。                             |
1982e41f4b71Sopenharmony_ci  | buffer   | ArrayBuffer \| string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
1983e41f4b71Sopenharmony_ci  | options | [WriteOptions](#writeoptions11)                          | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。当前仅支持&nbsp;'utf-8'。|
1984e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;number&gt;     | 是    | 异步将数据写入完成后执行的回调函数。                       |
1985e41f4b71Sopenharmony_ci
1986e41f4b71Sopenharmony_ci**错误码:**
1987e41f4b71Sopenharmony_ci
1988e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
1989e41f4b71Sopenharmony_ci
1990e41f4b71Sopenharmony_ci**示例:**
1991e41f4b71Sopenharmony_ci
1992e41f4b71Sopenharmony_ci  ```ts
1993e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
1994e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
1995e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
1996e41f4b71Sopenharmony_ci  let str: string = "hello, world";
1997e41f4b71Sopenharmony_ci  fs.write(file.fd, str, (err: BusinessError, writeLen: number) => {
1998e41f4b71Sopenharmony_ci    if (err) {
1999e41f4b71Sopenharmony_ci      console.error("write data to file failed with error message:" + err.message + ", error code: " + err.code);
2000e41f4b71Sopenharmony_ci    } else {
2001e41f4b71Sopenharmony_ci      console.info("write data to file succeed and size is:" + writeLen);
2002e41f4b71Sopenharmony_ci    }
2003e41f4b71Sopenharmony_ci    fs.closeSync(file);
2004e41f4b71Sopenharmony_ci  });
2005e41f4b71Sopenharmony_ci  ```
2006e41f4b71Sopenharmony_ci
2007e41f4b71Sopenharmony_ci## fs.writeSync
2008e41f4b71Sopenharmony_ci
2009e41f4b71Sopenharmony_ciwriteSync(fd: number, buffer: ArrayBuffer | string, options?: WriteOptions): number
2010e41f4b71Sopenharmony_ci
2011e41f4b71Sopenharmony_ci以同步方法将数据写入文件。
2012e41f4b71Sopenharmony_ci
2013e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2014e41f4b71Sopenharmony_ci
2015e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2016e41f4b71Sopenharmony_ci
2017e41f4b71Sopenharmony_ci**参数:**
2018e41f4b71Sopenharmony_ci
2019e41f4b71Sopenharmony_ci  | 参数名     | 类型                              | 必填   | 说明                                       |
2020e41f4b71Sopenharmony_ci  | ------- | ------------------------------- | ---- | ---------------------------------------- |
2021e41f4b71Sopenharmony_ci  | fd      | number                          | 是    | 已打开的文件描述符。                             |
2022e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer \| string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
2023e41f4b71Sopenharmony_ci  | options | [WriteOptions](#writeoptions11)                          | 否    | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。当前仅支持&nbsp;'utf-8'。|
2024e41f4b71Sopenharmony_ci
2025e41f4b71Sopenharmony_ci**返回值:**
2026e41f4b71Sopenharmony_ci
2027e41f4b71Sopenharmony_ci  | 类型     | 说明       |
2028e41f4b71Sopenharmony_ci  | ------ | -------- |
2029e41f4b71Sopenharmony_ci  | number | 返回实际写入的数据长度,单位字节。 |
2030e41f4b71Sopenharmony_ci
2031e41f4b71Sopenharmony_ci**错误码:**
2032e41f4b71Sopenharmony_ci
2033e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2034e41f4b71Sopenharmony_ci
2035e41f4b71Sopenharmony_ci**示例:**
2036e41f4b71Sopenharmony_ci
2037e41f4b71Sopenharmony_ci  ```ts
2038e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2039e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
2040e41f4b71Sopenharmony_ci  let str: string = "hello, world";
2041e41f4b71Sopenharmony_ci  let writeLen = fs.writeSync(file.fd, str);
2042e41f4b71Sopenharmony_ci  console.info("write data to file succeed and size is:" + writeLen);
2043e41f4b71Sopenharmony_ci  fs.closeSync(file);
2044e41f4b71Sopenharmony_ci  ```
2045e41f4b71Sopenharmony_ci
2046e41f4b71Sopenharmony_ci## fs.truncate
2047e41f4b71Sopenharmony_ci
2048e41f4b71Sopenharmony_citruncate(file: string | number, len?: number): Promise&lt;void&gt;
2049e41f4b71Sopenharmony_ci
2050e41f4b71Sopenharmony_ci截断文件内容,使用Promise异步返回。
2051e41f4b71Sopenharmony_ci
2052e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2053e41f4b71Sopenharmony_ci
2054e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2055e41f4b71Sopenharmony_ci
2056e41f4b71Sopenharmony_ci**参数:**
2057e41f4b71Sopenharmony_ci
2058e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                             |
2059e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------- |
2060e41f4b71Sopenharmony_ci| file   | string \| number | 是   | 文件的应用沙箱路径或已打开的文件描述符fd。       |
2061e41f4b71Sopenharmony_ci| len    | number | 否   | 文件截断后的长度,以字节为单位。默认为0。 |
2062e41f4b71Sopenharmony_ci
2063e41f4b71Sopenharmony_ci**返回值:**
2064e41f4b71Sopenharmony_ci
2065e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
2066e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
2067e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
2068e41f4b71Sopenharmony_ci
2069e41f4b71Sopenharmony_ci**错误码:**
2070e41f4b71Sopenharmony_ci
2071e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2072e41f4b71Sopenharmony_ci
2073e41f4b71Sopenharmony_ci**示例:**
2074e41f4b71Sopenharmony_ci
2075e41f4b71Sopenharmony_ci  ```ts
2076e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2077e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2078e41f4b71Sopenharmony_ci  let len: number = 5;
2079e41f4b71Sopenharmony_ci  fs.truncate(filePath, len).then(() => {
2080e41f4b71Sopenharmony_ci    console.info("truncate file succeed");
2081e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2082e41f4b71Sopenharmony_ci    console.error("truncate file failed with error message: " + err.message + ", error code: " + err.code);
2083e41f4b71Sopenharmony_ci  });
2084e41f4b71Sopenharmony_ci  ```
2085e41f4b71Sopenharmony_ci
2086e41f4b71Sopenharmony_ci## fs.truncate
2087e41f4b71Sopenharmony_ci
2088e41f4b71Sopenharmony_citruncate(file: string | number, len?: number, callback: AsyncCallback&lt;void&gt;): void
2089e41f4b71Sopenharmony_ci
2090e41f4b71Sopenharmony_ci截断文件内容,使用callback异步回调。
2091e41f4b71Sopenharmony_ci
2092e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2093e41f4b71Sopenharmony_ci
2094e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2095e41f4b71Sopenharmony_ci
2096e41f4b71Sopenharmony_ci**参数:**
2097e41f4b71Sopenharmony_ci
2098e41f4b71Sopenharmony_ci| 参数名   | 类型                      | 必填 | 说明                             |
2099e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | -------------------------------- |
2100e41f4b71Sopenharmony_ci| file     | string \| number                    | 是   | 文件的应用沙箱路径或已打开的文件描述符fd。       |
2101e41f4b71Sopenharmony_ci| len      | number                    | 否   | 文件截断后的长度,以字节为单位。默认为0。 |
2102e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数,本调用无返回值。   |
2103e41f4b71Sopenharmony_ci
2104e41f4b71Sopenharmony_ci**错误码:**
2105e41f4b71Sopenharmony_ci
2106e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2107e41f4b71Sopenharmony_ci
2108e41f4b71Sopenharmony_ci**示例:**
2109e41f4b71Sopenharmony_ci
2110e41f4b71Sopenharmony_ci  ```ts
2111e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2112e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2113e41f4b71Sopenharmony_ci  let len: number = 5;
2114e41f4b71Sopenharmony_ci  fs.truncate(filePath, len, (err: BusinessError) => {
2115e41f4b71Sopenharmony_ci    if (err) {
2116e41f4b71Sopenharmony_ci      console.error("truncate failed with error message: " + err.message + ", error code: " + err.code);
2117e41f4b71Sopenharmony_ci    } else {
2118e41f4b71Sopenharmony_ci      console.info("truncate succeed");
2119e41f4b71Sopenharmony_ci    }
2120e41f4b71Sopenharmony_ci  });
2121e41f4b71Sopenharmony_ci  ```
2122e41f4b71Sopenharmony_ci
2123e41f4b71Sopenharmony_ci## fs.truncateSync
2124e41f4b71Sopenharmony_ci
2125e41f4b71Sopenharmony_citruncateSync(file: string | number, len?: number): void
2126e41f4b71Sopenharmony_ci
2127e41f4b71Sopenharmony_ci以同步方法截断文件内容。
2128e41f4b71Sopenharmony_ci
2129e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2130e41f4b71Sopenharmony_ci
2131e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2132e41f4b71Sopenharmony_ci
2133e41f4b71Sopenharmony_ci**参数:**
2134e41f4b71Sopenharmony_ci
2135e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                             |
2136e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------- |
2137e41f4b71Sopenharmony_ci| file   | string \| number | 是   | 文件的应用沙箱路径或已打开的文件描述符fd。       |
2138e41f4b71Sopenharmony_ci| len    | number | 否   | 文件截断后的长度,以字节为单位。默认为0。 |
2139e41f4b71Sopenharmony_ci
2140e41f4b71Sopenharmony_ci**错误码:**
2141e41f4b71Sopenharmony_ci
2142e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2143e41f4b71Sopenharmony_ci
2144e41f4b71Sopenharmony_ci**示例:**
2145e41f4b71Sopenharmony_ci
2146e41f4b71Sopenharmony_ci  ```ts
2147e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2148e41f4b71Sopenharmony_ci  let len: number = 5;
2149e41f4b71Sopenharmony_ci  fs.truncateSync(filePath, len);
2150e41f4b71Sopenharmony_ci  ```
2151e41f4b71Sopenharmony_ci
2152e41f4b71Sopenharmony_ci## fs.readLines<sup>11+</sup>
2153e41f4b71Sopenharmony_ci
2154e41f4b71Sopenharmony_cireadLines(filePath: string, options?: Options): Promise&lt;ReaderIterator&gt;
2155e41f4b71Sopenharmony_ci
2156e41f4b71Sopenharmony_ci逐行读取文件文本内容,使用Promise异步返回,只支持读取utf-8格式文件。
2157e41f4b71Sopenharmony_ci
2158e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2159e41f4b71Sopenharmony_ci
2160e41f4b71Sopenharmony_ci**参数:**
2161e41f4b71Sopenharmony_ci
2162e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明                                                         |
2163e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ |
2164e41f4b71Sopenharmony_ci| filePath | string | 是   | 文件的应用沙箱路径。                                   |
2165e41f4b71Sopenharmony_ci| options | [Options](#options11) | 否   | 可选项。支持以下选项:<br/>-&nbsp;encoding,string类型,当数据是&nbsp;string&nbsp;类型时有效,表示数据的编码方式,默认&nbsp;'utf-8',仅支持&nbsp;'utf-8'。|
2166e41f4b71Sopenharmony_ci
2167e41f4b71Sopenharmony_ci**返回值:**
2168e41f4b71Sopenharmony_ci
2169e41f4b71Sopenharmony_ci  | 类型                    | 说明         |
2170e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
2171e41f4b71Sopenharmony_ci  | Promise&lt;[ReaderIterator](#readeriterator11)&gt; | Promise对象。返回文件读取迭代器。 |
2172e41f4b71Sopenharmony_ci
2173e41f4b71Sopenharmony_ci**错误码:**
2174e41f4b71Sopenharmony_ci
2175e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2176e41f4b71Sopenharmony_ci
2177e41f4b71Sopenharmony_ci**示例:**
2178e41f4b71Sopenharmony_ci
2179e41f4b71Sopenharmony_ci  ```ts
2180e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2181e41f4b71Sopenharmony_ci  import { fileIo as fs, Options } from '@kit.CoreFileKit';
2182e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2183e41f4b71Sopenharmony_ci  let options: Options = {
2184e41f4b71Sopenharmony_ci    encoding: 'utf-8'
2185e41f4b71Sopenharmony_ci  };
2186e41f4b71Sopenharmony_ci  fs.readLines(filePath, options).then((readerIterator: fs.ReaderIterator) => {
2187e41f4b71Sopenharmony_ci    for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) {
2188e41f4b71Sopenharmony_ci      console.info("content: " + it.value);
2189e41f4b71Sopenharmony_ci    }
2190e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2191e41f4b71Sopenharmony_ci    console.error("readLines failed with error message: " + err.message + ", error code: " + err.code);
2192e41f4b71Sopenharmony_ci  });
2193e41f4b71Sopenharmony_ci  ```
2194e41f4b71Sopenharmony_ci
2195e41f4b71Sopenharmony_ci## fs.readLines<sup>11+</sup>
2196e41f4b71Sopenharmony_ci
2197e41f4b71Sopenharmony_cireadLines(filePath: string, options?: Options, callback: AsyncCallback&lt;ReaderIterator&gt;): void
2198e41f4b71Sopenharmony_ci
2199e41f4b71Sopenharmony_ci逐行读取文件文本内容,使用callback异步回调,只支持读取utf-8格式文件。
2200e41f4b71Sopenharmony_ci
2201e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2202e41f4b71Sopenharmony_ci
2203e41f4b71Sopenharmony_ci**参数:**
2204e41f4b71Sopenharmony_ci
2205e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明                                                         |
2206e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ |
2207e41f4b71Sopenharmony_ci| filePath | string | 是   | 文件的应用沙箱路径。                                   |
2208e41f4b71Sopenharmony_ci| options | [Options](#options11) | 否   | 可选项。支持以下选项:<br/>-&nbsp;encoding,string类型,当数据是&nbsp;string&nbsp;类型时有效,表示数据的编码方式,默认&nbsp;'utf-8',仅支持&nbsp;'utf-8'。|
2209e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;[ReaderIterator](#readeriterator11)&gt; | 是   | 逐行读取文件文本内容回调。                                   |
2210e41f4b71Sopenharmony_ci
2211e41f4b71Sopenharmony_ci**错误码:**
2212e41f4b71Sopenharmony_ci
2213e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2214e41f4b71Sopenharmony_ci
2215e41f4b71Sopenharmony_ci**示例:**
2216e41f4b71Sopenharmony_ci
2217e41f4b71Sopenharmony_ci  ```ts
2218e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2219e41f4b71Sopenharmony_ci  import { fileIo as fs, Options } from '@kit.CoreFileKit';
2220e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2221e41f4b71Sopenharmony_ci  let options: Options = {
2222e41f4b71Sopenharmony_ci    encoding: 'utf-8'
2223e41f4b71Sopenharmony_ci  };
2224e41f4b71Sopenharmony_ci  fs.readLines(filePath, options, (err: BusinessError, readerIterator: fs.ReaderIterator) => {
2225e41f4b71Sopenharmony_ci    if (err) {
2226e41f4b71Sopenharmony_ci      console.error("readLines failed with error message: " + err.message + ", error code: " + err.code);
2227e41f4b71Sopenharmony_ci    } else {
2228e41f4b71Sopenharmony_ci      for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) {
2229e41f4b71Sopenharmony_ci        console.info("content: " + it.value);
2230e41f4b71Sopenharmony_ci      }
2231e41f4b71Sopenharmony_ci    }
2232e41f4b71Sopenharmony_ci  });
2233e41f4b71Sopenharmony_ci  ```
2234e41f4b71Sopenharmony_ci
2235e41f4b71Sopenharmony_ci## fs.readLinesSync<sup>11+</sup>
2236e41f4b71Sopenharmony_ci
2237e41f4b71Sopenharmony_cireadLinesSync(filePath: string, options?: Options): ReaderIterator
2238e41f4b71Sopenharmony_ci
2239e41f4b71Sopenharmony_ci以同步方式逐行读取文件文本内容。
2240e41f4b71Sopenharmony_ci
2241e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2242e41f4b71Sopenharmony_ci
2243e41f4b71Sopenharmony_ci**参数:**
2244e41f4b71Sopenharmony_ci
2245e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明                                                         |
2246e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ |
2247e41f4b71Sopenharmony_ci| filePath | string | 是   | 文件的应用沙箱路径。                                   |
2248e41f4b71Sopenharmony_ci| options | [Options](#options11) | 否   | 可选项。支持以下选项:<br/>-&nbsp;encoding,string类型,当数据是&nbsp;string&nbsp;类型时有效,表示数据的编码方式,默认&nbsp;'utf-8',仅支持&nbsp;'utf-8'。|
2249e41f4b71Sopenharmony_ci
2250e41f4b71Sopenharmony_ci**返回值:**
2251e41f4b71Sopenharmony_ci
2252e41f4b71Sopenharmony_ci  | 类型                    | 说明         |
2253e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
2254e41f4b71Sopenharmony_ci  | [ReaderIterator](#readeriterator11) | 返回文件读取迭代器。 |
2255e41f4b71Sopenharmony_ci
2256e41f4b71Sopenharmony_ci**错误码:**
2257e41f4b71Sopenharmony_ci
2258e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2259e41f4b71Sopenharmony_ci
2260e41f4b71Sopenharmony_ci**示例:**
2261e41f4b71Sopenharmony_ci
2262e41f4b71Sopenharmony_ci  ```ts
2263e41f4b71Sopenharmony_ci  import { fileIo as fs, Options } from '@kit.CoreFileKit';
2264e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2265e41f4b71Sopenharmony_ci  let options: Options = {
2266e41f4b71Sopenharmony_ci    encoding: 'utf-8'
2267e41f4b71Sopenharmony_ci  };
2268e41f4b71Sopenharmony_ci  let readerIterator = fs.readLinesSync(filePath, options);
2269e41f4b71Sopenharmony_ci  for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) {
2270e41f4b71Sopenharmony_ci    console.info("content: " + it.value);
2271e41f4b71Sopenharmony_ci  }
2272e41f4b71Sopenharmony_ci  ```
2273e41f4b71Sopenharmony_ci
2274e41f4b71Sopenharmony_ci## ReaderIterator<sup>11+</sup>
2275e41f4b71Sopenharmony_ci
2276e41f4b71Sopenharmony_ci文件读取迭代器。在调用ReaderIterator的方法前,需要先通过readLines方法(同步或异步)来构建一个ReaderIterator实例。
2277e41f4b71Sopenharmony_ci
2278e41f4b71Sopenharmony_ci### next<sup>11+</sup>
2279e41f4b71Sopenharmony_ci
2280e41f4b71Sopenharmony_cinext(): ReaderIteratorResult
2281e41f4b71Sopenharmony_ci
2282e41f4b71Sopenharmony_ci获取迭代器下一项内容。
2283e41f4b71Sopenharmony_ci
2284e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2285e41f4b71Sopenharmony_ci
2286e41f4b71Sopenharmony_ci**返回值:**
2287e41f4b71Sopenharmony_ci
2288e41f4b71Sopenharmony_ci  | 类型                    | 说明         |
2289e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
2290e41f4b71Sopenharmony_ci  | [ReaderIteratorResult](#readeriteratorresult) | 文件读取迭代器返回结果。 |
2291e41f4b71Sopenharmony_ci
2292e41f4b71Sopenharmony_ci**错误码:**
2293e41f4b71Sopenharmony_ci
2294e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2295e41f4b71Sopenharmony_ci
2296e41f4b71Sopenharmony_ci> **说明**:
2297e41f4b71Sopenharmony_ci>
2298e41f4b71Sopenharmony_ci> 如果ReaderIterator读取的当前行的编码方式不是 'utf-8',接口返回错误码13900037。
2299e41f4b71Sopenharmony_ci
2300e41f4b71Sopenharmony_ci**示例:**
2301e41f4b71Sopenharmony_ci
2302e41f4b71Sopenharmony_ci  ```ts
2303e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2304e41f4b71Sopenharmony_ci  import { fileIo as fs, Options } from '@kit.CoreFileKit';
2305e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2306e41f4b71Sopenharmony_ci  let options: Options = {
2307e41f4b71Sopenharmony_ci    encoding: 'utf-8'
2308e41f4b71Sopenharmony_ci  };
2309e41f4b71Sopenharmony_ci  fs.readLines(filePath, options).then((readerIterator: fs.ReaderIterator) => {
2310e41f4b71Sopenharmony_ci    for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) {
2311e41f4b71Sopenharmony_ci      console.info("content: " + it.value);
2312e41f4b71Sopenharmony_ci    }
2313e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2314e41f4b71Sopenharmony_ci    console.error("readLines failed with error message: " + err.message + ", error code: " + err.code);
2315e41f4b71Sopenharmony_ci  });
2316e41f4b71Sopenharmony_ci  ```
2317e41f4b71Sopenharmony_ci
2318e41f4b71Sopenharmony_ci## ReaderIteratorResult
2319e41f4b71Sopenharmony_ci
2320e41f4b71Sopenharmony_ci文件读取迭代器返回结果,支持ReaderIterator接口使用。
2321e41f4b71Sopenharmony_ci
2322e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2323e41f4b71Sopenharmony_ci
2324e41f4b71Sopenharmony_ci| 名称        | 类型       | 说明                |
2325e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ |
2326e41f4b71Sopenharmony_ci| done | boolean     |  迭代器是否已完成迭代。          |
2327e41f4b71Sopenharmony_ci| value    | string     | 逐行读取的文件文本内容。 |
2328e41f4b71Sopenharmony_ci
2329e41f4b71Sopenharmony_ci## fs.readText
2330e41f4b71Sopenharmony_ci
2331e41f4b71Sopenharmony_cireadText(filePath: string, options?: ReadTextOptions): Promise&lt;string&gt;
2332e41f4b71Sopenharmony_ci
2333e41f4b71Sopenharmony_ci基于文本方式读取文件(即直接读取文件的文本内容),使用Promise异步返回。
2334e41f4b71Sopenharmony_ci
2335e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2336e41f4b71Sopenharmony_ci
2337e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2338e41f4b71Sopenharmony_ci
2339e41f4b71Sopenharmony_ci**参数:**
2340e41f4b71Sopenharmony_ci
2341e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明                                                         |
2342e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ |
2343e41f4b71Sopenharmony_ci| filePath | string | 是   | 文件的应用沙箱路径。                                   |
2344e41f4b71Sopenharmony_ci| options  | [ReadTextOptions](#readtextoptions11) | 否   | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读取。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认文件长度。<br/>-&nbsp;encoding,string类型,当数据是&nbsp;string&nbsp;类型时有效,表示数据的编码方式,默认&nbsp;'utf-8',仅支持&nbsp;'utf-8'。 |
2345e41f4b71Sopenharmony_ci
2346e41f4b71Sopenharmony_ci**返回值:**
2347e41f4b71Sopenharmony_ci
2348e41f4b71Sopenharmony_ci  | 类型                    | 说明         |
2349e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
2350e41f4b71Sopenharmony_ci  | Promise&lt;string&gt; | Promise对象。返回读取文件的内容。 |
2351e41f4b71Sopenharmony_ci
2352e41f4b71Sopenharmony_ci**错误码:**
2353e41f4b71Sopenharmony_ci
2354e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2355e41f4b71Sopenharmony_ci
2356e41f4b71Sopenharmony_ci**示例:**
2357e41f4b71Sopenharmony_ci
2358e41f4b71Sopenharmony_ci  ```ts
2359e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2360e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2361e41f4b71Sopenharmony_ci  fs.readText(filePath).then((str: string) => {
2362e41f4b71Sopenharmony_ci    console.info("readText succeed:" + str);
2363e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2364e41f4b71Sopenharmony_ci    console.error("readText failed with error message: " + err.message + ", error code: " + err.code);
2365e41f4b71Sopenharmony_ci  });
2366e41f4b71Sopenharmony_ci  ```
2367e41f4b71Sopenharmony_ci
2368e41f4b71Sopenharmony_ci## fs.readText
2369e41f4b71Sopenharmony_ci
2370e41f4b71Sopenharmony_cireadText(filePath: string, options?: ReadTextOptions, callback: AsyncCallback&lt;string&gt;): void
2371e41f4b71Sopenharmony_ci
2372e41f4b71Sopenharmony_ci基于文本方式读取文件(即直接读取文件的文本内容),使用callback异步回调。
2373e41f4b71Sopenharmony_ci
2374e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2375e41f4b71Sopenharmony_ci
2376e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2377e41f4b71Sopenharmony_ci
2378e41f4b71Sopenharmony_ci**参数:**
2379e41f4b71Sopenharmony_ci
2380e41f4b71Sopenharmony_ci| 参数名   | 类型                        | 必填 | 说明                                                         |
2381e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
2382e41f4b71Sopenharmony_ci| filePath | string                      | 是   | 文件的应用沙箱路径。                                   |
2383e41f4b71Sopenharmony_ci| options  | [ReadTextOptions](#readtextoptions11)                      | 否   | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读取。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认文件长度。<br/>-&nbsp;encoding,string类型,表示数据的编码方式,默认&nbsp;'utf-8',仅支持&nbsp;'utf-8'。 |
2384e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;string&gt; | 是   | 回调函数,返回读取文件的内容。                         |
2385e41f4b71Sopenharmony_ci
2386e41f4b71Sopenharmony_ci**错误码:**
2387e41f4b71Sopenharmony_ci
2388e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2389e41f4b71Sopenharmony_ci
2390e41f4b71Sopenharmony_ci**示例:**
2391e41f4b71Sopenharmony_ci
2392e41f4b71Sopenharmony_ci  ```ts
2393e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2394e41f4b71Sopenharmony_ci  import { fileIo as fs, ReadTextOptions } from '@kit.CoreFileKit';
2395e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2396e41f4b71Sopenharmony_ci  let readTextOption: ReadTextOptions = {
2397e41f4b71Sopenharmony_ci      offset: 1,
2398e41f4b71Sopenharmony_ci      length: 0,
2399e41f4b71Sopenharmony_ci      encoding: 'utf-8'
2400e41f4b71Sopenharmony_ci  };
2401e41f4b71Sopenharmony_ci  let stat = fs.statSync(filePath);
2402e41f4b71Sopenharmony_ci  readTextOption.length = stat.size;
2403e41f4b71Sopenharmony_ci  fs.readText(filePath, readTextOption, (err: BusinessError, str: string) => {
2404e41f4b71Sopenharmony_ci    if (err) {
2405e41f4b71Sopenharmony_ci      console.error("readText failed with error message: " + err.message + ", error code: " + err.code);
2406e41f4b71Sopenharmony_ci    } else {
2407e41f4b71Sopenharmony_ci      console.info("readText succeed:" + str);
2408e41f4b71Sopenharmony_ci    }
2409e41f4b71Sopenharmony_ci  });
2410e41f4b71Sopenharmony_ci  ```
2411e41f4b71Sopenharmony_ci
2412e41f4b71Sopenharmony_ci## fs.readTextSync
2413e41f4b71Sopenharmony_ci
2414e41f4b71Sopenharmony_cireadTextSync(filePath: string, options?: ReadTextOptions): string
2415e41f4b71Sopenharmony_ci
2416e41f4b71Sopenharmony_ci以同步方法基于文本方式读取文件(即直接读取文件的文本内容)。
2417e41f4b71Sopenharmony_ci
2418e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2419e41f4b71Sopenharmony_ci
2420e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2421e41f4b71Sopenharmony_ci
2422e41f4b71Sopenharmony_ci**参数:**
2423e41f4b71Sopenharmony_ci
2424e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明                                                         |
2425e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ |
2426e41f4b71Sopenharmony_ci| filePath | string | 是   | 文件的应用沙箱路径。                                   |
2427e41f4b71Sopenharmony_ci| options  | [ReadTextOptions](#readtextoptions11) | 否   | 支持如下选项:<br/>-&nbsp;offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读取。<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认文件长度。<br/>-&nbsp;encoding,string类型,当数据是&nbsp;string&nbsp;类型时有效,表示数据的编码方式,默认&nbsp;'utf-8',仅支持&nbsp;'utf-8'。 |
2428e41f4b71Sopenharmony_ci
2429e41f4b71Sopenharmony_ci**返回值:**
2430e41f4b71Sopenharmony_ci
2431e41f4b71Sopenharmony_ci  | 类型   | 说明                 |
2432e41f4b71Sopenharmony_ci  | ------ | -------------------- |
2433e41f4b71Sopenharmony_ci  | string | 返回读取文件的内容。 |
2434e41f4b71Sopenharmony_ci
2435e41f4b71Sopenharmony_ci**错误码:**
2436e41f4b71Sopenharmony_ci
2437e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2438e41f4b71Sopenharmony_ci
2439e41f4b71Sopenharmony_ci**示例:**
2440e41f4b71Sopenharmony_ci
2441e41f4b71Sopenharmony_ci  ```ts
2442e41f4b71Sopenharmony_ci  import { fileIo as fs, ReadTextOptions } from '@kit.CoreFileKit';
2443e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2444e41f4b71Sopenharmony_ci  let readTextOptions: ReadTextOptions = {
2445e41f4b71Sopenharmony_ci    offset: 1,
2446e41f4b71Sopenharmony_ci    length: 0,
2447e41f4b71Sopenharmony_ci    encoding: 'utf-8'
2448e41f4b71Sopenharmony_ci  };
2449e41f4b71Sopenharmony_ci  let stat = fs.statSync(filePath);
2450e41f4b71Sopenharmony_ci  readTextOptions.length = stat.size;
2451e41f4b71Sopenharmony_ci  let str = fs.readTextSync(filePath, readTextOptions);
2452e41f4b71Sopenharmony_ci  console.info("readText succeed:" + str);
2453e41f4b71Sopenharmony_ci  ```
2454e41f4b71Sopenharmony_ci
2455e41f4b71Sopenharmony_ci## fs.lstat
2456e41f4b71Sopenharmony_ci
2457e41f4b71Sopenharmony_cilstat(path: string): Promise&lt;Stat&gt;
2458e41f4b71Sopenharmony_ci
2459e41f4b71Sopenharmony_ci获取链接文件信息,使用Promise异步返回。
2460e41f4b71Sopenharmony_ci
2461e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2462e41f4b71Sopenharmony_ci
2463e41f4b71Sopenharmony_ci**参数:**
2464e41f4b71Sopenharmony_ci
2465e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                   |
2466e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------- |
2467e41f4b71Sopenharmony_ci| path   | string | 是   | 文件的应用沙箱路径。 |
2468e41f4b71Sopenharmony_ci
2469e41f4b71Sopenharmony_ci**返回值:**
2470e41f4b71Sopenharmony_ci
2471e41f4b71Sopenharmony_ci  | 类型                           | 说明         |
2472e41f4b71Sopenharmony_ci  | ---------------------------- | ---------- |
2473e41f4b71Sopenharmony_ci  | Promise&lt;[Stat](#stat)&gt; | Promise对象,返回文件对象,表示文件的具体信息,详情见stat。 |
2474e41f4b71Sopenharmony_ci
2475e41f4b71Sopenharmony_ci**错误码:**
2476e41f4b71Sopenharmony_ci
2477e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2478e41f4b71Sopenharmony_ci
2479e41f4b71Sopenharmony_ci**示例:**
2480e41f4b71Sopenharmony_ci
2481e41f4b71Sopenharmony_ci  ```ts
2482e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2483e41f4b71Sopenharmony_ci  let filePath = pathDir + "/linkToFile";
2484e41f4b71Sopenharmony_ci  fs.lstat(filePath).then((stat: fs.Stat) => {
2485e41f4b71Sopenharmony_ci    console.info("lstat succeed, the size of file is " + stat.size);
2486e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2487e41f4b71Sopenharmony_ci    console.error("lstat failed with error message: " + err.message + ", error code: " + err.code);
2488e41f4b71Sopenharmony_ci  });
2489e41f4b71Sopenharmony_ci  ```
2490e41f4b71Sopenharmony_ci
2491e41f4b71Sopenharmony_ci## fs.lstat
2492e41f4b71Sopenharmony_ci
2493e41f4b71Sopenharmony_cilstat(path: string, callback: AsyncCallback&lt;Stat&gt;): void
2494e41f4b71Sopenharmony_ci
2495e41f4b71Sopenharmony_ci获取链接文件信息,使用callback异步回调。
2496e41f4b71Sopenharmony_ci
2497e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2498e41f4b71Sopenharmony_ci
2499e41f4b71Sopenharmony_ci**参数:**
2500e41f4b71Sopenharmony_ci
2501e41f4b71Sopenharmony_ci| 参数名   | 类型                               | 必填 | 说明                                   |
2502e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | ---- | -------------------------------------- |
2503e41f4b71Sopenharmony_ci| path     | string                             | 是   | 文件的应用沙箱路径。 |
2504e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;[Stat](#stat)&gt; | 是   | 回调函数,返回文件的具体信息。       |
2505e41f4b71Sopenharmony_ci
2506e41f4b71Sopenharmony_ci**错误码:**
2507e41f4b71Sopenharmony_ci
2508e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2509e41f4b71Sopenharmony_ci
2510e41f4b71Sopenharmony_ci**示例:**
2511e41f4b71Sopenharmony_ci
2512e41f4b71Sopenharmony_ci  ```ts
2513e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2514e41f4b71Sopenharmony_ci  let filePath = pathDir + "/linkToFile";
2515e41f4b71Sopenharmony_ci  fs.lstat(filePath, (err: BusinessError, stat: fs.Stat) => {
2516e41f4b71Sopenharmony_ci    if (err) {
2517e41f4b71Sopenharmony_ci      console.error("lstat failed with error message: " + err.message + ", error code: " + err.code);
2518e41f4b71Sopenharmony_ci    } else {
2519e41f4b71Sopenharmony_ci      console.info("lstat succeed, the size of file is" + stat.size);
2520e41f4b71Sopenharmony_ci    }
2521e41f4b71Sopenharmony_ci  });
2522e41f4b71Sopenharmony_ci  ```
2523e41f4b71Sopenharmony_ci
2524e41f4b71Sopenharmony_ci## fs.lstatSync
2525e41f4b71Sopenharmony_ci
2526e41f4b71Sopenharmony_cilstatSync(path: string): Stat
2527e41f4b71Sopenharmony_ci
2528e41f4b71Sopenharmony_ci以同步方法获取链接文件信息。
2529e41f4b71Sopenharmony_ci
2530e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2531e41f4b71Sopenharmony_ci
2532e41f4b71Sopenharmony_ci**参数:**
2533e41f4b71Sopenharmony_ci
2534e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                   |
2535e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------- |
2536e41f4b71Sopenharmony_ci| path   | string | 是   | 文件的应用沙箱路径。 |
2537e41f4b71Sopenharmony_ci
2538e41f4b71Sopenharmony_ci**返回值:**
2539e41f4b71Sopenharmony_ci
2540e41f4b71Sopenharmony_ci  | 类型            | 说明         |
2541e41f4b71Sopenharmony_ci  | ------------- | ---------- |
2542e41f4b71Sopenharmony_ci  | [Stat](#stat) | 表示文件的具体信息。 |
2543e41f4b71Sopenharmony_ci
2544e41f4b71Sopenharmony_ci**错误码:**
2545e41f4b71Sopenharmony_ci
2546e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2547e41f4b71Sopenharmony_ci
2548e41f4b71Sopenharmony_ci**示例:**
2549e41f4b71Sopenharmony_ci
2550e41f4b71Sopenharmony_ci  ```ts
2551e41f4b71Sopenharmony_ci  let filePath = pathDir + "/linkToFile";
2552e41f4b71Sopenharmony_ci  let fileStat = fs.lstatSync(filePath);
2553e41f4b71Sopenharmony_ci  console.info("lstat succeed, the size of file is" + fileStat.size);
2554e41f4b71Sopenharmony_ci  ```
2555e41f4b71Sopenharmony_ci
2556e41f4b71Sopenharmony_ci## fs.rename
2557e41f4b71Sopenharmony_ci
2558e41f4b71Sopenharmony_cirename(oldPath: string, newPath: string): Promise&lt;void&gt;
2559e41f4b71Sopenharmony_ci
2560e41f4b71Sopenharmony_ci重命名文件或文件夹,使用Promise异步返回。
2561e41f4b71Sopenharmony_ci
2562e41f4b71Sopenharmony_ci> **说明:**
2563e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
2564e41f4b71Sopenharmony_ci
2565e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2566e41f4b71Sopenharmony_ci
2567e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2568e41f4b71Sopenharmony_ci
2569e41f4b71Sopenharmony_ci**参数:**
2570e41f4b71Sopenharmony_ci
2571e41f4b71Sopenharmony_ci| 参数名  | 类型   | 必填 | 说明                         |
2572e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------- |
2573e41f4b71Sopenharmony_ci| oldPath | string | 是   | 文件的应用沙箱原路径。 |
2574e41f4b71Sopenharmony_ci| newPath | string | 是   | 文件的应用沙箱新路径。   |
2575e41f4b71Sopenharmony_ci
2576e41f4b71Sopenharmony_ci**返回值:**
2577e41f4b71Sopenharmony_ci
2578e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
2579e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
2580e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
2581e41f4b71Sopenharmony_ci
2582e41f4b71Sopenharmony_ci**错误码:**
2583e41f4b71Sopenharmony_ci
2584e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2585e41f4b71Sopenharmony_ci
2586e41f4b71Sopenharmony_ci**示例:**
2587e41f4b71Sopenharmony_ci
2588e41f4b71Sopenharmony_ci  ```ts
2589e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2590e41f4b71Sopenharmony_ci  let srcFile = pathDir + "/test.txt";
2591e41f4b71Sopenharmony_ci  let dstFile = pathDir + "/new.txt";
2592e41f4b71Sopenharmony_ci  fs.rename(srcFile, dstFile).then(() => {
2593e41f4b71Sopenharmony_ci    console.info("rename succeed");
2594e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2595e41f4b71Sopenharmony_ci    console.error("rename failed with error message: " + err.message + ", error code: " + err.code);
2596e41f4b71Sopenharmony_ci  });
2597e41f4b71Sopenharmony_ci  ```
2598e41f4b71Sopenharmony_ci
2599e41f4b71Sopenharmony_ci## fs.rename
2600e41f4b71Sopenharmony_ci
2601e41f4b71Sopenharmony_cirename(oldPath: string, newPath: string, callback: AsyncCallback&lt;void&gt;): void
2602e41f4b71Sopenharmony_ci
2603e41f4b71Sopenharmony_ci重命名文件或文件夹,使用callback异步回调。
2604e41f4b71Sopenharmony_ci
2605e41f4b71Sopenharmony_ci> **说明:**
2606e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
2607e41f4b71Sopenharmony_ci
2608e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2609e41f4b71Sopenharmony_ci
2610e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2611e41f4b71Sopenharmony_ci
2612e41f4b71Sopenharmony_ci**参数:**
2613e41f4b71Sopenharmony_ci
2614e41f4b71Sopenharmony_ci| 参数名   | 类型                      | 必填 | 说明                         |
2615e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------------- |
2616e41f4b71Sopenharmony_ci| oldPath | string | 是   | 文件的应用沙箱原路径。 |
2617e41f4b71Sopenharmony_ci| newPath | string | 是   | 文件的应用沙箱新路径。   |
2618e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | 是   | 异步重命名文件之后的回调。   |
2619e41f4b71Sopenharmony_ci
2620e41f4b71Sopenharmony_ci**错误码:**
2621e41f4b71Sopenharmony_ci
2622e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2623e41f4b71Sopenharmony_ci
2624e41f4b71Sopenharmony_ci**示例:**
2625e41f4b71Sopenharmony_ci
2626e41f4b71Sopenharmony_ci  ```ts
2627e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2628e41f4b71Sopenharmony_ci  let srcFile = pathDir + "/test.txt";
2629e41f4b71Sopenharmony_ci  let dstFile = pathDir + "/new.txt";
2630e41f4b71Sopenharmony_ci  fs.rename(srcFile, dstFile, (err: BusinessError) => {
2631e41f4b71Sopenharmony_ci    if (err) {
2632e41f4b71Sopenharmony_ci      console.error("rename failed with error message: " + err.message + ", error code: " + err.code);
2633e41f4b71Sopenharmony_ci    } else {
2634e41f4b71Sopenharmony_ci      console.info("rename succeed");
2635e41f4b71Sopenharmony_ci    }
2636e41f4b71Sopenharmony_ci  });
2637e41f4b71Sopenharmony_ci  ```
2638e41f4b71Sopenharmony_ci
2639e41f4b71Sopenharmony_ci## fs.renameSync
2640e41f4b71Sopenharmony_ci
2641e41f4b71Sopenharmony_cirenameSync(oldPath: string, newPath: string): void
2642e41f4b71Sopenharmony_ci
2643e41f4b71Sopenharmony_ci以同步方法重命名文件或文件夹。
2644e41f4b71Sopenharmony_ci
2645e41f4b71Sopenharmony_ci> **说明:**
2646e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
2647e41f4b71Sopenharmony_ci
2648e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2649e41f4b71Sopenharmony_ci
2650e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2651e41f4b71Sopenharmony_ci
2652e41f4b71Sopenharmony_ci**参数:**
2653e41f4b71Sopenharmony_ci
2654e41f4b71Sopenharmony_ci| 参数名  | 类型   | 必填 | 说明                         |
2655e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------- |
2656e41f4b71Sopenharmony_ci| oldPath | string | 是   | 文件的应用沙箱原路径。 |
2657e41f4b71Sopenharmony_ci| newPath | string | 是   | 文件的应用沙箱新路径。   |
2658e41f4b71Sopenharmony_ci
2659e41f4b71Sopenharmony_ci**错误码:**
2660e41f4b71Sopenharmony_ci
2661e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2662e41f4b71Sopenharmony_ci
2663e41f4b71Sopenharmony_ci**示例:**
2664e41f4b71Sopenharmony_ci
2665e41f4b71Sopenharmony_ci  ```ts
2666e41f4b71Sopenharmony_ci  let srcFile = pathDir + "/test.txt";
2667e41f4b71Sopenharmony_ci  let dstFile = pathDir + "/new.txt";
2668e41f4b71Sopenharmony_ci  fs.renameSync(srcFile, dstFile);
2669e41f4b71Sopenharmony_ci  ```
2670e41f4b71Sopenharmony_ci
2671e41f4b71Sopenharmony_ci## fs.fsync
2672e41f4b71Sopenharmony_ci
2673e41f4b71Sopenharmony_cifsync(fd: number): Promise&lt;void&gt;
2674e41f4b71Sopenharmony_ci
2675e41f4b71Sopenharmony_ci将文件系统缓存数据写入磁盘,使用Promise异步返回。
2676e41f4b71Sopenharmony_ci
2677e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2678e41f4b71Sopenharmony_ci
2679e41f4b71Sopenharmony_ci**参数:**
2680e41f4b71Sopenharmony_ci
2681e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明           |
2682e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ------------ |
2683e41f4b71Sopenharmony_ci  | fd   | number | 是    | 已打开的文件描述符。 |
2684e41f4b71Sopenharmony_ci
2685e41f4b71Sopenharmony_ci**返回值:**
2686e41f4b71Sopenharmony_ci
2687e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
2688e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
2689e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
2690e41f4b71Sopenharmony_ci
2691e41f4b71Sopenharmony_ci**错误码:**
2692e41f4b71Sopenharmony_ci
2693e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2694e41f4b71Sopenharmony_ci
2695e41f4b71Sopenharmony_ci**示例:**
2696e41f4b71Sopenharmony_ci
2697e41f4b71Sopenharmony_ci  ```ts
2698e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2699e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2700e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
2701e41f4b71Sopenharmony_ci  fs.fsync(file.fd).then(() => {
2702e41f4b71Sopenharmony_ci    console.info("sync data succeed");
2703e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2704e41f4b71Sopenharmony_ci    console.error("sync data failed with error message: " + err.message + ", error code: " + err.code);
2705e41f4b71Sopenharmony_ci  }).finally(() => {
2706e41f4b71Sopenharmony_ci    fs.closeSync(file);
2707e41f4b71Sopenharmony_ci  });
2708e41f4b71Sopenharmony_ci  ```
2709e41f4b71Sopenharmony_ci
2710e41f4b71Sopenharmony_ci## fs.fsync
2711e41f4b71Sopenharmony_ci
2712e41f4b71Sopenharmony_cifsync(fd: number, callback: AsyncCallback&lt;void&gt;): void
2713e41f4b71Sopenharmony_ci
2714e41f4b71Sopenharmony_ci将文件系统缓存数据写入磁盘,使用callback异步回调。
2715e41f4b71Sopenharmony_ci
2716e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2717e41f4b71Sopenharmony_ci
2718e41f4b71Sopenharmony_ci**参数:**
2719e41f4b71Sopenharmony_ci
2720e41f4b71Sopenharmony_ci  | 参数名      | 类型                        | 必填   | 说明              |
2721e41f4b71Sopenharmony_ci  | -------- | ------------------------- | ---- | --------------- |
2722e41f4b71Sopenharmony_ci  | fd       | number                    | 是    | 已打开的文件描述符。    |
2723e41f4b71Sopenharmony_ci  | Callback | AsyncCallback&lt;void&gt; | 是    | 异步将文件数据同步之后的回调。 |
2724e41f4b71Sopenharmony_ci
2725e41f4b71Sopenharmony_ci**错误码:**
2726e41f4b71Sopenharmony_ci
2727e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2728e41f4b71Sopenharmony_ci
2729e41f4b71Sopenharmony_ci**示例:**
2730e41f4b71Sopenharmony_ci
2731e41f4b71Sopenharmony_ci  ```ts
2732e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2733e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2734e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
2735e41f4b71Sopenharmony_ci  fs.fsync(file.fd, (err: BusinessError) => {
2736e41f4b71Sopenharmony_ci    if (err) {
2737e41f4b71Sopenharmony_ci      console.error("fsync failed with error message: " + err.message + ", error code: " + err.code);
2738e41f4b71Sopenharmony_ci    } else {
2739e41f4b71Sopenharmony_ci      console.info("fsync succeed");
2740e41f4b71Sopenharmony_ci    }
2741e41f4b71Sopenharmony_ci    fs.closeSync(file);
2742e41f4b71Sopenharmony_ci  });
2743e41f4b71Sopenharmony_ci  ```
2744e41f4b71Sopenharmony_ci
2745e41f4b71Sopenharmony_ci
2746e41f4b71Sopenharmony_ci## fs.fsyncSync
2747e41f4b71Sopenharmony_ci
2748e41f4b71Sopenharmony_cifsyncSync(fd: number): void
2749e41f4b71Sopenharmony_ci
2750e41f4b71Sopenharmony_ci以同步方法将文件系统缓存数据写入磁盘。
2751e41f4b71Sopenharmony_ci
2752e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2753e41f4b71Sopenharmony_ci
2754e41f4b71Sopenharmony_ci**参数:**
2755e41f4b71Sopenharmony_ci
2756e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明           |
2757e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ------------ |
2758e41f4b71Sopenharmony_ci  | fd   | number | 是    | 已打开的文件描述符。 |
2759e41f4b71Sopenharmony_ci
2760e41f4b71Sopenharmony_ci**错误码:**
2761e41f4b71Sopenharmony_ci
2762e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2763e41f4b71Sopenharmony_ci
2764e41f4b71Sopenharmony_ci**示例:**
2765e41f4b71Sopenharmony_ci
2766e41f4b71Sopenharmony_ci  ```ts
2767e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2768e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
2769e41f4b71Sopenharmony_ci  fs.fsyncSync(file.fd);
2770e41f4b71Sopenharmony_ci  fs.closeSync(file);
2771e41f4b71Sopenharmony_ci  ```
2772e41f4b71Sopenharmony_ci
2773e41f4b71Sopenharmony_ci## fs.fdatasync
2774e41f4b71Sopenharmony_ci
2775e41f4b71Sopenharmony_cifdatasync(fd: number): Promise&lt;void&gt;
2776e41f4b71Sopenharmony_ci
2777e41f4b71Sopenharmony_ci实现文件内容数据同步,使用Promise异步返回。
2778e41f4b71Sopenharmony_ci
2779e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2780e41f4b71Sopenharmony_ci
2781e41f4b71Sopenharmony_ci**参数:**
2782e41f4b71Sopenharmony_ci
2783e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明           |
2784e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ------------ |
2785e41f4b71Sopenharmony_ci  | fd   | number | 是    | 已打开的文件描述符。 |
2786e41f4b71Sopenharmony_ci
2787e41f4b71Sopenharmony_ci**返回值:**
2788e41f4b71Sopenharmony_ci
2789e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
2790e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
2791e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
2792e41f4b71Sopenharmony_ci
2793e41f4b71Sopenharmony_ci**错误码:**
2794e41f4b71Sopenharmony_ci
2795e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2796e41f4b71Sopenharmony_ci
2797e41f4b71Sopenharmony_ci**示例:**
2798e41f4b71Sopenharmony_ci
2799e41f4b71Sopenharmony_ci  ```ts
2800e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2801e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2802e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
2803e41f4b71Sopenharmony_ci  fs.fdatasync(file.fd).then(() => {
2804e41f4b71Sopenharmony_ci    console.info("sync data succeed");
2805e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2806e41f4b71Sopenharmony_ci    console.error("sync data failed with error message: " + err.message + ", error code: " + err.code);
2807e41f4b71Sopenharmony_ci  }).finally(() => {
2808e41f4b71Sopenharmony_ci    fs.closeSync(file);
2809e41f4b71Sopenharmony_ci  });
2810e41f4b71Sopenharmony_ci  ```
2811e41f4b71Sopenharmony_ci
2812e41f4b71Sopenharmony_ci## fs.fdatasync
2813e41f4b71Sopenharmony_ci
2814e41f4b71Sopenharmony_cifdatasync(fd: number, callback: AsyncCallback&lt;void&gt;): void
2815e41f4b71Sopenharmony_ci
2816e41f4b71Sopenharmony_ci实现文件内容数据同步,使用callback异步回调。
2817e41f4b71Sopenharmony_ci
2818e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2819e41f4b71Sopenharmony_ci
2820e41f4b71Sopenharmony_ci**参数:**
2821e41f4b71Sopenharmony_ci
2822e41f4b71Sopenharmony_ci  | 参数名      | 类型                              | 必填   | 说明                |
2823e41f4b71Sopenharmony_ci  | -------- | ------------------------------- | ---- | ----------------- |
2824e41f4b71Sopenharmony_ci  | fd       | number                          | 是    | 已打开的文件描述符。      |
2825e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void&gt; | 是    | 异步将文件内容数据同步之后的回调。 |
2826e41f4b71Sopenharmony_ci
2827e41f4b71Sopenharmony_ci**错误码:**
2828e41f4b71Sopenharmony_ci
2829e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2830e41f4b71Sopenharmony_ci
2831e41f4b71Sopenharmony_ci**示例:**
2832e41f4b71Sopenharmony_ci
2833e41f4b71Sopenharmony_ci  ```ts
2834e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2835e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2836e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
2837e41f4b71Sopenharmony_ci  fs.fdatasync (file.fd, (err: BusinessError) => {
2838e41f4b71Sopenharmony_ci    if (err) {
2839e41f4b71Sopenharmony_ci      console.error("fdatasync failed with error message: " + err.message + ", error code: " + err.code);
2840e41f4b71Sopenharmony_ci    } else {
2841e41f4b71Sopenharmony_ci      console.info("fdatasync succeed");
2842e41f4b71Sopenharmony_ci    }
2843e41f4b71Sopenharmony_ci    fs.closeSync(file);
2844e41f4b71Sopenharmony_ci  });
2845e41f4b71Sopenharmony_ci  ```
2846e41f4b71Sopenharmony_ci
2847e41f4b71Sopenharmony_ci## fs.fdatasyncSync
2848e41f4b71Sopenharmony_ci
2849e41f4b71Sopenharmony_cifdatasyncSync(fd: number): void
2850e41f4b71Sopenharmony_ci
2851e41f4b71Sopenharmony_ci以同步方法实现文件内容数据同步。
2852e41f4b71Sopenharmony_ci
2853e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2854e41f4b71Sopenharmony_ci
2855e41f4b71Sopenharmony_ci**参数:**
2856e41f4b71Sopenharmony_ci
2857e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明           |
2858e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ------------ |
2859e41f4b71Sopenharmony_ci  | fd   | number | 是    | 已打开的文件描述符。 |
2860e41f4b71Sopenharmony_ci
2861e41f4b71Sopenharmony_ci**错误码:**
2862e41f4b71Sopenharmony_ci
2863e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2864e41f4b71Sopenharmony_ci
2865e41f4b71Sopenharmony_ci**示例:**
2866e41f4b71Sopenharmony_ci
2867e41f4b71Sopenharmony_ci  ```ts
2868e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
2869e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
2870e41f4b71Sopenharmony_ci  fs.fdatasyncSync(file.fd);
2871e41f4b71Sopenharmony_ci  fs.closeSync(file);
2872e41f4b71Sopenharmony_ci  ```
2873e41f4b71Sopenharmony_ci
2874e41f4b71Sopenharmony_ci## fs.symlink
2875e41f4b71Sopenharmony_ci
2876e41f4b71Sopenharmony_cisymlink(target: string, srcPath: string): Promise&lt;void&gt;
2877e41f4b71Sopenharmony_ci
2878e41f4b71Sopenharmony_ci基于文件路径创建符号链接,使用Promise异步返回。
2879e41f4b71Sopenharmony_ci
2880e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2881e41f4b71Sopenharmony_ci
2882e41f4b71Sopenharmony_ci**参数:**
2883e41f4b71Sopenharmony_ci
2884e41f4b71Sopenharmony_ci| 参数名  | 类型   | 必填 | 说明                         |
2885e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------- |
2886e41f4b71Sopenharmony_ci| target  | string | 是   | 源文件的应用沙箱路径。     |
2887e41f4b71Sopenharmony_ci| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
2888e41f4b71Sopenharmony_ci
2889e41f4b71Sopenharmony_ci**返回值:**
2890e41f4b71Sopenharmony_ci
2891e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
2892e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
2893e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
2894e41f4b71Sopenharmony_ci
2895e41f4b71Sopenharmony_ci**错误码:**
2896e41f4b71Sopenharmony_ci
2897e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2898e41f4b71Sopenharmony_ci
2899e41f4b71Sopenharmony_ci**示例:**
2900e41f4b71Sopenharmony_ci
2901e41f4b71Sopenharmony_ci  ```ts
2902e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2903e41f4b71Sopenharmony_ci  let srcFile = pathDir + "/test.txt";
2904e41f4b71Sopenharmony_ci  let dstFile = pathDir + "/test";
2905e41f4b71Sopenharmony_ci  fs.symlink(srcFile, dstFile).then(() => {
2906e41f4b71Sopenharmony_ci    console.info("symlink succeed");
2907e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
2908e41f4b71Sopenharmony_ci    console.error("symlink failed with error message: " + err.message + ", error code: " + err.code);
2909e41f4b71Sopenharmony_ci  });
2910e41f4b71Sopenharmony_ci  ```
2911e41f4b71Sopenharmony_ci
2912e41f4b71Sopenharmony_ci
2913e41f4b71Sopenharmony_ci## fs.symlink
2914e41f4b71Sopenharmony_cisymlink(target: string, srcPath: string, callback: AsyncCallback&lt;void&gt;): void
2915e41f4b71Sopenharmony_ci
2916e41f4b71Sopenharmony_ci基于文件路径创建符号链接,使用callback异步回调。
2917e41f4b71Sopenharmony_ci
2918e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2919e41f4b71Sopenharmony_ci
2920e41f4b71Sopenharmony_ci**参数:**
2921e41f4b71Sopenharmony_ci
2922e41f4b71Sopenharmony_ci| 参数名   | 类型                      | 必填 | 说明                             |
2923e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | -------------------------------- |
2924e41f4b71Sopenharmony_ci| target   | string                    | 是   | 源文件的应用沙箱路径。         |
2925e41f4b71Sopenharmony_ci| srcPath  | string                    | 是   | 符号链接文件的应用沙箱路径。     |
2926e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | 是   | 异步创建符号链接信息之后的回调。 |
2927e41f4b71Sopenharmony_ci
2928e41f4b71Sopenharmony_ci**错误码:**
2929e41f4b71Sopenharmony_ci
2930e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2931e41f4b71Sopenharmony_ci
2932e41f4b71Sopenharmony_ci**示例:**
2933e41f4b71Sopenharmony_ci
2934e41f4b71Sopenharmony_ci  ```ts
2935e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
2936e41f4b71Sopenharmony_ci  let srcFile = pathDir + "/test.txt";
2937e41f4b71Sopenharmony_ci  let dstFile = pathDir + "/test";
2938e41f4b71Sopenharmony_ci  fs.symlink(srcFile, dstFile, (err: BusinessError) => {
2939e41f4b71Sopenharmony_ci    if (err) {
2940e41f4b71Sopenharmony_ci      console.error("symlink failed with error message: " + err.message + ", error code: " + err.code);
2941e41f4b71Sopenharmony_ci    } else {
2942e41f4b71Sopenharmony_ci      console.info("symlink succeed");
2943e41f4b71Sopenharmony_ci    }
2944e41f4b71Sopenharmony_ci  });
2945e41f4b71Sopenharmony_ci  ```
2946e41f4b71Sopenharmony_ci
2947e41f4b71Sopenharmony_ci## fs.symlinkSync
2948e41f4b71Sopenharmony_ci
2949e41f4b71Sopenharmony_cisymlinkSync(target: string, srcPath: string): void
2950e41f4b71Sopenharmony_ci
2951e41f4b71Sopenharmony_ci以同步的方法基于文件路径创建符号链接。
2952e41f4b71Sopenharmony_ci
2953e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2954e41f4b71Sopenharmony_ci
2955e41f4b71Sopenharmony_ci**参数:**
2956e41f4b71Sopenharmony_ci
2957e41f4b71Sopenharmony_ci| 参数名  | 类型   | 必填 | 说明                         |
2958e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------- |
2959e41f4b71Sopenharmony_ci| target  | string | 是   | 源文件的应用沙箱路径。     |
2960e41f4b71Sopenharmony_ci| srcPath | string | 是   | 符号链接文件的应用沙箱路径。 |
2961e41f4b71Sopenharmony_ci
2962e41f4b71Sopenharmony_ci**错误码:**
2963e41f4b71Sopenharmony_ci
2964e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
2965e41f4b71Sopenharmony_ci
2966e41f4b71Sopenharmony_ci**示例:**
2967e41f4b71Sopenharmony_ci
2968e41f4b71Sopenharmony_ci  ```ts
2969e41f4b71Sopenharmony_ci  let srcFile = pathDir + "/test.txt";
2970e41f4b71Sopenharmony_ci  let dstFile = pathDir + "/test";
2971e41f4b71Sopenharmony_ci  fs.symlinkSync(srcFile, dstFile);
2972e41f4b71Sopenharmony_ci  ```
2973e41f4b71Sopenharmony_ci
2974e41f4b71Sopenharmony_ci## fs.listFile
2975e41f4b71Sopenharmony_cilistFile(path: string, options?: ListFileOptions): Promise<string[]>
2976e41f4b71Sopenharmony_ci
2977e41f4b71Sopenharmony_ci列出文件夹下所有文件名,支持递归列出所有文件名(包含子目录下),支持文件过滤,使用Promise异步返回。
2978e41f4b71Sopenharmony_ci
2979e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
2980e41f4b71Sopenharmony_ci
2981e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
2982e41f4b71Sopenharmony_ci
2983e41f4b71Sopenharmony_ci**参数:**
2984e41f4b71Sopenharmony_ci
2985e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
2986e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
2987e41f4b71Sopenharmony_ci  | path | string | 是    | 文件夹的应用沙箱路径。 |
2988e41f4b71Sopenharmony_ci  | options | [ListFileOptions](#listfileoptions11) | 否    | 文件过滤选项。默认不进行过滤。 |
2989e41f4b71Sopenharmony_ci
2990e41f4b71Sopenharmony_ci
2991e41f4b71Sopenharmony_ci**返回值:**
2992e41f4b71Sopenharmony_ci
2993e41f4b71Sopenharmony_ci  | 类型                   | 说明         |
2994e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
2995e41f4b71Sopenharmony_ci  | Promise&lt;string[]&gt; | Promise对象。返回文件名数组。 |
2996e41f4b71Sopenharmony_ci
2997e41f4b71Sopenharmony_ci**错误码:**
2998e41f4b71Sopenharmony_ci
2999e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3000e41f4b71Sopenharmony_ci
3001e41f4b71Sopenharmony_ci**示例:**
3002e41f4b71Sopenharmony_ci
3003e41f4b71Sopenharmony_ci  ```ts
3004e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3005e41f4b71Sopenharmony_ci  import { fileIo as fs, Filter, ListFileOptions } from '@kit.CoreFileKit';
3006e41f4b71Sopenharmony_ci  let listFileOption: ListFileOptions = {
3007e41f4b71Sopenharmony_ci    recursion: false,
3008e41f4b71Sopenharmony_ci    listNum: 0,
3009e41f4b71Sopenharmony_ci    filter: {
3010e41f4b71Sopenharmony_ci      suffix: [".png", ".jpg", ".jpeg"],
3011e41f4b71Sopenharmony_ci      displayName: ["*abc", "efg*"],
3012e41f4b71Sopenharmony_ci      fileSizeOver: 1024
3013e41f4b71Sopenharmony_ci    }
3014e41f4b71Sopenharmony_ci  }
3015e41f4b71Sopenharmony_ci  fs.listFile(pathDir, listFileOption).then((filenames: Array<string>) => {
3016e41f4b71Sopenharmony_ci    console.info("listFile succeed");
3017e41f4b71Sopenharmony_ci    for (let i = 0; i < filenames.length; i++) {
3018e41f4b71Sopenharmony_ci      console.info("fileName: %s", filenames[i]);
3019e41f4b71Sopenharmony_ci    }
3020e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3021e41f4b71Sopenharmony_ci    console.error("list file failed with error message: " + err.message + ", error code: " + err.code);
3022e41f4b71Sopenharmony_ci  });
3023e41f4b71Sopenharmony_ci  ```
3024e41f4b71Sopenharmony_ci
3025e41f4b71Sopenharmony_ci## fs.listFile
3026e41f4b71Sopenharmony_cilistFile(path: string, options?: ListFileOptions, callback: AsyncCallback<string[]>): void
3027e41f4b71Sopenharmony_ci
3028e41f4b71Sopenharmony_ci列出文件夹下所有文件名,支持递归列出所有文件名(包含子目录下),支持文件过滤,使用Callback异步回调。
3029e41f4b71Sopenharmony_ci
3030e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
3031e41f4b71Sopenharmony_ci
3032e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3033e41f4b71Sopenharmony_ci
3034e41f4b71Sopenharmony_ci**参数:**
3035e41f4b71Sopenharmony_ci
3036e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3037e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3038e41f4b71Sopenharmony_ci  | path | string | 是    | 文件夹的应用沙箱路径。 |
3039e41f4b71Sopenharmony_ci  | options | [ListFileOptions](#listfileoptions11) | 否    | 文件过滤选项。默认不进行过滤。 |
3040e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;string[]&gt; | 是    | 异步列出文件名数组之后的回调。              |
3041e41f4b71Sopenharmony_ci
3042e41f4b71Sopenharmony_ci
3043e41f4b71Sopenharmony_ci**错误码:**
3044e41f4b71Sopenharmony_ci
3045e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3046e41f4b71Sopenharmony_ci
3047e41f4b71Sopenharmony_ci**示例:**
3048e41f4b71Sopenharmony_ci
3049e41f4b71Sopenharmony_ci  ```ts
3050e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3051e41f4b71Sopenharmony_ci  import { fileIo as fs, Filter, ListFileOptions } from '@kit.CoreFileKit';
3052e41f4b71Sopenharmony_ci  let listFileOption: ListFileOptions = {
3053e41f4b71Sopenharmony_ci    recursion: false,
3054e41f4b71Sopenharmony_ci    listNum: 0,
3055e41f4b71Sopenharmony_ci    filter: {
3056e41f4b71Sopenharmony_ci      suffix: [".png", ".jpg", ".jpeg"],
3057e41f4b71Sopenharmony_ci      displayName: ["*abc", "efg*"],
3058e41f4b71Sopenharmony_ci      fileSizeOver: 1024
3059e41f4b71Sopenharmony_ci    }
3060e41f4b71Sopenharmony_ci  };
3061e41f4b71Sopenharmony_ci  fs.listFile(pathDir, listFileOption, (err: BusinessError, filenames: Array<string>) => {
3062e41f4b71Sopenharmony_ci    if (err) {
3063e41f4b71Sopenharmony_ci      console.error("list file failed with error message: " + err.message + ", error code: " + err.code);
3064e41f4b71Sopenharmony_ci    } else {
3065e41f4b71Sopenharmony_ci      console.info("listFile succeed");
3066e41f4b71Sopenharmony_ci      for (let i = 0; i < filenames.length; i++) {
3067e41f4b71Sopenharmony_ci        console.info("filename: %s", filenames[i]);
3068e41f4b71Sopenharmony_ci      }
3069e41f4b71Sopenharmony_ci    }
3070e41f4b71Sopenharmony_ci  });
3071e41f4b71Sopenharmony_ci  ```
3072e41f4b71Sopenharmony_ci
3073e41f4b71Sopenharmony_ci## fs.listFileSync
3074e41f4b71Sopenharmony_ci
3075e41f4b71Sopenharmony_cilistFileSync(path: string, options?: ListFileOptions): string[]
3076e41f4b71Sopenharmony_ci
3077e41f4b71Sopenharmony_ci以同步方式列出文件夹下所有文件名,支持递归列出所有文件名(包含子目录下),支持文件过滤。
3078e41f4b71Sopenharmony_ci
3079e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
3080e41f4b71Sopenharmony_ci
3081e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3082e41f4b71Sopenharmony_ci
3083e41f4b71Sopenharmony_ci**参数:**
3084e41f4b71Sopenharmony_ci
3085e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3086e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3087e41f4b71Sopenharmony_ci  | path | string | 是    | 文件夹的应用沙箱路径。 |
3088e41f4b71Sopenharmony_ci  | options | [ListFileOptions](#listfileoptions11) | 否    | 文件过滤选项。默认不进行过滤。 |
3089e41f4b71Sopenharmony_ci
3090e41f4b71Sopenharmony_ci
3091e41f4b71Sopenharmony_ci**返回值:**
3092e41f4b71Sopenharmony_ci
3093e41f4b71Sopenharmony_ci  | 类型                   | 说明         |
3094e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
3095e41f4b71Sopenharmony_ci  | string[] | 返回文件名数组。 |
3096e41f4b71Sopenharmony_ci
3097e41f4b71Sopenharmony_ci**错误码:**
3098e41f4b71Sopenharmony_ci
3099e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3100e41f4b71Sopenharmony_ci
3101e41f4b71Sopenharmony_ci**示例:**
3102e41f4b71Sopenharmony_ci
3103e41f4b71Sopenharmony_ci  ```ts
3104e41f4b71Sopenharmony_ci  import { fileIo as fs, Filter, ListFileOptions} from '@kit.CoreFileKit';
3105e41f4b71Sopenharmony_ci  let listFileOption: ListFileOptions = {
3106e41f4b71Sopenharmony_ci    recursion: false,
3107e41f4b71Sopenharmony_ci    listNum: 0,
3108e41f4b71Sopenharmony_ci    filter: {
3109e41f4b71Sopenharmony_ci      suffix: [".png", ".jpg", ".jpeg"],
3110e41f4b71Sopenharmony_ci      displayName: ["*abc", "efg*"],
3111e41f4b71Sopenharmony_ci      fileSizeOver: 1024
3112e41f4b71Sopenharmony_ci    }
3113e41f4b71Sopenharmony_ci  };
3114e41f4b71Sopenharmony_ci  let filenames = fs.listFileSync(pathDir, listFileOption);
3115e41f4b71Sopenharmony_ci  console.info("listFile succeed");
3116e41f4b71Sopenharmony_ci  for (let i = 0; i < filenames.length; i++) {
3117e41f4b71Sopenharmony_ci    console.info("filename: %s", filenames[i]);
3118e41f4b71Sopenharmony_ci  }
3119e41f4b71Sopenharmony_ci  ```
3120e41f4b71Sopenharmony_ci
3121e41f4b71Sopenharmony_ci## fs.lseek<sup>11+</sup>
3122e41f4b71Sopenharmony_ci
3123e41f4b71Sopenharmony_cilseek(fd: number, offset: number, whence?: WhenceType): number
3124e41f4b71Sopenharmony_ci
3125e41f4b71Sopenharmony_ci调整文件偏置指针位置。
3126e41f4b71Sopenharmony_ci
3127e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3128e41f4b71Sopenharmony_ci
3129e41f4b71Sopenharmony_ci**参数:**
3130e41f4b71Sopenharmony_ci
3131e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3132e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3133e41f4b71Sopenharmony_ci  | fd | number | 是    | 文件描述符。 |
3134e41f4b71Sopenharmony_ci  | offset | number | 是    | 相对偏移位置,单位为字节。 |
3135e41f4b71Sopenharmony_ci  | whence | [WhenceType](#whencetype11) | 否    | 偏移指针相对位置类型。 |
3136e41f4b71Sopenharmony_ci
3137e41f4b71Sopenharmony_ci**返回值:**
3138e41f4b71Sopenharmony_ci
3139e41f4b71Sopenharmony_ci  | 类型                   | 说明         |
3140e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
3141e41f4b71Sopenharmony_ci  | number | 当前文件偏置指针位置(相对于文件头的偏移量,单位为字节)。 |
3142e41f4b71Sopenharmony_ci
3143e41f4b71Sopenharmony_ci**错误码:**
3144e41f4b71Sopenharmony_ci
3145e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3146e41f4b71Sopenharmony_ci
3147e41f4b71Sopenharmony_ci**示例:**
3148e41f4b71Sopenharmony_ci
3149e41f4b71Sopenharmony_ci  ```ts
3150e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3151e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
3152e41f4b71Sopenharmony_ci  console.info('The current offset is at ' + fs.lseek(file.fd, 5, fs.WhenceType.SEEK_SET));
3153e41f4b71Sopenharmony_ci  fs.closeSync(file);
3154e41f4b71Sopenharmony_ci  ```
3155e41f4b71Sopenharmony_ci
3156e41f4b71Sopenharmony_ci## fs.moveDir<sup>10+</sup>
3157e41f4b71Sopenharmony_ci
3158e41f4b71Sopenharmony_cimoveDir(src: string, dest: string, mode?: number): Promise\<void>
3159e41f4b71Sopenharmony_ci
3160e41f4b71Sopenharmony_ci移动源文件夹至目标路径下,使用Promise异步返回。
3161e41f4b71Sopenharmony_ci
3162e41f4b71Sopenharmony_ci> **说明:**
3163e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
3164e41f4b71Sopenharmony_ci
3165e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3166e41f4b71Sopenharmony_ci
3167e41f4b71Sopenharmony_ci**参数:**
3168e41f4b71Sopenharmony_ci
3169e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3170e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3171e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件夹的应用沙箱路径。 |
3172e41f4b71Sopenharmony_ci  | dest | string | 是    | 目标文件夹的应用沙箱路径。 |
3173e41f4b71Sopenharmony_ci  | mode | number | 否    | 移动模式。默认mode为0。<br/>-&nbsp;mode为0,文件夹级别抛异常。若目标文件夹下存在与源文件夹名冲突的非空文件夹,则抛出异常。<br/>-&nbsp;mode为1,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以Array\<[ConflictFiles](#conflictfiles10)>形式提供。<br/>-&nbsp; mode为2,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。<br/>-&nbsp; mode为3,文件夹级别强制覆盖。移动源文件夹至目标文件夹下,目标文件夹下移动的文件夹内容与源文件夹完全一致。若目标文件夹下存在与源文件夹名冲突的文件夹,该文件夹下所有原始文件将不会保留。|
3174e41f4b71Sopenharmony_ci
3175e41f4b71Sopenharmony_ci**返回值:**
3176e41f4b71Sopenharmony_ci
3177e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
3178e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
3179e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
3180e41f4b71Sopenharmony_ci
3181e41f4b71Sopenharmony_ci**错误码:**
3182e41f4b71Sopenharmony_ci
3183e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3184e41f4b71Sopenharmony_ci
3185e41f4b71Sopenharmony_ci**示例:**
3186e41f4b71Sopenharmony_ci
3187e41f4b71Sopenharmony_ci  ```ts
3188e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3189e41f4b71Sopenharmony_ci  // move directory from srcPath to destPath
3190e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/";
3191e41f4b71Sopenharmony_ci  let destPath = pathDir + "/destDir/";
3192e41f4b71Sopenharmony_ci  fs.moveDir(srcPath, destPath, 1).then(() => {
3193e41f4b71Sopenharmony_ci    console.info("move directory succeed");
3194e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3195e41f4b71Sopenharmony_ci    console.error("move directory failed with error message: " + err.message + ", error code: " + err.code);
3196e41f4b71Sopenharmony_ci  });
3197e41f4b71Sopenharmony_ci  ```
3198e41f4b71Sopenharmony_ci
3199e41f4b71Sopenharmony_ci## fs.moveDir<sup>10+</sup>
3200e41f4b71Sopenharmony_ci
3201e41f4b71Sopenharmony_cimoveDir(src: string, dest: string, mode: number, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void
3202e41f4b71Sopenharmony_ci
3203e41f4b71Sopenharmony_ci移动源文件夹至目标路径下,支持设置移动模式。使用callback异步回调。
3204e41f4b71Sopenharmony_ci
3205e41f4b71Sopenharmony_ci> **说明:**
3206e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
3207e41f4b71Sopenharmony_ci
3208e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3209e41f4b71Sopenharmony_ci
3210e41f4b71Sopenharmony_ci**参数:**
3211e41f4b71Sopenharmony_ci
3212e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3213e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3214e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件夹的应用沙箱路径。 |
3215e41f4b71Sopenharmony_ci  | dest | string | 是    | 目标文件夹的应用沙箱路径。 |
3216e41f4b71Sopenharmony_ci  | mode | number | 是    | 移动模式。默认mode为0。<br/>-&nbsp;mode为0,文件夹级别抛异常。若目标文件夹下存在与源文件夹名冲突的文件夹,则抛出异常。<br/>-&nbsp;mode为1,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以Array\<[ConflictFiles](#conflictfiles10)>形式提供。<br/>-&nbsp; mode为2,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。<br/>-&nbsp; mode为3,文件夹级别强制覆盖。移动源文件夹至目标文件夹下,目标文件夹下移动的文件夹内容与源文件夹完全一致。若目标文件夹下存在与源文件夹名冲突的文件夹,该文件夹下所有原始文件将不会保留。|
3217e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void, Array&lt;[ConflictFiles](#conflictfiles10)&gt;&gt; | 是    | 异步移动文件夹之后的回调。              |
3218e41f4b71Sopenharmony_ci
3219e41f4b71Sopenharmony_ci**错误码:**
3220e41f4b71Sopenharmony_ci
3221e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3222e41f4b71Sopenharmony_ci
3223e41f4b71Sopenharmony_ci**示例:**
3224e41f4b71Sopenharmony_ci
3225e41f4b71Sopenharmony_ci  ```ts
3226e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3227e41f4b71Sopenharmony_ci  import { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit';
3228e41f4b71Sopenharmony_ci  // move directory from srcPath to destPath
3229e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/";
3230e41f4b71Sopenharmony_ci  let destPath = pathDir + "/destDir/";
3231e41f4b71Sopenharmony_ci  fs.moveDir(srcPath, destPath, 1, (err: BusinessError<Array<ConflictFiles>>) => {
3232e41f4b71Sopenharmony_ci    if (err && err.code == 13900015 && err.data?.length !== undefined) {
3233e41f4b71Sopenharmony_ci      for (let i = 0; i < err.data.length; i++) {
3234e41f4b71Sopenharmony_ci        console.error("move directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile);
3235e41f4b71Sopenharmony_ci      }
3236e41f4b71Sopenharmony_ci    } else if (err) {
3237e41f4b71Sopenharmony_ci      console.error("move directory failed with error message: " + err.message + ", error code: " + err.code);
3238e41f4b71Sopenharmony_ci    } else {
3239e41f4b71Sopenharmony_ci      console.info("move directory succeed");
3240e41f4b71Sopenharmony_ci    }
3241e41f4b71Sopenharmony_ci  });
3242e41f4b71Sopenharmony_ci  ```
3243e41f4b71Sopenharmony_ci
3244e41f4b71Sopenharmony_ci  ## fs.moveDir<sup>10+</sup>
3245e41f4b71Sopenharmony_ci
3246e41f4b71Sopenharmony_cimoveDir(src: string, dest: string, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void
3247e41f4b71Sopenharmony_ci
3248e41f4b71Sopenharmony_ci移动源文件夹至目标路径下。使用callback异步回调。
3249e41f4b71Sopenharmony_ci
3250e41f4b71Sopenharmony_ci移动模式为文件夹级别抛异常,当目标文件夹下存在与源文件夹名冲突的文件夹,则抛出异常。
3251e41f4b71Sopenharmony_ci
3252e41f4b71Sopenharmony_ci> **说明:**
3253e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
3254e41f4b71Sopenharmony_ci
3255e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3256e41f4b71Sopenharmony_ci
3257e41f4b71Sopenharmony_ci**参数:**
3258e41f4b71Sopenharmony_ci
3259e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3260e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3261e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件夹的应用沙箱路径。 |
3262e41f4b71Sopenharmony_ci  | dest | string | 是    | 目标文件夹的应用沙箱路径。 |
3263e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void, Array&lt;[ConflictFiles](#conflictfiles10)&gt;&gt; | 是    | 异步移动文件夹之后的回调。              |
3264e41f4b71Sopenharmony_ci
3265e41f4b71Sopenharmony_ci**错误码:**
3266e41f4b71Sopenharmony_ci
3267e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3268e41f4b71Sopenharmony_ci
3269e41f4b71Sopenharmony_ci**示例:**
3270e41f4b71Sopenharmony_ci
3271e41f4b71Sopenharmony_ci  ```ts
3272e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3273e41f4b71Sopenharmony_ci  import { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit';
3274e41f4b71Sopenharmony_ci  // move directory from srcPath to destPath
3275e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/srcDir/";
3276e41f4b71Sopenharmony_ci  let destPath = pathDir + "/destDir/";
3277e41f4b71Sopenharmony_ci  fs.moveDir(srcPath, destPath, (err: BusinessError<Array<ConflictFiles>>) => {
3278e41f4b71Sopenharmony_ci    if (err && err.code == 13900015 && err.data?.length !== undefined) {
3279e41f4b71Sopenharmony_ci      for (let i = 0; i < err.data.length; i++) {
3280e41f4b71Sopenharmony_ci        console.error("move directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile);
3281e41f4b71Sopenharmony_ci      }
3282e41f4b71Sopenharmony_ci    } else if (err) {
3283e41f4b71Sopenharmony_ci      console.error("move directory failed with error message: " + err.message + ", error code: " + err.code);
3284e41f4b71Sopenharmony_ci    } else {
3285e41f4b71Sopenharmony_ci      console.info("move directory succeed");
3286e41f4b71Sopenharmony_ci    }
3287e41f4b71Sopenharmony_ci  });
3288e41f4b71Sopenharmony_ci  ```
3289e41f4b71Sopenharmony_ci
3290e41f4b71Sopenharmony_ci## fs.moveDirSync<sup>10+</sup>
3291e41f4b71Sopenharmony_ci
3292e41f4b71Sopenharmony_cimoveDirSync(src: string, dest: string, mode?: number): void
3293e41f4b71Sopenharmony_ci
3294e41f4b71Sopenharmony_ci以同步方法移动源文件夹至目标路径下。
3295e41f4b71Sopenharmony_ci
3296e41f4b71Sopenharmony_ci> **说明:**
3297e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
3298e41f4b71Sopenharmony_ci
3299e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3300e41f4b71Sopenharmony_ci
3301e41f4b71Sopenharmony_ci**参数:**
3302e41f4b71Sopenharmony_ci
3303e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3304e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3305e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件夹的应用沙箱路径。 |
3306e41f4b71Sopenharmony_ci  | dest | string | 是    | 目标文件夹的应用沙箱路径。 |
3307e41f4b71Sopenharmony_ci  | mode | number | 否    | 移动模式。默认mode为0。<br/>-&nbsp;mode为0,文件夹级别抛异常。若目标文件夹下存在与源文件夹名冲突的文件夹,则抛出异常。<br/>-&nbsp;mode为1,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以Array\<[ConflictFiles](#conflictfiles10)>形式提供。<br/>-&nbsp; mode为2,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。<br/>-&nbsp; mode为3,文件夹级别强制覆盖。移动源文件夹至目标文件夹下,目标文件夹下移动的文件夹内容与源文件夹完全一致。若目标文件夹下存在与源文件夹名冲突的文件夹,该文件夹下所有原始文件将不会保留。|
3308e41f4b71Sopenharmony_ci
3309e41f4b71Sopenharmony_ci**错误码:**
3310e41f4b71Sopenharmony_ci
3311e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3312e41f4b71Sopenharmony_ci
3313e41f4b71Sopenharmony_ci**示例:**
3314e41f4b71Sopenharmony_ci
3315e41f4b71Sopenharmony_ci  ```ts
3316e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3317e41f4b71Sopenharmony_ciimport { fileIo as fs, ConflictFiles } from '@kit.CoreFileKit';
3318e41f4b71Sopenharmony_ci// move directory from srcPath to destPath
3319e41f4b71Sopenharmony_cilet srcPath = pathDir + "/srcDir/";
3320e41f4b71Sopenharmony_cilet destPath = pathDir + "/destDir/";
3321e41f4b71Sopenharmony_citry {
3322e41f4b71Sopenharmony_ci  fs.moveDirSync(srcPath, destPath, 1);
3323e41f4b71Sopenharmony_ci  console.info("move directory succeed");
3324e41f4b71Sopenharmony_ci} catch (error) {
3325e41f4b71Sopenharmony_ci  let err: BusinessError<Array<ConflictFiles>> = error as BusinessError<Array<ConflictFiles>>;
3326e41f4b71Sopenharmony_ci  if (err.code == 13900015 && err.data?.length !== undefined) {
3327e41f4b71Sopenharmony_ci    for (let i = 0; i < err.data.length; i++) {
3328e41f4b71Sopenharmony_ci      console.error("move directory failed with conflicting files: " + err.data[i].srcFile + " " + err.data[i].destFile);
3329e41f4b71Sopenharmony_ci    }
3330e41f4b71Sopenharmony_ci  } else {
3331e41f4b71Sopenharmony_ci    console.error("move directory failed with error message: " + err.message + ", error code: " + err.code);
3332e41f4b71Sopenharmony_ci  }
3333e41f4b71Sopenharmony_ci}
3334e41f4b71Sopenharmony_ci  ```
3335e41f4b71Sopenharmony_ci
3336e41f4b71Sopenharmony_ci## fs.moveFile
3337e41f4b71Sopenharmony_ci
3338e41f4b71Sopenharmony_cimoveFile(src: string, dest: string, mode?: number): Promise\<void>
3339e41f4b71Sopenharmony_ci
3340e41f4b71Sopenharmony_ci移动文件,使用Promise异步返回。
3341e41f4b71Sopenharmony_ci
3342e41f4b71Sopenharmony_ci> **说明:**
3343e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
3344e41f4b71Sopenharmony_ci
3345e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3346e41f4b71Sopenharmony_ci
3347e41f4b71Sopenharmony_ci**参数:**
3348e41f4b71Sopenharmony_ci
3349e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3350e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3351e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件的应用沙箱路径。 |
3352e41f4b71Sopenharmony_ci  | dest | string | 是    | 目的文件的应用沙箱路径。 |
3353e41f4b71Sopenharmony_ci  | mode | number | 否    | 移动模式。若mode为0,移动位置存在同名文件时,强制移动覆盖。若mode为1,移动位置存在同名文件时,抛出异常。默认为0。 |
3354e41f4b71Sopenharmony_ci
3355e41f4b71Sopenharmony_ci**返回值:**
3356e41f4b71Sopenharmony_ci
3357e41f4b71Sopenharmony_ci  | 类型                  | 说明                           |
3358e41f4b71Sopenharmony_ci  | ------------------- | ---------------------------- |
3359e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
3360e41f4b71Sopenharmony_ci
3361e41f4b71Sopenharmony_ci**错误码:**
3362e41f4b71Sopenharmony_ci
3363e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3364e41f4b71Sopenharmony_ci
3365e41f4b71Sopenharmony_ci**示例:**
3366e41f4b71Sopenharmony_ci
3367e41f4b71Sopenharmony_ci  ```ts
3368e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3369e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/source.txt";
3370e41f4b71Sopenharmony_ci  let destPath = pathDir + "/dest.txt";
3371e41f4b71Sopenharmony_ci  fs.moveFile(srcPath, destPath, 0).then(() => {
3372e41f4b71Sopenharmony_ci    console.info("move file succeed");
3373e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3374e41f4b71Sopenharmony_ci    console.error("move file failed with error message: " + err.message + ", error code: " + err.code);
3375e41f4b71Sopenharmony_ci  });
3376e41f4b71Sopenharmony_ci  ```
3377e41f4b71Sopenharmony_ci
3378e41f4b71Sopenharmony_ci## fs.moveFile
3379e41f4b71Sopenharmony_ci
3380e41f4b71Sopenharmony_cimoveFile(src: string, dest: string, mode: number, callback: AsyncCallback\<void>): void
3381e41f4b71Sopenharmony_ci
3382e41f4b71Sopenharmony_ci移动文件,支持设置移动模式。使用callback异步回调。
3383e41f4b71Sopenharmony_ci
3384e41f4b71Sopenharmony_ci> **说明:**
3385e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
3386e41f4b71Sopenharmony_ci
3387e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3388e41f4b71Sopenharmony_ci
3389e41f4b71Sopenharmony_ci**参数:**
3390e41f4b71Sopenharmony_ci
3391e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3392e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3393e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件的应用沙箱路径。 |
3394e41f4b71Sopenharmony_ci  | dest | string | 是    | 目的文件的应用沙箱路径。 |
3395e41f4b71Sopenharmony_ci  | mode | number | 是    | 移动模式。若mode为0,移动位置存在同名文件时,强制移动覆盖。若mode为1,移动位置存在同名文件时,抛出异常。默认为0。 |
3396e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void&gt; | 是    | 异步移动文件之后的回调。              |
3397e41f4b71Sopenharmony_ci
3398e41f4b71Sopenharmony_ci**错误码:**
3399e41f4b71Sopenharmony_ci
3400e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3401e41f4b71Sopenharmony_ci
3402e41f4b71Sopenharmony_ci**示例:**
3403e41f4b71Sopenharmony_ci
3404e41f4b71Sopenharmony_ci  ```ts
3405e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3406e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/source.txt";
3407e41f4b71Sopenharmony_ci  let destPath = pathDir + "/dest.txt";
3408e41f4b71Sopenharmony_ci  fs.moveFile(srcPath, destPath, 0, (err: BusinessError) => {
3409e41f4b71Sopenharmony_ci    if (err) {
3410e41f4b71Sopenharmony_ci      console.error("move file failed with error message: " + err.message + ", error code: " + err.code);
3411e41f4b71Sopenharmony_ci    } else {
3412e41f4b71Sopenharmony_ci      console.info("move file succeed");
3413e41f4b71Sopenharmony_ci    }
3414e41f4b71Sopenharmony_ci  });
3415e41f4b71Sopenharmony_ci  ```
3416e41f4b71Sopenharmony_ci
3417e41f4b71Sopenharmony_ci## fs.moveFile
3418e41f4b71Sopenharmony_ci
3419e41f4b71Sopenharmony_cimoveFile(src: string, dest: string, callback: AsyncCallback\<void>): void
3420e41f4b71Sopenharmony_ci
3421e41f4b71Sopenharmony_ci移动文件,当移动位置存在同名文件时,将强制移动覆盖。使用callback异步回调。
3422e41f4b71Sopenharmony_ci
3423e41f4b71Sopenharmony_ci> **说明:**
3424e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
3425e41f4b71Sopenharmony_ci
3426e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3427e41f4b71Sopenharmony_ci
3428e41f4b71Sopenharmony_ci**参数:**
3429e41f4b71Sopenharmony_ci
3430e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3431e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3432e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件的应用沙箱路径。 |
3433e41f4b71Sopenharmony_ci  | dest | string | 是    | 目的文件的应用沙箱路径。 |
3434e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void&gt; | 是    | 异步移动文件之后的回调。 |
3435e41f4b71Sopenharmony_ci
3436e41f4b71Sopenharmony_ci**错误码:**
3437e41f4b71Sopenharmony_ci
3438e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3439e41f4b71Sopenharmony_ci
3440e41f4b71Sopenharmony_ci**示例:**
3441e41f4b71Sopenharmony_ci
3442e41f4b71Sopenharmony_ci  ```ts
3443e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3444e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/source.txt";
3445e41f4b71Sopenharmony_ci  let destPath = pathDir + "/dest.txt";
3446e41f4b71Sopenharmony_ci  fs.moveFile(srcPath, destPath, (err: BusinessError) => {
3447e41f4b71Sopenharmony_ci    if (err) {
3448e41f4b71Sopenharmony_ci      console.error("move file failed with error message: " + err.message + ", error code: " + err.code);
3449e41f4b71Sopenharmony_ci    } else {
3450e41f4b71Sopenharmony_ci      console.info("move file succeed");
3451e41f4b71Sopenharmony_ci    }
3452e41f4b71Sopenharmony_ci  });
3453e41f4b71Sopenharmony_ci  ```
3454e41f4b71Sopenharmony_ci
3455e41f4b71Sopenharmony_ci## fs.moveFileSync
3456e41f4b71Sopenharmony_ci
3457e41f4b71Sopenharmony_cimoveFileSync(src: string, dest: string, mode?: number): void
3458e41f4b71Sopenharmony_ci
3459e41f4b71Sopenharmony_ci以同步方式移动文件。
3460e41f4b71Sopenharmony_ci
3461e41f4b71Sopenharmony_ci> **说明:**
3462e41f4b71Sopenharmony_ci> 该接口不支持在分布式文件路径下操作。
3463e41f4b71Sopenharmony_ci
3464e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3465e41f4b71Sopenharmony_ci
3466e41f4b71Sopenharmony_ci**参数:**
3467e41f4b71Sopenharmony_ci
3468e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3469e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3470e41f4b71Sopenharmony_ci  | src | string | 是    | 源文件的应用沙箱路径。 |
3471e41f4b71Sopenharmony_ci  | dest | string | 是    | 目的文件的应用沙箱路径。 |
3472e41f4b71Sopenharmony_ci  | mode | number | 否    | 移动模式。若mode为0,移动位置存在同名文件时,强制移动覆盖。若mode为1,移动位置存在同名文件时,抛出异常。默认为0。 |
3473e41f4b71Sopenharmony_ci
3474e41f4b71Sopenharmony_ci**错误码:**
3475e41f4b71Sopenharmony_ci
3476e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3477e41f4b71Sopenharmony_ci
3478e41f4b71Sopenharmony_ci**示例:**
3479e41f4b71Sopenharmony_ci
3480e41f4b71Sopenharmony_ci  ```ts
3481e41f4b71Sopenharmony_ci  let srcPath = pathDir + "/source.txt";
3482e41f4b71Sopenharmony_ci  let destPath = pathDir + "/dest.txt";
3483e41f4b71Sopenharmony_ci  fs.moveFileSync(srcPath, destPath, 0);
3484e41f4b71Sopenharmony_ci  console.info("move file succeed");
3485e41f4b71Sopenharmony_ci  ```
3486e41f4b71Sopenharmony_ci
3487e41f4b71Sopenharmony_ci## fs.mkdtemp
3488e41f4b71Sopenharmony_ci
3489e41f4b71Sopenharmony_cimkdtemp(prefix: string): Promise&lt;string&gt;
3490e41f4b71Sopenharmony_ci
3491e41f4b71Sopenharmony_ci创建临时目录,使用Promise异步返回。
3492e41f4b71Sopenharmony_ci
3493e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3494e41f4b71Sopenharmony_ci
3495e41f4b71Sopenharmony_ci**参数:**
3496e41f4b71Sopenharmony_ci
3497e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3498e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3499e41f4b71Sopenharmony_ci  | prefix | string | 是    | 指定目录路径,命名时需要以"XXXXXX"作为结尾。路径末尾的"XXXXXX"字符串将被替换为随机字符,以创建唯一的目录名。 |
3500e41f4b71Sopenharmony_ci
3501e41f4b71Sopenharmony_ci**返回值:**
3502e41f4b71Sopenharmony_ci
3503e41f4b71Sopenharmony_ci  | 类型                   | 说明         |
3504e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
3505e41f4b71Sopenharmony_ci  | Promise&lt;string&gt; | Promise对象。返回生成的唯一目录路径。 |
3506e41f4b71Sopenharmony_ci
3507e41f4b71Sopenharmony_ci**错误码:**
3508e41f4b71Sopenharmony_ci
3509e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3510e41f4b71Sopenharmony_ci
3511e41f4b71Sopenharmony_ci**示例:**
3512e41f4b71Sopenharmony_ci
3513e41f4b71Sopenharmony_ci  ```ts
3514e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3515e41f4b71Sopenharmony_ci  fs.mkdtemp(pathDir + "/XXXXXX").then((dir: string) => {
3516e41f4b71Sopenharmony_ci    console.info("mkdtemp succeed:" + dir);
3517e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3518e41f4b71Sopenharmony_ci    console.error("mkdtemp failed with error message: " + err.message + ", error code: " + err.code);
3519e41f4b71Sopenharmony_ci  });
3520e41f4b71Sopenharmony_ci  ```
3521e41f4b71Sopenharmony_ci
3522e41f4b71Sopenharmony_ci## fs.mkdtemp
3523e41f4b71Sopenharmony_ci
3524e41f4b71Sopenharmony_cimkdtemp(prefix: string, callback: AsyncCallback&lt;string&gt;): void
3525e41f4b71Sopenharmony_ci
3526e41f4b71Sopenharmony_ci创建临时目录,使用callback异步回调。
3527e41f4b71Sopenharmony_ci
3528e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3529e41f4b71Sopenharmony_ci
3530e41f4b71Sopenharmony_ci**参数:**
3531e41f4b71Sopenharmony_ci
3532e41f4b71Sopenharmony_ci  | 参数名      | 类型                          | 必填   | 说明                          |
3533e41f4b71Sopenharmony_ci  | -------- | --------------------------- | ---- | --------------------------- |
3534e41f4b71Sopenharmony_ci  | prefix   | string                      | 是    | 指定目录路径,命名时需要以"XXXXXX"作为结尾。路径末尾的"XXXXXX"字符串将被替换为随机字符,以创建唯一的目录名。 |
3535e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;string&gt; | 是    | 异步创建临时目录之后的回调。              |
3536e41f4b71Sopenharmony_ci
3537e41f4b71Sopenharmony_ci**错误码:**
3538e41f4b71Sopenharmony_ci
3539e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3540e41f4b71Sopenharmony_ci
3541e41f4b71Sopenharmony_ci**示例:**
3542e41f4b71Sopenharmony_ci
3543e41f4b71Sopenharmony_ci  ```ts
3544e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3545e41f4b71Sopenharmony_ci  fs.mkdtemp(pathDir + "/XXXXXX", (err: BusinessError, res: string) => {
3546e41f4b71Sopenharmony_ci    if (err) {
3547e41f4b71Sopenharmony_ci      console.error("mkdtemp failed with error message: " + err.message + ", error code: " + err.code);
3548e41f4b71Sopenharmony_ci    } else {
3549e41f4b71Sopenharmony_ci      console.info("mkdtemp succeed");
3550e41f4b71Sopenharmony_ci    }
3551e41f4b71Sopenharmony_ci  });
3552e41f4b71Sopenharmony_ci  ```
3553e41f4b71Sopenharmony_ci
3554e41f4b71Sopenharmony_ci## fs.mkdtempSync
3555e41f4b71Sopenharmony_ci
3556e41f4b71Sopenharmony_cimkdtempSync(prefix: string): string
3557e41f4b71Sopenharmony_ci
3558e41f4b71Sopenharmony_ci以同步的方法创建临时目录。
3559e41f4b71Sopenharmony_ci
3560e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3561e41f4b71Sopenharmony_ci
3562e41f4b71Sopenharmony_ci**参数:**
3563e41f4b71Sopenharmony_ci
3564e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
3565e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
3566e41f4b71Sopenharmony_ci  | prefix | string | 是    | 指定目录路径,命名时需要以"XXXXXX"作为结尾。路径末尾的"XXXXXX"字符串将被替换为随机字符,以创建唯一的目录名。 |
3567e41f4b71Sopenharmony_ci
3568e41f4b71Sopenharmony_ci**返回值:**
3569e41f4b71Sopenharmony_ci
3570e41f4b71Sopenharmony_ci  | 类型    | 说明         |
3571e41f4b71Sopenharmony_ci  | ------ | ---------- |
3572e41f4b71Sopenharmony_ci  | string | 产生的唯一目录路径。 |
3573e41f4b71Sopenharmony_ci
3574e41f4b71Sopenharmony_ci**错误码:**
3575e41f4b71Sopenharmony_ci
3576e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3577e41f4b71Sopenharmony_ci
3578e41f4b71Sopenharmony_ci**示例:**
3579e41f4b71Sopenharmony_ci
3580e41f4b71Sopenharmony_ci  ```ts
3581e41f4b71Sopenharmony_ci  let res = fs.mkdtempSync(pathDir + "/XXXXXX");
3582e41f4b71Sopenharmony_ci  ```
3583e41f4b71Sopenharmony_ci
3584e41f4b71Sopenharmony_ci## fs.utimes<sup>11+</sup>
3585e41f4b71Sopenharmony_ci
3586e41f4b71Sopenharmony_ciutimes(path: string, mtime: number): void
3587e41f4b71Sopenharmony_ci
3588e41f4b71Sopenharmony_ci修改文件最近访问时间属性。
3589e41f4b71Sopenharmony_ci
3590e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3591e41f4b71Sopenharmony_ci
3592e41f4b71Sopenharmony_ci**参数:**
3593e41f4b71Sopenharmony_ci|    参数名    | 类型     | 必填   | 说明                          |
3594e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ |
3595e41f4b71Sopenharmony_ci| path  | string  |  是    | 文件的应用沙箱路径。 |
3596e41f4b71Sopenharmony_ci| mtime  | number  |  是   | 待更新的时间戳。自1970年1月1日起至目标时间的毫秒数。仅支持修改文件最近访问时间属性。 |
3597e41f4b71Sopenharmony_ci
3598e41f4b71Sopenharmony_ci**错误码:**
3599e41f4b71Sopenharmony_ci
3600e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3601e41f4b71Sopenharmony_ci
3602e41f4b71Sopenharmony_ci**示例:**
3603e41f4b71Sopenharmony_ci
3604e41f4b71Sopenharmony_ci  ```ts
3605e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3606e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
3607e41f4b71Sopenharmony_ci  fs.writeSync(file.fd, 'test data');
3608e41f4b71Sopenharmony_ci  fs.closeSync(file);
3609e41f4b71Sopenharmony_ci  fs.utimes(filePath, new Date().getTime());
3610e41f4b71Sopenharmony_ci  ```
3611e41f4b71Sopenharmony_ci
3612e41f4b71Sopenharmony_ci## fs.createRandomAccessFile<sup>10+</sup>
3613e41f4b71Sopenharmony_ci
3614e41f4b71Sopenharmony_cicreateRandomAccessFile(file: string | File, mode?: number): Promise&lt;RandomAccessFile&gt;
3615e41f4b71Sopenharmony_ci
3616e41f4b71Sopenharmony_ci基于文件路径或文件对象创建RandomAccessFile文件对象,使用Promise异步返回。
3617e41f4b71Sopenharmony_ci
3618e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3619e41f4b71Sopenharmony_ci
3620e41f4b71Sopenharmony_ci**参数:**
3621e41f4b71Sopenharmony_ci|    参数名    | 类型     | 必填   | 说明                          |
3622e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ |
3623e41f4b71Sopenharmony_ci|     file     | string \| [File](#file) | 是    | 文件的应用沙箱路径或已打开的File对象 |
3624e41f4b71Sopenharmony_ci|     mode     | number | 否   | 创建文件RandomAccessFile对象的[选项](#openmode),仅当传入文件沙箱路径时生效,必须指定如下选项中的一个,默认以只读方式创建:<br/>-&nbsp;OpenMode.READ_ONLY(0o0):只读创建。<br/>-&nbsp;OpenMode.WRITE_ONLY(0o1):只写创建。<br/>-&nbsp;OpenMode.READ_WRITE(0o2):读写创建。<br/>给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;OpenMode.CREATE(0o100):若文件不存在,则创建文件。<br/>-&nbsp;OpenMode.TRUNC(0o1000):如果RandomAccessFile对象存在且对应文件具有写权限,则将其长度裁剪为零。<br/>-&nbsp;OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到RandomAccessFile对象末尾。<br/>-&nbsp;OpenMode.NONBLOCK(0o4000):如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。<br/>-&nbsp;OpenMode.NOFOLLOW(0o400000):如果path指向符号链接,则出错。<br/>-&nbsp;OpenMode.SYNC(0o4010000):以同步IO的方式创建RandomAccessFile对象。 |
3625e41f4b71Sopenharmony_ci
3626e41f4b71Sopenharmony_ci**返回值:**
3627e41f4b71Sopenharmony_ci
3628e41f4b71Sopenharmony_ci  | 类型                                | 说明        |
3629e41f4b71Sopenharmony_ci  | --------------------------------- | --------- |
3630e41f4b71Sopenharmony_ci  | Promise&lt;[RandomAccessFile](#randomaccessfile)&gt; | Promise对象。返回RandomAccessFile文件对象的结果。 |
3631e41f4b71Sopenharmony_ci
3632e41f4b71Sopenharmony_ci**错误码:**
3633e41f4b71Sopenharmony_ci
3634e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3635e41f4b71Sopenharmony_ci
3636e41f4b71Sopenharmony_ci**示例:**
3637e41f4b71Sopenharmony_ci
3638e41f4b71Sopenharmony_ci  ```ts
3639e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3640e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3641e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
3642e41f4b71Sopenharmony_ci  fs.createRandomAccessFile(file).then((randomAccessFile: fs.RandomAccessFile) => {
3643e41f4b71Sopenharmony_ci    console.info("randomAccessFile fd: " + randomAccessFile.fd);
3644e41f4b71Sopenharmony_ci    randomAccessFile.close();
3645e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3646e41f4b71Sopenharmony_ci    console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
3647e41f4b71Sopenharmony_ci  }).finally(() => {
3648e41f4b71Sopenharmony_ci    fs.closeSync(file);
3649e41f4b71Sopenharmony_ci  });
3650e41f4b71Sopenharmony_ci  ```
3651e41f4b71Sopenharmony_ci
3652e41f4b71Sopenharmony_ci## fs.createRandomAccessFile<sup>10+</sup>
3653e41f4b71Sopenharmony_ci
3654e41f4b71Sopenharmony_cicreateRandomAccessFile(file: string | File, callback: AsyncCallback&lt;RandomAccessFile&gt;): void
3655e41f4b71Sopenharmony_ci
3656e41f4b71Sopenharmony_ci基于文件路径或文件对象,以只读方式创建RandomAccessFile文件对象,使用callback异步回调。
3657e41f4b71Sopenharmony_ci
3658e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3659e41f4b71Sopenharmony_ci
3660e41f4b71Sopenharmony_ci**参数:**
3661e41f4b71Sopenharmony_ci
3662e41f4b71Sopenharmony_ci|  参数名    | 类型     | 必填   | 说明                          |
3663e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ |
3664e41f4b71Sopenharmony_ci|     file     | string \| [File](#file) | 是    | 文件的应用沙箱路径或已打开的File对象 |
3665e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;[RandomAccessFile](#randomaccessfile)&gt; | 是   | 异步创建RandomAccessFile对象之后的回调。                                   |
3666e41f4b71Sopenharmony_ci
3667e41f4b71Sopenharmony_ci**错误码:**
3668e41f4b71Sopenharmony_ci
3669e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3670e41f4b71Sopenharmony_ci
3671e41f4b71Sopenharmony_ci**示例:**
3672e41f4b71Sopenharmony_ci  ```ts
3673e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3674e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3675e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
3676e41f4b71Sopenharmony_ci  fs.createRandomAccessFile(file, (err: BusinessError, randomAccessFile: fs.RandomAccessFile) => {
3677e41f4b71Sopenharmony_ci    if (err) {
3678e41f4b71Sopenharmony_ci      console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
3679e41f4b71Sopenharmony_ci    } else {
3680e41f4b71Sopenharmony_ci      console.info("randomAccessFile fd: " + randomAccessFile.fd);
3681e41f4b71Sopenharmony_ci      randomAccessFile.close();
3682e41f4b71Sopenharmony_ci    }
3683e41f4b71Sopenharmony_ci    fs.closeSync(file);
3684e41f4b71Sopenharmony_ci  });
3685e41f4b71Sopenharmony_ci  ```
3686e41f4b71Sopenharmony_ci
3687e41f4b71Sopenharmony_ci  ## fs.createRandomAccessFile<sup>10+</sup>
3688e41f4b71Sopenharmony_ci
3689e41f4b71Sopenharmony_cicreateRandomAccessFile(file: string | File, mode: number, callback: AsyncCallback&lt;RandomAccessFile&gt;): void
3690e41f4b71Sopenharmony_ci
3691e41f4b71Sopenharmony_ci基于文件路径或文件对象创建RandomAccessFile文件对象。使用callback异步回调。
3692e41f4b71Sopenharmony_ci
3693e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3694e41f4b71Sopenharmony_ci
3695e41f4b71Sopenharmony_ci**参数:**
3696e41f4b71Sopenharmony_ci
3697e41f4b71Sopenharmony_ci|  参数名    | 类型     | 必填   | 说明                          |
3698e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ |
3699e41f4b71Sopenharmony_ci|     file     | string \| [File](#file) | 是    | 文件的应用沙箱路径或已打开的File对象 |
3700e41f4b71Sopenharmony_ci|     mode     | number | 是   | 创建文件RandomAccessFile对象的[选项](#openmode),仅当传入文件沙箱路径时生效,必须指定如下选项中的一个,默认以只读方式创建:<br/>-&nbsp;OpenMode.READ_ONLY(0o0):只读创建。<br/>-&nbsp;OpenMode.WRITE_ONLY(0o1):只写创建。<br/>-&nbsp;OpenMode.READ_WRITE(0o2):读写创建。<br/>给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;OpenMode.CREATE(0o100):若文件不存在,则创建文件。<br/>-&nbsp;OpenMode.TRUNC(0o1000):如果RandomAccessFile对象存在且对应文件具有写权限,则将其长度裁剪为零。<br/>-&nbsp;OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到RandomAccessFile对象末尾。<br/>-&nbsp;OpenMode.NONBLOCK(0o4000):如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。<br/>-&nbsp;OpenMode.NOFOLLOW(0o400000):如果path指向符号链接,则出错。<br/>-&nbsp;OpenMode.SYNC(0o4010000):以同步IO的方式创建RandomAccessFile对象。 |
3701e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;[RandomAccessFile](#randomaccessfile)&gt; | 是   | 异步创建RandomAccessFile对象之后的回调。                                   |
3702e41f4b71Sopenharmony_ci
3703e41f4b71Sopenharmony_ci**错误码:**
3704e41f4b71Sopenharmony_ci
3705e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3706e41f4b71Sopenharmony_ci
3707e41f4b71Sopenharmony_ci**示例:**
3708e41f4b71Sopenharmony_ci  ```ts
3709e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3710e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3711e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
3712e41f4b71Sopenharmony_ci  fs.createRandomAccessFile(file, fs.OpenMode.READ_ONLY, (err: BusinessError, randomAccessFile: fs.RandomAccessFile) => {
3713e41f4b71Sopenharmony_ci    if (err) {
3714e41f4b71Sopenharmony_ci      console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
3715e41f4b71Sopenharmony_ci    } else {
3716e41f4b71Sopenharmony_ci      console.info("randomAccessFile fd: " + randomAccessFile.fd);
3717e41f4b71Sopenharmony_ci      randomAccessFile.close();
3718e41f4b71Sopenharmony_ci    }
3719e41f4b71Sopenharmony_ci    fs.closeSync(file);
3720e41f4b71Sopenharmony_ci  });
3721e41f4b71Sopenharmony_ci  ```
3722e41f4b71Sopenharmony_ci
3723e41f4b71Sopenharmony_ci## fs.createRandomAccessFile<sup>12+</sup>
3724e41f4b71Sopenharmony_ci
3725e41f4b71Sopenharmony_cicreateRandomAccessFile(file: string | File, mode?: number, options?: RandomAccessFileOptions): Promise&lt;RandomAccessFile&gt;
3726e41f4b71Sopenharmony_ci
3727e41f4b71Sopenharmony_ci基于文件路径或文件对象创建RandomAccessFile文件对象。使用Promise异步返回。
3728e41f4b71Sopenharmony_ci
3729e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3730e41f4b71Sopenharmony_ci
3731e41f4b71Sopenharmony_ci**参数:**
3732e41f4b71Sopenharmony_ci
3733e41f4b71Sopenharmony_ci|  参数名    | 类型     | 必填   | 说明                          |
3734e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ |
3735e41f4b71Sopenharmony_ci|     file     | string \| [File](#file) | 是    | 文件的应用沙箱路径或已打开的File对象 |
3736e41f4b71Sopenharmony_ci|     mode     | number | 否   | 创建文件RandomAccessFile对象的[选项](#openmode),仅当传入文件沙箱路径时生效,必须指定如下选项中的一个,默认以只读方式创建:<br/>-&nbsp;OpenMode.READ_ONLY(0o0):只读创建。<br/>-&nbsp;OpenMode.WRITE_ONLY(0o1):只写创建。<br/>-&nbsp;OpenMode.READ_WRITE(0o2):读写创建。<br/>给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;OpenMode.CREATE(0o100):若文件不存在,则创建文件。<br/>-&nbsp;OpenMode.TRUNC(0o1000):如果RandomAccessFile对象存在且对应文件具有写权限,则将其长度裁剪为零。<br/>-&nbsp;OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到RandomAccessFile对象末尾。<br/>-&nbsp;OpenMode.NONBLOCK(0o4000):如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。<br/>-&nbsp;OpenMode.NOFOLLOW(0o400000):如果path指向符号链接,则出错。<br/>-&nbsp;OpenMode.SYNC(0o4010000):以同步IO的方式创建RandomAccessFile对象。 |
3737e41f4b71Sopenharmony_ci|options|[RandomAccessFileOptions](#randomaccessfileoptions12)|否|支持如下选项:<br/>- start,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>- end,number类型,表示期望读取结束的位置。可选,默认文件末尾。|
3738e41f4b71Sopenharmony_ci
3739e41f4b71Sopenharmony_ci**返回值:**
3740e41f4b71Sopenharmony_ci
3741e41f4b71Sopenharmony_ci  | 类型                                | 说明        |
3742e41f4b71Sopenharmony_ci  | --------------------------------- | --------- |
3743e41f4b71Sopenharmony_ci  | Promise&lt;[RandomAccessFile](#randomaccessfile)&gt; | Promise对象。返回RandomAccessFile文件对象的结果。 |
3744e41f4b71Sopenharmony_ci
3745e41f4b71Sopenharmony_ci**错误码:**
3746e41f4b71Sopenharmony_ci
3747e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3748e41f4b71Sopenharmony_ci
3749e41f4b71Sopenharmony_ci```ts
3750e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
3751e41f4b71Sopenharmony_cilet filePath = pathDir + "/test.txt";
3752e41f4b71Sopenharmony_cifs.createRandomAccessFile(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE, { start: 10, end: 100 })
3753e41f4b71Sopenharmony_ci  .then((randomAccessFile: fs.RandomAccessFile) => {
3754e41f4b71Sopenharmony_ci    console.info("randomAccessFile fd: " + randomAccessFile.fd);
3755e41f4b71Sopenharmony_ci    randomAccessFile.close();
3756e41f4b71Sopenharmony_ci  })
3757e41f4b71Sopenharmony_ci  .catch((err: BusinessError) => {
3758e41f4b71Sopenharmony_ci    console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
3759e41f4b71Sopenharmony_ci  });
3760e41f4b71Sopenharmony_ci```
3761e41f4b71Sopenharmony_ci
3762e41f4b71Sopenharmony_ci
3763e41f4b71Sopenharmony_ci## fs.createRandomAccessFileSync<sup>10+</sup>
3764e41f4b71Sopenharmony_ci
3765e41f4b71Sopenharmony_cicreateRandomAccessFileSync(file: string | File, mode?: number): RandomAccessFile
3766e41f4b71Sopenharmony_ci
3767e41f4b71Sopenharmony_ci基于文件路径或文件对象创建RandomAccessFile文件对象。
3768e41f4b71Sopenharmony_ci
3769e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3770e41f4b71Sopenharmony_ci
3771e41f4b71Sopenharmony_ci**参数:**
3772e41f4b71Sopenharmony_ci
3773e41f4b71Sopenharmony_ci|  参数名    | 类型     | 必填   | 说明                          |
3774e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ |
3775e41f4b71Sopenharmony_ci|     file     | string \| [File](#file) | 是    | 文件的应用沙箱路径或已打开的File对象 |
3776e41f4b71Sopenharmony_ci|     mode     | number | 否   | 创建文件RandomAccessFile对象的[选项](#openmode),仅当传入文件沙箱路径时生效,必须指定如下选项中的一个,默认以只读方式创建:<br/>-&nbsp;OpenMode.READ_ONLY(0o0):只读创建。<br/>-&nbsp;OpenMode.WRITE_ONLY(0o1):只写创建。<br/>-&nbsp;OpenMode.READ_WRITE(0o2):读写创建。<br/>给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;OpenMode.CREATE(0o100):若文件不存在,则创建文件。<br/>-&nbsp;OpenMode.TRUNC(0o1000):如果RandomAccessFile对象存在且对应文件具有写权限,则将其长度裁剪为零。<br/>-&nbsp;OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到RandomAccessFile对象末尾。<br/>-&nbsp;OpenMode.NONBLOCK(0o4000):如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。<br/>-&nbsp;OpenMode.NOFOLLOW(0o400000):如果path指向符号链接,则出错。<br/>-&nbsp;OpenMode.SYNC(0o4010000):以同步IO的方式创建RandomAccessFile对象。 |
3777e41f4b71Sopenharmony_ci
3778e41f4b71Sopenharmony_ci**返回值:**
3779e41f4b71Sopenharmony_ci
3780e41f4b71Sopenharmony_ci  | 类型                | 说明        |
3781e41f4b71Sopenharmony_ci  | ------------------ | --------- |
3782e41f4b71Sopenharmony_ci  | [RandomAccessFile](#randomaccessfile) | 返回RandomAccessFile文件对象。 |
3783e41f4b71Sopenharmony_ci
3784e41f4b71Sopenharmony_ci**错误码:**
3785e41f4b71Sopenharmony_ci
3786e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3787e41f4b71Sopenharmony_ci
3788e41f4b71Sopenharmony_ci**示例:**
3789e41f4b71Sopenharmony_ci
3790e41f4b71Sopenharmony_ci  ```ts
3791e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3792e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
3793e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(file);
3794e41f4b71Sopenharmony_ci  randomAccessFile.close();
3795e41f4b71Sopenharmony_ci  ```
3796e41f4b71Sopenharmony_ci
3797e41f4b71Sopenharmony_ci## fs.createRandomAccessFileSync<sup>12+</sup>
3798e41f4b71Sopenharmony_ci
3799e41f4b71Sopenharmony_cicreateRandomAccessFileSync(file: string | File, mode?: number,
3800e41f4b71Sopenharmony_ci  options?: RandomAccessFileOptions): RandomAccessFile;
3801e41f4b71Sopenharmony_ci
3802e41f4b71Sopenharmony_ci基于文件路径或文件对象创建RandomAccessFile文件对象。
3803e41f4b71Sopenharmony_ci
3804e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3805e41f4b71Sopenharmony_ci
3806e41f4b71Sopenharmony_ci**参数:**
3807e41f4b71Sopenharmony_ci
3808e41f4b71Sopenharmony_ci|  参数名    | 类型     | 必填   | 说明                          |
3809e41f4b71Sopenharmony_ci| ------------ | ------ | ------ | ------------------------------------------------------------ |
3810e41f4b71Sopenharmony_ci|     file     | string \| [File](#file) | 是    | 文件的应用沙箱路径或已打开的File对象 |
3811e41f4b71Sopenharmony_ci|     mode     | number | 否   | 创建文件RandomAccessFile对象的[选项](#openmode),仅当传入文件沙箱路径时生效,必须指定如下选项中的一个,默认以只读方式创建:<br/>-&nbsp;OpenMode.READ_ONLY(0o0):只读创建。<br/>-&nbsp;OpenMode.WRITE_ONLY(0o1):只写创建。<br/>-&nbsp;OpenMode.READ_WRITE(0o2):读写创建。<br/>给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;OpenMode.CREATE(0o100):若文件不存在,则创建文件。<br/>-&nbsp;OpenMode.TRUNC(0o1000):如果RandomAccessFile对象存在且对应文件具有写权限,则将其长度裁剪为零。<br/>-&nbsp;OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到RandomAccessFile对象末尾。<br/>-&nbsp;OpenMode.NONBLOCK(0o4000):如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。<br/>-&nbsp;OpenMode.NOFOLLOW(0o400000):如果path指向符号链接,则出错。<br/>-&nbsp;OpenMode.SYNC(0o4010000):以同步IO的方式创建RandomAccessFile对象。 |
3812e41f4b71Sopenharmony_ci|options|[RandomAccessFileOptions](#randomaccessfileoptions12)|否|支持如下选项:<br/>- start,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>- end,number类型,表示期望读取结束的位置。可选,默认文件末尾。|
3813e41f4b71Sopenharmony_ci
3814e41f4b71Sopenharmony_ci**返回值:**
3815e41f4b71Sopenharmony_ci
3816e41f4b71Sopenharmony_ci  | 类型                | 说明        |
3817e41f4b71Sopenharmony_ci  | ------------------ | --------- |
3818e41f4b71Sopenharmony_ci  | [RandomAccessFile](#randomaccessfile) | 返回RandomAccessFile文件对象。 |
3819e41f4b71Sopenharmony_ci
3820e41f4b71Sopenharmony_ci**错误码:**
3821e41f4b71Sopenharmony_ci
3822e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3823e41f4b71Sopenharmony_ci
3824e41f4b71Sopenharmony_ci**示例:**
3825e41f4b71Sopenharmony_ci
3826e41f4b71Sopenharmony_ci  ```ts
3827e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3828e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE,
3829e41f4b71Sopenharmony_ci    { start: 10, end: 100 });
3830e41f4b71Sopenharmony_ci  randomAccessFile.close();
3831e41f4b71Sopenharmony_ci  ```
3832e41f4b71Sopenharmony_ci
3833e41f4b71Sopenharmony_ci## fs.createStream
3834e41f4b71Sopenharmony_ci
3835e41f4b71Sopenharmony_cicreateStream(path: string, mode: string): Promise&lt;Stream&gt;
3836e41f4b71Sopenharmony_ci
3837e41f4b71Sopenharmony_ci基于文件路径创建文件流,使用Promise异步返回。需要配合[Stream](#stream)中的close()函数关闭文件流。
3838e41f4b71Sopenharmony_ci
3839e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3840e41f4b71Sopenharmony_ci
3841e41f4b71Sopenharmony_ci**参数:**
3842e41f4b71Sopenharmony_ci
3843e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
3844e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
3845e41f4b71Sopenharmony_ci| path   | string | 是   | 文件的应用沙箱路径。                                   |
3846e41f4b71Sopenharmony_ci| mode   | string | 是   | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
3847e41f4b71Sopenharmony_ci
3848e41f4b71Sopenharmony_ci**返回值:**
3849e41f4b71Sopenharmony_ci
3850e41f4b71Sopenharmony_ci  | 类型                                | 说明        |
3851e41f4b71Sopenharmony_ci  | --------------------------------- | --------- |
3852e41f4b71Sopenharmony_ci  | Promise&lt;[Stream](#stream)&gt; | Promise对象。返回文件流的结果。 |
3853e41f4b71Sopenharmony_ci
3854e41f4b71Sopenharmony_ci**错误码:**
3855e41f4b71Sopenharmony_ci
3856e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3857e41f4b71Sopenharmony_ci
3858e41f4b71Sopenharmony_ci**示例:**
3859e41f4b71Sopenharmony_ci
3860e41f4b71Sopenharmony_ci  ```ts
3861e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3862e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3863e41f4b71Sopenharmony_ci  fs.createStream(filePath, "a+").then((stream: fs.Stream) => {
3864e41f4b71Sopenharmony_ci    stream.closeSync();
3865e41f4b71Sopenharmony_ci    console.info("createStream succeed");
3866e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3867e41f4b71Sopenharmony_ci    console.error("createStream failed with error message: " + err.message + ", error code: " + err.code);
3868e41f4b71Sopenharmony_ci  });
3869e41f4b71Sopenharmony_ci  ```
3870e41f4b71Sopenharmony_ci
3871e41f4b71Sopenharmony_ci
3872e41f4b71Sopenharmony_ci## fs.createStream
3873e41f4b71Sopenharmony_ci
3874e41f4b71Sopenharmony_cicreateStream(path: string, mode: string, callback: AsyncCallback&lt;Stream&gt;): void
3875e41f4b71Sopenharmony_ci
3876e41f4b71Sopenharmony_ci基于文件路径创建文件流,使用callback异步回调。需要配合[Stream](#stream)中的close()函数关闭文件流。
3877e41f4b71Sopenharmony_ci
3878e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3879e41f4b71Sopenharmony_ci
3880e41f4b71Sopenharmony_ci**参数:**
3881e41f4b71Sopenharmony_ci
3882e41f4b71Sopenharmony_ci| 参数名   | 类型                                    | 必填 | 说明                                                         |
3883e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
3884e41f4b71Sopenharmony_ci| path     | string                                  | 是   | 文件的应用沙箱路径。                                   |
3885e41f4b71Sopenharmony_ci| mode     | string                                  | 是   | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
3886e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;[Stream](#stream)&gt; | 是   | 异步打开文件流之后的回调。                                   |
3887e41f4b71Sopenharmony_ci
3888e41f4b71Sopenharmony_ci**错误码:**
3889e41f4b71Sopenharmony_ci
3890e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3891e41f4b71Sopenharmony_ci
3892e41f4b71Sopenharmony_ci**示例:**
3893e41f4b71Sopenharmony_ci
3894e41f4b71Sopenharmony_ci  ```ts
3895e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3896e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3897e41f4b71Sopenharmony_ci  fs.createStream(filePath, "r+", (err: BusinessError, stream: fs.Stream) => {
3898e41f4b71Sopenharmony_ci    if (err) {
3899e41f4b71Sopenharmony_ci      console.error("create stream failed with error message: " + err.message + ", error code: " + err.code);
3900e41f4b71Sopenharmony_ci    } else {
3901e41f4b71Sopenharmony_ci      console.info("createStream succeed");
3902e41f4b71Sopenharmony_ci    }
3903e41f4b71Sopenharmony_ci    stream.closeSync();
3904e41f4b71Sopenharmony_ci  })
3905e41f4b71Sopenharmony_ci  ```
3906e41f4b71Sopenharmony_ci
3907e41f4b71Sopenharmony_ci## fs.createStreamSync
3908e41f4b71Sopenharmony_ci
3909e41f4b71Sopenharmony_cicreateStreamSync(path: string, mode: string): Stream
3910e41f4b71Sopenharmony_ci
3911e41f4b71Sopenharmony_ci以同步方法基于文件路径创建文件流。需要配合[Stream](#stream)中的close()函数关闭文件流。
3912e41f4b71Sopenharmony_ci
3913e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3914e41f4b71Sopenharmony_ci
3915e41f4b71Sopenharmony_ci**参数:**
3916e41f4b71Sopenharmony_ci
3917e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                                                         |
3918e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ |
3919e41f4b71Sopenharmony_ci| path   | string | 是   | 文件的应用沙箱路径。                                   |
3920e41f4b71Sopenharmony_ci| mode   | string | 是   | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
3921e41f4b71Sopenharmony_ci
3922e41f4b71Sopenharmony_ci**返回值:**
3923e41f4b71Sopenharmony_ci
3924e41f4b71Sopenharmony_ci  | 类型                | 说明        |
3925e41f4b71Sopenharmony_ci  | ------------------ | --------- |
3926e41f4b71Sopenharmony_ci  | [Stream](#stream) | 返回文件流的结果。 |
3927e41f4b71Sopenharmony_ci
3928e41f4b71Sopenharmony_ci**错误码:**
3929e41f4b71Sopenharmony_ci
3930e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3931e41f4b71Sopenharmony_ci
3932e41f4b71Sopenharmony_ci**示例:**
3933e41f4b71Sopenharmony_ci
3934e41f4b71Sopenharmony_ci  ```ts
3935e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3936e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
3937e41f4b71Sopenharmony_ci  console.info("createStream succeed");
3938e41f4b71Sopenharmony_ci  stream.closeSync();
3939e41f4b71Sopenharmony_ci  ```
3940e41f4b71Sopenharmony_ci
3941e41f4b71Sopenharmony_ci
3942e41f4b71Sopenharmony_ci## fs.fdopenStream
3943e41f4b71Sopenharmony_ci
3944e41f4b71Sopenharmony_cifdopenStream(fd: number, mode: string): Promise&lt;Stream&gt;
3945e41f4b71Sopenharmony_ci
3946e41f4b71Sopenharmony_ci基于文件描述符打开文件流,使用Promise异步返回。需要配合[Stream](#stream)中的close()函数关闭文件流。
3947e41f4b71Sopenharmony_ci
3948e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3949e41f4b71Sopenharmony_ci
3950e41f4b71Sopenharmony_ci**参数:**
3951e41f4b71Sopenharmony_ci
3952e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明                                       |
3953e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ---------------------------------------- |
3954e41f4b71Sopenharmony_ci  | fd   | number | 是    | 已打开的文件描述符。                             |
3955e41f4b71Sopenharmony_ci  | mode | string | 是    | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
3956e41f4b71Sopenharmony_ci
3957e41f4b71Sopenharmony_ci**返回值:**
3958e41f4b71Sopenharmony_ci
3959e41f4b71Sopenharmony_ci  | 类型                               | 说明        |
3960e41f4b71Sopenharmony_ci  | --------------------------------- | --------- |
3961e41f4b71Sopenharmony_ci  | Promise&lt;[Stream](#stream)&gt; | Promise对象。返回文件流的结果。 |
3962e41f4b71Sopenharmony_ci
3963e41f4b71Sopenharmony_ci**错误码:**
3964e41f4b71Sopenharmony_ci
3965e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
3966e41f4b71Sopenharmony_ci
3967e41f4b71Sopenharmony_ci**示例:**
3968e41f4b71Sopenharmony_ci
3969e41f4b71Sopenharmony_ci  ```ts
3970e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
3971e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
3972e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath);
3973e41f4b71Sopenharmony_ci  fs.fdopenStream(file.fd, "r+").then((stream: fs.Stream) => {
3974e41f4b71Sopenharmony_ci    console.info("openStream succeed");
3975e41f4b71Sopenharmony_ci    stream.closeSync();
3976e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
3977e41f4b71Sopenharmony_ci    console.error("openStream failed with error message: " + err.message + ", error code: " + err.code);
3978e41f4b71Sopenharmony_ci    // 文件流打开失败后,文件描述符需要手动关闭
3979e41f4b71Sopenharmony_ci    fs.closeSync(file);
3980e41f4b71Sopenharmony_ci  });
3981e41f4b71Sopenharmony_ci  ```
3982e41f4b71Sopenharmony_ci
3983e41f4b71Sopenharmony_ci> **注意:**
3984e41f4b71Sopenharmony_ci>
3985e41f4b71Sopenharmony_ci> 使用文件描述符创建的文件流,文件描述符的生命周期也交由文件流对象,在调用文件流的close()函数后,初始的文件描述符也会被关闭。
3986e41f4b71Sopenharmony_ci
3987e41f4b71Sopenharmony_ci## fs.fdopenStream
3988e41f4b71Sopenharmony_ci
3989e41f4b71Sopenharmony_cifdopenStream(fd: number, mode: string, callback: AsyncCallback&lt;Stream&gt;): void
3990e41f4b71Sopenharmony_ci
3991e41f4b71Sopenharmony_ci基于文件描述符打开文件流,使用callback异步回调。需要配合[Stream](#stream)中的close()函数关闭文件流。
3992e41f4b71Sopenharmony_ci
3993e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
3994e41f4b71Sopenharmony_ci
3995e41f4b71Sopenharmony_ci**参数:**
3996e41f4b71Sopenharmony_ci
3997e41f4b71Sopenharmony_ci  | 参数名      | 类型                                       | 必填   | 说明                                       |
3998e41f4b71Sopenharmony_ci  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
3999e41f4b71Sopenharmony_ci  | fd       | number                                   | 是    | 已打开的文件描述符。                             |
4000e41f4b71Sopenharmony_ci  | mode     | string                                   | 是    | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
4001e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;[Stream](#stream)&gt; | 是    | 异步打开文件流之后的回调。                            |
4002e41f4b71Sopenharmony_ci
4003e41f4b71Sopenharmony_ci**错误码:**
4004e41f4b71Sopenharmony_ci
4005e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4006e41f4b71Sopenharmony_ci
4007e41f4b71Sopenharmony_ci**示例:**
4008e41f4b71Sopenharmony_ci
4009e41f4b71Sopenharmony_ci  ```ts
4010e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
4011e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4012e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
4013e41f4b71Sopenharmony_ci  fs.fdopenStream(file.fd, "r+", (err: BusinessError, stream: fs.Stream) => {
4014e41f4b71Sopenharmony_ci    if (err) {
4015e41f4b71Sopenharmony_ci      console.error("fdopen stream failed with error message: " + err.message + ", error code: " + err.code);
4016e41f4b71Sopenharmony_ci      stream.closeSync();
4017e41f4b71Sopenharmony_ci    } else {
4018e41f4b71Sopenharmony_ci      console.info("fdopen stream succeed");
4019e41f4b71Sopenharmony_ci      // 文件流打开失败后,文件描述符需要手动关闭
4020e41f4b71Sopenharmony_ci      fs.closeSync(file);
4021e41f4b71Sopenharmony_ci    }
4022e41f4b71Sopenharmony_ci  });
4023e41f4b71Sopenharmony_ci  ```
4024e41f4b71Sopenharmony_ci
4025e41f4b71Sopenharmony_ci> **注意:**
4026e41f4b71Sopenharmony_ci>
4027e41f4b71Sopenharmony_ci> 使用文件描述符创建的文件流,文件描述符的生命周期也交由文件流对象,在调用文件流的close()函数后,初始的文件描述符也会被关闭。
4028e41f4b71Sopenharmony_ci
4029e41f4b71Sopenharmony_ci## fs.fdopenStreamSync
4030e41f4b71Sopenharmony_ci
4031e41f4b71Sopenharmony_cifdopenStreamSync(fd: number, mode: string): Stream
4032e41f4b71Sopenharmony_ci
4033e41f4b71Sopenharmony_ci以同步方法基于文件描述符打开文件流。需要配合[Stream](#stream)中的close()函数关闭文件流。
4034e41f4b71Sopenharmony_ci
4035e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4036e41f4b71Sopenharmony_ci
4037e41f4b71Sopenharmony_ci**参数:**
4038e41f4b71Sopenharmony_ci
4039e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明                                       |
4040e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ---------------------------------------- |
4041e41f4b71Sopenharmony_ci  | fd   | number | 是    | 已打开的文件描述符。                             |
4042e41f4b71Sopenharmony_ci  | mode | string | 是    | -&nbsp;r:打开只读文件,该文件必须存在。<br/>-&nbsp;r+:打开可读写的文件,该文件必须存在。<br/>-&nbsp;w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。<br/>-&nbsp;a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。<br/>-&nbsp;a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 |
4043e41f4b71Sopenharmony_ci
4044e41f4b71Sopenharmony_ci**返回值:**
4045e41f4b71Sopenharmony_ci
4046e41f4b71Sopenharmony_ci  | 类型                | 说明        |
4047e41f4b71Sopenharmony_ci  | ------------------ | --------- |
4048e41f4b71Sopenharmony_ci  | [Stream](#stream) | 返回文件流的结果。 |
4049e41f4b71Sopenharmony_ci
4050e41f4b71Sopenharmony_ci**错误码:**
4051e41f4b71Sopenharmony_ci
4052e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4053e41f4b71Sopenharmony_ci
4054e41f4b71Sopenharmony_ci**示例:**
4055e41f4b71Sopenharmony_ci
4056e41f4b71Sopenharmony_ci  ```ts
4057e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4058e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY | fs.OpenMode.CREATE);
4059e41f4b71Sopenharmony_ci  let stream = fs.fdopenStreamSync(file.fd, "r+");
4060e41f4b71Sopenharmony_ci  stream.closeSync();
4061e41f4b71Sopenharmony_ci  ```
4062e41f4b71Sopenharmony_ci
4063e41f4b71Sopenharmony_ci> **注意:**
4064e41f4b71Sopenharmony_ci>
4065e41f4b71Sopenharmony_ci> 使用文件描述符创建的文件流,文件描述符的生命周期也交由文件流对象,在调用文件流的close()函数后,初始的文件描述符也会被关闭。
4066e41f4b71Sopenharmony_ci
4067e41f4b71Sopenharmony_ci## fs.createReadStream<sup>12+</sup>
4068e41f4b71Sopenharmony_ci
4069e41f4b71Sopenharmony_cicreateReadStream(path: string, options?: ReadStreamOptions ): ReadStream;
4070e41f4b71Sopenharmony_ci
4071e41f4b71Sopenharmony_ci以同步方法打开文件可读流。
4072e41f4b71Sopenharmony_ci
4073e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4074e41f4b71Sopenharmony_ci
4075e41f4b71Sopenharmony_ci**参数:**
4076e41f4b71Sopenharmony_ci
4077e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明                                       |
4078e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ---------------------------------------- |
4079e41f4b71Sopenharmony_ci  | path   | string | 是    | 文件路径                             |
4080e41f4b71Sopenharmony_ci  | options | [ReadStreamOptions](#readstreamoptions12) | 否    | 支持如下选项:<br/>- start,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>- end,number类型,表示期望读取结束的位置。可选,默认文件末尾。 |
4081e41f4b71Sopenharmony_ci
4082e41f4b71Sopenharmony_ci**返回值:**
4083e41f4b71Sopenharmony_ci
4084e41f4b71Sopenharmony_ci  | 类型                | 说明        |
4085e41f4b71Sopenharmony_ci  | ------------------ | --------- |
4086e41f4b71Sopenharmony_ci  | [ReadStream](#readstream12) | 文件可读流 |
4087e41f4b71Sopenharmony_ci
4088e41f4b71Sopenharmony_ci**错误码:**
4089e41f4b71Sopenharmony_ci
4090e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4091e41f4b71Sopenharmony_ci
4092e41f4b71Sopenharmony_ci**示例:**
4093e41f4b71Sopenharmony_ci
4094e41f4b71Sopenharmony_ci  ```ts
4095e41f4b71Sopenharmony_ci // 创建文件可读流
4096e41f4b71Sopenharmony_ci  const rs = fs.createReadStream(`${pathDir}/read.txt`);
4097e41f4b71Sopenharmony_ci  // 创建文件可写流
4098e41f4b71Sopenharmony_ci  const ws = fs.createWriteStream(`${pathDir}/write.txt`);
4099e41f4b71Sopenharmony_ci  // 暂停模式拷贝文件
4100e41f4b71Sopenharmony_ci  rs.on('readable', () => {
4101e41f4b71Sopenharmony_ci    const data = rs.read();
4102e41f4b71Sopenharmony_ci    if (!data) {
4103e41f4b71Sopenharmony_ci      return;
4104e41f4b71Sopenharmony_ci    }
4105e41f4b71Sopenharmony_ci    ws.write(data);
4106e41f4b71Sopenharmony_ci  });
4107e41f4b71Sopenharmony_ci  ```
4108e41f4b71Sopenharmony_ci
4109e41f4b71Sopenharmony_ci## fs.createWriteStream<sup>12+</sup>
4110e41f4b71Sopenharmony_ci
4111e41f4b71Sopenharmony_cicreateWriteStream(path: string, options?: WriteStreamOptions): WriteStream;
4112e41f4b71Sopenharmony_ci
4113e41f4b71Sopenharmony_ci以同步方法打开文件可写流。
4114e41f4b71Sopenharmony_ci
4115e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4116e41f4b71Sopenharmony_ci
4117e41f4b71Sopenharmony_ci**参数:**
4118e41f4b71Sopenharmony_ci
4119e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明                                       |
4120e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ---------------------------------------- |
4121e41f4b71Sopenharmony_ci  | path   | string | 是    | 文件路径                             |
4122e41f4b71Sopenharmony_ci  | options | [WriteStreamOptions](#writestreamoptions12) | 否    | 支持如下选项:<br/>- start,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>- mode,number 类型,创建文件可写流的[选项](#openmode),可选,默认以只写方式创建。 |
4123e41f4b71Sopenharmony_ci
4124e41f4b71Sopenharmony_ci**返回值:**
4125e41f4b71Sopenharmony_ci
4126e41f4b71Sopenharmony_ci  | 类型                | 说明        |
4127e41f4b71Sopenharmony_ci  | ------------------ | --------- |
4128e41f4b71Sopenharmony_ci  | [WriteStream](#writestream12) | 文件可写流 |
4129e41f4b71Sopenharmony_ci
4130e41f4b71Sopenharmony_ci**错误码:**
4131e41f4b71Sopenharmony_ci
4132e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4133e41f4b71Sopenharmony_ci
4134e41f4b71Sopenharmony_ci**示例:**
4135e41f4b71Sopenharmony_ci
4136e41f4b71Sopenharmony_ci  ```ts
4137e41f4b71Sopenharmony_ci // 创建文件可读流
4138e41f4b71Sopenharmony_ci  const rs = fs.createReadStream(`${pathDir}/read.txt`);
4139e41f4b71Sopenharmony_ci  // 创建文件可写流
4140e41f4b71Sopenharmony_ci  const ws = fs.createWriteStream(`${pathDir}/write.txt`);
4141e41f4b71Sopenharmony_ci  // 暂停模式拷贝文件
4142e41f4b71Sopenharmony_ci  rs.on('readable', () => {
4143e41f4b71Sopenharmony_ci    const data = rs.read();
4144e41f4b71Sopenharmony_ci    if (!data) {
4145e41f4b71Sopenharmony_ci      return;
4146e41f4b71Sopenharmony_ci    }
4147e41f4b71Sopenharmony_ci    ws.write(data);
4148e41f4b71Sopenharmony_ci  });
4149e41f4b71Sopenharmony_ci  ```
4150e41f4b71Sopenharmony_ci
4151e41f4b71Sopenharmony_ci## fs.createWatcher<sup>10+</sup>
4152e41f4b71Sopenharmony_ci
4153e41f4b71Sopenharmony_cicreateWatcher(path: string, events: number, listener: WatchEventListener): Watcher
4154e41f4b71Sopenharmony_ci
4155e41f4b71Sopenharmony_ci创建Watcher对象,用来监听文件或目录变动。
4156e41f4b71Sopenharmony_ci
4157e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4158e41f4b71Sopenharmony_ci
4159e41f4b71Sopenharmony_ci**参数:**
4160e41f4b71Sopenharmony_ci
4161e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明                                       |
4162e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ---------------------------------------- |
4163e41f4b71Sopenharmony_ci  | path   | string | 是    | 监听文件或目录的沙箱路径。                             |
4164e41f4b71Sopenharmony_ci  | events | number | 是    | 监听变动的事件集,多个事件通过或(\|)的方式进行集合。<br/>-&nbsp;0x1: IN_ACCESS, 文件被访问。<br/>-&nbsp;0x2: IN_MODIFY,文件内容被修改。<br/>-&nbsp;0x4: IN_ATTRIB,文件元数据被修改。<br/>-&nbsp;0x8: IN_CLOSE_WRITE,文件在打开时进行了写操作,然后被关闭。<br/>-&nbsp;0x10: IN_CLOSE_NOWRITE,文件或目录在打开时未进行写操作,然后被关闭。<br/>-&nbsp;0x20: IN_OPEN,文件或目录被打开。 <br/>-&nbsp;0x40: IN_MOVED_FROM,监听目录中文件被移动走。<br/>-&nbsp;0x80: IN_MOVED_TO,监听目录中文件被移动过来。<br/>-&nbsp;0x100: IN_CREATE,监听目录中文件或子目录被创建。<br/>-&nbsp;0x200: IN_DELETE,监听目录中文件或子目录被删除。<br/>-&nbsp;0x400: IN_DELETE_SELF,监听的目录被删除,删除后监听停止。<br/>-&nbsp;0x800: IN_MOVE_SELF,监听的文件或目录被移动,移动后监听继续。<br/>-&nbsp;0xfff: IN_ALL_EVENTS,监听以上所有事件。|
4165e41f4b71Sopenharmony_ci  | listener   | [WatchEventListener](#watcheventlistener10) | 是    | 监听事件发生后的回调。监听事件每发生一次,回调一次。                             |
4166e41f4b71Sopenharmony_ci
4167e41f4b71Sopenharmony_ci**返回值:**
4168e41f4b71Sopenharmony_ci
4169e41f4b71Sopenharmony_ci  | 类型                | 说明        |
4170e41f4b71Sopenharmony_ci  | ------------------ | --------- |
4171e41f4b71Sopenharmony_ci  | [Watcher](#watcher10) | 返回Watcher对象。 |
4172e41f4b71Sopenharmony_ci
4173e41f4b71Sopenharmony_ci**错误码:**
4174e41f4b71Sopenharmony_ci
4175e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4176e41f4b71Sopenharmony_ci
4177e41f4b71Sopenharmony_ci**示例:**
4178e41f4b71Sopenharmony_ci
4179e41f4b71Sopenharmony_ci  ```ts
4180e41f4b71Sopenharmony_ci  import { fileIo as fs, WatchEvent } from '@kit.CoreFileKit';
4181e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4182e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
4183e41f4b71Sopenharmony_ci  let watcher = fs.createWatcher(filePath, 0x2 | 0x10, (watchEvent: WatchEvent) => {
4184e41f4b71Sopenharmony_ci    if (watchEvent.event == 0x2) {
4185e41f4b71Sopenharmony_ci      console.info(watchEvent.fileName + 'was modified');
4186e41f4b71Sopenharmony_ci    } else if (watchEvent.event == 0x10) {
4187e41f4b71Sopenharmony_ci      console.info(watchEvent.fileName + 'was closed');
4188e41f4b71Sopenharmony_ci    }
4189e41f4b71Sopenharmony_ci  });
4190e41f4b71Sopenharmony_ci  watcher.start();
4191e41f4b71Sopenharmony_ci  fs.writeSync(file.fd, 'test');
4192e41f4b71Sopenharmony_ci  fs.closeSync(file);
4193e41f4b71Sopenharmony_ci  watcher.stop();
4194e41f4b71Sopenharmony_ci  ```
4195e41f4b71Sopenharmony_ci
4196e41f4b71Sopenharmony_ci## WatchEventListener<sup>10+</sup>
4197e41f4b71Sopenharmony_ci
4198e41f4b71Sopenharmony_ci(event: WatchEvent): void
4199e41f4b71Sopenharmony_ci
4200e41f4b71Sopenharmony_ci事件监听类。
4201e41f4b71Sopenharmony_ci
4202e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4203e41f4b71Sopenharmony_ci
4204e41f4b71Sopenharmony_ci**参数:**
4205e41f4b71Sopenharmony_ci
4206e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明                                       |
4207e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ---------------------------------------- |
4208e41f4b71Sopenharmony_ci  | event   | [WatchEvent](#watchevent10) | 是    | 回调的事件类。                             |
4209e41f4b71Sopenharmony_ci
4210e41f4b71Sopenharmony_ci## WatchEvent<sup>10+</sup>
4211e41f4b71Sopenharmony_ci
4212e41f4b71Sopenharmony_ci事件类
4213e41f4b71Sopenharmony_ci
4214e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4215e41f4b71Sopenharmony_ci
4216e41f4b71Sopenharmony_ci### 属性
4217e41f4b71Sopenharmony_ci
4218e41f4b71Sopenharmony_ci| 名称   | 类型   | 只读   | 可写   | 说明      |
4219e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------- |
4220e41f4b71Sopenharmony_ci| fileName | string | 是    | 否    | 发生监听事件的文件名。 |
4221e41f4b71Sopenharmony_ci| event | number | 是    | 否    | 监听变动的事件集,多个事件通过或(\|)的方式进行集合。<br/>-&nbsp;0x1: IN_ACCESS, 文件被访问。<br/>-&nbsp;0x2: IN_MODIFY,文件内容被修改。<br/>-&nbsp;0x4: IN_ATTRIB,文件元数据被修改。<br/>-&nbsp;0x8: IN_CLOSE_WRITE,文件在打开时进行了写操作,然后被关闭。<br/>-&nbsp;0x10: IN_CLOSE_NOWRITE,文件或目录在打开时未进行写操作,然后被关闭。<br/>-&nbsp;0x20: IN_OPEN,文件或目录被打开。 <br/>-&nbsp;0x40: IN_MOVED_FROM,监听目录中文件被移动走。<br/>-&nbsp;0x80: IN_MOVED_TO,监听目录中文件被移动过来。<br/>-&nbsp;0x100: IN_CREATE,监听目录中文件或子目录被创建。<br/>-&nbsp;0x200: IN_DELETE,监听目录中文件或子目录被删除。<br/>-&nbsp;0x400: IN_DELETE_SELF,监听的目录被删除,删除后监听停止。<br/>-&nbsp;0x800: IN_MOVE_SELF,监听的文件或目录被移动,移动后监听继续。<br/>-&nbsp;0xfff: IN_ALL_EVENTS,监听以上所有事件。 |
4222e41f4b71Sopenharmony_ci| cookie | number | 是    | 否    | 绑定相关事件的cookie。当前仅支持事件IN_MOVED_FROM与IN_MOVED_TO,同一个文件的移动事件IN_MOVED_FROM和IN_MOVED_TO具有相同的cookie值。 |
4223e41f4b71Sopenharmony_ci
4224e41f4b71Sopenharmony_ci## Progress<sup>11+</sup>
4225e41f4b71Sopenharmony_ci
4226e41f4b71Sopenharmony_ci拷贝进度回调数据
4227e41f4b71Sopenharmony_ci
4228e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4229e41f4b71Sopenharmony_ci
4230e41f4b71Sopenharmony_ci| 名称   | 类型   | 只读   | 可写   | 说明      |
4231e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------- |
4232e41f4b71Sopenharmony_ci| processedSize | number | 是    | 否    | 已拷贝的数据大小。 |
4233e41f4b71Sopenharmony_ci| totalSize | number | 是    | 否    | 待拷贝的数据总大小。 |
4234e41f4b71Sopenharmony_ci
4235e41f4b71Sopenharmony_ci## TaskSignal<sup>12+</sup>
4236e41f4b71Sopenharmony_ci
4237e41f4b71Sopenharmony_ci拷贝中断信号。
4238e41f4b71Sopenharmony_ci
4239e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4240e41f4b71Sopenharmony_ci
4241e41f4b71Sopenharmony_ci### cancel<sup>12+</sup>
4242e41f4b71Sopenharmony_ci
4243e41f4b71Sopenharmony_cicancel(): void
4244e41f4b71Sopenharmony_ci
4245e41f4b71Sopenharmony_ci取消拷贝任务。
4246e41f4b71Sopenharmony_ci
4247e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4248e41f4b71Sopenharmony_ci
4249e41f4b71Sopenharmony_ci**错误码:**
4250e41f4b71Sopenharmony_ci
4251e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4252e41f4b71Sopenharmony_ci
4253e41f4b71Sopenharmony_ci**示例:**
4254e41f4b71Sopenharmony_ci
4255e41f4b71Sopenharmony_ci```ts
4256e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
4257e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit';
4258e41f4b71Sopenharmony_ciimport { fileUri } from '@kit.CoreFileKit';
4259e41f4b71Sopenharmony_ciimport common from '@ohos.app.ability.common';
4260e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext;
4261e41f4b71Sopenharmony_cilet pathDir: string = context.filesDir;
4262e41f4b71Sopenharmony_cilet srcDirPathLocal: string = pathDir + "/src";
4263e41f4b71Sopenharmony_cilet dstDirPathLocal: string = pathDir + "/dest";
4264e41f4b71Sopenharmony_cilet srcDirUriLocal: string = fileUri.getUriFromPath(srcDirPathLocal);
4265e41f4b71Sopenharmony_cilet dstDirUriLocal: string = fileUri.getUriFromPath(dstDirPathLocal);
4266e41f4b71Sopenharmony_cilet copySignal = new fs.TaskSignal;
4267e41f4b71Sopenharmony_cilet progressListener: fs.ProgressListener = (progress: fs.Progress) => {
4268e41f4b71Sopenharmony_ci  console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`);
4269e41f4b71Sopenharmony_ci  if (progress.processedSize / progress.totalSize > 0.5) {
4270e41f4b71Sopenharmony_ci    copySignal.cancel();
4271e41f4b71Sopenharmony_ci  }
4272e41f4b71Sopenharmony_ci};
4273e41f4b71Sopenharmony_cilet options: fs.CopyOptions = {
4274e41f4b71Sopenharmony_ci  "progressListener" : progressListener,
4275e41f4b71Sopenharmony_ci  "copySignal" : new fs.TaskSignal,
4276e41f4b71Sopenharmony_ci}
4277e41f4b71Sopenharmony_ciconsole.info("copyFileWithCancel success.");
4278e41f4b71Sopenharmony_citry {
4279e41f4b71Sopenharmony_ci  fs.copy(srcDirPathLocal, dstDirUriLocal, options, (err: BusinessError) => {
4280e41f4b71Sopenharmony_ci    if (err) {
4281e41f4b71Sopenharmony_ci      console.info("copyFileWithCancel fail.");
4282e41f4b71Sopenharmony_ci      return;
4283e41f4b71Sopenharmony_ci    }
4284e41f4b71Sopenharmony_ci    console.info("copyFileWithCancel success.");
4285e41f4b71Sopenharmony_ci  })
4286e41f4b71Sopenharmony_ci} catch (err) {
4287e41f4b71Sopenharmony_ci  console.error("copyFileWithCancel failed with invalid param.");
4288e41f4b71Sopenharmony_ci}
4289e41f4b71Sopenharmony_ci
4290e41f4b71Sopenharmony_ci```
4291e41f4b71Sopenharmony_ci
4292e41f4b71Sopenharmony_ci### onCancel<sup>12+</sup>
4293e41f4b71Sopenharmony_ci
4294e41f4b71Sopenharmony_cionCancel(): Promise&lt;string&gt;
4295e41f4b71Sopenharmony_ci
4296e41f4b71Sopenharmony_ci取消拷贝事件监听。
4297e41f4b71Sopenharmony_ci
4298e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4299e41f4b71Sopenharmony_ci
4300e41f4b71Sopenharmony_ci**返回值:**
4301e41f4b71Sopenharmony_ci
4302e41f4b71Sopenharmony_ci  | 类型                   | 说明         |
4303e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
4304e41f4b71Sopenharmony_ci  | Promise&lt;string&gt; | Promise对象。最后一个拷贝的文件路径。 |
4305e41f4b71Sopenharmony_ci
4306e41f4b71Sopenharmony_ci**错误码:**
4307e41f4b71Sopenharmony_ci
4308e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4309e41f4b71Sopenharmony_ci
4310e41f4b71Sopenharmony_ci**示例:**
4311e41f4b71Sopenharmony_ci
4312e41f4b71Sopenharmony_ci```ts
4313e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit';
4314e41f4b71Sopenharmony_ciimport { TaskSignal } from '@ohos.file.fs';
4315e41f4b71Sopenharmony_cilet copySignal: fs.TaskSignal = new TaskSignal();
4316e41f4b71Sopenharmony_cicopySignal.onCancel().then(() => {
4317e41f4b71Sopenharmony_ci    console.info("copyFileWithCancel success.");
4318e41f4b71Sopenharmony_ci});
4319e41f4b71Sopenharmony_ci```
4320e41f4b71Sopenharmony_ci
4321e41f4b71Sopenharmony_ci## CopyOptions<sup>11+</sup>
4322e41f4b71Sopenharmony_ci
4323e41f4b71Sopenharmony_ci拷贝进度回调监听
4324e41f4b71Sopenharmony_ci
4325e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4326e41f4b71Sopenharmony_ci
4327e41f4b71Sopenharmony_ci| 名称   | 类型   | 可读   | 可写   | 说明      |
4328e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------- |
4329e41f4b71Sopenharmony_ci| progressListener | [ProgressListener](#progresslistener11) | 是    | 是    | 拷贝进度监听。 |
4330e41f4b71Sopenharmony_ci| copySignal | [TaskSignal](#tasksignal12) | 是    | 是    | 取消拷贝信号。 |
4331e41f4b71Sopenharmony_ci
4332e41f4b71Sopenharmony_ci## ProgressListener<sup>11+</sup>
4333e41f4b71Sopenharmony_ci
4334e41f4b71Sopenharmony_ci拷贝进度监听。
4335e41f4b71Sopenharmony_ci
4336e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4337e41f4b71Sopenharmony_ci
4338e41f4b71Sopenharmony_ci| 类型 | 说明 |
4339e41f4b71Sopenharmony_ci| ----| ------|
4340e41f4b71Sopenharmony_ci|(progress: [Progress](#progress11)) => void| 拷贝进度监听|
4341e41f4b71Sopenharmony_ci
4342e41f4b71Sopenharmony_ci**示例:**
4343e41f4b71Sopenharmony_ci
4344e41f4b71Sopenharmony_ci  ```ts
4345e41f4b71Sopenharmony_ci  let copySignal: fs.TaskSignal = new TaskSignal();
4346e41f4b71Sopenharmony_ci  let progressListener: fs.ProgressListener = (progress: fs.Progress) => {
4347e41f4b71Sopenharmony_ci    console.info(`processedSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`);
4348e41f4b71Sopenharmony_ci  };
4349e41f4b71Sopenharmony_ci  let copyOption: fs.CopyOptions = {
4350e41f4b71Sopenharmony_ci    "progressListener" : progressListener,
4351e41f4b71Sopenharmony_ci    "copySignal" : copySignal,
4352e41f4b71Sopenharmony_ci  }
4353e41f4b71Sopenharmony_ci  ```
4354e41f4b71Sopenharmony_ci
4355e41f4b71Sopenharmony_ci## Stat
4356e41f4b71Sopenharmony_ci
4357e41f4b71Sopenharmony_ci文件具体信息,在调用Stat的方法前,需要先通过[stat()](#fsstat)方法(同步或异步)来构建一个Stat实例。
4358e41f4b71Sopenharmony_ci
4359e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4360e41f4b71Sopenharmony_ci
4361e41f4b71Sopenharmony_ci### 属性
4362e41f4b71Sopenharmony_ci
4363e41f4b71Sopenharmony_ci| 名称     | 类型   | 只读   | 可写   | 说明                                       |
4364e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ---------------------------------------- |
4365e41f4b71Sopenharmony_ci| ino    | bigint | 是    | 否    | 标识该文件。通常同设备上的不同文件的INO不同。|                 |
4366e41f4b71Sopenharmony_ci| mode   | number | 是    | 否    | 表示文件权限,各特征位的含义如下:<br/>**说明:** 以下值为八进制,取得的返回值为十进制,请换算后查看。<br/>-&nbsp;0o400:用户读,对于普通文件,所有者可读取文件;对于目录,所有者可读取目录项。<br/>-&nbsp;0o200:用户写,对于普通文件,所有者可写入文件;对于目录,所有者可创建/删除目录项。<br/>-&nbsp;0o100:用户执行,对于普通文件,所有者可执行文件;对于目录,所有者可在目录中搜索给定路径名。<br/>-&nbsp;0o040:用户组读,对于普通文件,所有用户组可读取文件;对于目录,所有用户组可读取目录项。<br/>-&nbsp;0o020:用户组写,对于普通文件,所有用户组可写入文件;对于目录,所有用户组可创建/删除目录项。<br/>-&nbsp;0o010:用户组执行,对于普通文件,所有用户组可执行文件;对于目录,所有用户组是否可在目录中搜索给定路径名。<br/>-&nbsp;0o004:其他读,对于普通文件,其余用户可读取文件;对于目录,其他用户组可读取目录项。<br/>-&nbsp;0o002:其他写,对于普通文件,其余用户可写入文件;对于目录,其他用户组可创建/删除目录项。<br/>-&nbsp;0o001:其他执行,对于普通文件,其余用户可执行文件;对于目录,其他用户组可在目录中搜索给定路径名。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
4367e41f4b71Sopenharmony_ci| uid    | number | 是    | 否    | 文件所有者的ID。|
4368e41f4b71Sopenharmony_ci| gid    | number | 是    | 否    | 文件所有组的ID。|
4369e41f4b71Sopenharmony_ci| size   | number | 是    | 否    | 文件的大小,以字节为单位。仅对普通文件有效。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
4370e41f4b71Sopenharmony_ci| atime  | number | 是    | 否    | 上次访问该文件的时间,表示距1970年1月1日0时0分0秒的秒数。  <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。      |
4371e41f4b71Sopenharmony_ci| mtime  | number | 是    | 否    | 上次修改该文件的时间,表示距1970年1月1日0时0分0秒的秒数。  <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。      |
4372e41f4b71Sopenharmony_ci| ctime  | number | 是    | 否    | 最近改变文件状态的时间,表示距1970年1月1日0时0分0秒的秒数。      |
4373e41f4b71Sopenharmony_ci| location<sup>11+</sup> | [LocaltionType](#locationtype11)| 是 |否| 文件的位置,表示该文件是本地文件或者云端文件。
4374e41f4b71Sopenharmony_ci
4375e41f4b71Sopenharmony_ci### isBlockDevice
4376e41f4b71Sopenharmony_ci
4377e41f4b71Sopenharmony_ciisBlockDevice(): boolean
4378e41f4b71Sopenharmony_ci
4379e41f4b71Sopenharmony_ci用于判断文件是否是块特殊文件。一个块特殊文件只能以块为粒度进行访问,且访问的时候带缓存。
4380e41f4b71Sopenharmony_ci
4381e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4382e41f4b71Sopenharmony_ci
4383e41f4b71Sopenharmony_ci**返回值:**
4384e41f4b71Sopenharmony_ci
4385e41f4b71Sopenharmony_ci  | 类型     | 说明               |
4386e41f4b71Sopenharmony_ci  | ------- | ---------------- |
4387e41f4b71Sopenharmony_ci  | boolean | 表示文件是否是块特殊设备。 |
4388e41f4b71Sopenharmony_ci
4389e41f4b71Sopenharmony_ci**错误码:**
4390e41f4b71Sopenharmony_ci
4391e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4392e41f4b71Sopenharmony_ci
4393e41f4b71Sopenharmony_ci**示例:**
4394e41f4b71Sopenharmony_ci
4395e41f4b71Sopenharmony_ci  ```ts
4396e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4397e41f4b71Sopenharmony_ci  let isBLockDevice = fs.statSync(filePath).isBlockDevice();
4398e41f4b71Sopenharmony_ci  ```
4399e41f4b71Sopenharmony_ci
4400e41f4b71Sopenharmony_ci### isCharacterDevice
4401e41f4b71Sopenharmony_ci
4402e41f4b71Sopenharmony_ciisCharacterDevice(): boolean
4403e41f4b71Sopenharmony_ci
4404e41f4b71Sopenharmony_ci用于判断文件是否是字符特殊文件。一个字符特殊设备可进行随机访问,且访问的时候不带缓存。
4405e41f4b71Sopenharmony_ci
4406e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4407e41f4b71Sopenharmony_ci
4408e41f4b71Sopenharmony_ci**返回值:**
4409e41f4b71Sopenharmony_ci
4410e41f4b71Sopenharmony_ci  | 类型      | 说明                |
4411e41f4b71Sopenharmony_ci  | ------- | ----------------- |
4412e41f4b71Sopenharmony_ci  | boolean | 表示文件是否是字符特殊设备。 |
4413e41f4b71Sopenharmony_ci
4414e41f4b71Sopenharmony_ci**错误码:**
4415e41f4b71Sopenharmony_ci
4416e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4417e41f4b71Sopenharmony_ci
4418e41f4b71Sopenharmony_ci**示例:**
4419e41f4b71Sopenharmony_ci
4420e41f4b71Sopenharmony_ci  ```ts
4421e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4422e41f4b71Sopenharmony_ci  let isCharacterDevice = fs.statSync(filePath).isCharacterDevice();
4423e41f4b71Sopenharmony_ci  ```
4424e41f4b71Sopenharmony_ci
4425e41f4b71Sopenharmony_ci### isDirectory
4426e41f4b71Sopenharmony_ci
4427e41f4b71Sopenharmony_ciisDirectory(): boolean
4428e41f4b71Sopenharmony_ci
4429e41f4b71Sopenharmony_ci用于判断文件是否是目录。
4430e41f4b71Sopenharmony_ci
4431e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
4432e41f4b71Sopenharmony_ci
4433e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4434e41f4b71Sopenharmony_ci
4435e41f4b71Sopenharmony_ci**返回值:**
4436e41f4b71Sopenharmony_ci
4437e41f4b71Sopenharmony_ci  | 类型      | 说明            |
4438e41f4b71Sopenharmony_ci  | ------- | ------------- |
4439e41f4b71Sopenharmony_ci  | boolean | 表示文件是否是目录。 |
4440e41f4b71Sopenharmony_ci
4441e41f4b71Sopenharmony_ci**错误码:**
4442e41f4b71Sopenharmony_ci
4443e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4444e41f4b71Sopenharmony_ci
4445e41f4b71Sopenharmony_ci**示例:**
4446e41f4b71Sopenharmony_ci
4447e41f4b71Sopenharmony_ci  ```ts
4448e41f4b71Sopenharmony_ci  let dirPath = pathDir + "/test";
4449e41f4b71Sopenharmony_ci  let isDirectory = fs.statSync(dirPath).isDirectory();
4450e41f4b71Sopenharmony_ci  ```
4451e41f4b71Sopenharmony_ci
4452e41f4b71Sopenharmony_ci### isFIFO
4453e41f4b71Sopenharmony_ci
4454e41f4b71Sopenharmony_ciisFIFO(): boolean
4455e41f4b71Sopenharmony_ci
4456e41f4b71Sopenharmony_ci用于判断文件是否是命名管道(有时也称为FIFO)。命名管道通常用于进程间通信。
4457e41f4b71Sopenharmony_ci
4458e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4459e41f4b71Sopenharmony_ci
4460e41f4b71Sopenharmony_ci**返回值:**
4461e41f4b71Sopenharmony_ci
4462e41f4b71Sopenharmony_ci  | 类型      | 说明                    |
4463e41f4b71Sopenharmony_ci  | ------- | --------------------- |
4464e41f4b71Sopenharmony_ci  | boolean | 表示文件是否是&nbsp;FIFO。 |
4465e41f4b71Sopenharmony_ci
4466e41f4b71Sopenharmony_ci**错误码:**
4467e41f4b71Sopenharmony_ci
4468e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4469e41f4b71Sopenharmony_ci
4470e41f4b71Sopenharmony_ci**示例:**
4471e41f4b71Sopenharmony_ci
4472e41f4b71Sopenharmony_ci  ```ts
4473e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4474e41f4b71Sopenharmony_ci  let isFIFO = fs.statSync(filePath).isFIFO();
4475e41f4b71Sopenharmony_ci  ```
4476e41f4b71Sopenharmony_ci
4477e41f4b71Sopenharmony_ci### isFile
4478e41f4b71Sopenharmony_ci
4479e41f4b71Sopenharmony_ciisFile(): boolean
4480e41f4b71Sopenharmony_ci
4481e41f4b71Sopenharmony_ci用于判断文件是否是普通文件。
4482e41f4b71Sopenharmony_ci
4483e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
4484e41f4b71Sopenharmony_ci
4485e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4486e41f4b71Sopenharmony_ci
4487e41f4b71Sopenharmony_ci**返回值:**
4488e41f4b71Sopenharmony_ci
4489e41f4b71Sopenharmony_ci  | 类型      | 说明              |
4490e41f4b71Sopenharmony_ci  | ------- | --------------- |
4491e41f4b71Sopenharmony_ci  | boolean | 表示文件是否是普通文件。 |
4492e41f4b71Sopenharmony_ci
4493e41f4b71Sopenharmony_ci**错误码:**
4494e41f4b71Sopenharmony_ci
4495e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4496e41f4b71Sopenharmony_ci
4497e41f4b71Sopenharmony_ci**示例:**
4498e41f4b71Sopenharmony_ci
4499e41f4b71Sopenharmony_ci  ```ts
4500e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4501e41f4b71Sopenharmony_ci  let isFile = fs.statSync(filePath).isFile();
4502e41f4b71Sopenharmony_ci  ```
4503e41f4b71Sopenharmony_ci
4504e41f4b71Sopenharmony_ci### isSocket
4505e41f4b71Sopenharmony_ci
4506e41f4b71Sopenharmony_ciisSocket(): boolean
4507e41f4b71Sopenharmony_ci
4508e41f4b71Sopenharmony_ci用于判断文件是否是套接字。
4509e41f4b71Sopenharmony_ci
4510e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4511e41f4b71Sopenharmony_ci
4512e41f4b71Sopenharmony_ci**返回值:**
4513e41f4b71Sopenharmony_ci
4514e41f4b71Sopenharmony_ci  | 类型      | 说明             |
4515e41f4b71Sopenharmony_ci  | ------- | -------------- |
4516e41f4b71Sopenharmony_ci  | boolean | 表示文件是否是套接字。 |
4517e41f4b71Sopenharmony_ci
4518e41f4b71Sopenharmony_ci**错误码:**
4519e41f4b71Sopenharmony_ci
4520e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4521e41f4b71Sopenharmony_ci
4522e41f4b71Sopenharmony_ci**示例:**
4523e41f4b71Sopenharmony_ci
4524e41f4b71Sopenharmony_ci  ```ts
4525e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4526e41f4b71Sopenharmony_ci  let isSocket = fs.statSync(filePath).isSocket();
4527e41f4b71Sopenharmony_ci  ```
4528e41f4b71Sopenharmony_ci
4529e41f4b71Sopenharmony_ci### isSymbolicLink
4530e41f4b71Sopenharmony_ci
4531e41f4b71Sopenharmony_ciisSymbolicLink(): boolean
4532e41f4b71Sopenharmony_ci
4533e41f4b71Sopenharmony_ci用于判断文件是否是符号链接。
4534e41f4b71Sopenharmony_ci
4535e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4536e41f4b71Sopenharmony_ci
4537e41f4b71Sopenharmony_ci**返回值:**
4538e41f4b71Sopenharmony_ci
4539e41f4b71Sopenharmony_ci  | 类型      | 说明              |
4540e41f4b71Sopenharmony_ci  | ------- | --------------- |
4541e41f4b71Sopenharmony_ci  | boolean | 表示文件是否是符号链接。 |
4542e41f4b71Sopenharmony_ci
4543e41f4b71Sopenharmony_ci**错误码:**
4544e41f4b71Sopenharmony_ci
4545e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4546e41f4b71Sopenharmony_ci
4547e41f4b71Sopenharmony_ci**示例:**
4548e41f4b71Sopenharmony_ci
4549e41f4b71Sopenharmony_ci  ```ts
4550e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test";
4551e41f4b71Sopenharmony_ci  let isSymbolicLink = fs.statSync(filePath).isSymbolicLink();
4552e41f4b71Sopenharmony_ci  ```
4553e41f4b71Sopenharmony_ci
4554e41f4b71Sopenharmony_ci## Stream
4555e41f4b71Sopenharmony_ci
4556e41f4b71Sopenharmony_ci文件流,在调用Stream的方法前,需要先通过[fs.createStream](#fscreatestream)方法或者[fs.fdopenStream](#fsfdopenstream)(同步或异步)来构建一个Stream实例。
4557e41f4b71Sopenharmony_ci
4558e41f4b71Sopenharmony_ci### close
4559e41f4b71Sopenharmony_ci
4560e41f4b71Sopenharmony_ciclose(): Promise&lt;void&gt;
4561e41f4b71Sopenharmony_ci
4562e41f4b71Sopenharmony_ci关闭文件流,使用Promise异步返回。
4563e41f4b71Sopenharmony_ci
4564e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4565e41f4b71Sopenharmony_ci
4566e41f4b71Sopenharmony_ci**返回值:**
4567e41f4b71Sopenharmony_ci
4568e41f4b71Sopenharmony_ci  | 类型                  | 说明            |
4569e41f4b71Sopenharmony_ci  | ------------------- | ------------- |
4570e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
4571e41f4b71Sopenharmony_ci
4572e41f4b71Sopenharmony_ci**错误码:**
4573e41f4b71Sopenharmony_ci
4574e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4575e41f4b71Sopenharmony_ci
4576e41f4b71Sopenharmony_ci**示例:**
4577e41f4b71Sopenharmony_ci
4578e41f4b71Sopenharmony_ci  ```ts
4579e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
4580e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4581e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4582e41f4b71Sopenharmony_ci  stream.close().then(() => {
4583e41f4b71Sopenharmony_ci    console.info("close fileStream succeed");
4584e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
4585e41f4b71Sopenharmony_ci    console.error("close fileStream  failed with error message: " + err.message + ", error code: " + err.code);
4586e41f4b71Sopenharmony_ci  });
4587e41f4b71Sopenharmony_ci  ```
4588e41f4b71Sopenharmony_ci
4589e41f4b71Sopenharmony_ci### close
4590e41f4b71Sopenharmony_ci
4591e41f4b71Sopenharmony_ciclose(callback: AsyncCallback&lt;void&gt;): void
4592e41f4b71Sopenharmony_ci
4593e41f4b71Sopenharmony_ci异步关闭文件流,使用callback异步回调。
4594e41f4b71Sopenharmony_ci
4595e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4596e41f4b71Sopenharmony_ci
4597e41f4b71Sopenharmony_ci**参数:**
4598e41f4b71Sopenharmony_ci
4599e41f4b71Sopenharmony_ci  | 参数名      | 类型                        | 必填   | 说明            |
4600e41f4b71Sopenharmony_ci  | -------- | ------------------------- | ---- | ------------- |
4601e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void&gt; | 是    | 异步关闭文件流之后的回调。 |
4602e41f4b71Sopenharmony_ci
4603e41f4b71Sopenharmony_ci**错误码:**
4604e41f4b71Sopenharmony_ci
4605e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4606e41f4b71Sopenharmony_ci
4607e41f4b71Sopenharmony_ci**示例:**
4608e41f4b71Sopenharmony_ci
4609e41f4b71Sopenharmony_ci  ```ts
4610e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
4611e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4612e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4613e41f4b71Sopenharmony_ci  stream.close((err: BusinessError) => {
4614e41f4b71Sopenharmony_ci    if (err) {
4615e41f4b71Sopenharmony_ci      console.error("close stream failed with error message: " + err.message + ", error code: " + err.code);
4616e41f4b71Sopenharmony_ci    } else {
4617e41f4b71Sopenharmony_ci      console.info("close stream succeed");
4618e41f4b71Sopenharmony_ci    }
4619e41f4b71Sopenharmony_ci  });
4620e41f4b71Sopenharmony_ci  ```
4621e41f4b71Sopenharmony_ci
4622e41f4b71Sopenharmony_ci### closeSync
4623e41f4b71Sopenharmony_ci
4624e41f4b71Sopenharmony_cicloseSync(): void
4625e41f4b71Sopenharmony_ci
4626e41f4b71Sopenharmony_ci同步关闭文件流。
4627e41f4b71Sopenharmony_ci
4628e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4629e41f4b71Sopenharmony_ci
4630e41f4b71Sopenharmony_ci**错误码:**
4631e41f4b71Sopenharmony_ci
4632e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4633e41f4b71Sopenharmony_ci
4634e41f4b71Sopenharmony_ci**示例:**
4635e41f4b71Sopenharmony_ci
4636e41f4b71Sopenharmony_ci  ```ts
4637e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4638e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4639e41f4b71Sopenharmony_ci  stream.closeSync();
4640e41f4b71Sopenharmony_ci  ```
4641e41f4b71Sopenharmony_ci
4642e41f4b71Sopenharmony_ci### flush
4643e41f4b71Sopenharmony_ci
4644e41f4b71Sopenharmony_ciflush(): Promise&lt;void&gt;
4645e41f4b71Sopenharmony_ci
4646e41f4b71Sopenharmony_ci刷新文件流,使用Promise异步返回。
4647e41f4b71Sopenharmony_ci
4648e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4649e41f4b71Sopenharmony_ci
4650e41f4b71Sopenharmony_ci**返回值:**
4651e41f4b71Sopenharmony_ci
4652e41f4b71Sopenharmony_ci  | 类型                  | 说明            |
4653e41f4b71Sopenharmony_ci  | ------------------- | ------------- |
4654e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。返回表示异步刷新文件流的结果。 |
4655e41f4b71Sopenharmony_ci
4656e41f4b71Sopenharmony_ci**错误码:**
4657e41f4b71Sopenharmony_ci
4658e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4659e41f4b71Sopenharmony_ci
4660e41f4b71Sopenharmony_ci**示例:**
4661e41f4b71Sopenharmony_ci
4662e41f4b71Sopenharmony_ci  ```ts
4663e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
4664e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4665e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4666e41f4b71Sopenharmony_ci  stream.flush().then(() => {
4667e41f4b71Sopenharmony_ci    console.info("flush succeed");
4668e41f4b71Sopenharmony_ci    stream.close();
4669e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
4670e41f4b71Sopenharmony_ci    console.error("flush failed with error message: " + err.message + ", error code: " + err.code);
4671e41f4b71Sopenharmony_ci  });
4672e41f4b71Sopenharmony_ci  ```
4673e41f4b71Sopenharmony_ci
4674e41f4b71Sopenharmony_ci### flush
4675e41f4b71Sopenharmony_ci
4676e41f4b71Sopenharmony_ciflush(callback: AsyncCallback&lt;void&gt;): void
4677e41f4b71Sopenharmony_ci
4678e41f4b71Sopenharmony_ci异步刷新文件流,使用callback异步回调。
4679e41f4b71Sopenharmony_ci
4680e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4681e41f4b71Sopenharmony_ci
4682e41f4b71Sopenharmony_ci**参数:**
4683e41f4b71Sopenharmony_ci
4684e41f4b71Sopenharmony_ci  | 参数名      | 类型                        | 必填   | 说明             |
4685e41f4b71Sopenharmony_ci  | -------- | ------------------------- | ---- | -------------- |
4686e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void&gt; | 是    | 异步刷新文件流后的回调函数。 |
4687e41f4b71Sopenharmony_ci
4688e41f4b71Sopenharmony_ci**错误码:**
4689e41f4b71Sopenharmony_ci
4690e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4691e41f4b71Sopenharmony_ci
4692e41f4b71Sopenharmony_ci**示例:**
4693e41f4b71Sopenharmony_ci
4694e41f4b71Sopenharmony_ci  ```ts
4695e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
4696e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4697e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4698e41f4b71Sopenharmony_ci  stream.flush((err: BusinessError) => {
4699e41f4b71Sopenharmony_ci    if (err) {
4700e41f4b71Sopenharmony_ci      console.error("flush stream failed with error message: " + err.message + ", error code: " + err.code);
4701e41f4b71Sopenharmony_ci    } else {
4702e41f4b71Sopenharmony_ci      console.info("flush succeed");
4703e41f4b71Sopenharmony_ci      stream.close();
4704e41f4b71Sopenharmony_ci    }
4705e41f4b71Sopenharmony_ci  });
4706e41f4b71Sopenharmony_ci  ```
4707e41f4b71Sopenharmony_ci
4708e41f4b71Sopenharmony_ci### flushSync
4709e41f4b71Sopenharmony_ci
4710e41f4b71Sopenharmony_ciflushSync(): void
4711e41f4b71Sopenharmony_ci
4712e41f4b71Sopenharmony_ci同步刷新文件流。
4713e41f4b71Sopenharmony_ci
4714e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4715e41f4b71Sopenharmony_ci
4716e41f4b71Sopenharmony_ci**错误码:**
4717e41f4b71Sopenharmony_ci
4718e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4719e41f4b71Sopenharmony_ci
4720e41f4b71Sopenharmony_ci**示例:**
4721e41f4b71Sopenharmony_ci
4722e41f4b71Sopenharmony_ci  ```ts
4723e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4724e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4725e41f4b71Sopenharmony_ci  stream.flushSync();
4726e41f4b71Sopenharmony_ci  stream.close();
4727e41f4b71Sopenharmony_ci  ```
4728e41f4b71Sopenharmony_ci
4729e41f4b71Sopenharmony_ci### write
4730e41f4b71Sopenharmony_ci
4731e41f4b71Sopenharmony_ciwrite(buffer: ArrayBuffer | string, options?: WriteOptions): Promise&lt;number&gt;
4732e41f4b71Sopenharmony_ci
4733e41f4b71Sopenharmony_ci将数据写入流文件,使用Promise异步返回。
4734e41f4b71Sopenharmony_ci
4735e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4736e41f4b71Sopenharmony_ci
4737e41f4b71Sopenharmony_ci**参数:**
4738e41f4b71Sopenharmony_ci
4739e41f4b71Sopenharmony_ci  | 参数名     | 类型                              | 必填   | 说明                                       |
4740e41f4b71Sopenharmony_ci  | ------- | ------------------------------- | ---- | ---------------------------------------- |
4741e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer \| string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
4742e41f4b71Sopenharmony_ci  | options | [WriteOptions](#writeoptions11)                          | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望写入数据的长度。默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。|
4743e41f4b71Sopenharmony_ci
4744e41f4b71Sopenharmony_ci**返回值:**
4745e41f4b71Sopenharmony_ci
4746e41f4b71Sopenharmony_ci  | 类型                    | 说明       |
4747e41f4b71Sopenharmony_ci  | --------------------- | -------- |
4748e41f4b71Sopenharmony_ci  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
4749e41f4b71Sopenharmony_ci
4750e41f4b71Sopenharmony_ci**错误码:**
4751e41f4b71Sopenharmony_ci
4752e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4753e41f4b71Sopenharmony_ci
4754e41f4b71Sopenharmony_ci**示例:**
4755e41f4b71Sopenharmony_ci
4756e41f4b71Sopenharmony_ci  ```ts
4757e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
4758e41f4b71Sopenharmony_ci  import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit';
4759e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4760e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4761e41f4b71Sopenharmony_ci  let writeOption: WriteOptions = {
4762e41f4b71Sopenharmony_ci    offset: 5,
4763e41f4b71Sopenharmony_ci    length: 5,
4764e41f4b71Sopenharmony_ci    encoding: 'utf-8'
4765e41f4b71Sopenharmony_ci  };
4766e41f4b71Sopenharmony_ci  stream.write("hello, world", writeOption).then((number: number) => {
4767e41f4b71Sopenharmony_ci    console.info("write succeed and size is:" + number);
4768e41f4b71Sopenharmony_ci    stream.close();
4769e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
4770e41f4b71Sopenharmony_ci    console.error("write failed with error message: " + err.message + ", error code: " + err.code);
4771e41f4b71Sopenharmony_ci  });
4772e41f4b71Sopenharmony_ci  ```
4773e41f4b71Sopenharmony_ci
4774e41f4b71Sopenharmony_ci### write
4775e41f4b71Sopenharmony_ci
4776e41f4b71Sopenharmony_ciwrite(buffer: ArrayBuffer | string, options?: WriteOptions, callback: AsyncCallback&lt;number&gt;): void
4777e41f4b71Sopenharmony_ci
4778e41f4b71Sopenharmony_ci将数据写入流文件,使用callback异步回调。
4779e41f4b71Sopenharmony_ci
4780e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4781e41f4b71Sopenharmony_ci
4782e41f4b71Sopenharmony_ci**参数:**
4783e41f4b71Sopenharmony_ci
4784e41f4b71Sopenharmony_ci  | 参数名   | 类型                            | 必填 | 说明                                                         |
4785e41f4b71Sopenharmony_ci  | -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
4786e41f4b71Sopenharmony_ci  | buffer   | ArrayBuffer \| string | 是   | 待写入文件的数据,可来自缓冲区或字符串。                     |
4787e41f4b71Sopenharmony_ci  | options  | [WriteOptions](#writeoptions11)                          | 否   | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。|
4788e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;number&gt;     | 是   | 异步写入完成后执行的回调函数。                               |
4789e41f4b71Sopenharmony_ci
4790e41f4b71Sopenharmony_ci**错误码:**
4791e41f4b71Sopenharmony_ci
4792e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4793e41f4b71Sopenharmony_ci
4794e41f4b71Sopenharmony_ci**示例:**
4795e41f4b71Sopenharmony_ci
4796e41f4b71Sopenharmony_ci  ```ts
4797e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
4798e41f4b71Sopenharmony_ci  import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit';
4799e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4800e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4801e41f4b71Sopenharmony_ci  let writeOption: WriteOptions = {
4802e41f4b71Sopenharmony_ci    offset: 5,
4803e41f4b71Sopenharmony_ci    length: 5,
4804e41f4b71Sopenharmony_ci    encoding: 'utf-8'
4805e41f4b71Sopenharmony_ci  };
4806e41f4b71Sopenharmony_ci  stream.write("hello, world", writeOption, (err: BusinessError, bytesWritten: number) => {
4807e41f4b71Sopenharmony_ci    if (err) {
4808e41f4b71Sopenharmony_ci      console.error("write stream failed with error message: " + err.message + ", error code: " + err.code);
4809e41f4b71Sopenharmony_ci    } else {
4810e41f4b71Sopenharmony_ci      if (bytesWritten) {
4811e41f4b71Sopenharmony_ci        console.info("write succeed and size is:" + bytesWritten);
4812e41f4b71Sopenharmony_ci        stream.close();
4813e41f4b71Sopenharmony_ci      }
4814e41f4b71Sopenharmony_ci    }
4815e41f4b71Sopenharmony_ci  });
4816e41f4b71Sopenharmony_ci  ```
4817e41f4b71Sopenharmony_ci
4818e41f4b71Sopenharmony_ci### writeSync
4819e41f4b71Sopenharmony_ci
4820e41f4b71Sopenharmony_ciwriteSync(buffer: ArrayBuffer | string, options?: WriteOptions): number
4821e41f4b71Sopenharmony_ci
4822e41f4b71Sopenharmony_ci以同步方法将数据写入流文件。
4823e41f4b71Sopenharmony_ci
4824e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4825e41f4b71Sopenharmony_ci
4826e41f4b71Sopenharmony_ci**参数:**
4827e41f4b71Sopenharmony_ci
4828e41f4b71Sopenharmony_ci  | 参数名     | 类型                              | 必填   | 说明                                       |
4829e41f4b71Sopenharmony_ci  | ------- | ------------------------------- | ---- | ---------------------------------------- |
4830e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer \| string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
4831e41f4b71Sopenharmony_ci  | options | [WriteOptions](#writeoptions11)                          | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。|
4832e41f4b71Sopenharmony_ci
4833e41f4b71Sopenharmony_ci**返回值:**
4834e41f4b71Sopenharmony_ci
4835e41f4b71Sopenharmony_ci  | 类型     | 说明       |
4836e41f4b71Sopenharmony_ci  | ------ | -------- |
4837e41f4b71Sopenharmony_ci  | number | 实际写入的长度。 |
4838e41f4b71Sopenharmony_ci
4839e41f4b71Sopenharmony_ci**错误码:**
4840e41f4b71Sopenharmony_ci
4841e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4842e41f4b71Sopenharmony_ci
4843e41f4b71Sopenharmony_ci**示例:**
4844e41f4b71Sopenharmony_ci
4845e41f4b71Sopenharmony_ci  ```ts
4846e41f4b71Sopenharmony_ci  import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit';
4847e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4848e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath,"r+");
4849e41f4b71Sopenharmony_ci  let writeOption: WriteOptions = {
4850e41f4b71Sopenharmony_ci    offset: 5,
4851e41f4b71Sopenharmony_ci    length: 5,
4852e41f4b71Sopenharmony_ci    encoding: 'utf-8'
4853e41f4b71Sopenharmony_ci  };
4854e41f4b71Sopenharmony_ci  let num = stream.writeSync("hello, world", writeOption);
4855e41f4b71Sopenharmony_ci  stream.close();
4856e41f4b71Sopenharmony_ci  ```
4857e41f4b71Sopenharmony_ci
4858e41f4b71Sopenharmony_ci### read
4859e41f4b71Sopenharmony_ci
4860e41f4b71Sopenharmony_ciread(buffer: ArrayBuffer, options?: ReadOptions): Promise&lt;number&gt;
4861e41f4b71Sopenharmony_ci
4862e41f4b71Sopenharmony_ci从流文件读取数据,使用Promise异步返回。
4863e41f4b71Sopenharmony_ci
4864e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4865e41f4b71Sopenharmony_ci
4866e41f4b71Sopenharmony_ci**参数:**
4867e41f4b71Sopenharmony_ci
4868e41f4b71Sopenharmony_ci  | 参数名     | 类型          | 必填   | 说明                                       |
4869e41f4b71Sopenharmony_ci  | ------- | ----------- | ---- | ---------------------------------------- |
4870e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer | 是    | 用于读取文件的缓冲区。                              |
4871e41f4b71Sopenharmony_ci  | options | [ReadOptions](#readoptions11)      | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。 |
4872e41f4b71Sopenharmony_ci
4873e41f4b71Sopenharmony_ci**返回值:**
4874e41f4b71Sopenharmony_ci
4875e41f4b71Sopenharmony_ci  | 类型                                 | 说明     |
4876e41f4b71Sopenharmony_ci  | ---------------------------------- | ------ |
4877e41f4b71Sopenharmony_ci  | Promise&lt;number&gt; | Promise对象。返回读取的结果。 |
4878e41f4b71Sopenharmony_ci
4879e41f4b71Sopenharmony_ci**错误码:**
4880e41f4b71Sopenharmony_ci
4881e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4882e41f4b71Sopenharmony_ci
4883e41f4b71Sopenharmony_ci**示例:**
4884e41f4b71Sopenharmony_ci
4885e41f4b71Sopenharmony_ci  ```ts
4886e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
4887e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
4888e41f4b71Sopenharmony_ci  import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit';
4889e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4890e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4891e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(4096);
4892e41f4b71Sopenharmony_ci  let readOption: ReadOptions = {
4893e41f4b71Sopenharmony_ci    offset: 5,
4894e41f4b71Sopenharmony_ci    length: 5
4895e41f4b71Sopenharmony_ci  };
4896e41f4b71Sopenharmony_ci  stream.read(arrayBuffer, readOption).then((readLen: number) => {
4897e41f4b71Sopenharmony_ci    console.info("read data succeed");
4898e41f4b71Sopenharmony_ci    let buf = buffer.from(arrayBuffer, 0, readLen);
4899e41f4b71Sopenharmony_ci    console.log(`The content of file: ${buf.toString()}`);
4900e41f4b71Sopenharmony_ci    stream.close();
4901e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
4902e41f4b71Sopenharmony_ci    console.error("read data failed with error message: " + err.message + ", error code: " + err.code);
4903e41f4b71Sopenharmony_ci  });
4904e41f4b71Sopenharmony_ci  ```
4905e41f4b71Sopenharmony_ci
4906e41f4b71Sopenharmony_ci### read
4907e41f4b71Sopenharmony_ci
4908e41f4b71Sopenharmony_ciread(buffer: ArrayBuffer, options?: ReadOptions, callback: AsyncCallback&lt;number&gt;): void
4909e41f4b71Sopenharmony_ci
4910e41f4b71Sopenharmony_ci从流文件读取数据,使用callback异步回调。
4911e41f4b71Sopenharmony_ci
4912e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4913e41f4b71Sopenharmony_ci
4914e41f4b71Sopenharmony_ci**参数:**
4915e41f4b71Sopenharmony_ci
4916e41f4b71Sopenharmony_ci  | 参数名      | 类型                                       | 必填   | 说明                                       |
4917e41f4b71Sopenharmony_ci  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
4918e41f4b71Sopenharmony_ci  | buffer   | ArrayBuffer                              | 是    | 用于读取文件的缓冲区。                              |
4919e41f4b71Sopenharmony_ci  | options  | [ReadOptions](#readoptions11)                                   | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读. |
4920e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;number&gt; | 是    | 异步从流文件读取数据之后的回调。                         |
4921e41f4b71Sopenharmony_ci
4922e41f4b71Sopenharmony_ci**错误码:**
4923e41f4b71Sopenharmony_ci
4924e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4925e41f4b71Sopenharmony_ci
4926e41f4b71Sopenharmony_ci**示例:**
4927e41f4b71Sopenharmony_ci
4928e41f4b71Sopenharmony_ci  ```ts
4929e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
4930e41f4b71Sopenharmony_ci  import { buffer } from '@kit.ArkTS';
4931e41f4b71Sopenharmony_ci  import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit';
4932e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4933e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4934e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(4096);
4935e41f4b71Sopenharmony_ci  let readOption: ReadOptions = {
4936e41f4b71Sopenharmony_ci    offset: 5,
4937e41f4b71Sopenharmony_ci    length: 5
4938e41f4b71Sopenharmony_ci  };
4939e41f4b71Sopenharmony_ci  stream.read(arrayBuffer, readOption, (err: BusinessError, readLen: number) => {
4940e41f4b71Sopenharmony_ci    if (err) {
4941e41f4b71Sopenharmony_ci      console.error("read stream failed with error message: " + err.message + ", error code: " + err.code);
4942e41f4b71Sopenharmony_ci    } else {
4943e41f4b71Sopenharmony_ci      console.info("read data succeed");
4944e41f4b71Sopenharmony_ci      let buf = buffer.from(arrayBuffer, 0, readLen);
4945e41f4b71Sopenharmony_ci      console.log(`The content of file: ${buf.toString()}`);
4946e41f4b71Sopenharmony_ci      stream.close();
4947e41f4b71Sopenharmony_ci    }
4948e41f4b71Sopenharmony_ci  });
4949e41f4b71Sopenharmony_ci  ```
4950e41f4b71Sopenharmony_ci
4951e41f4b71Sopenharmony_ci### readSync
4952e41f4b71Sopenharmony_ci
4953e41f4b71Sopenharmony_cireadSync(buffer: ArrayBuffer, options?: ReadOptions): number
4954e41f4b71Sopenharmony_ci
4955e41f4b71Sopenharmony_ci以同步方法从流文件读取数据。
4956e41f4b71Sopenharmony_ci
4957e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4958e41f4b71Sopenharmony_ci
4959e41f4b71Sopenharmony_ci**参数:**
4960e41f4b71Sopenharmony_ci
4961e41f4b71Sopenharmony_ci  | 参数名     | 类型          | 必填   | 说明                                       |
4962e41f4b71Sopenharmony_ci  | ------- | ----------- | ---- | ---------------------------------------- |
4963e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer | 是    | 用于读取文件的缓冲区。                              |
4964e41f4b71Sopenharmony_ci  | options | [ReadOptions](#readoptions11)      | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。<br/>  |
4965e41f4b71Sopenharmony_ci
4966e41f4b71Sopenharmony_ci**返回值:**
4967e41f4b71Sopenharmony_ci
4968e41f4b71Sopenharmony_ci  | 类型     | 说明       |
4969e41f4b71Sopenharmony_ci  | ------ | -------- |
4970e41f4b71Sopenharmony_ci  | number | 实际读取的长度。 |
4971e41f4b71Sopenharmony_ci
4972e41f4b71Sopenharmony_ci**错误码:**
4973e41f4b71Sopenharmony_ci
4974e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
4975e41f4b71Sopenharmony_ci
4976e41f4b71Sopenharmony_ci**示例:**
4977e41f4b71Sopenharmony_ci
4978e41f4b71Sopenharmony_ci  ```ts
4979e41f4b71Sopenharmony_ci  import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit';
4980e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
4981e41f4b71Sopenharmony_ci  let stream = fs.createStreamSync(filePath, "r+");
4982e41f4b71Sopenharmony_ci  let readOption: ReadOptions = {
4983e41f4b71Sopenharmony_ci    offset: 5,
4984e41f4b71Sopenharmony_ci    length: 5
4985e41f4b71Sopenharmony_ci  };
4986e41f4b71Sopenharmony_ci  let buf = new ArrayBuffer(4096);
4987e41f4b71Sopenharmony_ci  let num = stream.readSync(buf, readOption);
4988e41f4b71Sopenharmony_ci  stream.close();
4989e41f4b71Sopenharmony_ci  ```
4990e41f4b71Sopenharmony_ci
4991e41f4b71Sopenharmony_ci## File
4992e41f4b71Sopenharmony_ci
4993e41f4b71Sopenharmony_ci由open接口打开的File对象。
4994e41f4b71Sopenharmony_ci
4995e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
4996e41f4b71Sopenharmony_ci
4997e41f4b71Sopenharmony_ci### 属性
4998e41f4b71Sopenharmony_ci
4999e41f4b71Sopenharmony_ci| 名称   | 类型   | 只读   | 可写   | 说明      |
5000e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ------- |
5001e41f4b71Sopenharmony_ci| fd | number | 是    | 否    | 打开的文件描述符。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
5002e41f4b71Sopenharmony_ci| path<sup>10+</sup> | string | 是    | 否    | 文件路径。 |
5003e41f4b71Sopenharmony_ci| name<sup>10+</sup> | string | 是    | 否    | 文件名。 |
5004e41f4b71Sopenharmony_ci
5005e41f4b71Sopenharmony_ci### getParent<sup>11+</sup>
5006e41f4b71Sopenharmony_ci
5007e41f4b71Sopenharmony_cigetParent(): string
5008e41f4b71Sopenharmony_ci
5009e41f4b71Sopenharmony_ci获取File对象对应文件父目录。
5010e41f4b71Sopenharmony_ci
5011e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5012e41f4b71Sopenharmony_ci
5013e41f4b71Sopenharmony_ci**返回值:**
5014e41f4b71Sopenharmony_ci
5015e41f4b71Sopenharmony_ci  | 类型                                 | 说明     |
5016e41f4b71Sopenharmony_ci  | ---------------------------------- | ------ |
5017e41f4b71Sopenharmony_ci  | string | 返回父目录路径。 |
5018e41f4b71Sopenharmony_ci
5019e41f4b71Sopenharmony_ci**错误码:**
5020e41f4b71Sopenharmony_ci
5021e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5022e41f4b71Sopenharmony_ci
5023e41f4b71Sopenharmony_ci**示例:**
5024e41f4b71Sopenharmony_ci
5025e41f4b71Sopenharmony_ci  ```ts
5026e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
5027e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5028e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
5029e41f4b71Sopenharmony_ci  console.info('The parent path is: ' + file.getParent());
5030e41f4b71Sopenharmony_ci  fs.closeSync(file);
5031e41f4b71Sopenharmony_ci  ```
5032e41f4b71Sopenharmony_ci
5033e41f4b71Sopenharmony_ci### lock
5034e41f4b71Sopenharmony_ci
5035e41f4b71Sopenharmony_cilock(exclusive?: boolean): Promise\<void>
5036e41f4b71Sopenharmony_ci
5037e41f4b71Sopenharmony_ci文件阻塞式施加共享锁或独占锁,使用Promise异步返回。
5038e41f4b71Sopenharmony_ci
5039e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5040e41f4b71Sopenharmony_ci
5041e41f4b71Sopenharmony_ci**参数:**
5042e41f4b71Sopenharmony_ci
5043e41f4b71Sopenharmony_ci  | 参数名     | 类型          | 必填   | 说明                                       |
5044e41f4b71Sopenharmony_ci  | ------- | ----------- | ---- | ---------------------------------------- |
5045e41f4b71Sopenharmony_ci  | exclusive  | boolean | 否   | 是否施加独占锁,默认false。       |
5046e41f4b71Sopenharmony_ci
5047e41f4b71Sopenharmony_ci**返回值:**
5048e41f4b71Sopenharmony_ci
5049e41f4b71Sopenharmony_ci  | 类型                                 | 说明     |
5050e41f4b71Sopenharmony_ci  | ---------------------------------- | ------ |
5051e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象。无返回值。 |
5052e41f4b71Sopenharmony_ci
5053e41f4b71Sopenharmony_ci**错误码:**
5054e41f4b71Sopenharmony_ci
5055e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5056e41f4b71Sopenharmony_ci
5057e41f4b71Sopenharmony_ci**示例:**
5058e41f4b71Sopenharmony_ci
5059e41f4b71Sopenharmony_ci  ```ts
5060e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
5061e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5062e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
5063e41f4b71Sopenharmony_ci  file.lock(true).then(() => {
5064e41f4b71Sopenharmony_ci    console.log("lock file succeed");
5065e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
5066e41f4b71Sopenharmony_ci    console.error("lock file failed with error message: " + err.message + ", error code: " + err.code);
5067e41f4b71Sopenharmony_ci  }).finally(() => {
5068e41f4b71Sopenharmony_ci    fs.closeSync(file);
5069e41f4b71Sopenharmony_ci  });
5070e41f4b71Sopenharmony_ci  ```
5071e41f4b71Sopenharmony_ci
5072e41f4b71Sopenharmony_ci### lock
5073e41f4b71Sopenharmony_ci
5074e41f4b71Sopenharmony_cilock(exclusive?: boolean, callback: AsyncCallback\<void>): void
5075e41f4b71Sopenharmony_ci
5076e41f4b71Sopenharmony_ci文件阻塞式施加共享锁或独占锁,使Callback异步回调。
5077e41f4b71Sopenharmony_ci
5078e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5079e41f4b71Sopenharmony_ci
5080e41f4b71Sopenharmony_ci**参数:**
5081e41f4b71Sopenharmony_ci
5082e41f4b71Sopenharmony_ci  | 参数名     | 类型          | 必填   | 说明                                       |
5083e41f4b71Sopenharmony_ci  | ------- | ----------- | ---- | ---------------------------------------- |
5084e41f4b71Sopenharmony_ci  | exclusive  | boolean | 否   | 是否施加独占锁,默认false。       |
5085e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;void&gt; | 是    | 异步文件上锁之后的回调。   |
5086e41f4b71Sopenharmony_ci
5087e41f4b71Sopenharmony_ci**错误码:**
5088e41f4b71Sopenharmony_ci
5089e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5090e41f4b71Sopenharmony_ci
5091e41f4b71Sopenharmony_ci**示例:**
5092e41f4b71Sopenharmony_ci
5093e41f4b71Sopenharmony_ci  ```ts
5094e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
5095e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5096e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
5097e41f4b71Sopenharmony_ci  file.lock(true, (err: BusinessError) => {
5098e41f4b71Sopenharmony_ci    if (err) {
5099e41f4b71Sopenharmony_ci      console.error("lock file failed with error message: " + err.message + ", error code: " + err.code);
5100e41f4b71Sopenharmony_ci    } else {
5101e41f4b71Sopenharmony_ci      console.log("lock file succeed");
5102e41f4b71Sopenharmony_ci    }
5103e41f4b71Sopenharmony_ci    fs.closeSync(file);
5104e41f4b71Sopenharmony_ci  });
5105e41f4b71Sopenharmony_ci  ```
5106e41f4b71Sopenharmony_ci
5107e41f4b71Sopenharmony_ci### tryLock
5108e41f4b71Sopenharmony_ci
5109e41f4b71Sopenharmony_citryLock(exclusive?: boolean): void
5110e41f4b71Sopenharmony_ci
5111e41f4b71Sopenharmony_ci文件非阻塞式施加共享锁或独占锁。
5112e41f4b71Sopenharmony_ci
5113e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5114e41f4b71Sopenharmony_ci
5115e41f4b71Sopenharmony_ci**参数:**
5116e41f4b71Sopenharmony_ci
5117e41f4b71Sopenharmony_ci  | 参数名     | 类型          | 必填   | 说明                                       |
5118e41f4b71Sopenharmony_ci  | ------- | ----------- | ---- | ---------------------------------------- |
5119e41f4b71Sopenharmony_ci  | exclusive  | boolean | 否   | 是否施加独占锁,默认false。       |
5120e41f4b71Sopenharmony_ci
5121e41f4b71Sopenharmony_ci**错误码:**
5122e41f4b71Sopenharmony_ci
5123e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5124e41f4b71Sopenharmony_ci
5125e41f4b71Sopenharmony_ci**示例:**
5126e41f4b71Sopenharmony_ci
5127e41f4b71Sopenharmony_ci  ```ts
5128e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5129e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
5130e41f4b71Sopenharmony_ci  file.tryLock(true);
5131e41f4b71Sopenharmony_ci  console.log("lock file succeed");
5132e41f4b71Sopenharmony_ci  fs.closeSync(file);
5133e41f4b71Sopenharmony_ci  ```
5134e41f4b71Sopenharmony_ci
5135e41f4b71Sopenharmony_ci### unlock
5136e41f4b71Sopenharmony_ci
5137e41f4b71Sopenharmony_ciunlock(): void
5138e41f4b71Sopenharmony_ci
5139e41f4b71Sopenharmony_ci以同步方式给文件解锁。
5140e41f4b71Sopenharmony_ci
5141e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5142e41f4b71Sopenharmony_ci
5143e41f4b71Sopenharmony_ci**错误码:**
5144e41f4b71Sopenharmony_ci
5145e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5146e41f4b71Sopenharmony_ci
5147e41f4b71Sopenharmony_ci**示例:**
5148e41f4b71Sopenharmony_ci
5149e41f4b71Sopenharmony_ci  ```ts
5150e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5151e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
5152e41f4b71Sopenharmony_ci  file.tryLock(true);
5153e41f4b71Sopenharmony_ci  file.unlock();
5154e41f4b71Sopenharmony_ci  console.log("unlock file succeed");
5155e41f4b71Sopenharmony_ci  fs.closeSync(file);
5156e41f4b71Sopenharmony_ci  ```
5157e41f4b71Sopenharmony_ci
5158e41f4b71Sopenharmony_ci  ## fs.DfsListeners<sup>12+</sup>
5159e41f4b71Sopenharmony_ci
5160e41f4b71Sopenharmony_ciinterface DfsListeners {
5161e41f4b71Sopenharmony_ci  onStatus(networkId: string, status: number): void;
5162e41f4b71Sopenharmony_ci}
5163e41f4b71Sopenharmony_ci
5164e41f4b71Sopenharmony_ci事件监听类。创建DFSListener对象,用于监听分布式文件系统状态。
5165e41f4b71Sopenharmony_ci
5166e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5167e41f4b71Sopenharmony_ci
5168e41f4b71Sopenharmony_ci### onStatus<sup>12+</sup>
5169e41f4b71Sopenharmony_ci
5170e41f4b71Sopenharmony_cionStatus(networkId: string, status: number): void;
5171e41f4b71Sopenharmony_ci
5172e41f4b71Sopenharmony_ci事件回调类。参数由[connectDfs](#fsconnectdfs12)传入。
5173e41f4b71Sopenharmony_ci
5174e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5175e41f4b71Sopenharmony_ci
5176e41f4b71Sopenharmony_ci**参数:**
5177e41f4b71Sopenharmony_ci
5178e41f4b71Sopenharmony_ci  | 参数名  | 类型     | 必填   | 说明                              |
5179e41f4b71Sopenharmony_ci  | ---- | ------ | ---- | ---------------------------------------- |
5180e41f4b71Sopenharmony_ci  | networkId   | string | 是    | 设备的网络Id。                             |
5181e41f4b71Sopenharmony_ci  | status | number | 是    | 分布式文件系统的状态码(以connectDfs回调onStatus的特定错误码作为入参)。触发场景为connectDfs调用过程中出现对端设备异常,对应错误码为:<br/>-&nbsp;[13900046](errorcode-filemanagement.md#13900046):软件造成连接中断。
5182e41f4b71Sopenharmony_ci
5183e41f4b71Sopenharmony_ci## RandomAccessFile
5184e41f4b71Sopenharmony_ci
5185e41f4b71Sopenharmony_ci随机读写文件流,在调用RandomAccessFile的方法前,需要先通过createRandomAccess()方法(同步或异步)来构建一个RandomAccessFile实例。
5186e41f4b71Sopenharmony_ci
5187e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5188e41f4b71Sopenharmony_ci
5189e41f4b71Sopenharmony_ci### 属性
5190e41f4b71Sopenharmony_ci
5191e41f4b71Sopenharmony_ci| 名称         | 类型   | 只读  | 可写  | 说明              |
5192e41f4b71Sopenharmony_ci| ----------- | ------ | ----  | ----- | ---------------- |
5193e41f4b71Sopenharmony_ci| fd          | number | 是    | 否    | 打开的文件描述符。 |
5194e41f4b71Sopenharmony_ci| filePointer | number | 是    | 是    | RandomAccessFile对象的偏置指针。 |
5195e41f4b71Sopenharmony_ci
5196e41f4b71Sopenharmony_ci### setFilePointer<sup>10+</sup>
5197e41f4b71Sopenharmony_ci
5198e41f4b71Sopenharmony_cisetFilePointer(): void
5199e41f4b71Sopenharmony_ci
5200e41f4b71Sopenharmony_ci设置文件偏置指针
5201e41f4b71Sopenharmony_ci
5202e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5203e41f4b71Sopenharmony_ci
5204e41f4b71Sopenharmony_ci**错误码:**
5205e41f4b71Sopenharmony_ci
5206e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5207e41f4b71Sopenharmony_ci
5208e41f4b71Sopenharmony_ci**示例:**
5209e41f4b71Sopenharmony_ci
5210e41f4b71Sopenharmony_ci  ```ts
5211e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5212e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
5213e41f4b71Sopenharmony_ci  randomAccessFile.setFilePointer(1);
5214e41f4b71Sopenharmony_ci  randomAccessFile.close();
5215e41f4b71Sopenharmony_ci  ```
5216e41f4b71Sopenharmony_ci
5217e41f4b71Sopenharmony_ci
5218e41f4b71Sopenharmony_ci### close<sup>10+</sup>
5219e41f4b71Sopenharmony_ci
5220e41f4b71Sopenharmony_ciclose(): void
5221e41f4b71Sopenharmony_ci
5222e41f4b71Sopenharmony_ci同步关闭RandomAccessFile对象。
5223e41f4b71Sopenharmony_ci
5224e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5225e41f4b71Sopenharmony_ci
5226e41f4b71Sopenharmony_ci**错误码:**
5227e41f4b71Sopenharmony_ci
5228e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5229e41f4b71Sopenharmony_ci
5230e41f4b71Sopenharmony_ci**示例:**
5231e41f4b71Sopenharmony_ci
5232e41f4b71Sopenharmony_ci  ```ts
5233e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5234e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
5235e41f4b71Sopenharmony_ci  randomAccessFile.close();
5236e41f4b71Sopenharmony_ci  ```
5237e41f4b71Sopenharmony_ci
5238e41f4b71Sopenharmony_ci### write<sup>10+</sup>
5239e41f4b71Sopenharmony_ci
5240e41f4b71Sopenharmony_ciwrite(buffer: ArrayBuffer | string, options?: WriteOptions): Promise&lt;number&gt;
5241e41f4b71Sopenharmony_ci
5242e41f4b71Sopenharmony_ci将数据写入文件,使用Promise异步返回。
5243e41f4b71Sopenharmony_ci
5244e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5245e41f4b71Sopenharmony_ci
5246e41f4b71Sopenharmony_ci**参数:**
5247e41f4b71Sopenharmony_ci
5248e41f4b71Sopenharmony_ci  | 参数名     | 类型                              | 必填   | 说明                                       |
5249e41f4b71Sopenharmony_ci  | ------- | ------------------------------- | ---- | ---------------------------------------- |
5250e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer \| string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
5251e41f4b71Sopenharmony_ci  | options | [WriteOptions](#writeoptions11)                          | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望写入数据的长度。默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望写入文件位置(基于当前filePointer加上offset的位置)。可选,默认从偏置指针(filePointer)开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。|
5252e41f4b71Sopenharmony_ci
5253e41f4b71Sopenharmony_ci**返回值:**
5254e41f4b71Sopenharmony_ci
5255e41f4b71Sopenharmony_ci  | 类型                    | 说明       |
5256e41f4b71Sopenharmony_ci  | --------------------- | -------- |
5257e41f4b71Sopenharmony_ci  | Promise&lt;number&gt; | Promise对象。返回实际写入的长度。 |
5258e41f4b71Sopenharmony_ci
5259e41f4b71Sopenharmony_ci**错误码:**
5260e41f4b71Sopenharmony_ci
5261e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5262e41f4b71Sopenharmony_ci
5263e41f4b71Sopenharmony_ci**示例:**
5264e41f4b71Sopenharmony_ci
5265e41f4b71Sopenharmony_ci  ```ts
5266e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
5267e41f4b71Sopenharmony_ci  import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit';
5268e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5269e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
5270e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(file);
5271e41f4b71Sopenharmony_ci  let bufferLength: number = 4096;
5272e41f4b71Sopenharmony_ci  let writeOption: WriteOptions = {
5273e41f4b71Sopenharmony_ci    offset: 1,
5274e41f4b71Sopenharmony_ci    length: 5,
5275e41f4b71Sopenharmony_ci    encoding: 'utf-8'
5276e41f4b71Sopenharmony_ci  };
5277e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(bufferLength);
5278e41f4b71Sopenharmony_ci  randomAccessFile.write(arrayBuffer, writeOption).then((bytesWritten: number) => {
5279e41f4b71Sopenharmony_ci    console.info("randomAccessFile bytesWritten: " + bytesWritten);
5280e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
5281e41f4b71Sopenharmony_ci    console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
5282e41f4b71Sopenharmony_ci  }).finally(() => {
5283e41f4b71Sopenharmony_ci    randomAccessFile.close();
5284e41f4b71Sopenharmony_ci    fs.closeSync(file);
5285e41f4b71Sopenharmony_ci  });
5286e41f4b71Sopenharmony_ci
5287e41f4b71Sopenharmony_ci  ```
5288e41f4b71Sopenharmony_ci
5289e41f4b71Sopenharmony_ci### write<sup>10+</sup>
5290e41f4b71Sopenharmony_ci
5291e41f4b71Sopenharmony_ciwrite(buffer: ArrayBuffer | string, options?: WriteOptions, callback: AsyncCallback&lt;number&gt;): void
5292e41f4b71Sopenharmony_ci
5293e41f4b71Sopenharmony_ci将数据写入文件,使用callback异步回调。
5294e41f4b71Sopenharmony_ci
5295e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5296e41f4b71Sopenharmony_ci
5297e41f4b71Sopenharmony_ci**参数:**
5298e41f4b71Sopenharmony_ci
5299e41f4b71Sopenharmony_ci  | 参数名   | 类型                            | 必填 | 说明                                                         |
5300e41f4b71Sopenharmony_ci  | -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
5301e41f4b71Sopenharmony_ci  | buffer   | ArrayBuffer \| string | 是   | 待写入文件的数据,可来自缓冲区或字符串。                     |
5302e41f4b71Sopenharmony_ci  | options  | [WriteOptions](#writeoptions11)                          | 否   | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望写入文件位置(基于当前filePointer加上offset的位置)。可选,默认从偏置指针(filePointer)开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。|
5303e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;number&gt;     | 是   | 异步写入完成后执行的回调函数。                               |
5304e41f4b71Sopenharmony_ci
5305e41f4b71Sopenharmony_ci**错误码:**
5306e41f4b71Sopenharmony_ci
5307e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5308e41f4b71Sopenharmony_ci
5309e41f4b71Sopenharmony_ci**示例:**
5310e41f4b71Sopenharmony_ci
5311e41f4b71Sopenharmony_ci  ```ts
5312e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
5313e41f4b71Sopenharmony_ci  import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit';
5314e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5315e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
5316e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(file);
5317e41f4b71Sopenharmony_ci  let bufferLength: number = 4096;
5318e41f4b71Sopenharmony_ci  let writeOption: WriteOptions = {
5319e41f4b71Sopenharmony_ci    offset: 1,
5320e41f4b71Sopenharmony_ci    length: bufferLength,
5321e41f4b71Sopenharmony_ci    encoding: 'utf-8'
5322e41f4b71Sopenharmony_ci  };
5323e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(bufferLength);
5324e41f4b71Sopenharmony_ci  randomAccessFile.write(arrayBuffer, writeOption, (err: BusinessError, bytesWritten: number) => {
5325e41f4b71Sopenharmony_ci    if (err) {
5326e41f4b71Sopenharmony_ci      console.error("write failed with error message: " + err.message + ", error code: " + err.code);
5327e41f4b71Sopenharmony_ci    } else {
5328e41f4b71Sopenharmony_ci      if (bytesWritten) {
5329e41f4b71Sopenharmony_ci        console.info("write succeed and size is:" + bytesWritten);
5330e41f4b71Sopenharmony_ci      }
5331e41f4b71Sopenharmony_ci    }
5332e41f4b71Sopenharmony_ci    randomAccessFile.close();
5333e41f4b71Sopenharmony_ci    fs.closeSync(file);
5334e41f4b71Sopenharmony_ci  });
5335e41f4b71Sopenharmony_ci  ```
5336e41f4b71Sopenharmony_ci
5337e41f4b71Sopenharmony_ci### writeSync<sup>10+</sup>
5338e41f4b71Sopenharmony_ci
5339e41f4b71Sopenharmony_ciwriteSync(buffer: ArrayBuffer | string, options?: WriteOptions): number
5340e41f4b71Sopenharmony_ci
5341e41f4b71Sopenharmony_ci以同步方法将数据写入文件。
5342e41f4b71Sopenharmony_ci
5343e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5344e41f4b71Sopenharmony_ci
5345e41f4b71Sopenharmony_ci**参数:**
5346e41f4b71Sopenharmony_ci
5347e41f4b71Sopenharmony_ci  | 参数名     | 类型                              | 必填   | 说明                                       |
5348e41f4b71Sopenharmony_ci  | ------- | ------------------------------- | ---- | ---------------------------------------- |
5349e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer \| string | 是    | 待写入文件的数据,可来自缓冲区或字符串。                     |
5350e41f4b71Sopenharmony_ci  | options | [WriteOptions](#writeoptions11)                          | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望写入文件位置(基于当前filePointer加上offset的位置)。可选,默认从偏置指针(filePointer)开始写。<br/>-&nbsp;encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认&nbsp;'utf-8'。仅支持&nbsp;'utf-8'。|
5351e41f4b71Sopenharmony_ci
5352e41f4b71Sopenharmony_ci**返回值:**
5353e41f4b71Sopenharmony_ci
5354e41f4b71Sopenharmony_ci  | 类型     | 说明       |
5355e41f4b71Sopenharmony_ci  | ------ | -------- |
5356e41f4b71Sopenharmony_ci  | number | 实际写入的长度。 |
5357e41f4b71Sopenharmony_ci
5358e41f4b71Sopenharmony_ci**错误码:**
5359e41f4b71Sopenharmony_ci
5360e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5361e41f4b71Sopenharmony_ci
5362e41f4b71Sopenharmony_ci**示例:**
5363e41f4b71Sopenharmony_ci
5364e41f4b71Sopenharmony_ci  ```ts
5365e41f4b71Sopenharmony_ci  import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit';
5366e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5367e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
5368e41f4b71Sopenharmony_ci  let writeOption: WriteOptions = {
5369e41f4b71Sopenharmony_ci    offset: 5,
5370e41f4b71Sopenharmony_ci    length: 5,
5371e41f4b71Sopenharmony_ci    encoding: 'utf-8'
5372e41f4b71Sopenharmony_ci  };
5373e41f4b71Sopenharmony_ci  let bytesWritten = randomAccessFile.writeSync("hello, world", writeOption);
5374e41f4b71Sopenharmony_ci  randomAccessFile.close();
5375e41f4b71Sopenharmony_ci  ```
5376e41f4b71Sopenharmony_ci
5377e41f4b71Sopenharmony_ci### read<sup>10+</sup>
5378e41f4b71Sopenharmony_ci
5379e41f4b71Sopenharmony_ciread(buffer: ArrayBuffer, options?: ReadOptions): Promise&lt;number&gt;
5380e41f4b71Sopenharmony_ci
5381e41f4b71Sopenharmony_ci从文件读取数据,使用Promise异步返回。
5382e41f4b71Sopenharmony_ci
5383e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5384e41f4b71Sopenharmony_ci
5385e41f4b71Sopenharmony_ci**参数:**
5386e41f4b71Sopenharmony_ci
5387e41f4b71Sopenharmony_ci  | 参数名     | 类型          | 必填   | 说明                                       |
5388e41f4b71Sopenharmony_ci  | ------- | ----------- | ---- | ---------------------------------------- |
5389e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer | 是    | 用于读取文件的缓冲区。                              |
5390e41f4b71Sopenharmony_ci  | options | [ReadOptions](#readoptions11)      | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望读取文件位置(基于当前filePointer加上offset的位置)。可选,默认从偏置指针(filePointer)开始读。 |
5391e41f4b71Sopenharmony_ci
5392e41f4b71Sopenharmony_ci**返回值:**
5393e41f4b71Sopenharmony_ci
5394e41f4b71Sopenharmony_ci  | 类型                                 | 说明     |
5395e41f4b71Sopenharmony_ci  | ---------------------------------- | ------ |
5396e41f4b71Sopenharmony_ci  | Promise&lt;number&gt; | Promise对象。返回读取的结果。 |
5397e41f4b71Sopenharmony_ci
5398e41f4b71Sopenharmony_ci**错误码:**
5399e41f4b71Sopenharmony_ci
5400e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5401e41f4b71Sopenharmony_ci
5402e41f4b71Sopenharmony_ci**示例:**
5403e41f4b71Sopenharmony_ci
5404e41f4b71Sopenharmony_ci  ```ts
5405e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
5406e41f4b71Sopenharmony_ci  import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit';
5407e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5408e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
5409e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(file);
5410e41f4b71Sopenharmony_ci  let bufferLength: number = 4096;
5411e41f4b71Sopenharmony_ci  let readOption: ReadOptions = {
5412e41f4b71Sopenharmony_ci    offset: 1,
5413e41f4b71Sopenharmony_ci    length: 5
5414e41f4b71Sopenharmony_ci  };
5415e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(bufferLength);
5416e41f4b71Sopenharmony_ci  randomAccessFile.read(arrayBuffer, readOption).then((readLength: number) => {
5417e41f4b71Sopenharmony_ci    console.info("randomAccessFile readLength: " + readLength);
5418e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
5419e41f4b71Sopenharmony_ci    console.error("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
5420e41f4b71Sopenharmony_ci  }).finally(() => {
5421e41f4b71Sopenharmony_ci    randomAccessFile.close();
5422e41f4b71Sopenharmony_ci    fs.closeSync(file);
5423e41f4b71Sopenharmony_ci  });
5424e41f4b71Sopenharmony_ci  ```
5425e41f4b71Sopenharmony_ci
5426e41f4b71Sopenharmony_ci### read<sup>10+</sup>
5427e41f4b71Sopenharmony_ci
5428e41f4b71Sopenharmony_ciread(buffer: ArrayBuffer, options?: ReadOptions, callback: AsyncCallback&lt;number&gt;): void
5429e41f4b71Sopenharmony_ci
5430e41f4b71Sopenharmony_ci从文件读取数据,使用callback异步回调。
5431e41f4b71Sopenharmony_ci
5432e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5433e41f4b71Sopenharmony_ci
5434e41f4b71Sopenharmony_ci**参数:**
5435e41f4b71Sopenharmony_ci
5436e41f4b71Sopenharmony_ci  | 参数名      | 类型                                       | 必填   | 说明                                       |
5437e41f4b71Sopenharmony_ci  | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
5438e41f4b71Sopenharmony_ci  | buffer   | ArrayBuffer                              | 是    | 用于读取文件的缓冲区。                              |
5439e41f4b71Sopenharmony_ci  | options  | [ReadOptions](#readoptions11)                                   | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望读取文件位置(基于当前filePointer加上offset的位置)。可选,默认从偏置指针(filePointer)开始读. |
5440e41f4b71Sopenharmony_ci  | callback | AsyncCallback&lt;number&gt; | 是    | 异步从流文件读取数据之后的回调。                         |
5441e41f4b71Sopenharmony_ci
5442e41f4b71Sopenharmony_ci**错误码:**
5443e41f4b71Sopenharmony_ci
5444e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5445e41f4b71Sopenharmony_ci
5446e41f4b71Sopenharmony_ci**示例:**
5447e41f4b71Sopenharmony_ci
5448e41f4b71Sopenharmony_ci  ```ts
5449e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
5450e41f4b71Sopenharmony_ci  import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit';
5451e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5452e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
5453e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(file);
5454e41f4b71Sopenharmony_ci  let length: number = 20;
5455e41f4b71Sopenharmony_ci  let readOption: ReadOptions = {
5456e41f4b71Sopenharmony_ci    offset: 1,
5457e41f4b71Sopenharmony_ci    length: 5
5458e41f4b71Sopenharmony_ci  };
5459e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(length);
5460e41f4b71Sopenharmony_ci  randomAccessFile.read(arrayBuffer, readOption, (err: BusinessError, readLength: number) => {
5461e41f4b71Sopenharmony_ci    if (err) {
5462e41f4b71Sopenharmony_ci      console.error("read failed with error message: " + err.message + ", error code: " + err.code);
5463e41f4b71Sopenharmony_ci    } else {
5464e41f4b71Sopenharmony_ci      if (readLength) {
5465e41f4b71Sopenharmony_ci        console.info("read succeed and size is:" + readLength);
5466e41f4b71Sopenharmony_ci      }
5467e41f4b71Sopenharmony_ci    }
5468e41f4b71Sopenharmony_ci    randomAccessFile.close();
5469e41f4b71Sopenharmony_ci    fs.closeSync(file);
5470e41f4b71Sopenharmony_ci  });
5471e41f4b71Sopenharmony_ci  ```
5472e41f4b71Sopenharmony_ci
5473e41f4b71Sopenharmony_ci### readSync<sup>10+</sup>
5474e41f4b71Sopenharmony_ci
5475e41f4b71Sopenharmony_cireadSync(buffer: ArrayBuffer, options?: ReadOptions): number
5476e41f4b71Sopenharmony_ci
5477e41f4b71Sopenharmony_ci以同步方法从文件读取数据。
5478e41f4b71Sopenharmony_ci
5479e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5480e41f4b71Sopenharmony_ci
5481e41f4b71Sopenharmony_ci**参数:**
5482e41f4b71Sopenharmony_ci
5483e41f4b71Sopenharmony_ci  | 参数名     | 类型          | 必填   | 说明                                       |
5484e41f4b71Sopenharmony_ci  | ------- | ----------- | ---- | ---------------------------------------- |
5485e41f4b71Sopenharmony_ci  | buffer  | ArrayBuffer | 是    | 用于读取文件的缓冲区。                              |
5486e41f4b71Sopenharmony_ci  | options | [ReadOptions](#readoptions11)      | 否    | 支持如下选项:<br/>-&nbsp;length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。<br/>-&nbsp;offset,number类型,表示期望读取文文件位置(基于当前filePointer加上offset的位置)。可选,默认从偏置指针(filePointer)开始读。<br/>  |
5487e41f4b71Sopenharmony_ci
5488e41f4b71Sopenharmony_ci**返回值:**
5489e41f4b71Sopenharmony_ci
5490e41f4b71Sopenharmony_ci  | 类型     | 说明       |
5491e41f4b71Sopenharmony_ci  | ------ | -------- |
5492e41f4b71Sopenharmony_ci  | number | 实际读取的长度。 |
5493e41f4b71Sopenharmony_ci
5494e41f4b71Sopenharmony_ci**错误码:**
5495e41f4b71Sopenharmony_ci
5496e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5497e41f4b71Sopenharmony_ci
5498e41f4b71Sopenharmony_ci**示例:**
5499e41f4b71Sopenharmony_ci
5500e41f4b71Sopenharmony_ci  ```ts
5501e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5502e41f4b71Sopenharmony_ci  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
5503e41f4b71Sopenharmony_ci  let randomAccessFile = fs.createRandomAccessFileSync(file);
5504e41f4b71Sopenharmony_ci  let length: number = 4096;
5505e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(length);
5506e41f4b71Sopenharmony_ci  let readLength = randomAccessFile.readSync(arrayBuffer);
5507e41f4b71Sopenharmony_ci  randomAccessFile.close();
5508e41f4b71Sopenharmony_ci  fs.closeSync(file);
5509e41f4b71Sopenharmony_ci  ```
5510e41f4b71Sopenharmony_ci
5511e41f4b71Sopenharmony_ci### getReadStream<sup>12+</sup>
5512e41f4b71Sopenharmony_ci
5513e41f4b71Sopenharmony_cigetReadStream(): ReadStream;
5514e41f4b71Sopenharmony_ci
5515e41f4b71Sopenharmony_ci获取当前 RandomAccessFile 的一个 ReadStream 实例。
5516e41f4b71Sopenharmony_ci
5517e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5518e41f4b71Sopenharmony_ci
5519e41f4b71Sopenharmony_ci**返回值:**
5520e41f4b71Sopenharmony_ci
5521e41f4b71Sopenharmony_ci  | 类型                | 说明        |
5522e41f4b71Sopenharmony_ci  | ------------------ | --------- |
5523e41f4b71Sopenharmony_ci  | [ReadStream](#readstream12) | 文件可读流 |
5524e41f4b71Sopenharmony_ci
5525e41f4b71Sopenharmony_ci**示例:**
5526e41f4b71Sopenharmony_ci
5527e41f4b71Sopenharmony_ci  ```ts
5528e41f4b71Sopenharmony_ci  const filePath = pathDir + "/test.txt";
5529e41f4b71Sopenharmony_ci  const randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
5530e41f4b71Sopenharmony_ci  const rs = randomAccessFile.getReadStream();
5531e41f4b71Sopenharmony_ci  rs.close();
5532e41f4b71Sopenharmony_ci  randomAccessFile.close();
5533e41f4b71Sopenharmony_ci  ```
5534e41f4b71Sopenharmony_ci
5535e41f4b71Sopenharmony_ci### getWriteStream<sup>12+</sup>
5536e41f4b71Sopenharmony_ci
5537e41f4b71Sopenharmony_cigetWriteStream(): WriteStream;
5538e41f4b71Sopenharmony_ci
5539e41f4b71Sopenharmony_ci获取当前 RandomAccessFile 的一个 WriteStream 实例。
5540e41f4b71Sopenharmony_ci
5541e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5542e41f4b71Sopenharmony_ci
5543e41f4b71Sopenharmony_ci**返回值:**
5544e41f4b71Sopenharmony_ci
5545e41f4b71Sopenharmony_ci  | 类型                | 说明        |
5546e41f4b71Sopenharmony_ci  | ------------------ | --------- |
5547e41f4b71Sopenharmony_ci  | [WriteStream](#writestream12) | 文件可写流 |
5548e41f4b71Sopenharmony_ci
5549e41f4b71Sopenharmony_ci**示例:**
5550e41f4b71Sopenharmony_ci
5551e41f4b71Sopenharmony_ci  ```ts
5552e41f4b71Sopenharmony_ci  const filePath = pathDir + "/test.txt";
5553e41f4b71Sopenharmony_ci  const randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
5554e41f4b71Sopenharmony_ci  const ws = randomAccessFile.getWriteStream();
5555e41f4b71Sopenharmony_ci  ws.close();
5556e41f4b71Sopenharmony_ci  randomAccessFile.close();
5557e41f4b71Sopenharmony_ci  ```
5558e41f4b71Sopenharmony_ci
5559e41f4b71Sopenharmony_ci
5560e41f4b71Sopenharmony_ci## Watcher<sup>10+</sup>
5561e41f4b71Sopenharmony_ci
5562e41f4b71Sopenharmony_ci文件目录变化监听对象。由createWatcher接口获得。
5563e41f4b71Sopenharmony_ci
5564e41f4b71Sopenharmony_ci### start<sup>10+</sup>
5565e41f4b71Sopenharmony_ci
5566e41f4b71Sopenharmony_cistart(): void
5567e41f4b71Sopenharmony_ci
5568e41f4b71Sopenharmony_ci开启监听。
5569e41f4b71Sopenharmony_ci
5570e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5571e41f4b71Sopenharmony_ci
5572e41f4b71Sopenharmony_ci**错误码:**
5573e41f4b71Sopenharmony_ci
5574e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5575e41f4b71Sopenharmony_ci
5576e41f4b71Sopenharmony_ci**示例:**
5577e41f4b71Sopenharmony_ci
5578e41f4b71Sopenharmony_ci  ```ts
5579e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5580e41f4b71Sopenharmony_ci  let watcher = fs.createWatcher(filePath, 0xfff, () => {});
5581e41f4b71Sopenharmony_ci  watcher.start();
5582e41f4b71Sopenharmony_ci  watcher.stop();
5583e41f4b71Sopenharmony_ci  ```
5584e41f4b71Sopenharmony_ci
5585e41f4b71Sopenharmony_ci### stop<sup>10+</sup>
5586e41f4b71Sopenharmony_ci
5587e41f4b71Sopenharmony_cistop(): void
5588e41f4b71Sopenharmony_ci
5589e41f4b71Sopenharmony_ci停止监听。
5590e41f4b71Sopenharmony_ci
5591e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5592e41f4b71Sopenharmony_ci
5593e41f4b71Sopenharmony_ci**错误码:**
5594e41f4b71Sopenharmony_ci
5595e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5596e41f4b71Sopenharmony_ci
5597e41f4b71Sopenharmony_ci**示例:**
5598e41f4b71Sopenharmony_ci
5599e41f4b71Sopenharmony_ci  ```ts
5600e41f4b71Sopenharmony_ci  let filePath = pathDir + "/test.txt";
5601e41f4b71Sopenharmony_ci  let watcher = fs.createWatcher(filePath, 0xfff, () => {});
5602e41f4b71Sopenharmony_ci  watcher.start();
5603e41f4b71Sopenharmony_ci  watcher.stop();
5604e41f4b71Sopenharmony_ci  ```
5605e41f4b71Sopenharmony_ci
5606e41f4b71Sopenharmony_ci## OpenMode
5607e41f4b71Sopenharmony_ci
5608e41f4b71Sopenharmony_ciopen接口flags参数常量。文件打开标签。
5609e41f4b71Sopenharmony_ci
5610e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5611e41f4b71Sopenharmony_ci
5612e41f4b71Sopenharmony_ci| 名称   | 类型   | 值  | 说明      |
5613e41f4b71Sopenharmony_ci| ---- | ------ |---- | ------- |
5614e41f4b71Sopenharmony_ci| READ_ONLY | number |  0o0   | 只读打开。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
5615e41f4b71Sopenharmony_ci| WRITE_ONLY | number | 0o1    | 只写打开。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
5616e41f4b71Sopenharmony_ci| READ_WRITE | number | 0o2    | 读写打开。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
5617e41f4b71Sopenharmony_ci| CREATE | number | 0o100    | 若文件不存在,则创建文件。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
5618e41f4b71Sopenharmony_ci| TRUNC | number | 0o1000    | 如果文件存在且以只写或读写的方式打开文件,则将其长度裁剪为零。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
5619e41f4b71Sopenharmony_ci| APPEND | number | 0o2000   | 以追加方式打开,后续写将追加到文件末尾。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
5620e41f4b71Sopenharmony_ci| NONBLOCK | number | 0o4000    | 如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续 IO 进行非阻塞操作。 |
5621e41f4b71Sopenharmony_ci| DIR | number | 0o200000    | 如果path不指向目录,则出错。 |
5622e41f4b71Sopenharmony_ci| NOFOLLOW | number | 0o400000    | 如果path指向符号链接,则出错。 |
5623e41f4b71Sopenharmony_ci| SYNC | number | 0o4010000    | 以同步IO的方式打开文件。 |
5624e41f4b71Sopenharmony_ci
5625e41f4b71Sopenharmony_ci## Filter<sup>10+</sup>
5626e41f4b71Sopenharmony_ci
5627e41f4b71Sopenharmony_ci文件过滤配置项类型,支持listFile接口使用。
5628e41f4b71Sopenharmony_ci
5629e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
5630e41f4b71Sopenharmony_ci
5631e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5632e41f4b71Sopenharmony_ci
5633e41f4b71Sopenharmony_ci| 名称        | 类型       | 必选       | 说明                |
5634e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ |
5635e41f4b71Sopenharmony_ci| suffix | Array&lt;string&gt;     | 否 | 文件后缀名完全匹配,各个关键词OR关系。           |
5636e41f4b71Sopenharmony_ci| displayName    | Array&lt;string&gt;     | 否 | 文件名模糊匹配,各个关键词OR关系。当前仅支持通配符*。 |
5637e41f4b71Sopenharmony_ci| mimeType    | Array&lt;string&gt; | 否 | mime类型完全匹配,各个关键词OR关系。       |
5638e41f4b71Sopenharmony_ci| fileSizeOver    | number | 否 | 文件大小匹配,大于等于指定大小的文件。       |
5639e41f4b71Sopenharmony_ci| lastModifiedAfter    | number | 否 | 文件最近修改时间匹配,在指定时间点及之后的文件。       |
5640e41f4b71Sopenharmony_ci| excludeMedia    | boolean | 否 | 是否排除Media中已有的文件。       |
5641e41f4b71Sopenharmony_ci
5642e41f4b71Sopenharmony_ci## ConflictFiles<sup>10+</sup>
5643e41f4b71Sopenharmony_ci
5644e41f4b71Sopenharmony_ci冲突文件信息,支持copyDir及moveDir接口使用。
5645e41f4b71Sopenharmony_ci
5646e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5647e41f4b71Sopenharmony_ci
5648e41f4b71Sopenharmony_ci| 名称        | 类型       | 说明                |
5649e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ |
5650e41f4b71Sopenharmony_ci| srcFile | string     | 源冲突文件路径。           |
5651e41f4b71Sopenharmony_ci| destFile    | string     | 目标冲突文件路径。 |
5652e41f4b71Sopenharmony_ci
5653e41f4b71Sopenharmony_ci## Options<sup>11+</sup>
5654e41f4b71Sopenharmony_ci
5655e41f4b71Sopenharmony_ci可选项类型,支持readLines接口使用。
5656e41f4b71Sopenharmony_ci
5657e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5658e41f4b71Sopenharmony_ci
5659e41f4b71Sopenharmony_ci| 名称        | 类型       | 说明                |
5660e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ |
5661e41f4b71Sopenharmony_ci| encoding | string     | 文件编码方式。可选项。           |
5662e41f4b71Sopenharmony_ci
5663e41f4b71Sopenharmony_ci## WhenceType<sup>11+</sup>
5664e41f4b71Sopenharmony_ci
5665e41f4b71Sopenharmony_ci枚举,文件偏移指针相对偏移位置类型,支持lseek接口使用。
5666e41f4b71Sopenharmony_ci
5667e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5668e41f4b71Sopenharmony_ci
5669e41f4b71Sopenharmony_ci| 名称        | 值       | 说明                |
5670e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ |
5671e41f4b71Sopenharmony_ci| SEEK_SET | 0     | 文件起始位置处。           |
5672e41f4b71Sopenharmony_ci| SEEK_CUR    | 1     | 当前文件偏置指针位置处。 |
5673e41f4b71Sopenharmony_ci| SEEK_END    | 2     | 文件末尾位置处。 |
5674e41f4b71Sopenharmony_ci
5675e41f4b71Sopenharmony_ci## LocationType<sup>11+</sup>
5676e41f4b71Sopenharmony_ci
5677e41f4b71Sopenharmony_ci枚举,文件位置,表示该文件是否在本地或者云端存在。
5678e41f4b71Sopenharmony_ci
5679e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5680e41f4b71Sopenharmony_ci
5681e41f4b71Sopenharmony_ci| 名称        | 值       | 说明                |
5682e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ |
5683e41f4b71Sopenharmony_ci| LOCAl | 1     | 文件在本地存在           |
5684e41f4b71Sopenharmony_ci| CLOUD    | 2     | 文件在云端存在 |
5685e41f4b71Sopenharmony_ci
5686e41f4b71Sopenharmony_ci## AccessModeType<sup>12+</sup>
5687e41f4b71Sopenharmony_ci
5688e41f4b71Sopenharmony_ci枚举,表示需要校验的具体权限,若不填,默认校验文件是否存在。
5689e41f4b71Sopenharmony_ci
5690e41f4b71Sopenharmony_ci**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
5691e41f4b71Sopenharmony_ci
5692e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5693e41f4b71Sopenharmony_ci
5694e41f4b71Sopenharmony_ci| 名称        | 值       | 说明                |
5695e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ |
5696e41f4b71Sopenharmony_ci| EXIST | 0     | 文件是否存在           |
5697e41f4b71Sopenharmony_ci| WRITE    | 2     | 文件是否具有写入权限 |
5698e41f4b71Sopenharmony_ci| READ    | 4     | 文件是否具有读取权限 |
5699e41f4b71Sopenharmony_ci| READ_WRITE    | 6     | 文件是否具有读写权限 |
5700e41f4b71Sopenharmony_ci
5701e41f4b71Sopenharmony_ci## AccessFlagType<sup>12+</sup>
5702e41f4b71Sopenharmony_ci
5703e41f4b71Sopenharmony_ci枚举,表示需要校验的文件位置
5704e41f4b71Sopenharmony_ci
5705e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5706e41f4b71Sopenharmony_ci
5707e41f4b71Sopenharmony_ci| 名称        | 值       | 说明                |
5708e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ |
5709e41f4b71Sopenharmony_ci| LOCAL | 0     | 文件是否在本地          |
5710e41f4b71Sopenharmony_ci
5711e41f4b71Sopenharmony_ci## ReadOptions<sup>11+</sup>
5712e41f4b71Sopenharmony_ci
5713e41f4b71Sopenharmony_ci可选项类型,支持read接口使用。
5714e41f4b71Sopenharmony_ci
5715e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
5716e41f4b71Sopenharmony_ci
5717e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5718e41f4b71Sopenharmony_ci
5719e41f4b71Sopenharmony_ci| 名称        | 类型       | 必选       | 说明                |
5720e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ |------------------ |
5721e41f4b71Sopenharmony_ci| length | number     | 否 | 期望读取数据的长度,单位为字节。可选,默认缓冲区长度。           |
5722e41f4b71Sopenharmony_ci|  offset    | number     | 否 | 期望读取文件位置,单位为字节(基于当前filePointer加上offset的位置)。可选,默认从偏置指针(filePointer)开始读。 |
5723e41f4b71Sopenharmony_ci
5724e41f4b71Sopenharmony_ci## ReadTextOptions<sup>11+</sup>
5725e41f4b71Sopenharmony_ci
5726e41f4b71Sopenharmony_ci可选项类型,支持readText接口使用,ReadTextOptions继承至[ReadOptions](#readoptions11)。
5727e41f4b71Sopenharmony_ci
5728e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5729e41f4b71Sopenharmony_ci
5730e41f4b71Sopenharmony_ci| 名称        | 类型       | 必选       | 说明                |
5731e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ |
5732e41f4b71Sopenharmony_ci| length | number     | 否 | 期望读取数据的长度,单位为字节。可选,默认文件长度。           |
5733e41f4b71Sopenharmony_ci|  offset    | number     | 否 | 期望读取文件的位置,单位为字节。可选,默认从当前位置开始读取。 |
5734e41f4b71Sopenharmony_ci| encoding    | string | 否 | 当数据是 string 类型时有效,表示数据的编码方式,默认 'utf-8',仅支持 'utf-8'。   <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。    |
5735e41f4b71Sopenharmony_ci
5736e41f4b71Sopenharmony_ci## WriteOptions<sup>11+</sup>
5737e41f4b71Sopenharmony_ci
5738e41f4b71Sopenharmony_ci可选项类型,支持write接口使用,WriteOptions继承至[Options](#options11)。
5739e41f4b71Sopenharmony_ci
5740e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5741e41f4b71Sopenharmony_ci
5742e41f4b71Sopenharmony_ci| 名称        | 类型       | 必选       | 说明                |
5743e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ |
5744e41f4b71Sopenharmony_ci| length | number     | 否 | 期望写入数据的长度,单位为字节。可选,默认缓冲区长度。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。           |
5745e41f4b71Sopenharmony_ci|  offset    | number     | 否 | 期望写入文件位置,单位为字节(基于当前filePointer加上offset的位置)。可选,默认从偏置指针(filePointer)开始写。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
5746e41f4b71Sopenharmony_ci| encoding    | string | 否 | 当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。仅支持 'utf-8'。       |
5747e41f4b71Sopenharmony_ci
5748e41f4b71Sopenharmony_ci## ListFileOptions<sup>11+</sup>
5749e41f4b71Sopenharmony_ci
5750e41f4b71Sopenharmony_ci可选项类型,支持listFile接口使用。
5751e41f4b71Sopenharmony_ci
5752e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
5753e41f4b71Sopenharmony_ci
5754e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5755e41f4b71Sopenharmony_ci
5756e41f4b71Sopenharmony_ci| 名称        | 类型       | 必选       |  说明                |
5757e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ |
5758e41f4b71Sopenharmony_ci| recursion | boolean     | 否 | 是否递归子目录下文件名。可选,默认为false。当recursion为false时,返回当前目录下满足过滤要求的文件名及文件夹名。当recursion为true时,返回此目录下所有满足过滤要求的文件的相对路径(以/开头)。           |
5759e41f4b71Sopenharmony_ci|  listNum    | number     | 否 | 列出文件名数量。可选,当设置0时,列出所有文件,默认为0。 |
5760e41f4b71Sopenharmony_ci| filter    | [Filter](#filter10) | 否 | 文件过滤配置项类型。 可选,设置文件的过滤条件 |
5761e41f4b71Sopenharmony_ci
5762e41f4b71Sopenharmony_ci## ReadStream<sup>12+</sup>
5763e41f4b71Sopenharmony_ci
5764e41f4b71Sopenharmony_ci文件可读流,需要先通过[fs.createReadStream](#fscreatereadstream12)方法来构建一个ReadStream实例。
5765e41f4b71Sopenharmony_ci
5766e41f4b71Sopenharmony_ci### 属性
5767e41f4b71Sopenharmony_ci
5768e41f4b71Sopenharmony_ci| 名称     | 类型   | 只读   | 可写   | 说明                                       |
5769e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ---------------------------------------- |
5770e41f4b71Sopenharmony_ci| bytesRead    | number | 是    | 否    | 可读流已经读取的字节数 |
5771e41f4b71Sopenharmony_ci| path    | string | 是    | 否    | 当前可读流对应的文件路径 |
5772e41f4b71Sopenharmony_ci
5773e41f4b71Sopenharmony_ci### Seek
5774e41f4b71Sopenharmony_ci
5775e41f4b71Sopenharmony_ciseek(offset: number, whence?: WhenceType): number;
5776e41f4b71Sopenharmony_ci
5777e41f4b71Sopenharmony_ci
5778e41f4b71Sopenharmony_ci调整可读流偏置指针位置。
5779e41f4b71Sopenharmony_ci
5780e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5781e41f4b71Sopenharmony_ci
5782e41f4b71Sopenharmony_ci**参数:**
5783e41f4b71Sopenharmony_ci
5784e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
5785e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
5786e41f4b71Sopenharmony_ci  | offset | number | 是    | 相对偏移位置,单位为字节。 |
5787e41f4b71Sopenharmony_ci  | whence | [WhenceType](#whencetype11) | 否    | 偏移指针相对位置类型,默认值:SEEK_SET,文件起始位置处。 |
5788e41f4b71Sopenharmony_ci
5789e41f4b71Sopenharmony_ci**返回值:**
5790e41f4b71Sopenharmony_ci
5791e41f4b71Sopenharmony_ci  | 类型                   | 说明         |
5792e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
5793e41f4b71Sopenharmony_ci  | number | 当前可读流偏置指针位置(相对于文件头的偏移量,单位为字节)。 |
5794e41f4b71Sopenharmony_ci
5795e41f4b71Sopenharmony_ci**错误码:**
5796e41f4b71Sopenharmony_ci
5797e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5798e41f4b71Sopenharmony_ci
5799e41f4b71Sopenharmony_ci**示例:**
5800e41f4b71Sopenharmony_ci
5801e41f4b71Sopenharmony_ci  ```ts
5802e41f4b71Sopenharmony_ci  const filePath = pathDir + "/test.txt";
5803e41f4b71Sopenharmony_ci  const rs = fs.createReadStream(filePath);
5804e41f4b71Sopenharmony_ci  const curOff = rs.seek(5, fs.WhenceType.SEEK_SET);
5805e41f4b71Sopenharmony_ci  console.info(`current offset is ${curOff}`);
5806e41f4b71Sopenharmony_ci  rs.close();
5807e41f4b71Sopenharmony_ci  ```
5808e41f4b71Sopenharmony_ci
5809e41f4b71Sopenharmony_ci### close
5810e41f4b71Sopenharmony_ci
5811e41f4b71Sopenharmony_ciclose(): void
5812e41f4b71Sopenharmony_ci
5813e41f4b71Sopenharmony_ci关闭可读流。
5814e41f4b71Sopenharmony_ci
5815e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5816e41f4b71Sopenharmony_ci
5817e41f4b71Sopenharmony_ci**错误码:**
5818e41f4b71Sopenharmony_ci
5819e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5820e41f4b71Sopenharmony_ci
5821e41f4b71Sopenharmony_ci**示例:**
5822e41f4b71Sopenharmony_ci
5823e41f4b71Sopenharmony_ci  ```ts
5824e41f4b71Sopenharmony_ci  const filePath = pathDir + "/test.txt";
5825e41f4b71Sopenharmony_ci  const rs = fs.createReadStream(filePath);
5826e41f4b71Sopenharmony_ci  rs.close();
5827e41f4b71Sopenharmony_ci  ```
5828e41f4b71Sopenharmony_ci
5829e41f4b71Sopenharmony_ci## WriteStream<sup>12+</sup>
5830e41f4b71Sopenharmony_ci
5831e41f4b71Sopenharmony_ci文件可写流,需要先通过[fs.createWriteStream](#fscreatewritestream12)方法来构建一个WriteStream实例。
5832e41f4b71Sopenharmony_ci
5833e41f4b71Sopenharmony_ci### 属性
5834e41f4b71Sopenharmony_ci
5835e41f4b71Sopenharmony_ci| 名称     | 类型   | 只读   | 可写   | 说明                                       |
5836e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ---------------------------------------- |
5837e41f4b71Sopenharmony_ci| bytesWritten    | number | 是    | 否    | 可写流已经写入的字节数 |
5838e41f4b71Sopenharmony_ci| path    | string | 是    | 否    | 当前可写流对应的文件路径 |
5839e41f4b71Sopenharmony_ci
5840e41f4b71Sopenharmony_ci### Seek
5841e41f4b71Sopenharmony_ci
5842e41f4b71Sopenharmony_ciseek(offset: number, whence?: WhenceType): number;
5843e41f4b71Sopenharmony_ci
5844e41f4b71Sopenharmony_ci调整可写流偏置指针位置。
5845e41f4b71Sopenharmony_ci
5846e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5847e41f4b71Sopenharmony_ci
5848e41f4b71Sopenharmony_ci**参数:**
5849e41f4b71Sopenharmony_ci
5850e41f4b71Sopenharmony_ci  | 参数名    | 类型     | 必填   | 说明                          |
5851e41f4b71Sopenharmony_ci  | ------ | ------ | ---- | --------------------------- |
5852e41f4b71Sopenharmony_ci  | offset | number | 是    | 相对偏移位置,单位为字节。 |
5853e41f4b71Sopenharmony_ci  | whence | [WhenceType](#whencetype11) | 否    | 偏移指针相对位置类型,默认值:SEEK_SET,文件起始位置处。 |
5854e41f4b71Sopenharmony_ci
5855e41f4b71Sopenharmony_ci**返回值:**
5856e41f4b71Sopenharmony_ci
5857e41f4b71Sopenharmony_ci  | 类型                   | 说明         |
5858e41f4b71Sopenharmony_ci  | --------------------- | ---------- |
5859e41f4b71Sopenharmony_ci  | number | 当前可写流偏置指针位置(相对于文件头的偏移量,单位为字节)。 |
5860e41f4b71Sopenharmony_ci
5861e41f4b71Sopenharmony_ci**错误码:**
5862e41f4b71Sopenharmony_ci
5863e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5864e41f4b71Sopenharmony_ci
5865e41f4b71Sopenharmony_ci**示例:**
5866e41f4b71Sopenharmony_ci
5867e41f4b71Sopenharmony_ci  ```ts
5868e41f4b71Sopenharmony_ci  const filePath = pathDir + "/test.txt";
5869e41f4b71Sopenharmony_ci  const ws = fs.createWriteStream(filePath);
5870e41f4b71Sopenharmony_ci  const curOff = ws.seek(5, fs.WhenceType.SEEK_SET);
5871e41f4b71Sopenharmony_ci  console.info(`current offset is ${curOff}`);
5872e41f4b71Sopenharmony_ci  ws.close();
5873e41f4b71Sopenharmony_ci  ```
5874e41f4b71Sopenharmony_ci
5875e41f4b71Sopenharmony_ci### close
5876e41f4b71Sopenharmony_ci
5877e41f4b71Sopenharmony_ciclose(): void
5878e41f4b71Sopenharmony_ci
5879e41f4b71Sopenharmony_ci关闭可写流。
5880e41f4b71Sopenharmony_ci
5881e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5882e41f4b71Sopenharmony_ci
5883e41f4b71Sopenharmony_ci**错误码:**
5884e41f4b71Sopenharmony_ci
5885e41f4b71Sopenharmony_ci接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
5886e41f4b71Sopenharmony_ci
5887e41f4b71Sopenharmony_ci**示例:**
5888e41f4b71Sopenharmony_ci
5889e41f4b71Sopenharmony_ci  ```ts
5890e41f4b71Sopenharmony_ci  const filePath = pathDir + "/test.txt";
5891e41f4b71Sopenharmony_ci  const ws = fs.createWriteStream(filePath);
5892e41f4b71Sopenharmony_ci  ws.close();
5893e41f4b71Sopenharmony_ci  ```
5894e41f4b71Sopenharmony_ci
5895e41f4b71Sopenharmony_ci## RandomAccessFileOptions<sup>12+</sup>
5896e41f4b71Sopenharmony_ci
5897e41f4b71Sopenharmony_ci可选项类型,支持 createRandomAccessFile 接口使用。
5898e41f4b71Sopenharmony_ci
5899e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5900e41f4b71Sopenharmony_ci
5901e41f4b71Sopenharmony_ci| 名称        | 类型       | 必选       |  说明                |
5902e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ |
5903e41f4b71Sopenharmony_ci| start   | number     | 否 | 表示期望读取文件的位置,单位为字节。可选,默认从当前位置开始读。           |
5904e41f4b71Sopenharmony_ci| end     | number     | 否 |  表示期望读取结束的位置,单位为字节。可选,默认文件末尾。 |
5905e41f4b71Sopenharmony_ci
5906e41f4b71Sopenharmony_ci## ReadStreamOptions<sup>12+</sup>
5907e41f4b71Sopenharmony_ci
5908e41f4b71Sopenharmony_ci可选项类型,支持 createReadStream 接口使用。
5909e41f4b71Sopenharmony_ci
5910e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5911e41f4b71Sopenharmony_ci
5912e41f4b71Sopenharmony_ci| 名称        | 类型       | 必选       |  说明                |
5913e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ |
5914e41f4b71Sopenharmony_ci| start   | number     | 否 | 表示期望读取文件的位置,单位为字节。可选,默认从当前位置开始读。           |
5915e41f4b71Sopenharmony_ci| end     | number     | 否 |  表示期望读取结束的位置,单位为字节。可选,默认文件末尾。 |
5916e41f4b71Sopenharmony_ci
5917e41f4b71Sopenharmony_ci## WriteStreamOptions<sup>12+</sup>
5918e41f4b71Sopenharmony_ci
5919e41f4b71Sopenharmony_ci可选项类型,支持 createWriteStream 接口使用。
5920e41f4b71Sopenharmony_ci
5921e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.File.FileIO
5922e41f4b71Sopenharmony_ci
5923e41f4b71Sopenharmony_ci| 名称        | 类型       | 必选       |  说明                |
5924e41f4b71Sopenharmony_ci| ----------- | --------------- | ------------------ | ------------------ |
5925e41f4b71Sopenharmony_ci| start   | number     | 否 | 表示期望写入文件的位置,单位为字节。可选,默认文件起始位置。           |
5926e41f4b71Sopenharmony_ci| mode     | number     | 否 | 创建文件可写流的[选项](#openmode),必须指定如下选项中的一个,默认只写方式创建:<br/>-&nbsp;OpenMode.READ_ONLY(0o0):只读。<br/>-&nbsp;OpenMode.WRITE_ONLY(0o1):只写。<br/>-&nbsp;OpenMode.READ_WRITE(0o2):读写。<br/>给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:<br/>-&nbsp;OpenMode.CREATE(0o100):若文件不存在,则创建文件。<br/>-&nbsp;OpenMode.TRUNC(0o1000):如果文件存在且文件具有写权限,则将其长度裁剪为零。<br/>-&nbsp;OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到文件末尾。<br/>-&nbsp;OpenMode.NONBLOCK(0o4000):如果path指向FIFO、块特殊文件或字符特殊文件,则本次打开及后续&nbsp;IO&nbsp;进行非阻塞操作。<br/>-&nbsp;OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。<br/>-&nbsp;OpenMode.NOFOLLOW(0o400000):如果path指向符号链接,则出错。<br/>-&nbsp;OpenMode.SYNC(0o4010000):以同步IO的方式打开文件。 |
5927