Differencebetween Reader and Inputstream

What is the difference between Reader and InputStream?

An InputStream is the raw method of getting information from a resource. It grabs the data byte by byte without performing any kind of translation. If you are reading image data, or any binary file, this is the stream to use.

A Reader is designed for character streams. If the information you are reading is all text, then the Reader will take care of the character decoding for you and give you unicode characters from the raw input stream. If you are reading any type of text, this is the stream to use.

You can wrap an InputStream and turn it into a Reader by using the InputStreamReader class.

Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);

Difference between InputStream and InputStreamReader in java

There's a big difference between what is an InputStream and an InputStreamReader. One reads bytes whereas the other one reads character. Depending on the encoding used, one character may be more than 1 byte.

From InputStream.read():

Reads the next byte of data from the input stream

From InputStreamReader.read():

Reads a single character.

InputStreamReader serves as a bridge between those two ways of reading data in Java: a way of bridging byte streams to character streams. From its Javadoc:

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

InputStream vs Reader

You can use a Reader to read text data. And it's supports some character encoding like - ISO, UTF-8. If you want to read a text file with some encoding then you can use Readers like - BufferedReader, StringReader etc.

And you can use Stream (InputStream, OutputStream) to manipulate binary data. For example you want to read a image file then you can use FileInputStream and when you want to save it to disk then you can use FileOutputStream.

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

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.

The difference between InputStream and InputStreamReader when reading multi-byte characters

An InputStream reads raw octet (8 bit) data. In Java, the byte type is equivalent to the char type in C. In C, this type can be used to represent character data or binary data. In Java, the char type shares greater similarities with the C wchar_t type.

An InputStreamReader then will transform data from some encoding into UTF-16. If "a你们" is encoded as UTF-8 on disk, it will be the byte sequence 61 E4 BD A0 E4 BB AC. When you pass the InputStream to InputStreamReader with the UTF-8 encoding, it will be read as the char sequence 0061 4F60 4EEC.

The character encoding API in Java contains the algorithms to perform this transformation. You can find a list of encodings supported by the Oracle JRE here. The ICU project is a good place to start if you want to understand the internals of how this works in practice.

As Alexander Pogrebnyak points out, you should almost always provide the encoding explicitly. byte-to-char methods that do not specify an encoding rely on the JRE default, which is dependent on operating systems and user settings.

Are `InputStream` and `Reader` essentially the same, and are `OutputStream` and `Writer` essentially the same?

They're not essentially the same, but they do the same sorts of things for different kinds of data.

InputStream and OutputStream work in bytes. You'd use them when dealing with non-textual information (such as an image).

Reader and Writer work in characters. You'd use them when dealing with textual information.

So "yes" and "no". :-) InputStream and Reader are both for reading information (a stream of bytes or a stream of characters, respectively), and OutputStream and Writer are both for writing information (a stream of bytes or a stream of characters, respectively). Which you use depends on what kind of data you're dealing with. The streams are byte-oriented. The readers/writers are character-oriented.

There are bridging classes between the two kinds of data:

  • InputStreamReader reads from an InputStream and converts bytes to characters using a CharSet (one provided explicitly or by name).
  • OutputStreamWriter does the converse: Converts characters to bytes (again via a CharSet) and writes the bytes to an OutputStream.

...but most Reader/Writer subclasses read from/write to sources/destinations that are already character-based, and so don't deal with bytes at all. For instance, StringReader reads characters from a string. Since the source (the string) is already character-based, the Reader doesn't ever deal with bytes, just characters.

InputStream vs InputStreamReader

They represent somewhat different things.

The InputStream is the ancestor class of all possible streams of bytes, it is not useful by itself but all the subclasses (like the FileInputStream that you are using) are great to deal with binary data.

On the other hand, the InputStreamReader (and its father Reader) are used specifically to deal with characters (so strings) so they handle charset encodings (utf8, iso-8859-1, and so on) gracefully.

The simple answer is: if you need binary data you can use an InputStream (also a specific one like a DataInputStream), if you need to work with text use an InputStreamReader..

What is the difference between Java's BufferedReader and InputStreamReader classes?

BufferedReader is a wrapper for both "InputStreamReader/FileReader", which buffers the information each time a native I/O is called.

You can imagine the efficiency difference with reading a character(or bytes) vis-a-vis reading a large no. of characters in one go(or bytes). With BufferedReader, if you wish to read single character, it will store the contents to fill its buffer (if it is empty) and for further requests, characters will directly be read from buffer, and hence achieves greater efficiency.

InputStreamReader converts byte streams to character streams. It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Hope it helps.

What is the relation between InputStream, BuffreredInputStream, InputStreamReader and BufferedReader?

Sample Image
InputStream is parent class of all input streams and readers. Classes that have Stream keyword will work with bytes whereas classes which have Reader keyword will work with characters.

Buffer is wrapper around these streams to decrease the system calls and increase performance and speed of reading.

Non buffered streams return single byte each time whereas Bufferd stream will not return until the buffer gets full.

For example if you take BufferedReader you can read a whole line using readLine() but in non buffered stream you must read single character using read() method.



Related Topics



Leave a reply



Submit