Jing Jie
Jing Jie

Reputation: 41

Issues in reading of data from text file Android Development

i have been experiencing problem reading data from text files which resides in my internal storage of my mobile phone. My code is as below:

   public void recommend(View view) {
        Context context = getApplicationContext();
        String filename = "SendLog.txt";
        TextView tv = (TextView)findViewById(R.id.textView134);
        try {
            FileInputStream fis = context.openFileInput(filename);
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line).append("\n");
                tv.getText();
                tv.setText(line);
            }
        }  catch (IOException e) {
            e.getStackTrace();
        }

I'm not getting any data input to my mobile phone. My "SendLog.txt" file reside in /storage/emulated/0/SendLog.txt directory. Anyone can assist me? I want to read everything from the text file and display in textView134. Anyone can assist?

Thank you in advance!

Upvotes: 0

Views: 40

Answers (2)

avk
avk

Reputation: 871

You are building a string using stringbuilder but instead are settexting line. The result is last line in file, which may be an empty line

Upvotes: 0

Chris Stratton
Chris Stratton

Reputation: 40337

"/storage/emulated/0/SendLog.txt" is located on the External Storage, not the internal.

Instead of context.openFileInput(filename) you should be using Environment.getExtenalStorageDirectory() for example,

FileInputStream fis = new FileInputStream(new File(Environment.getExtenalStorageDirectory(), filename));

Though you will need to handle possibilities such as the External Storage being unavailable / unmounted.

You will also need the manifest permission to read external storage, or alternately to write it if you plan to do that as well (you only need one of the two, as write implies read).

Upvotes: 1

Related Questions