鸿蒙:将项目的rawfile目录下全部文件拷贝到app沙箱目录(第二种方案)

该文章已生成可运行项目,

1. 前言

上篇博客跟大家分享了一种压缩后拷贝的方法,这种的相对比较麻烦,本篇博客分享一个更为简单的方法。

2. 参考文档

https://developer.huawei.com/consumer/cn/doc/architecture-guides/common-v1_26-ts_93-0000002306064776https://developer.huawei.com/consumer/cn/doc/architecture-guides/common-v1_26-ts_93-0000002306064776

3. 核心思路

rawfile目录下的文件夹直接复制到resfile目录下,然后再复制到沙箱。

4. 核心代码

async copyResfileDir() {
  await fs.copyDir(this.resfileDir, this.filesDir)
  this.showAlertDialog("拷贝成功")
}

5. 运行效果

6. 完整代码

import { fileIo as fs } from '@kit.CoreFileKit'

@Entry
@ComponentV2
struct Index {
  context = this.getUIContext().getHostContext()!
  @Local resfileDir: string = ""
  @Local filesDir: string = ""

  showAlertDialog(mes: string) {
    this.getUIContext().showAlertDialog({ message: mes })
  }

  getResfileDir() {
    this.resfileDir = this.context.resourceDir
    this.showAlertDialog("resfileDir路径" + this.resfileDir)
  }

  getFilesDir() {
    this.filesDir = this.context.filesDir
    this.showAlertDialog("filesDir路径" + this.filesDir)
  }

  async copyResfileDir() {
    await fs.copyDir(this.resfileDir, this.filesDir)
    this.showAlertDialog("拷贝成功")
  }

  async printFilesDir() {
    const list = await fs.listFile(this.filesDir)
    this.showAlertDialog(list.join("\n"))
  }

  async showRawfiles() {
    const list = await fs.listFile(this.filesDir + "/resfile")
    this.showAlertDialog(list.join("\n"))
  }

  build() {
    Column({ space: 100 }) {
      Text("依次点击下方按钮")
        .fontSize(30)
        .fontWeight(FontWeight.Bold)


      Column({ space: 20 }) {
        Text("resfileDir路径" + this.resfileDir)
        Button("获取resfileDir路径")
          .onClick(() => {
            this.getResfileDir()
          })

        Text("filesDir路径" + this.filesDir)
        Button("获取filesDir路径")
          .onClick(() => {
            this.getFilesDir()
          })
        Button("将resfileDir下的文件全部拷贝到沙箱目录")
          .onClick(() => {
            this.copyResfileDir()
          })
        Button("获取沙箱目录下的所有文件")
          .onClick(() => {
            this.printFilesDir()
          })

        Button("进入沙箱的resfile文件夹查看")
          .onClick(() => {
            this.showRawfiles()
          })

      }
    }
    .width('100%')
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

觉得有帮助可以点赞或收藏

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值