【问题标题】:Android: Read a text file from assets from custom lineAndroid:从自定义行的资产中读取文本文件
【发布时间】:2015-10-10 22:11:29
【问题描述】:

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

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

这不好,无论如何关闭它们并再次打开它们并从最后一行开始阅读,或者任何想法如何从前开始阅读。第 50 行,然后从第 100 行,等等。?

【问题讨论】:

  • “应用程序冻结很长时间......” 为什么不使用后台线程?
  • 我是 android 新手,我不知道怎么做,所以如果您解释一下您的想法,我们将不胜感激...
  • 你刚刚得到了答案,哦,甚至两个 :)...所以,请听从那些人的建议。

标签: android assets readfile line-by-line


【解决方案1】:

这看起来是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]);
        }
    }

【讨论】:

    【解决方案2】:

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

    请阅读此Asynctask in Android

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      • 2012-05-11
      相关资源
      最近更新 更多