tags:

views:

114

answers:

4

What's the benefit of using InputStream over InputStreamReader, or vice versa.

Here is an example of InputStream in action:

InputStream input = new FileInputStream("c:\\data\\input-text.txt");

int data = input.read();
while(data != -1) {
  //do something with data...
  doSomethingWithData(data);

  data = input.read();
}
input.close();

And here is an example of using InputStreamReader (obviously with the help of InputStream):

InputStream inputStream = new FileInputStream("c:\\data\\input.txt");
Reader      reader      = new InputStreamReader(inputStream);

int data = reader.read();
while(data != -1){
    char theChar = (char) data;
    data = reader.read();
}

reader.close();  

Does the Reader process the data in a special way?

Just trying to get my head around the whole i/o streaming data aspect in Java.

+3  A: 

If you want to read binary data use InputStream.

If you want to read strings from a binary stream, use InputStreamReader. One of its constructors allows you to specify a character set.

For this reason do not use FileReader as it uses a platform default for a character set, which is, in many cases, not practical.

Alexander Pogrebnyak
+2  A: 

Hi,

Well InputStreamReader is used to directly read characters.

So reading them as int and then converting to char is not really optimal.

That is the main difference I believe.

InputStream gives you the bytes, and the Reader gives you already chars so it reads the InputStream 8bits at a time.

In addition, if you're reading big chunks of text, you can even wrap the InputStreamReader in a BufferedReader which provides you with some nice methods to let's say read whole lines at once.

This helping you out ?

Cheers,

Trefex
Yea, it makes sense.Is there a way to specify a buffer, so that it reads more than 1byte at a time - say for instance I wanted it to read 1024 bytes at a time.
xil3
Hi,Well if you use BufferedReader you can define the size of the Buffer as per the Javadoc "BufferedReader(Reader in, int sz)". However, for an InputStreamReader I believe this only ready 1 byte at a time. You can't change that.
Trefex
Alright, thanks :)
xil3
+1  A: 

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 counterpart 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) in a gracefully way.

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 InputReader..

Jack
A: 

From InputStreamReader javadoc:

A class for turning a byte stream into a character stream. Data read from the source input stream is converted into characters by either a default or a provided character converter. The default encoding is taken from the "file.encoding" system property. {@code InputStreamReader} contains a buffer of bytes read from the source stream and converts these into characters as needed.

For InputStreams, that actually contain characters in a known encoding, use the reader. Otherwise you just get the bytes and will have to do the conversion to char 'by hand'.

The difference between the two read methods:

InputStream::read reads a single byte and returns it as an int while InputStreamReader::read reads a single char (respecting the encoding) and returns this as an int.

Andreas_D