Web.11
Web.11

Reputation: 416

Android: Read a text file from assets from custom line

I'm using this code to read text from assets:

private void Read(String file){
        try{
            String Name = file;
            Name = Name.replaceAll("'", "");
            file = getAssets().open(Name + ".txt");
            reader = new BufferedReader(new InputStreamReader(file));
            line = reader.readLine();
            Text ="";
            while(line != null){
                line = reader.readLine();
                if (line !=null){
                    Text += line+"\n";
                    LineNumber++;
                    if(LineNumber==50){btnv.setVisibility(View.VISIBLE);break; }
                }
            }
                if (LineNumber<50){btnv.setVisibility(View.GONE);}
                 txtv.setText(Text);
        }
            catch(IOException ioe){
            ioe.printStackTrace();
        }

    }

So I have to read first 50 lines of text, because the text is more then 300 lines, and all I know is to read file line by line, so if I read 300 lines line by line the app freezes for to long, so I read 50 first lines then 50 other lines and so on... So after I read the first 50 lines with that code I call this other code to read next ones:

private void ContinueReading(){
    if (LineNumber >= 50){
    try{
    while(line != null){
        line = reader.readLine();
        if (line !=null){
            Text += line+"\n";
            LineNumber++;
            if (LineNumber==100){break;}
            if (LineNumber==150){break;}
            if (LineNumber==200){break;}
            if (LineNumber==250){break;}
            if (LineNumber==300){break;}
            if (LineNumber==350){break;}
            if (LineNumber==400){break;}
            if (LineNumber==450){break;}
        }
        else{
            btnv.setVisibility(View.GONE);
            }
    }
         txtv.setText(Text);
    }
    catch(IOException ioe){ioe.printStackTrace();}
    }
}

But as you see I leave open :

        file = getAssets().open(emri + ".txt");
        reader = new BufferedReader(new InputStreamReader(file));

And this is no good, is anyway to close them and open them again and start reading from last line, or any idea how to start reading from ex. line 50, then from line 100, etc.. ?

Upvotes: 0

Views: 1134

Answers (2)

kris larson
kris larson

Reputation: 30985

This looks like a good place for an AsyncTask. You can even update the TextView with the text as it's being read from the file.

    txtv.setText("");
    new MyFileReader().execute(filename);
    .
    .
    .


    // inner class
    public class MyFileReader extends AsyncTask<String, String, Void> {
        @Override
        protected Void doInBackground(String... params) {

            try{
                InputStream file = getAssets().open(params[0].replaceAll("'", "") + ".txt");
                BufferedReader reader = new BufferedReader(new InputStreamReader(file));
                String line;
                while ((line = reader.readLine()) != null) {
                    publishProgress(line + "\n");
                }
                reader.close();
            } catch(IOException ioe){
                Log.e(TAG, ioe);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            txtv.append(values[0]);
        }
    }

Upvotes: 2

Arsal Imam
Arsal Imam

Reputation: 2982

You should use another Thread to read the complete file at once,

Please read this Asynctask in Android

But be care full you can not perform any UI related operations(Like change text of a TextView) on a different Thread instead Of Main Thread....! For that purpose, please also concern below link,

Android “Only the original thread that created a view hierarchy can touch its views.”

Upvotes: 1

Related Questions