我想从文本文件中读取多行。当我点击下一个按钮时,这些行应该逐一显示。我能够将字符串存储在一个文件中并读取第一行。但是当我尝试使用下一个按钮阅读下一行时,它停止工作。任何人都可以告诉我一个解决方案。提前致谢。如何从文本文件中读取下一行
我已经定义了以下
BufferedReader buffreader;
String line;
String fav;
StringBuilder text;
InputStream instream;
String favQuote0;
这是我的onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save);
StringBuilder text = new StringBuilder();
try {
// open the file for reading
InputStream instream = openFileInput("myfilename.txt");
// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
while ((line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
fav = text.toString();
}
}
} catch (IOException e) {}
TextView tv1 = (TextView)findViewById(R.id.textView2);
tv1.setText(fav);
}
这是我的下一个按钮
public void next (View view1) {
try {
// open the file for reading
InputStream instream = openFileInput("myfilename.txt");
// if file the available for reading
if (instream != null) {
while ((line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
fav = text.toString();
}
}
} catch (IOException e) {}
TextView tv1 = (TextView)findViewById(R.id.textView2);
tv1.setText(fav);
}
您是否在LogCat中看到任何错误? –
什么都没有显示 – user1995307