3
votes

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.

1
Please define "didn't work".Barns
@HoàngVũAnh Thanks. RNFS.ExternalDirectoryPath RNFS.ExternalStorageDirectoryPath Both are worjkingImran Noor

1 Answers

2
votes

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