【问题标题】:How to read the data from Text File in Android React Native?如何从 Android React Native 中的文本文件中读取数据?
【发布时间】:2018-12-04 10:45:45
【问题描述】:

我想使用 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} ,我不知道它来自哪里。

请有人提出工作示例。

【问题讨论】:

标签: android react-native text-files read-data


【解决方案1】:

成功了……

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

【讨论】:

    猜你喜欢
    • 2015-06-09
    • 2012-06-10
    • 2019-03-13
    • 2020-06-30
    • 2021-08-12
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 2019-05-14
    相关资源
    最近更新 更多