HarmonyOS鸿蒙Next中用FA模型架构JS语言开发的项目如何读取项目内本地的txt文件?

HarmonyOS鸿蒙Next中用FA模型架构JS语言开发的项目如何读取项目内本地的txt文件? 项目在JS、chart组件示例代码上改写:https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/tutorials_SwitchChart。我想读取本地的txt文件并把其中数据显示在echart图表上。

1 根据官方教程对文件资源的要求:https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/js-framework-file-0000001427744628-V3?catalogVersion=V3#:引用资源文件,推荐使用绝对路径。比如:/common/xxx.png。

2 根据官方教程对读取文件的要求:https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/js-apis-file-fs-0000001451843016-V3?catalogVersion=V3#ZH-CN_TOPIC_0000001574088233__fsread

let filePath = pathDir + "/test.txt";
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
let buf = new ArrayBuffer(4096);
fs.read(file.fd, buf).then((readLen) => {
    console.info("read file data succeed");
    console.info(String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen))));
    fs.closeSync(file);
}).catch((err) => {
    console.info("read file data failed with error message: " + err.message + ", error code: " + err.code);
});

我严格按照官方要求放置文件位置,并引用示例代码。我用

let filePath = pathDir + "/CS137.txt";

获取到的路径去读,以及绝对路径访问

filePath = "E:\\project\\harmonyOS\\SwitchChart\\entry\\" +"src\\main\\js\\MainAbility\\common\\context\\CS137.txt"

当程序执行到:

let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);

就停止了说妈妈路径有问题,请问各位大佬是我代码问题还是文件放置位置问题???

这些文档都试过了没用:

https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/app-file-overview-0000001455719906-V3

https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/app-sandbox-directory-0000001491863498-V3

https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/app-file-access-0000001544302293-V3


更多关于HarmonyOS鸿蒙Next中用FA模型架构JS语言开发的项目如何读取项目内本地的txt文件?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

9 回复

时到今日,不知楼主解决没有,可以读取么,怎么做的,多谢了

更多关于HarmonyOS鸿蒙Next中用FA模型架构JS语言开发的项目如何读取项目内本地的txt文件?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


20231017-181859(WeLinkPC).png

开发者您好,针对您的问题请参考如下链接:
https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/app-file-overview-0000001455719906-V3?catalogVersion=V3

应用只能操作应用沙箱里的文件,其他路径下的文件没法访问。

我应该把txt文件放在项目结构里面具体哪个包里,才能对应应用沙箱里的文件?

FA模型,楼主用的API9吗还是

对就是FA模型API9,

在HarmonyOS鸿蒙Next中,使用FA模型架构和JS语言开发的项目可以通过[@ohos](/user/ohos).fileio模块来读取项目内的本地txt文件。具体步骤如下:

  1. 导入模块:首先需要导入[@ohos](/user/ohos).fileio模块,该模块提供了文件操作的相关API。

    import fileio from '[@ohos](/user/ohos).fileio';
    
  2. 获取文件路径:假设txt文件存放在项目的resources/rawfile目录下,可以通过this.$context获取文件的绝对路径。

    const filePath = this.$context.resourceManager.getRawFileContentSync('example.txt');
    
  3. 打开文件:使用fileio.openSync方法打开文件,获取文件描述符。

    const fd = fileio.openSync(filePath, fileio.OpenMode.READ_ONLY);
    
  4. 读取文件内容:使用fileio.readSync方法读取文件内容,并将其转换为字符串。

    const buffer = new ArrayBuffer(1024);
    const bytesRead = fileio.readSync(fd, buffer, { offset: 0, length: buffer.byteLength });
    const content = String.fromCharCode.apply(null, new Uint8Array(buffer.slice(0, bytesRead)));
    
  5. 关闭文件:读取完成后,使用fileio.closeSync方法关闭文件。

    fileio.closeSync(fd);
    
  6. 处理文件内容:可以根据需要对读取到的文件内容进行处理。

    console.log(content);
    

通过以上步骤,你可以在HarmonyOS鸿蒙Next中使用FA模型架构和JS语言读取项目内的本地txt文件。

在HarmonyOS鸿蒙Next中,使用FA模型架构和JS语言开发时,可以通过@ohos.file.fs模块读取项目内的本地txt文件。首先,在resources目录下放置txt文件,然后使用fs.readText方法读取文件内容。示例代码如下:

import fs from '@ohos.file.fs';

const filePath = 'resources/rawfile/example.txt';
fs.readText(filePath, (err, data) => {
  if (err) {
    console.error('读取文件失败', err);
  } else {
    console.log('文件内容:', data);
  }
});

确保文件路径正确,并处理可能的错误。

回到顶部