Android: 从自定义行的资产中读取文本文件

Android: Read a text file from assets from custom line

我正在使用此代码从资产中读取文本:

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();
        }

    }

所以我必须先阅读 50 行文本,因为文本超过 300 行,而我所知道的是逐行读取文件,所以如果我逐行读取 300 行,应用程序会冻结太长了,所以我先读了 50 行,然后再读了 50 行,依此类推...... 因此,在我使用该代码阅读前 50 行后,我调用其他代码来阅读下一行:

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();}
    }
}

但是如你所见,我保持打开状态:

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

而且这也不好,无论如何要关闭它们并再次打开它们并从最后一行开始阅读,或者知道如何从 ex 开始阅读。第 50 行,然后从第 100 行开始,依此类推。 ?

你应该使用另一个线程一次读取完整的文件,

请阅读此Asynctask in Android

但请注意,您不能在不同的线程而不是主线程上执行任何 UI 相关操作(如更改 TextView 的文本)....!为此,还请关注下方link、

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

这看起来是 AsyncTask 的好去处。您甚至可以在从文件 .

中读取文本 来更新 TextView
    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]);
        }
    }