0

The question is very simple: I have those who methods in my class. This one saves the data in a text file:

//Store the data in a text file
public void saveScores(String testo, TextView text) {

 try {

    FileOutputStream fileout = getActivity().openFileOutput("flagQuiz.txt", getActivity().MODE_PRIVATE);
    OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
    outputWriter.write(testo);
    outputWriter.close();

    text.setText(testo);

 } catch (Exception e) {
    e.printStackTrace();
 }  

}

This method instead reads the data from a text file.

//Load data from text file
public void loadScores() {

 try {

    FileInputStream fileIn = getActivity().openFileInput("flagQuiz.txt");
    InputStreamReader InputRead= new InputStreamReader(fileIn);

    char[] inputBuffer= new char[100];
    String s="";
    int charRead;

    while ((charRead=InputRead.read(inputBuffer))>0) {

        String readstring=String.copyValueOf(inputBuffer,0,charRead);
        s +=readstring;                 

    }

    InputRead.close();

    } catch (Exception e) {
     e.printStackTrace();
    }

  }

From what I can gauge the first method works pretty well because it doens't raise exceptions and the text.setText(testo); works fine.

PROBLEM

I cannot read the text file when the app opens (onCreate).

enter image description here

As you can see here there is a null pointer exception, which means I guess that the file is not saved or I am typing a wrong path for the InputStream maybe.

Any suggestion about this? I am going to write this very little text file on the internal storage.

The complete log can be found here: link.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Alberto Rossi
  • 1,800
  • 4
  • 36
  • 58

2 Answers2

1

below code will read data from .txt file on resume.

public class MainActivity extends Activity {

    private final static String NOTES = "notes.txt";
    private EditText editText;
    private Button Btn;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            editText = (EditText) findViewById(R.id.editor);
            Btn = (Button) findViewById(R.id.close);

            Btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }

        public void onResume(){ //this method will read data from .txt file
            super.onResume();
            try {
                InputStream in = openFileInput(NOTES);
                if (in != null) {
                    InputStreamReader tmp = new InputStreamReader(in);
                    BufferedReader reader = new BufferedReader(tmp);
                    String str;
                    StringBuffer buf = new StringBuffer();

                    while ((str = reader.readLine()) != null) {
                        buf.append(str + "\n");
                    }
                    in.close();
                    editText.setText(buf.toString());
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (Throwable t) {
                Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
            }

    }
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

An example of use of OpenFileInput:

    FileInputStream in = openFileInput("filename.txt");
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line);
    }
arodriguezdonaire
  • 5,396
  • 1
  • 26
  • 50