我不明白如何清除BufferedReader。当我在Activity中按下按钮时,变量设置为1或2.取决于BufferedReader中的数字更改文件。当我在缓冲区中第二次按下时会有两个文件。如何在第二次上传之前告诉BR清除缓冲区。反之亦然,当然。
public List<String> getQuestionLinesList() {
String line;
List<String> lines = new ArrayList<String>();
Log.d(TAG, " Trying to get resourses");
Resources res = context.getResources();
try {
if (selectedBox == 1) {
bufferedQuestions = new BufferedReader(new InputStreamReader(res.openRawResource(R.raw.questions_list)));
} else if (selectedBox == 2) {
bufferedQuestions = new BufferedReader(new InputStreamReader(res.openRawResource(R.raw.questions_list_art)));
}
Log.d(TAG, "number i = " + Integer.toString(selectedBox));
while ((line = bufferedQuestions.readLine()) != null) {
lines.add(line);
Log.d(LINETAG, line);
}
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
答案 0 :(得分:0)
在文件的末尾添加一个/ / n,然后readLine()应该能够清除你的BufferedReader。
public String readLine() throws IOException
读取一行文字。一条线被认为是由换行符('\ n'),回车符('\ r')或回车符后面的任何一个终止。
返回: 包含行内容的String,不包括任何行终止字符;如果已到达流的末尾,则为null
答案 1 :(得分:0)
也许你可以尝试在BufferedReader的方法中使用局部变量而不是&#34; bufferedQuestions&#34;您可能在类中声明了该方法的字段:&#34; getQuestionLinesList&#34;所属