如何在Android React Native中读取文本文件中的数据?

4

我想在使用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},但我不知道它的来源。
请有经验的人提供一个可用的例子。

1
请定义“没有工作”。 - Barns
这里是:https://github.com/itinance/react-native-fs#constants - Hoàng Vũ Anh
@HoàngVũAnh 谢谢。 RNFS.ExternalDirectoryPath RNFS.ExternalStorageDirectoryPath 这两个都可以工作。 - Imran Noor
1个回答

2

成功了...

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接