gpt4 book ai didi

android - 未找到 react-native-fs 路径?

转载 作者:行者123 更新时间:2023-11-29 02:17:47 30 4
gpt4 key购买 nike

import RNFS from 'react-native-fs';

var path = RNFS.DocumentDirectoryPath + '/test.txt';
RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
.then(success => {
console.log('FILE WRITTEN!');
})
.catch(err => {
console.log(err.message);
});

console.log(RNFS.DocumentDirectoryPath)
// /data/data/xxx.xxx.xxx/files/

但是我在手机的data目录下没有找到/data/xxx.xxx.xxx/files/这样的路径/文件

但代码中的条目存在

RNFS.readDir(RNFS.DocumentDirectoryPath).then(result => {
console.log('DocumentDirectoryPath GOT RESULT', result);
});

我想知道RNFS.DocumentDirectoryPath在手机中的路径是什么?

最佳答案

首先你需要检查文件是否存在

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);
});
};

关于android - 未找到 react-native-fs 路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59208084/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com