中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何读取文本文件的内容,并将其转换为string。
微信扫码分享
import { common } from '@kit.AbilityKit'; import { BusinessError } from '@ohos.base'; import util from '@ohos.util'; function certVerification(context: common.UIAbilityContext){ try { context.resourceManager.getRawFileContent("test.txt", (error: BusinessError, value: Uint8Array) => { if (error != null) { console.error("error is " + error); } else { let rawFile = value; const result=util.TextDecoder.create().decodeWithStream(rawFile) console.info('result:'+result) } }); } catch (error) { let code = (error as BusinessError).code; let message = (error as BusinessError).message; console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`); } } @Entry @Component struct Index { private context=getContext(this) as common.UIAbilityContext; build() { Button('测试rawfile') .onClick(()=>{ certVerification(this.context) }) } }