简体   繁体   English

Java中InputStream和InputStreamReader之间的区别

[英]Difference between InputStream and InputStreamReader in java

I read that InputStream is used for byte based reading it reads 1 byte at a time. 我读到InputStream用于基于字节的读取,它一次读取1个字节。 And InputStreamReader is used for character based reading so it reads one character at a time so no need to convert this first to int then read it. InputStreamReader用于基于字符的读取,因此它一次读取一个字符,因此无需先将其转换为int然后再读取它。

Here is reading using InputStream. 这是使用InputStream进行阅读。

 InputStream input=new FileInputStream("D:/input.txt");

 int c;

 while((c=input.read())!=-1)
 {
    System.out.print((char)c);
 }

here is reading using InputStreamReader 这是使用InputStreamReader阅读

InputStream input=new FileInputStream("D:/input.txt");

reader=new InputStreamReader(input,"UTF-8");

int c;

while((c=reader.read())!=-1)
{
    System.out.print((char)c);
}

What is difference between InputStream and InputStreamReader ? InputStreamInputStreamReader什么区别? In both case I have to use a int and then read it and at the end if I want to print that data I have to cast that with "(char)c". 在这两种情况下,我都必须使用int然后读取它,如果要打印该数据,最后必须使用“(char)c”进行强制转换。

So what is advantage of using InputStreamReader ? 那么使用InputStreamReader好处是什么?

There's a big difference between what is an InputStream and an InputStreamReader . 什么是InputStreamInputStreamReader之间有很大的区别。 One reads bytes whereas the other one reads character. 一个读取字节,而另一个读取字符。 Depending on the encoding used, one character may be more than 1 byte. 根据所使用的编码,一个字符可能超过1个字节。

From InputStream.read() : InputStream.read()

Reads the next byte of data from the input stream 从输入流中读取下一个数据字节

From InputStreamReader.read() : 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. InputStreamReader充当这两种Java读取数据方法之间的桥梁:一种将字节流桥接到字符流的方法。 From its Javadoc : 从其Javadoc中

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. InputStreamReader是从字节流到字符流的桥梁:它读取字节,并使用指定的字符集将其解码为字符。 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. InputStreamReaderread()方法之一的每次调用都可能导致从基础字节输入流中读取一个或多个字节。 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. 为了实现字节到字符的有效转换,与满足当前读取操作所需的字节数相比,可以从基础流中提前读取更多字节。

Some encodings have characters that span multiple bytes. 某些编码的字符跨越多个字节。 By reading with InputStream, you're stupidly reading the next byte as opposed to InputStreamReader that may read ahead as necessary in order to give you the next character as opposed to byte. 通过使用InputStream进行读取,您将愚蠢地读取下一个字节,而不是InputStreamReader,后者可能会在必要时提前读取,以便为您提供下一个字符而不是字节。 In other words, suppose you have 0x00 0xA7 as the next two bytes on the stream. 换句话说,假设流中的后两个字节为0x00 0xA7。 InputStream will give you 0x0 on first read and then 0xA7 on the next. InputStream在第一次读取时将为您提供0x0,然后在下次读取时将您提供0xA7。 An InputStreamReader with unicode encoding will return you 0x00A7 on first read which is character §. 具有unicode编码的InputStreamReader在第一次读取时将返回0x00A7,即字符§。

Please note that it is probably best if you wrap InputStreamReader with BufferedReader in most cases. 请注意,在大多数情况下,最好用BufferedReader包装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. InputStream通常总是连接到某些数据源,例如文件,网络连接,管道等。这在Java IO概述文本中也有更详细的说明。

InputStreamReader takes an inputstream and converts the bytes Strem into characters when you are reading it. InputStreamReader接受一个输入流,并在读取它时将Strem字节转换为字符。 For example, some UTF characters are 2 bytes, a call to an input stream reader will read the two bytes and convert it into a character automatically. 例如,某些UTF字符为2个字节,调用输入流读取器将读取这两个字节并将其自动转换为字符。 It is used to read text streams. 它用于读取文本流。

In the case of InputStreamReader you can use 'char c' in place of 'int c' and instead of '-1' value, you can use 'null'. 对于InputStreamReader,可以使用“ char c”代替“ int c”,而可以使用“ null”代替“ -1”值。 Like this : 像这样 :

InputStream input=new FileInputStream("D:/input.txt");

reader=new InputStreamReader(input,"UTF-8");

char c;

while((c=reader.read())!=null)
{
System.out.print(c);
}

Because you are using InputStreamReader which do character based reading. 因为您正在使用InputStreamReader进行基于字符的阅读。 So, there is no need to set datatype 'int' and then to cast it using 'char', it is unnecessary. 因此,无需设置数据类型'int',然后使用'char'进行转换,则没有必要。 We need to do this in the case of InputStream as it does byte based reading. 对于InputStream,我们需要这样做,因为它基于字节进行读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM