我真的不知道这有什么问题,我想逐行读取txt文件(目前只有10行)并将每行存储在一些名为mChoices的arraylist中.
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz_vieaaaw);
try {
InputStream inputStream = getApplicationContext().getAssets().open("questions.txt");
BufferedReader buffReader = new BufferedReader(new InputStreamReader(inputStream));
String line = buffReader.readLine();
while (line != null) {
mChoices.add(line);
}
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
for (int i=0; i < mChoices.size(); i++) {
String line = mChoices.get(i);
Log.d("LINE", line);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在13571696字节的分配上失去了内存.
如果我在条件时注释掉它只返回第一行,但显然我想要在txt中的每一行.
谢谢
while (line != null) {
mChoices.add(line);
}
Run Code Online (Sandbox Code Playgroud)
您需要每次都更新该行,否则您将始终读取第一行(在您的情况下不是空的,因此您将在第一行写入无限次,直到可用内存为止).
要在每次迭代时更新行,请执行以下操作:
String line;
while ((line = buffReader.readLine()) != null) {
mChoices.add(line);
}
Run Code Online (Sandbox Code Playgroud)