whileループでBufferedReaderを閉じるときに、ループが2回目に実行されたときに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のを閉じるにはどうすればよいですか?前もって感謝します。あなたが最近十分バージョン
try {
br = new BufferedReader(new InputStreamReader(System.in));
try {
option = Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
option = -1;
}
} catch (IOException e) {
System.err.println(e);
System.exit(-4);
} finally {
br.close();
}
を使用している場合は例外が発生している場合は
バッファリングされたリーダーをループの前に開くと、そのバッファリングされたリーダーを閉じる必要はありませんか? – Fingal
いいえ、私はあなたが 'System.in'の周りに巻かれているので、閉じてはいけないということです。ああ待って、それは私が実際に言ったことだ。 – EJP