使用resourceManagergetRawFileContent(path: string, callback: AsyncCallback<Uint8Array>): void获取resources/rawfile目录下对应的rawfile文件内容,callback异步回调。import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getRawFileContent("test.txt", (error: BusinessError, value: Uint8Array) => { if (error != null) { console.error("error is " + error); } else { let rawFile = value; } }); } catch (error) { let code = (error as BusinessError).code; let message = (error as BusinessError).message; console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`); }
在HarmonyOS开发中,如果想要在代码中读取自定义在resources文件夹里面的JSON文件,可以通过ResourceManager类来实现。基本步骤:1.将JSON文件放置在resources文件夹中2.访问ResourceManager3.读取JSON文件
使用resourceManager
getRawFileContent(path: string, callback: AsyncCallback<Uint8Array>): void
获取resources/rawfile目录下对应的rawfile文件内容,callback异步回调。