首页 > 解决方案 > 如何从 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} 这是我不知道它来自哪里。

请有人提出工作示例。

标签: androidreact-nativetext-filesread-data

解决方案


有效....

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)} />

推荐阅读