在鸿蒙系统中,可以将数据文件如 JSON 文件放置在 rawfile 目录下进行存储。可以使用getRawFileContent或getRawFileContentSync接口来读取 rawfile 目录下的文件内容,例如读取 JSON 文件内容,代码如下:import { Context } from '@ohos.abilityAccessCtrl'; import buffer from '@ohos.buffer'; @Entry @Component struct Index { private context:Context = getContext(this) ; private str:string = '' getRawFile(): ESObject{ //调用getRawFileContent接口获取 json 文件内容,并读为 string getContext(this).resourceManager.getRawFileContent("data.json",(err,data)=>{ try { this.str = buffer.from(data.buffer).toString(); console.info(JSON.stringify(this.str)) }catch (e){ console.info(JSON.stringify(e)) } }) //也可以调用getRawFileContentSync接口获取 json 文件内容,并读为 string try { let data: Uint8Array= this.context.resourceManager.getRawFileContentSync("data.json"); this.str = buffer.from(data.buffer).toString(); } catch (e) { console.info(JSON.stringify(e)) } // string 转为 ESObject } }
在鸿蒙系统中,可以将数据文件如 JSON 文件放置在 rawfile 目录下进行存储。可以使用
getRawFileContent
或getRawFileContentSync
接口来读取 rawfile 目录下的文件内容,例如读取 JSON 文件内容,代码如下: