android - 如何从 Android React Native 中的文本文件中读取数据?
问题描述
我想使用 react native 从 Android 中的文本文件中读取单元格编号并将所有数据转换为字符串。
现在我在这里:
import RNFS from "react-native-fs";
const rootPath = RNFS.DocumentDirectoryPath;
readFile = async () => {
const path = rootPath + "/rn.txt";
var content = await RNFS.readFile(path, "utf8");
return content;
};
render() {
return (
<View style={styles.container}>
<Text>{JSON.stringify(this.readFile())}</Text>
</View>
);
}
我得到文本 {"_40":0,"_65":0,"_55":null,"_72":null} 这是我不知道它来自哪里。
请有人提出工作示例。
解决方案
有效....
readFile = async (MyPath) => {
try {
const path =MyPath+ "/rn.txt";
const contents = await RNFS.readFile(path, "utf8");
return("" + contents);
} catch (e) {
alert("" + e);
}
};
<Button title="AppFilesDir" onPress={() => this.readFile(RNFS.ExternalDirectoryPath)} />
<Button title="InternalStorageDir" onPress={() => this.readFile(RNFS.ExternalStorageDirectoryPath)} />
推荐阅读
- javascript - 无法在 Laravel 刀片脚本标签中导入 JS 模块
- c++ - 查找所需编辑数量的数组问题
- android - 为什么当我将一个位图切换为另一个相同大小的位图时,组件会四处移动?
- javascript - 未选择无线电输入时禁用按钮
- javascript - JS - 使用按钮插入行有效,按钮无效
- python - 遍历字典列表
- django - 当我尝试注册用户时,/accounts/register/ 处的 ConnectionRefusedError
- ios - 如何在 ios 目标 c 中将背景图像设置为 rootViewController?
- flutter - 如何使颤动的网页大小像应用程序大小一样
- python - 我想从第 2 行删除行到 CSV 文件中的特定数字,但它会从文件中删除所有