Java InputStream 与 InputStreamReader

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3194918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:17:34  来源:igfitidea点击:

InputStream vs InputStreamReader

javaio

提问by xil3

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

使用InputStreamover 有什么好处,InputStreamReader反之亦然。

Here is an example of InputStreamin action:

下面是一个InputStream在行动中的例子:

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):

这是使用 InputStreamReader 的示例(显然是在 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?

Reader 是否以特殊方式处理数据?

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

只是想了解i/oJava 中的整个流数据方面。

采纳答案by Hyman

They represent somewhat different things.

它们代表了一些不同的东西。

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

InputStream是父类中的所有可能的字节,它本身不是有用的,但所有的子类(如FileInputStream您正在使用)是伟大的处理二进制数据。

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 InputStreamReader(及其父亲Reader)专门用于处理字符(因此是字符串),因此它们可以优雅地处理字符集编码(utf8、iso-8859-1 等)。

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

简单的答案是:如果您需要二进制数据,您可以使用一个InputStream(也是一个特定的,如 a DataInputStream),如果您需要处理文本,请使用InputStreamReader..

回答by Alexander Pogrebnyak

If you want to read binary data use InputStream.

如果要读取二进制数据,请使用 InputStream。

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

如果要从二进制流中读取字符串,请使用 InputStreamReader。它的构造函数之一允许您指定字符集。

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

出于这个原因,不要使用 FileReader,因为它使用字符集的平台默认值,这在许多情况下并不实用。

回答by Trefex

Well InputStreamReaderis used to directly read characters.

WellInputStreamReader用于直接读取字符。

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

所以将它们读为 int 然后转换为 char 并不是真正的最佳选择。

That is the main difference I believe.

这是我认为的主要区别。

InputStreamgives you the bytes, and the InputStreamReadergives you already chars so it reads the InputStream8bits at a time.

InputStream给你字节,InputStreamReader给你已经字符,所以它一次读取 8InputStream位。

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

此外,如果您正在阅读大量文本,您甚至可以将InputStreamReadera包裹起来,BufferedReader这为您提供了一些很好的方法,让我们可以说一次阅读整行。

This helping you out ?

这对你有帮助吗?

You can also read this article: https://docs.oracle.com/javase/tutorial/essential/io/charstreams.html

您还可以阅读这篇文章:https: //docs.oracle.com/javase/tutorial/essential/io/charstreams.html

Cheers,

干杯,

回答by Andreas Dolk

From InputStreamReader javadoc:

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

用于将字节流转换为字符流的类。从源输入流读取的数据通过默认或提供的字符转换器转换为字符。默认编码取自“file.encoding”系统属性。{@code InputStreamReader} 包含从源流读取的字节缓冲区,并根据需要将这些字节转换为字符。

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

对于实际包含已知编码字符的 InputStreams,请使用读取器。否则,您只会获得字节,并且必须“手动”转换为字符。

The difference between the two readmethods:

两种read方法的区别:

InputStream::readreads a single byteand returns it as an intwhile InputStreamReader::readreads a single char(respecting the encoding) and returns this as an int.

InputStream::read读取单个byte并将其返回为intwhileInputStreamReader::read读取单个char(尊重编码)并将其作为int.

回答by Asthme

InputstreamReader is used to read the Unicode's data which you can't read in inputstream.

InputstreamReader 用于读取在 inputstream 中无法读取的 Unicode 数据。

回答by AbSak-The Humble Learner

InputStreamhelps us read byte streams
whereasInputStreamReaderhelps decode those byte streams into char streams using some charsets like UTF-8 or others.
Butfor more efficiency Java API recommends to use BufferedReaderalso along with InputStreamand InputStreamReader

InputStream的帮助我们读字节流
InputStreamReader的帮助解码这些字节流成char使用一些字符集如UTF-8或其他流。
但是为了提高效率,Java API 建议同时使用BufferedReaderInputStreamInputStreamReader