Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native-fs path not found?

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?

like image 593
ebyte Avatar asked Feb 11 '26 17:02

ebyte


1 Answers

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);
    });
 };
like image 148
Srinivasan Lakshmanan Avatar answered Feb 16 '26 08:02

Srinivasan Lakshmanan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!