package ReaderPack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReaderDemo {
public static void main(String[] args) throws IOException {
while (true) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg = br.readLine();
System.out.println(msg);
br.close();
}
}
}
Output: Hello Hello Exception in thread "main" java.io.IOException: Stream closed at ReaderPack.ReaderDemo.main(ReaderDemo.java:13)
My Question: The first round of execution of while loop, I can input something via the keyboard. Why it does not allow me to do this a second time? I think every round of the while loop, a new BufferedReader br should be initiated with System.in, and the next round it should be a new BufferedReader stream, which is irrelevant with the previous one. Seems like if I close the previous stream, then even in the future round a new stream cannot be re-initiated? Thank you!