1# File Management Development 2 3## What if I failed to open the URI returned by picker with the read/write permissions? (API version 10) 4 5**Symptom** 6 7When **fs.openSync()** with **mode** set to **OpenMode.READ_WRITE** is called to open a URI returned by a **picker** API, the file cannot be opened. 8 9**Cause Analysis** 10 11The permission on the URI obtained by **picker** is write-only. As a result, you cannot open the file in read/write mode. 12 13**Solution** 14 15Currently, you can open the file URI in write-only mode. 16 17``` 18fs.openSync(uri, fs.OpenMode.WRITE_ONLY) 19``` 20 21In later versions, when a file is opened or saved by using a picker API, the URI returned will be granted with the read/write permissions. You can open and edit the file as required. 22 23## How do I obtain the path of system screenshots? (API version 9) 24 25**Solution** 26 27The screenshots are stored in **/storage/media/100/local/files/Pictures/Screenshots/**. 28 29 30## How do I change the permissions on a directory to read/write? (API 9) 31 32**Symptom** 33 34When the hdc command is used to send a file to a device, "permission denied" is displayed. 35 36**Solution** 37 38Run the **hdc shell mount -o remount,rw /** command to grant the read/write permissions. 39 40 41## What is the best way to create a file if the file to open does not exist? (API version 9) 42 43**Solution** 44 45Use **fs.open(path: string, mode?: number)** with **mode** set to **fs.OpenMode.CREATE**. 46 47 48## How do I solve the problem of garbled Chinese characters in a file? (API version 9) 49 50**Solution** 51 52After the buffer data of the file is read, use **TextDecoder** of @ohos.util to decode the file content. 53 54``` 55let filePath = getContext(this).filesDir + "/test0.txt"; 56let stream = fs.createStreamSync(filePath, "r+"); 57let buffer = new ArrayBuffer(4096) 58let readOut = stream.readSync(buffer); 59let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true }) 60let readString = textDecoder.decodeWithStream(new Uint8Array(buffer), { stream: false }); 61console.log ("File content read: "+ readString); 62``` 63 64 65## Why is an error reported when fs.copyFile is used to copy a datashare:// file opened by fs.open? (API version 9) 66 67**Solution** 68 69**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. 70 71``` 72let file = fs.openSync("datashare://...") 73fs.copyFile(file.fd, 'dstPath', 0).then(() => { 74 console.info('copyFile success') 75}).catch((err) => { 76 console.info("copy file failed with error message: " + err.message + ", error code: " + err.code); 77}) 78``` 79 80 81## How do I modify the content of a JSON file in a sandbox directory? (API version 9) 82 83**Solution** 84 85Perform the following steps: 86 871. Use **fs.openSyn** to obtain the FD of the JSON file. 88 89 ``` 90 import fs from '@ohos.file.fs'; 91 let sanFile = fs.open(basePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 92 let fd = sanFile.fd; 93 ``` 94 952. Use **fs.readSync** to read the file content. 96 97 ``` 98 let content = fs.readSync(basePath); 99 ``` 100 1013. Modify the file content. 102 103 ``` 104 obj.name = 'new name'; 105 ``` 106 1074. Use **fs.writeSync** to write the data to the JSON file. 108 109 ``` 110 fs.writeSync(file.fd, JSON.stringify(obj)); 111 ``` 112 113For more information, see [@ohos.file.fs](../reference/apis/js-apis-file-fs.md). 114 115## What is the real path corresponding to the file path obtained through the fileAccess module? (API version 9) 116 117Applicable to: stage model 118 119**Solution** 120 121The 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] 122 123**References** 124 125For more information, see [Uploading and Downloading an Application File](../file-management/app-file-upload-download.md). 126 127## How do I listen for the changes of a file or folder? (API version 10) 128 129**Solution** 130 131You can use **fs.createWatcher** to listen for the changes of a file or folder with the registered callback. 132 133**References** 134 135[@ohos.file.fs](../reference/apis/js-apis-file-fs.md#fscreatewatcher10) 136