Search code examples
androidstringsettext-fileswords

How to read a Text file (.txt) and set the words inside in String Variables on Android?


I am trying to read a Text file (.txt), Im done with that.

But I need to set in String variables every word inside the Text file.

For example this words are inside the Text file called words.txt :

whatsup; superman; heroe; batman;

And this is the code I am using to read words.txt :

    File directory = Environment.getExternalStorageDirectory();
    File file = new File(directory.getAbsolutePath()+"/HiMom", "words.txt");        
    try {
        FileInputStream fIn = new FileInputStream(file);         
        InputStreamReader file = new InputStreamReader(fIn);
        BufferedReader br=new BufferedReader(file);
        String line = br.readLine();
        String text = "";
        while (line!=null)
        {
            text = text + line + "\n";
            line = br.readLine();
        }
        br.close();
        file.close();
        etContentArchivo.setText(text);

    } catch (IOException e) {
    }

Now I need to read the Text file and set every word before a ";" in String variables, for Example:

String get1 = whatsup;
String get2 = superman;
String get3 = heroe;
String get4 = batman;

It can be also a String Array. But I dont know how to set the words in String variables.

Thanks.


Solution

  • You could do String[] words = text.split(";");