查询缓存用storageStatistics.getCurrentBundleStats()接口,清除文件缓存,需要调用context的cacheDir获取缓存,然后调用系统文件fs接口,判断是文件或者文件夹,再分别消除缓存https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-storage-statistics-V5\#storagestatisticsgetcurrentbundlestats9清理:import fs from ‘@ohos.file.fs’; let cacheDir = context.cacheDir; @Entry @Component struct Clear_cache { clearCache() { // let cacheDir = getContext(this).cacheDir // fs.rmdirSync(cacheDir) // console.log(“delete !!!”) fs.listFile(cacheDir).then((filenames) => { for (let i = 0;i < filenames.length; i++) { // let dirPath = cacheDir+filenames[i] let dirPath = ${cacheDir}/${filenames[i]} // 判断是否文件夹 let isDirectory try { isDirectory = fs.statSync(dirPath).isDirectory() } catch (e) { console.log(e) } if (isDirectory) { fs.rmdirSync(dirPath) } else { fs.unlink(dirPath).then(() => { console.info(“remove file succeed”); }).catch((err) => { console.info("remove file failed with error message: " + err.message + ", error code: " + err.code); }); } } }) } }
查询缓存用storageStatistics.getCurrentBundleStats()接口,清除文件缓存,需要调用context的cacheDir获取缓存,然后调用系统文件fs接口,判断是文件或者文件夹,再分别消除缓存https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-storage-statistics-V5\#storagestatisticsgetcurrentbundlestats9清理: