2

I am trying to read input from console using two classes in the same method InputStreamReader and BufferedReader . I have closed the stream of former class and read input again but now using latter class. It is showing error if I close the former class stream before invoking the BufferedReader stream read() method. But when closing the InputStreamReader stream at the end of the method, it is working fine.

My thoughts are - Since I have closed the stream of the former used class, latter stream is independent of it and thus should not affect the running of code.

public static void main(String[] args) throws Exception {


    //File file = new File("D:\\IOSUMIT\\new_file.txt");

    InputStreamReader isr= new InputStreamReader(System.in);

    System.out.println("your input " + (char)isr.read());



    isr.close();  //here error occurs


    InputStreamReader isrp= new InputStreamReader(System.in); // Line 1

    BufferedReader br = new BufferedReader(isrp);
    int temp = br.read();

    System.out.println("your input  Buffered" + (char)temp);



    br.close();

OUTPUT ERROR

4

your input 4Exception in thread "main" java.io.IOException: Stream closed
    at java.io.BufferedInputStream.getBufIfOpen(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.read(Unknown Source)
    at IO.Q7.main(Q7.java:60)
angrysumit
  • 221
  • 4
  • 16

3 Answers3

3

Lets start with this:

isr.close();  //here error occurs

Actually, that is not where the error occurs. That is a red herring. According to the stacktrace, the exception is actually thrown by the following statement:

int temp = br.read();

Which makes sense. The isr.close() is closing both the Reader and the input stream that it wraps. That input stream is the System.in stream.

So, when you then create a second InputStreamReader and BufferedReader you are wrapping a stream that you have previously closed.

Hence, when you try to read from your new readers, you get an exception.

Solution: Once closed, the System.in stream will stay closed. So DON'T close System.in.


You asked:

Would not input stream be open again at commented Line 1 inside the BufferReader constructor ?

 InputStreamReader isrp= new InputStreamReader(System.in); // Line 1

Short answer: No.

The new is creating a new InputStreamReader instance that wraps the current value of System.in. In this case, that value is a reference for a FileInputStream object for file descriptor 0 ... which you previously closed.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1
InputStreamReader isr= new InputStreamReader(System.in);

In the above line, isr is a reference acting as the input stream reader(from input source like keyboard). So once when you close it, the actual input stream gets closed. It is not that isr is closed.

 InputStreamReader isrp= new InputStreamReader(System.in);

Another reference(isrp) just tries to use that stream which has been previously closed before creating a buffered-reader on the stream, and so no further reading is allowed (as the standard input stream, on System.in, has already been closed).

Also, it is just an insane idea to think of using 2 input-streams for the same input source. You should use the buffered-reader on the opened input-stream. Only after you read your whole-input, then only you should close the stream, finally.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • Would not input stream be open again at commented Line 1 inside the BufferReader constructor ??. Since reference isrp is using **new**, instead of using previous, it would make a new one. – angrysumit Apr 14 '16 at 11:55
  • 1
    @angrysumit - No, as I already answered in the 2nd paragraph, the input stream gets closed on standard input(System.in), the same you're trying to open in the 2nd line. You can use any other input like file, for serving as an input stream. But, this same standard-input stream won't open again. And, this results in the throwing of that exception(`java.io.IOException: Stream closed`). – Am_I_Helpful Apr 14 '16 at 12:03
1

The problem is with

isr.close();  //here error occurs

When the line is actually executed, the input stream closes and the buffered reader is not able to find out the closed stream. The fix you can do is comment out the close line and execute it after all the computations. Once an input stream is closed it cannot be reopened again

sameera sy
  • 1,708
  • 13
  • 19