#鸿蒙学习大百科#如何基于文件路径创建文件流?

如何基于文件路径创建文件流?

HarmonyOS
2024-10-17 09:14:07
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
花鸟吹雪
import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';

@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;
            fs.open(cacheDir + "/test.txt", fs.OpenMode.READ_WRITE).then((f: fs.File) => {
              fs.createStream(f.path, "r", (err: BusinessError, stream: fs.Stream) => {
                if (err) {
                  console.error("create stream failed with error message: " + err.message + ", error code: " +
                  err.code);
                } else {
                  console.info("createStream succeed");
                }
                stream.closeSync();
              })
            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-10-17 14:46:52
相关问题
#鸿蒙学习大百科#什么是用户文件
222浏览 • 0回复 待解决