-4

I have .txt File with Strings and Integers like (very simplified):

String Q1 = "aflasjke";
String Q2 = "agdsgsdg";
String Q3 = "asdgdsgn";

String Q1_1 = "sgadg";
String Q1_2 = "agajgsdgn";
String Q1_3 = "sdgjsdgj";

int M1 = 1;
int M2 = 2;
int M3 = 4;

I need those Strings and int in my MainActivity. How can I "import/read" them?

Thanks!

1 Answers1

0

This two methods bellow can help you

EDIT

Here you can read the file from SdCard

public static String getStringFromFile (String filePath) throws Exception {
    File dir = Environment.getExternalStorageDirectory();
    File fl = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
    FileInputStream fin = new FileInputStream(fl);
    String ret = convertStreamToString(fin);
    //Make sure you close all streams.
    fin.close();        
    return ret;
}

Here you have the strings from the file you get above

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line).append("\n");
    }
    reader.close();
    return sb.toString();
}

and add the right permission to your manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

I found in this answer here

Community
  • 1
  • 1
diogojme
  • 2,309
  • 1
  • 19
  • 25