【发布时间】: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