How to read the data from Text File in Android React Native?

Imran Noor picture Imran Noor · Jun 25, 2018 · Viewed 7.4k times · Source

I want to read the cell numbers from the text file in Android using react native and convert all data to string.

Right now I am here:

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

I get text {"_40":0,"_65":0,"_55":null,"_72":null} which is i don't know where it is coming from.

Please someone put the working example.

Answer

Imran Noor picture Imran Noor · Jun 28, 2018

It Worked....

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