#鸿蒙通关秘籍#如何使用FileAccessHelper模块复制文件?

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
云海谜OLAP

使用FileAccessHelper模块可以方便地复制文件。在需要操作文件时,需要创建FileAccessHelper实例并提供正确的路径。

bash import { BusinessError } from '@kit.BasicServicesKit'; import { Want } from '@kit.AbilityKit'; import { common } from '@kit.AbilityKit'; import { fileAccess } from '@kit.CoreFileKit';

let context = getContext(this) as common.UIAbilityContext; async function example() { let fileAccessHelper: fileAccess.FileAccessHelper; let wantInfos: Array<Want> = [ { bundleName: "com.ohos.UserFile.ExternalFileManager", abilityName: "FileExtensionAbility", }, ] try { fileAccessHelper = fileAccess.createFileAccessHelper(context, wantInfos); let sourceUri: string = "file://docs/storage/Users/currentUser/Download/one.txt"; let destUri: string = "file://docs/storage/Users/currentUser/Documents"; let displayName: string = "file1.txt"; let fileUri: string; fileUri = await fileAccessHelper.copyFile(sourceUri, destUri, displayName); console.log("copyFile success, fileUri: " + JSON.stringify(fileUri)); } catch (err) { let error: BusinessError = err as BusinessError; console.error("createFileAccessHelper failed, errCode:" + error.code + ", errMessage:" + error.message); } }

分享
微博
QQ
微信
回复
2天前
相关问题