Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a stream and a reader in Java?

Tags:

java

stream

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.

like image 317
bragboy Avatar asked Mar 11 '10 19:03

bragboy


2 Answers

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

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

like image 139
brabster Avatar answered Oct 25 '22 03:10

brabster


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.

like image 37
Tom Neyland Avatar answered Oct 25 '22 03:10

Tom Neyland