0

I am trying to write and read a text file which is full of words and add it to an ArrayList. The ArrayList later is used from another part of the program to display text in a TextView. But when i run the program and open the specific part of it, then there is nothing. The ArrayList is just empty. I don't get any exceptions but for some reason it doesn't work. Please help me.

I don't seem to have problems with the file writing:

TextView txt = (TextView)findViewById(R.id.testTxt);
            safe = txt.getText().toString();

            try {
                FileOutputStream fOut = openFileOutput("test.txt", MODE_WORLD_READABLE);
                OutputStreamWriter osw = new OutputStreamWriter(fOut);
                try {
                    osw.write(safe);
                    osw.flush();
                    osw.close();
                    Toast.makeText(getBaseContext(), "Added to favorites", Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException ex){
                    Log.e("Exception", "File write failed: ");
                }
            } catch (IOException e) {
                Log.e("Exception", "File write failed: ");
            }

But I think the problem is in the file reading. I made some "Log.d" and found out that everything works fine till the InputStreamReader line:

public  favHacks() {       
    testList = new ArrayList<String>();

    try {

       //Works fine till here

        InputStreamReader inputStreamReader = new InputStreamReader(openFileInput("test.txt"));

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String receiveString = "";

        while ( (receiveString = bufferedReader.readLine()) != null ) 
        {
                testList.add(receiveString);
        }

        bufferedReader.close();


    }
    catch (FileNotFoundException ex) {
        Log.d("login activity", "File not found: ");
    } catch (IOException ex) {
        Log.d("login activity", "Can not read file: ");
    }


}
Silas
  • 21
  • 4
  • What's your problem? A crash? Then post the log. Corrupted data? Post a small file example- what you expect and what you get. – Gabe Sechan May 26 '15 at 21:00
  • Edited what I expect to happen. But like i said, I don't get any crashes or things like that. – Silas May 26 '15 at 21:17
  • Is your inputStreamReader null? – Kristy Welsh May 26 '15 at 21:34
  • Don't put that try/catch in a try/catch. I am guessing you are using `MODE_WORLD_READABLE` because you are doing something "hackish" and based on your method "favHacks". What is `openFileInput`? – Jared Burrows May 26 '15 at 21:40
  • Oh no the favHacks is just a shortcut for something but I thought it sounds funny so I let it this way. I actually 'fixed' my problem, but now I'm getting a FileNotFoundException. – Silas May 26 '15 at 22:13

1 Answers1

0

If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs.

http://developer.android.com/training/basics/data-storage/shared-preferences.html

also if you want to write/read files, or do any kind of operations that can block the Main Thread try to use another Thread like when you are trying to save the data in a file or use a Handler if you have multiple Threads (one for saving and one for reading).

https://developer.android.com/training/multiple-threads/index.html

In your code the method called favHacks can return an ArrayList with the list of all the strings Something like

//   
public ArrayList<String> readFromFile(String file){
        ArrayList<String> mArrayList= new ArrayList<String>();
        //read from file here
        return mArrayList;
    }

but as I said before, you need to the operations that can block the UI thread in a new Thread. https://developer.android.com/training/multiple-threads/communicate-ui.html

And also I think that the best way to do this is using Asynk task

Why and how to use asynctask

Rodolfo Abarca
  • 565
  • 7
  • 15