1. 前言
上篇博客跟大家分享了一种压缩后拷贝的方法,这种的相对比较麻烦,本篇博客分享一个更为简单的方法。
2. 参考文档
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)
}
}
觉得有帮助可以点赞或收藏
&spm=1001.2101.3001.5002&articleId=153199546&d=1&t=3&u=4e49a72c57874017b3f6667d5a1fc1af)
93

被折叠的 条评论
为什么被折叠?



