【HarmonyOS鸿蒙Next】【ARKUI】【API8】【eTS】读取的数据中文乱码怎么办?

【HarmonyOS鸿蒙Next】【ARKUI】【API8】【eTS】读取的数据中文乱码怎么办? 书接上回,https://developer.huawei.com/consumer/cn/forum/topic/0203955315374850486?fid=0102683795438680754

终于读取出来一个txt文件了,但是直接打印出来全是乱码

参考了一位大佬的解法([https://developer.huawei.com/consumer/cn/forum/topic/0204809455827330196?fid=0102683795438680754](https://developer.huawei.com/consumer/cn/forum/topic/0204809455827330196?fid=0102683795438680754)),let decodedString = decodeURIComponent(escape(encodedString));//没有这一步中文会乱码,使用escape,decodeURIComponent转一下数据,但是代码卡在了这一行,报错

点进去escape的方法里也没发现使用的有问题,或者接口不存在,

希望有大佬能教一教escape的用法,或者其他的解决乱码的方法


更多关于【HarmonyOS鸿蒙Next】【ARKUI】【API8】【eTS】读取的数据中文乱码怎么办?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复
楼主你好,读取文件内容参考以下代码:

```javascript
import util from '[@ohos](/user/ohos).util'
private async readFile() {
  let ss = fileio.createStreamSync(this.path + "/111.txt", "r+");
  const readOut = await ss.read(new ArrayBuffer(4096));
  let textDecoder = new util.TextDecoder("utf-8", { ignoreBOM: true });
  let retStr = textDecoder.decode(new Uint8Array(readOut.buffer), { stream: false });
  console.log("[Demo] 读取文件内容:" + retStr);
}

更多关于【HarmonyOS鸿蒙Next】【ARKUI】【API8】【eTS】读取的数据中文乱码怎么办?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


private ReadFile(){ let Filepath = this.path+"/111.txt"; let fd = fileio.openSync(Filepath, 0o2); let buf = new ArrayBuffer(4096); fileio.read(fd, buf).then(function(readOut){ // let encodedString = String.fromCodePoint.apply(null, new Uint8Array(readOut.buffer)); //// let decodedString = decodeURIComponent(escape(encodedString));//没有这一步中文会乱码 let array=readOut.buffer; var out,i,len,c; var char2,char3; if (array instanceof ArrayBuffer) { array = new Uint8Array(array); } out = “”; len = array.byteLength; i = 0; while(i < len) { c = array[i++]; switch(c >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: // 0xxxxxxx out += String.fromCharCode©; break; case 12: case 13: // 110x xxxx 10xx xxxx char2 = array[i++]; out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); break; case 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = array[i++]; char3 = array[i++]; out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); break; } } console.log(“读取文件内容”+out); }).catch(function(err){ console.info(“read file data failed with error:”+ err); }); }

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17

补充说明一下,es5里的六个方法,decodeURIComponentdecodeURIecodeURIComponentecodeURI,都好用,只有escapeunescape报为定义的错,

在HarmonyOS鸿蒙Next中使用ARKUI和eTS进行数据读取时,如果遇到中文乱码问题,通常是因为数据的编码格式与系统或应用默认的编码格式不一致。可以通过以下步骤解决:

  1. 确认数据源编码:首先确认数据源的编码格式,常见的有UTF-8、GBK等。

  2. 设置编码格式:在读取数据时,明确指定数据的编码格式。例如,如果数据是UTF-8编码,可以在读取数据时使用TextDecoder指定编码格式。

let decoder = new TextDecoder('utf-8');
let decodedString = decoder.decode(data);
  1. 检查网络请求头:如果数据来自网络请求,确保请求头中包含了正确的Accept-CharsetContent-Type,如Content-Type: application/json; charset=utf-8

  2. 数据库编码:如果数据来自数据库,确保数据库和表的编码格式为UTF-8。

  3. 文件编码:如果是读取本地文件,确保文件保存时的编码格式为UTF-8。

通过以上步骤,可以有效解决中文乱码问题。

在HarmonyOS鸿蒙Next中使用ARKUI和eTS开发时,如果读取的数据出现中文乱码,可能是编码格式不匹配导致的。建议检查数据源的编码格式,确保与读取时使用的编码一致。通常使用UTF-8编码可以解决大部分乱码问题。可以在读取数据时指定编码格式,例如使用TextDecoder进行解码:

let decoder = new TextDecoder('utf-8');
let decodedString = decoder.decode(data);

确保数据源和读取方式都使用UTF-8编码,可以有效避免中文乱码问题。

回到顶部