I am trying to use react-native-fs for some basic file storage. Here is some code.
var RNFS = require('react-native-fs');
static testSave() {
var count = this.countFiles(".txt");
var path = RNFS.DocumentDirectoryPath + '/test' + count + '.txt';
console.log("PATH: " + path);
// write the file
RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
.then((success) => {
console.log('FILE WRITTEN!');
})
.catch((err) => {
console.log(err.message);
});
}
static countFiles(extension) {
var allNames = RNFS.readdir(RNFS.DocumentDirectoryPath);
var count = 0;
for(var name in allNames) {
if (name.includes(extension)) {
++count;
}
}
console.log("DIR COUNT: " + count);
return count;
}
So, I have attempted using both DocumentDirectoryPath and ExternalStorageDirectoryPath, as suggested by an answer here: react native fs library not writing files
I cannot find these files on my phone despite it saying they are being saved. Also here are some runtime logs of me executing testSave() twice (in two different react-native program executions).
12-19 21:47:46.920 30468 4162 I ReactNativeJS: Running application "AwesomeProject"
12-19 21:47:46.961 30468 4162 I ReactNativeJS: DIR COUNT: 0
12-19 21:47:46.961 30468 4162 I ReactNativeJS: PATH: /data/user/0/com.awesomeproject/files/test0.txt
12-19 21:47:47.165 30468 4162 I ReactNativeJS: FILE WRITTEN!
12-19 21:47:51.345 30468 4174 I ReactNativeJS: Running application "AwesomeProject"
12-19 21:47:51.389 30468 4174 I ReactNativeJS: DIR COUNT: 0
12-19 21:47:51.389 30468 4174 I ReactNativeJS: PATH: /data/user/0/com.awesomeproject/files/test0.txt
12-19 21:47:51.596 30468 4174 I ReactNativeJS: FILE WRITTEN!