Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InputStream vs InputStreamReader

Tags:

java

io

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.

like image 362
xil3 Avatar asked Jul 07 '10 13:07

xil3


People also ask

What is the difference between InputStream and InputStreamReader?

An InputStream is typically always connected to some data source, like a file, network connection, pipe etc. This is also explained in more detail in the Java IO Overview text. InputStreamReader takes an inputstream and converts the bytes Strem into characters when you are reading it.

What is InputStreamReader InputStream?

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.

What is the difference between InputStreamReader and BufferedReader?

BufferedReader reads a couple of characters from the Input Stream and stores them in a buffer. InputStreamReader reads only one character from the input stream and the remaining characters still remain in the streams hence There is no buffer in this case.

Why do we use InputStreamReader?

The InputStreamReader class of the java.io package can be used to convert data in bytes into data in characters. It extends the abstract class Reader . The InputStreamReader class works with other input streams. It is also known as a bridge between byte streams and character streams.


1 Answers

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

like image 131
Jack Avatar answered Sep 22 '22 02:09

Jack