#鸿蒙学习大百科#如何读取文件内容并打印?

如何读取文件内容并打印?

HarmonyOS
2024-10-16 08:47:01
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
鹈鹕说蟹蟹你
import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';
import buffer from '@ohos.buffer';
@Entry
@Component
struct Index {
  private context = getContext(this) as common.UIAbilityContext;

  build() {
    Row() {
      Column() {
        Text("Hello")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let applicationContext = this.context.getApplicationContext();
            let cacheDir = applicationContext.cacheDir;
            let dirPath = cacheDir + "/aaa";
            fs.open(dirPath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE).then((file: fs.File) => {
              let arrayBuffer = new ArrayBuffer(4096);
              fs.read(file.fd,arrayBuffer, (err: BusinessError, readLen: number) => {
                if (err) {
                  console.error("read failed with error message: " + err.message + ", error code: " + err.code);
                } else {
                  console.info("read file data succeed");
                  let buf = buffer.from(arrayBuffer, 0, readLen);
                  console.info(`The content of file: ${buf.toString()}`);
                }
                fs.closeSync(file);
              })
            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

分享
微博
QQ
微信
回复
2024-10-16 16:38:09
相关问题
#鸿蒙学习大百科#什么是用户文件
156浏览 • 0回复 待解决