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

[2651 byte] By [tuamana] at [2007-11-27 11:46:39]
# 1

java.io.IOException: Stream closed

Oh come on. How mch more clearer can the error message be? Can you walk through a door after it has been closed?

floundera at 2007-7-29 18:08:46 > top of Java-index,Java Essentials,New To Java...
# 2

> java.io.IOException: Stream closed

>

> Oh come on. How mch more clearer can the error

> message be? Can you walk through a door after it has

> been closed?

May I interest you in some Midol?

cotton.ma at 2007-7-29 18:08:46 > top of Java-index,Java Essentials,New To Java...
# 3

> > java.io.IOException: Stream closed

> >

> > Oh come on. How mch more clearer can the error

> > message be? Can you walk through a door after it

> has

> > been closed?

>

> May I interest you in some Midol?

Bite me!

floundera at 2007-7-29 18:08:46 > top of Java-index,Java Essentials,New To Java...
# 4

tuaman,

The answer to your question is that when you close a Stream you close the underlying streams as well. So that is why it is closed in the second method. Because when you called close the first time you closed your Reader and the underlying stream.

cotton.ma at 2007-7-29 18:08:46 > top of Java-index,Java Essentials,New To Java...
# 5

> > > java.io.IOException: Stream closed

> > >

> > > Oh come on. How mch more clearer can the error

> > > message be? Can you walk through a door after it

> > has

> > > been closed?

> >

> > May I interest you in some Midol?

>

> Bite me!

I'm not big on seafood.

I just don't think it's that bad a question.

cotton.ma at 2007-7-29 18:08:46 > top of Java-index,Java Essentials,New To Java...
# 6

Hi cotton.m,

Thank you so much for your reply. Yep ok so I also closed the underlying streams, so is the design of the code wrong? I mean how could I use the stream again? I just thought that by calling the

new InputStreamReader(System.in) would again open that stream. Can you please further explain on what would by my code lacks in order to read again from the input stream? Thanks again.

Regards,

Rhani

tuamana at 2007-7-29 18:08:46 > top of Java-index,Java Essentials,New To Java...