當我試圖在while循環中關閉BufferedReader時,當循環第二次執行時它將拋出IOException: Stream closed
。如何在while循環中關閉BufferedReader
我的代碼是這樣的:
import java.io.*;
class Test {
public static void main(String[] args) {
int option = 0;
BufferedReader br = null;
while (option != -1) {
try {
br = new BufferedReader(new InputStreamReader(System.in));
try {
option = Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
option = -1;
}
br.close();
} catch (IOException e) {
System.err.println(e);
System.exit(-4);
}
System.out.println(option);
}
}
}
我應該怎麼做關閉的BufferedReader?提前致謝。
你的意思是我不需要關閉BufferedReader,如果我在循環之前打開它的話? – Fingal
不,我的意思是你不應該關閉它,因爲它纏繞着'System.in'。哦,等等,這就是我說的。 – EJP