开发者

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

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 techniqu开发者_高级运维e 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.


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

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


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.


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


Stream classes are byte-oriented classes, that mean all InputStream classes (Buffered and non-buffered) read data byte by byte from stream and all OutputStream(Buffered and non-buffered) classes writes data byte by byte to the stream. Stream classes are useful when you have small data or if you are dealing with binary files like images.

On the other handReader/Writer are character based classes. These classes read or write one character at time from or into stream. These classes extends either java.io.Reader (all character input classes) or java.io.Writer (all character output classes). These classes are useful if you are dealing with text file or other textual stream. These classes are also Buffered and Non-Buffered.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜