中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何实现文件的拷贝并监听拷贝进度?
微信扫码分享
import { common } from '@kit.AbilityKit'; import fs from '@ohos.file.fs'; import fileUri from '@ohos.file.fileuri'; @Entry @Component struct Index { @State message: string = '12312 121' private context = getContext(this) as common.UIAbilityContext; build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { try { let progressListener: fs.ProgressListener = (progress: fs.Progress) => { console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`); }; let copyoption: fs.CopyOptions = { "progressListener": progressListener } let applicationContext = this.context.getApplicationContext(); let cacheDir = applicationContext.cacheDir; //将aaa文件拷贝到bbb文件,如果bbb不存在系统会自动创建一个bbb文件 fs.copy( fileUri.getUriFromPath(cacheDir + "/aaa"), fileUri.getUriFromPath(cacheDir + "/bbb"), copyoption).then(() => { }) } catch (err) { console.error(`Failed to copy: ${JSON.stringify(err)}`); } }) } .width('100%') } .height('100%') } }