views:

174

answers:

3

Hi,

Today I got this question for which I think I answered very bad. I said stream is a data that flows and reader is a technique where we read from that is a static data. I know this is an awful answer, so please provide me the crisp difference and definitions between these two with example in Java.

Thanks.

+4  A: 

Stream is for reading bytes, Reader is for reading characters. One character may take one byte or more, depending on character set.

Peter Štibraný
+7  A: 

An InputStream is byte-oriented. A Reader is character-oriented.

The javadocs are your friend, explaining the difference. Reader, InputStream

Brabster
+1 for suggesting RTFM
Liz Albin
Without saying RTFM.
instanceofTom
what is expansion of RTFM?
Bragboy
RTFM == Read The F***ing Manual
NomeN
+2  A: 

As others have said, the use cases for each are slightly different (even though they often can be used interchangeably)

Since readers are for reading characters, they are better when you are dealing with input that is of a textual nature (or data represented as characters). I say better because Readers (in the context of typical usage) are essentially streams with methods that easily facilitate reading character input.

instanceofTom
+1 that is really the point, a Reader is generally backed by an InputStream of some sort (not always - see StringReader) and performing a conversion of bytes to 16 bit unicode characters.
Yishai