鸿蒙:将项目的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.resourceDirthis.showAlertDialog("resfileDir路径" + this.resfileDir)}getFilesDir() {this.filesDir = this.context.filesDirthis.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)}
}
觉得有帮助可以点赞或收藏