使用react-native-fs模块的moveFile方法时,如果移动目标目录中已存在同名文件,会报错。需要先判断目标目录中是否存在同名文件,如果存在则先删除再移动或重命名文件。以下是示例代码:
import RNFS from 'react-native-fs';
const sourcePath = RNFS.ExternalCachesDirectoryPath + '/test.jpg';
const destinationPath = RNFS.ExternalDirectoryPath + '/test.jpg';
RNFS.exists(destinationPath)
.then((exists) => {
if (exists) {
return RNFS.unlink(destinationPath);
return Promise.resolve();
.then(() => {
return RNFS.moveFile(sourcePath, destinationPath);
.then(() => {
console.log('File moved successfully');
.catch((err) => {
console.log(err.message);