Many people asked question like that but this one is a little bit different. Here is the code:
public static BufferedReader reader;
public static String readString() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
reader.close();
return s;
}
While program runtime readString
method is invoked many times. The second call causes exception: stream closed
. I can not understand: why it ends up so? Every time we declare new BufferedReader
. So the stream
must be also new. Or not?
If not, how should I organize my program so that it will close reader after all invocations and after my program is over?