繁体   English   中英

未找到 react-native-fs 路径?

[英]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/

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

但是代码中的条目存在

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

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

试试这个方法

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

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

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 模拟器中找到 ..[路径-设备资源管理器/'你的包名称'例如:'com.'/data/files/你有你的文件]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM