如何解决文件的中文乱码问题
=============

读取文件内容的buffer数据后，通过TextDecoder对文件内容进行解码。

import { util } from '@kit.ArkTS';
import { fileIo } from '@kit.CoreFileKit';

_// In the utility class, retrieve the Context from the Entry Ability and save it to AppStore, then use AppStore to retrieve it in the utility class_
let context = AppStorage.get("context") as UIContext;
_// Create a file to write Chinese characters_
let filePath = context.getHostContext()!.filesDir + "/test0.txt";
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ\_WRITE | fileIo.OpenMode.CREATE);
_// Write a paragraph of content to a file_
let writeLen = fileIo.writeSync(file.fd, "Hello, world");
fileIo.closeSync(file);
console.info(\`GarbledCnCharacters The length of str is: ${writeLen}\`);
let stream = fileIo.createStreamSync(filePath, "r+");
let buffer = new ArrayBuffer(4096);
stream.readSync(buffer);
_// Set the encoding format to "utf-8"_
let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true });
_// Retrieve the corresponding text after decoding the input_
let readString = textDecoder.decodeToString(new Uint8Array(buffer), { stream: false });
console.info(\`GarbledCnCharacters read content is:${readString}\`);

**参考链接**

[TextDecoder](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-util#textdecoder)