1e41f4b71Sopenharmony_ci# Accessing Application Files (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThis topic describes how to enable an application to view, create, read, write, delete, move, or copy an application file and obtain file information.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci## Available APIs
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciYou can use [ohos.file.fs](../reference/apis-core-file-kit/js-apis-file-fs.md) to implement access to application files. The following table describes the commonly used APIs.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci**Table 1** APIs for basic application file operations
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci| API| Description| Category| Synchronous Programming| Asynchronous Programming|
12e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- |
13e41f4b71Sopenharmony_ci| access | Checks whether a file exists.| Method| Supported| Supported|
14e41f4b71Sopenharmony_ci| close | Closes a file.| Method| Supported| Supported|
15e41f4b71Sopenharmony_ci| copyFile | Copies a file.| Method| Supported| Supported|
16e41f4b71Sopenharmony_ci| createStream | Creates a stream based on a file path.| Method| Supported| Supported|
17e41f4b71Sopenharmony_ci| listFile | Lists all files in a directory.| Method| Supported| Supported|
18e41f4b71Sopenharmony_ci| mkdir | Creates a directory.| Method| Supported| Supported|
19e41f4b71Sopenharmony_ci| moveFile | Moves a file.| Method| Supported| Supported|
20e41f4b71Sopenharmony_ci| open | Opens a file.| Method| Supported| Supported|
21e41f4b71Sopenharmony_ci| read | Reads data from a file.| Method| Supported| Supported|
22e41f4b71Sopenharmony_ci| rename | Renames a file or folder.| Method| Supported| Supported|
23e41f4b71Sopenharmony_ci| rmdir | Deletes a directory.| Method| Supported| Supported|
24e41f4b71Sopenharmony_ci| stat | Obtains detailed file information.| Method| Supported| Supported|
25e41f4b71Sopenharmony_ci| unlink | Deletes a single file.| Method| Supported| Supported|
26e41f4b71Sopenharmony_ci| write | Writes data to a file.| Method| Supported| Supported|
27e41f4b71Sopenharmony_ci| Stream.close | Closes a stream.| Method| Supported| Supported|
28e41f4b71Sopenharmony_ci| Stream.flush | Flushes all data from this stream.| Method| Supported| Supported|
29e41f4b71Sopenharmony_ci| Stream.write | Writes data to a stream.| Method| Supported| Supported|
30e41f4b71Sopenharmony_ci| Stream.read | Reads data from a stream.| Method| Supported| Supported|
31e41f4b71Sopenharmony_ci| File.fd | Defines a file descriptor.| Attribute| N/A| N/A|
32e41f4b71Sopenharmony_ci| OpenMode | Defines the mode for opening a file.| Attribute| N/A| N/A|
33e41f4b71Sopenharmony_ci| Filter | Defines the options for filtering files.| Type| N/A| N/A|
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci## Development Example
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ciBefore performing any file operation, obtain the [application file path](../application-models/application-context-stage.md#obtaining-application-file-paths). The following example shows how to obtain a HAP file path using **UIAbilityContext**. For details about how to obtain **UIAbilityContext**, see [Obtaining the Context of UIAbility](../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciThe following walks you through on how to perform common file operations.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci### Creating, Reading, and Writing a File
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ciThe following example demonstrates how to create a file, read data from it, and write data to it.
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci```ts
46e41f4b71Sopenharmony_ci// pages/xxx.ets
47e41f4b71Sopenharmony_ciimport { fileIo as fs, ReadOptions } from '@kit.CoreFileKit';
48e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
49e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS';
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci// Obtain the application file path.
52e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext;
53e41f4b71Sopenharmony_cilet filesDir = context.filesDir;
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_cifunction createFile(): void {
56e41f4b71Sopenharmony_ci  // Create a file and open it.
57e41f4b71Sopenharmony_ci  let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
58e41f4b71Sopenharmony_ci  // Write data to the file.
59e41f4b71Sopenharmony_ci  let writeLen = fs.writeSync(file.fd, "Try to write str.");
60e41f4b71Sopenharmony_ci  console.info("The length of str is: " + writeLen);
61e41f4b71Sopenharmony_ci  // Read data from the file.
62e41f4b71Sopenharmony_ci  let arrayBuffer = new ArrayBuffer(1024);
63e41f4b71Sopenharmony_ci  let readOptions: ReadOptions = {
64e41f4b71Sopenharmony_ci    offset: 0,
65e41f4b71Sopenharmony_ci    length: arrayBuffer.byteLength
66e41f4b71Sopenharmony_ci  };
67e41f4b71Sopenharmony_ci  let readLen = fs.readSync(file.fd, arrayBuffer, readOptions);
68e41f4b71Sopenharmony_ci  let buf = buffer.from(arrayBuffer, 0, readLen);
69e41f4b71Sopenharmony_ci  console.info("the content of file: " + buf.toString());
70e41f4b71Sopenharmony_ci  // Close the file.
71e41f4b71Sopenharmony_ci  fs.closeSync(file);
72e41f4b71Sopenharmony_ci}
73e41f4b71Sopenharmony_ci```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci### Copying Data to Another File
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ciThe following example demonstrates how to read data from a file and copy it to another file.
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci```ts
80e41f4b71Sopenharmony_ci// pages/xxx.ets
81e41f4b71Sopenharmony_ciimport { fileIo as fs, ReadOptions, WriteOptions } from '@kit.CoreFileKit';
82e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci// Obtain the application file path.
85e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext;
86e41f4b71Sopenharmony_cilet filesDir = context.filesDir;
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_cifunction readWriteFile(): void {
89e41f4b71Sopenharmony_ci  // Open the source and destination files.
90e41f4b71Sopenharmony_ci  let srcFile = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
91e41f4b71Sopenharmony_ci  let destFile = fs.openSync(filesDir + '/destFile.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
92e41f4b71Sopenharmony_ci  // Read data from the source file and copy it to the destination file.
93e41f4b71Sopenharmony_ci  let bufSize = 4096;
94e41f4b71Sopenharmony_ci  let readSize = 0;
95e41f4b71Sopenharmony_ci  let buf = new ArrayBuffer(bufSize);
96e41f4b71Sopenharmony_ci  let readOptions: ReadOptions = {
97e41f4b71Sopenharmony_ci    offset: readSize,
98e41f4b71Sopenharmony_ci    length: bufSize
99e41f4b71Sopenharmony_ci  };
100e41f4b71Sopenharmony_ci  let readLen = fs.readSync(srcFile.fd, buf, readOptions);
101e41f4b71Sopenharmony_ci  while (readLen > 0) {
102e41f4b71Sopenharmony_ci    readSize += readLen;
103e41f4b71Sopenharmony_ci    let writeOptions: WriteOptions = {
104e41f4b71Sopenharmony_ci      length: readLen
105e41f4b71Sopenharmony_ci    };
106e41f4b71Sopenharmony_ci    fs.writeSync(destFile.fd, buf, writeOptions);
107e41f4b71Sopenharmony_ci    readOptions.offset = readSize;
108e41f4b71Sopenharmony_ci    readLen = fs.readSync(srcFile.fd, buf, readOptions);
109e41f4b71Sopenharmony_ci  }
110e41f4b71Sopenharmony_ci  // Close the files.
111e41f4b71Sopenharmony_ci  fs.closeSync(srcFile);
112e41f4b71Sopenharmony_ci  fs.closeSync(destFile);
113e41f4b71Sopenharmony_ci}
114e41f4b71Sopenharmony_ci```
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci> **NOTE**
117e41f4b71Sopenharmony_ci>
118e41f4b71Sopenharmony_ci> When using **read()** or **write()**, pay attention to the optional parameter **offset**. For a file that has been read or written, **offset** points to the end position of the last read or write operation by default.
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci### Reading and Writing Files in a Stream
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ciThe following example demonstrates how to read and write file data using a stream.
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci```ts
125e41f4b71Sopenharmony_ci// pages/xxx.ets
126e41f4b71Sopenharmony_ciimport { fileIo as fs, ReadOptions } from '@kit.CoreFileKit';
127e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci// Obtain the application file path.
130e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext;
131e41f4b71Sopenharmony_cilet filesDir = context.filesDir;
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ciasync function readWriteFileWithStream(): Promise<void> {
134e41f4b71Sopenharmony_ci  // Open the file streams.
135e41f4b71Sopenharmony_ci  let inputStream = fs.createStreamSync(filesDir + '/test.txt', 'r+');
136e41f4b71Sopenharmony_ci  let outputStream = fs.createStreamSync(filesDir + '/destFile.txt', "w+");
137e41f4b71Sopenharmony_ci  // Read data from the source file and write the data to the destination file using a stream.
138e41f4b71Sopenharmony_ci  let bufSize = 4096;
139e41f4b71Sopenharmony_ci  let readSize = 0;
140e41f4b71Sopenharmony_ci  let buf = new ArrayBuffer(bufSize);
141e41f4b71Sopenharmony_ci  let readOptions: ReadOptions = {
142e41f4b71Sopenharmony_ci    offset: readSize,
143e41f4b71Sopenharmony_ci    length: bufSize
144e41f4b71Sopenharmony_ci  };
145e41f4b71Sopenharmony_ci  let readLen = await inputStream.read(buf, readOptions);
146e41f4b71Sopenharmony_ci  readSize += readLen;
147e41f4b71Sopenharmony_ci  while (readLen > 0) {
148e41f4b71Sopenharmony_ci    const writeBuf = readLen < bufSize ? buf.slice(0, readLen) : buf;
149e41f4b71Sopenharmony_ci    await outputStream.write(writeBuf);
150e41f4b71Sopenharmony_ci    readOptions.offset = readSize;
151e41f4b71Sopenharmony_ci    readLen = await inputStream.read(buf, readOptions);
152e41f4b71Sopenharmony_ci    readSize += readLen;
153e41f4b71Sopenharmony_ci  }
154e41f4b71Sopenharmony_ci  // Close the streams.
155e41f4b71Sopenharmony_ci  inputStream.closeSync();
156e41f4b71Sopenharmony_ci  outputStream.closeSync();
157e41f4b71Sopenharmony_ci}
158e41f4b71Sopenharmony_ci```
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci> **NOTE**
161e41f4b71Sopenharmony_ci>
162e41f4b71Sopenharmony_ci> - Close the stream once it is not required.
163e41f4b71Sopenharmony_ci> - Comply with the programming specifications for **Stream** APIs in asynchronous mode and avoid mixed use of the APIs in synchronous mode and asynchronous mode. 
164e41f4b71Sopenharmony_ci> - The **Stream** APIs do not support concurrent read and write operations.
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci### Listing Files
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ciThe following example demonstrates how to list files that meet the specified conditions.
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci```ts
171e41f4b71Sopenharmony_ciimport { fileIo as fs, Filter, ListFileOptions } from '@kit.CoreFileKit';
172e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci// Obtain the application file path.
175e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext;
176e41f4b71Sopenharmony_cilet filesDir = context.filesDir;
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci// List files that meet the specified conditions.
179e41f4b71Sopenharmony_cifunction getListFile(): void {
180e41f4b71Sopenharmony_ci  let listFileOption: ListFileOptions = {
181e41f4b71Sopenharmony_ci    recursion: false,
182e41f4b71Sopenharmony_ci    listNum: 0,
183e41f4b71Sopenharmony_ci    filter: {
184e41f4b71Sopenharmony_ci      suffix: [".png", ".jpg", ".txt"],
185e41f4b71Sopenharmony_ci      displayName: ["test*"],
186e41f4b71Sopenharmony_ci      fileSizeOver: 0,
187e41f4b71Sopenharmony_ci      lastModifiedAfter: new Date(0).getTime()
188e41f4b71Sopenharmony_ci    }
189e41f4b71Sopenharmony_ci  };
190e41f4b71Sopenharmony_ci  let files = fs.listFileSync(filesDir, listFileOption);
191e41f4b71Sopenharmony_ci  for (let i = 0; i < files.length; i++) {
192e41f4b71Sopenharmony_ci    console.info(`The name of file: ${files[i]}`);
193e41f4b71Sopenharmony_ci  }
194e41f4b71Sopenharmony_ci}
195e41f4b71Sopenharmony_ci```
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci### Using File Streams
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ciThe following sample code demonstrates how to use readable and writable streams.
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci```ts
202e41f4b71Sopenharmony_ci// pages/xxx.ets
203e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit';
204e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci// Obtain the application file path.
207e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext;
208e41f4b71Sopenharmony_cilet filesDir = context.filesDir;
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_cifunction copyFileWithReadable(): void {
211e41f4b71Sopenharmony_ci  // Create a readable stream.
212e41f4b71Sopenharmony_ci  const rs = fs.createReadStream(`${filesDir}/read.txt`);
213e41f4b71Sopenharmony_ci  // Create a writable stream.
214e41f4b71Sopenharmony_ci  const ws = fs.createWriteStream(`${filesDir}/write.txt`);
215e41f4b71Sopenharmony_ci  // Copy files in paused mode. 
216e41f4b71Sopenharmony_ci  rs.on('readable', () => {
217e41f4b71Sopenharmony_ci    const data = rs.read();
218e41f4b71Sopenharmony_ci    if (!data) {
219e41f4b71Sopenharmony_ci      return;
220e41f4b71Sopenharmony_ci    }
221e41f4b71Sopenharmony_ci    ws.write(data);
222e41f4b71Sopenharmony_ci  });
223e41f4b71Sopenharmony_ci}
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_cifunction copyFileWithData(): void {
226e41f4b71Sopenharmony_ci  // Create a readable stream.
227e41f4b71Sopenharmony_ci  const rs = fs.createReadStream(`${filesDir}/read.txt`);
228e41f4b71Sopenharmony_ci  // Create a writable stream.
229e41f4b71Sopenharmony_ci  const ws = fs.createWriteStream(`${filesDir}/write.txt`);
230e41f4b71Sopenharmony_ci  // Copy files in flowing mode.
231e41f4b71Sopenharmony_ci  rs.on('data', (emitData) => {
232e41f4b71Sopenharmony_ci    const data = emitData?.data;
233e41f4b71Sopenharmony_ci    if (!data) {
234e41f4b71Sopenharmony_ci      return;
235e41f4b71Sopenharmony_ci    }
236e41f4b71Sopenharmony_ci    ws.write(data as Uint8Array);
237e41f4b71Sopenharmony_ci  });
238e41f4b71Sopenharmony_ci}
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci```
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ciThe following code demonstrates how to use a file hash stream.
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci```ts
245e41f4b71Sopenharmony_ci// pages/xxx.ets
246e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit';
247e41f4b71Sopenharmony_ciimport { hash } from '@kit.CoreFileKit';
248e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci// Obtain the application file path.
251e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext;
252e41f4b71Sopenharmony_cilet filesDir = context.filesDir;
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_cifunction hashFileWithStream() {
255e41f4b71Sopenharmony_ci  const filePath = `${filesDir}/test.txt`;
256e41f4b71Sopenharmony_ci  // Create a readable stream.
257e41f4b71Sopenharmony_ci  const rs = fs.createReadStream(filePath);
258e41f4b71Sopenharmony_ci  // Create a hash stream.
259e41f4b71Sopenharmony_ci  const hs = hash.createHash('sha256');
260e41f4b71Sopenharmony_ci  rs.on('data', (emitData) => {
261e41f4b71Sopenharmony_ci    const data = emitData?.data;
262e41f4b71Sopenharmony_ci    hs.update(new Uint8Array(data?.split('').map((x: string) => x.charCodeAt(0))).buffer);
263e41f4b71Sopenharmony_ci  });
264e41f4b71Sopenharmony_ci  rs.on('close', async () => {
265e41f4b71Sopenharmony_ci    const hashResult = hs.digest();
266e41f4b71Sopenharmony_ci    const fileHash = await hash.hash(filePath, 'sha256');
267e41f4b71Sopenharmony_ci    console.info(`hashResult: ${hashResult}, fileHash: ${fileHash}`);
268e41f4b71Sopenharmony_ci  });
269e41f4b71Sopenharmony_ci}
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci```
272