• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# File Management Development
2
3## How do I obtain the path of system screenshots?
4
5Applicable to: OpenHarmony 3.2 Beta5 (API version 9)
6
7**Solution**
8
9The screenshots are stored in **/storage/media/100/local/files/Pictures/Screenshots/**.
10
11## How do I change the permissions on a directory to read/write on a device?
12
13Applicable to: OpenHarmony 3.2 Beta5 (API version 9)
14
15**Symptom**
16
17When the hdc command is used to send a file to a device, "permission denied" is displayed.
18
19**Solution**
20
21Run the **hdc shell mount -o remount,rw /** command to grant the read/write permissions.
22
23## What is the best way to create a file if the file to open does not exist?
24
25Applicable to: OpenHarmony 3.2 (API version 9)
26
27**Solution**
28
29Use **fs.open(path: string, mode?: number)** with **mode** set to **fs.OpenMode.CREATE**. **fs.OpenMode.CREATE** creates a file if it does not exist.
30
31## How do I solve the problem of garbled Chinese characters in a file?
32
33Applicable to: OpenHarmony 3.2 (API version 9)
34
35**Solution**
36
37After the buffer data of the file is read, use **TextDecoder** of @ohos.util to decode the file content.
38
39```
40let filePath = getContext(this).filesDir + "/test0.txt";
41let stream = fs.createStreamSync(filePath, "r+");
42let buffer = new ArrayBuffer(4096)
43let readOut = stream.readSync(buffer);
44let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true })
45let readString = textDecoder.decodeWithStream(new Uint8Array(buffer), { stream: false });
46console.log ("File content read: "+ readString);
47```
48
49## Why is an error reported when fs.copyFile is used to copy a datashare:// file opened by fs.open()?
50
51Applicable to: OpenHarmony 3.2 (API version 9)
52
53**Solution**
54
55**fs.copyFile** does not support URIs. You can use **fs.open()** to obtain the URI, obtain the file descriptor (FD) based on the URI, and then use **fs.copyFile** to copy the file based on the FD.
56
57```
58let file = fs.openSync("datashare://...")
59fs.copyFile(file.fd, 'dstPath', 0).then(() => {
60  console.info('copyFile success')
61}).catch((err) => {
62  console.info("copy file failed with error message: " + err.message + ", error code: " + err.code);
63})
64```
65
66## How do I modify the content of a JSON file in a sandbox directory?
67
68Applicable to: OpenHarmony 3.2 (API version 9)
69
70**Solution**
71
72Perform the following steps:
73
741. Use **fs.openSyn** to obtain the FD of the JSON file.
75
76```
77import fs from '@ohos.file.fs';
78let sanFile = fs.open(basePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
79let fd = sanFile.fd;
80```
81
822. Use **fs.readSync** to read the file content.
83
84```
85let content = fs.readSync(basePath);
86```
87
883. Modify the file content.
89
90```
91obj.name = 'new name';
92```
93
944. Use **fs.writeSync** to write the data to the JSON file.
95
96```
97fs.writeSync(file.fd, JSON.stringify(obj));
98```
99
100For more information, see [@ohos.file.fs](../reference/apis/js-apis-file-fs.md).
101
102## What is the real path corresponding to the file path obtained through the FileAccess module?
103
104Applicable to: OpenHarmony 3.2 (API version 9, stage model)
105
106**Solution**
107
108The files are stored in the **/storage/media/100/local/files** directory. The specific file path varies with the file type and source. To obtain the file path based on the file name, run the following command in the **/storage/media/100/local/files** directory:<br>-name \[filename\]
109
110For more information, see [Uploading and Downloading an Application File](../file-management/app-file-upload-download.md).
111
112## How do I listen for the changes of a file or folder?
113
114Applicable to: OpenHarmony SDK 4.0 Release API 10
115
116**Solution**
117
118You can use **fs.createWatcher** to listen for the changes of a file or folder with the registered callback.
119
120**References**
121
122[@ohos.file.fs](../reference/apis/js-apis-file-fs.md#fscreatewatcher10)
123