2
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/

But I didn't find a path/file like /data/xxx.xxx.xxx/files/ in the data directory of the mobile phone

But the entry in the code exists

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

I want to know what is the path of RNFS.DocumentDirectoryPath in the phone?

ebyte
  • 969
  • 5
  • 15

2 Answers2

0

Try This Way

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

First you need to Check the file is existing or not

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