3
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/xxx.xxx.xxx/files/data手机目录中的路径/文件

但是代码中的入口是存在的

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

我想知道RNFS.DocumentDirectoryPath电话里的路径是什么?

4

3 回答 3

0

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

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);
    });
 };
于 2019-12-10T15:57:41.813 回答
0

For finding files in the mobile storage. You should have to use External Directory Path

Document's directory path could be found in the android emulator.

[path- Device Explorer/'Your Package name 'eg: 'com.'/data/files/you have your file ]

于 2021-10-19T06:01:02.940 回答
0

试试这种方式

RNFS.readDir(RNFS.DocumentDirectoryPath) 
.then((result) => {
console.log('GOT RESULT', result); 
})
于 2019-12-06T07:03:06.320 回答