tags:

views:

143

answers:

7
+1  Q: 

basic I/O question

What is the difference between Reader and InputStream?
Or in other words,what is difference between "the stream of bytes" and "the stream of characters"?
I wonder when I should use InputStream or Reader otherwise,Could you suggest the material to
me for understanding the question by myself?

+2  A: 

Basic I/O question leads to the Basic I/O tutorial :)

InputStream/OutputStream handle bytes, Reader/Writer handle multi-byte characters.

skaffman
+1  A: 

Basically a Reader's works with the type char useful for human readable text.

Reader:

...class for reading character streams...

The Stream's work directly with the type byte useful for binary data.

InputStream:

..class [..] representing an input stream of bytes...

Link to read further: Java I/O streams

Quote:

Unless you are working with binary data, such as image and sound files, you should use readers and writers (character streams) to read and write information for the following reasons:

-They can handle any character in the Unicode character set (while the byte streams are limited to ISO-Latin-1 8-bit bytes).

-They are easier to internationalize because they are not dependent upon a specific character encoding.

-They use buffering techniques internally and are therefore potentially much more efficient than byte streams.

The article dates '98 but it is still valid.

OscarRyz
A: 

From Java - Primitive Data Types:

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Ben S
+8  A: 

One is a sequence of bytes (binary data), the other a sequence of characters (text). Bytes and characters are totally different things (don't believe the C standard, it lies!). You can convert between them using a character encoding - this is what e.g. InputStreamReader does.

Further reading material: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Michael Borgwardt
A: 

InputStreams deal at the lowest level with raw data. Readers are at a slightly higer level of abstraction, dealing with characters (which are still ultimately encoded in an underlying stream of bytes)

alasdairg
A: 

There is a good overview of the differences in the various InputStream's, OutputStreams, Reader's and Writer's here, as part of a larger tutorial covering Java IO:

http://tutorials.jenkov.com/java-io/overview.html

Jakob Jenkov
A: 

In simple talk: Open the file of interest in notepad or some other text editor. Can you read it as if it's a natural language? Then you likely need a Reader to read it in Java as well, you would end up with Strings. Do you instead see some "garbage"? Then you need an InputStream to read it in Java, you would end up with byte arrays (or streams). Known examples of binary files are image files (gif, jpg, png, etcetera) and office documents (doc, xls, ppt, pdf, etcetera).

You can also use an InputStream to open text files, but you really don't want do use a Reader to open binary files, because there's no means of any character encoding. The same story applies on using OutputStream and Writer. Do in no instance use Writer to create binary files.

BalusC