首先你需要检查文件是否存在
const filePath = RNFS.DocumentDirectoryPath + "/test" + ".txt";
RNFS.exists(filePath)
.then(success => {
if (success) {
readFile(filePath, logData);
} else {
writeFile(filePath, logData);
}
})
.catch(err => {
console.log(err.message, err.code);
});
const readFile = (filePath, logData) => {
RNFS.readFile(filePath, "utf8")
.then(content => {
// Do what you need if the file exists
})
.catch(err => {
console.log(err.message, err.code);
});
};
const writeFile = (filePath, logData) => {
RNFS.writeFile(filePath, logData, "utf8")
.then(() => {
// This will create new file for you in the name of test.txt
})
.catch(err => {
console.log(err.message, err.code);
});
};