Questions on Stream Closed
Hi,
I was just testing on this sequence of reopening the input stream. Im having an error when trying to read again to the System.in after I've closed in another function. Please advise what could be the cause of it. Here's the code and output. Thanks.
import java.io.InputStreamReader;
import java.io.IOException;
publicclass Streams{
publicstaticvoid main(String[] args){
Streams s =new Streams();
s.closeOne();
s.closeTwo();
}
publicvoid closeOne(){
InputStreamReader ir =new InputStreamReader(System.in);
try{
ir.read();
ir.close();
}catch(IOException io){}
}
publicvoid closeTwo(){
InputStreamReader ir =new InputStreamReader(System.in);
try{
ir.read();
ir.close();
}catch(IOException ioe){ ioe.printStackTrace();}
}
}
java Streams
3
java.io.IOException: Stream closed
at java.io.BufferedInputStream.ensureOpen(BufferedInputStream.java:120)
at java.io.BufferedInputStream.read(BufferedInputStream.java:270)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:408)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:450)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:182)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:131)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:117)
at java.io.InputStreamReader.read(InputStreamReader.java:151)
at Streams.closeTwo(Streams.java:21)
at Streams.main(Streams.java:7)
Regards,
Rhani

