user3501758 user3501758 - 1 year ago 89
Java Question

close() closes input streams permanently

I am trying to make a simple method readIn() that reads something in from (Can't use Console because System.console() returns null when I run in Eclipse). The idea is to call readIn as needed, like this

classs Foo{
public static void(String[] arg){

String first = readIn("First, please");

String second = readIn("Second, please");

Here is the simplest form of readIn():

static String readIn(String prompt){

System.out.println(prompt + ": ");
BufferedReader br = new BufferedReader(new InputStreamReader(;
String line = br.readLine();
br.close(); // PROBLEM STATEMENT
return line;


If I omit the br.close() statement it works fine - I can call readIn repeatedly. But if I close br, as I should, then only the first call works. Second call throws IOException: Stream closed. As expected, the same thing happens with try-with-resources in readIn:

static String readIn(String prompt){

System.out.println(prompt + ": ");
String line;
BufferedReader br = new BufferedReader(
new InputStreamReader( )
line = br.readLine();

return line;

Same thing happens with Scanner( instead of BufferedReader. It does not happen when reading from files.

Which stream is closed if a new BufferedReader or Scanner is made in every call to readIn? Is this something about close() closing the "underlying Readable/Closeable" ( Can it be reopened? Trying to understand, thanks.

Answer Source

br.close(); closes also system input stream, so you can't use it unless you restart JVM. You can use the same BufferedReader instance for all required input from

Don't be shy also to mix calls:


They are related to different streams, so there shouldn't be any issues.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download