HarmonyOS 无法读取文件

Configs.ets有如下方法

private static async readWriteFileWithStream(): Promise<Configs> {
  return new Promise((resolve: Function, reject: Function) => {
      try {
        let file = fs.openSync('../resources/rawfile/config.json', fs.OpenMode.READ_WRITE)
        let arrayBuffer = new ArrayBuffer(4096)
        fs.read(file.fd, arrayBuffer).then((readLen: number) => {
          let buf = buffer.from(arrayBuffer, 0, readLen)
          let configs: Configs = JSON.parse(buf.toString())
          resolve(configs)
      }).catch((err: BusinessError) => {
          Logger.error("read file data failed with error message: " + err.message + ", error code: " + err.code);
      }).finally(() => {
         fs.closeSync(file)
      });
      } catch (e) {
        Logger.error(JSON.stringify(e))
     }
  })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

这一行直接报错:

let file = fs.openSync('../resources/rawfile/config.json', fs.OpenMode.READ_WRITE)
  • 1.

报错:{"code":13900002}

@throws { BusinessError } 13900002 - No such file or directory

工程目录确实有该文件,而且编辑器里可以点击该路径直接跳转对于文件

HarmonyOS
2024-12-23 15:35:58
979浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

openSync是用于读取应用沙箱文件的,需要先获取其应用沙箱路径,读取rawfile下的文件是使用另外的方式。见以下两类说明:

1、读取应用内沙箱目录文件参考:

https://developer.huawei.com/consumer/cn/doc/atomic-guides-V5/atomic-app-file-access-V5#新建并读写一个文件

2、读取rawfile目录参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5#getrawfilecontent9

示例代码:

import { buffer } from '@kit.ArkTS';
import { common } from '@kit.AbilityKit';

@Entry
@Component
struct Index {
  async readFile(){
    try {
      let context = getContext(this) as common.UIAbilityContext;
      let value: Uint8Array = await context.resourceManager.getRawFileContent('config.json');
      let str = buffer.from(value.buffer).toString();
      console.log(str)
    } catch (e) {
      console.error('[File]', JSON.stringify(e))
    }
  }

  build() {
    Text('点击读取文件')
      .onClick(() => {
        this.readFile()
      })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

getContext(this)整个context默认为entry的context,在hsp中需要指定hsp的context,示例如下:

举例:

this.context.resourceManager.getRawFileContentSync("Bridge.js")
  • 1.

在context后加上

createModuleContext(‘library’) ,library为module.json5中的name
  • 1.

修改后:

this.context.createModuleContext('library').resourceManager.getRawFileContentSync("Bridge.js")
  • 1.

参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/application-context-stage-V5#获取本应用中其他module的context

分享
微博
QQ
微信
回复
2024-12-23 19:26:17


相关问题
HarmonyOS db文件无法读取到数据
1279浏览 • 1回复 待解决
无法读取到hsp模块中的rawfile文件
2838浏览 • 1回复 待解决
HarmonyOS 文件读取
637浏览 • 1回复 待解决
HarmonyOS 无法读取NM卡
1472浏览 • 1回复 待解决
HarmonyOS如何读取文件
1389浏览 • 1回复 待解决
HarmonyOS photoPicker文件读取问题
620浏览 • 1回复 待解决
HarmonyOS 如何读取项目文件
377浏览 • 1回复 待解决
HarmonyOS fs读取本地文件
1079浏览 • 1回复 待解决
HarmonyOS 读取.properties文件的api
591浏览 • 1回复 待解决
HarmonyOS 文件上传 & 资源读取问题
1531浏览 • 1回复 待解决
HarmonyOS 读取本地RawFile文件失败
1203浏览 • 1回复 待解决
HarmonyOS 如何读取本地文件流?
787浏览 • 1回复 待解决
HarmonyOS 读取raw文件参考示例
1104浏览 • 1回复 待解决
HarmonyOS libs下文件如何读取
534浏览 • 1回复 待解决
HarmonyOS 读取raw文件参考示例?
647浏览 • 0回复 待解决
HarmonyOS 如何读取本地json文件
1038浏览 • 1回复 待解决
如何读取本地JSON文件
3074浏览 • 1回复 待解决
如何根据uri读取文件?
4111浏览 • 1回复 待解决
HarmonyOS 数据库拷贝后无法读取
911浏览 • 1回复 待解决
如何读取读取分布式路径下的文件
1077浏览 • 1回复 待解决
HarmonyOS fs模块读取文件的问题
1223浏览 • 1回复 待解决
HarmonyOS ArkTS如何读取本地json文件
866浏览 • 1回复 待解决
HarmonyOS Rawfile中的json文件读取
958浏览 • 1回复 待解决
HarmonyOS 读取相册文件问题咨询
590浏览 • 1回复 待解决