首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未找到react-native-fs路径?

未找到react-native-fs路径?
EN

Stack Overflow用户
提问于 2019-12-06 14:37:24
回答 3查看 3.4K关注 0票数 3
代码语言:javascript
运行
复制
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/这样的路径/文件

但是代码中的条目存在

代码语言:javascript
运行
复制
RNFS.readDir(RNFS.DocumentDirectoryPath).then(result => {
  console.log('DocumentDirectoryPath GOT RESULT', result);
});

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

EN

回答 3

Stack Overflow用户

发布于 2019-12-06 15:03:06

试试这条路

代码语言:javascript
运行
复制
RNFS.readDir(RNFS.DocumentDirectoryPath) 
.then((result) => {
console.log('GOT RESULT', result); 
})
票数 0
EN

Stack Overflow用户

发布于 2019-12-10 23:57:42

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

代码语言:javascript
运行
复制
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);
    });
 };
票数 0
EN

Stack Overflow用户

发布于 2021-10-19 06:01:03

用于在移动存储中查找文件。您必须使用外部目录路径

文档的目录路径可以在android模拟器中找到。

数据路径-设备资源管理器/‘您的包名’,例如:‘com.’/

//您有您的文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59208084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档