函数差别InputStream.Read 与 InputStreamReader.Read的使用

时间:13-02-25 栏目:android学习笔记 作者:鲁智森也有文化 评论:0 点击: 9,868 次

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... 
data = input.read(); } 

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(); } 

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.


Answer Form  Dan Halbert & Jack

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

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

Answer Form TerFex

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 ?


Answer Form  Andreas_D

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 whileInputStreamReader::read reads a single char (respecting the encoding) and returns this as anint.



声明: 本文由( 鲁智森也有文化 )原创编译,转载请保留链接: 函数差别InputStream.Read 与 InputStreamReader.Read的使用

函数差别InputStream.Read 与 InputStreamReader.Read的使用:等您坐沙发呢!