📌 从基础库 3.125.4 版本开始支持
重命名文件。可以把文件从 oldPath 移动到 newPath
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
oldPath | string | 是 | 源文件路径,支持本地路径 | |
newPath | string | 是 | 新文件路径,支持本地路径 | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
const fs = xhs.getFileSystemManager() // 异步重命名文件 fs.rename({ oldPath: `${xhs.env.USER_DATA_PATH}/hello.txt`, newPath: `${xhs.env.USER_DATA_PATH}/hello_new.txt`, success(res) { console.log('文件重命名成功') }, fail(res) { console.error('文件重命名失败:', res) } }) // 同步重命名文件 try { fs.renameSync( `${xhs.env.USER_DATA_PATH}/hello.txt`, `${xhs.env.USER_DATA_PATH}/hello_new.txt` ) console.log('文件重命名成功') } catch(e) { console.error('文件重命名失败:', e) }