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
InputStream vs InputStreamReader
提问by xil3
What's the benefit of using InputStream
over InputStreamReader
, or vice versa.
使用InputStream
over 有什么好处,InputStreamReader
反之亦然。
Here is an example of InputStream
in 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/o
streaming data aspect in Java.
只是想了解i/o
Java 中的整个流数据方面。
采纳答案by Hyman
They represent somewhat different things.
它们代表了一些不同的东西。
The InputStream
is the ancestor class of all possible streamsof 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.
该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 InputStreamReader
is 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.
这是我认为的主要区别。
InputStream
gives you the bytes, and the InputStreamReader
gives you already chars so it reads the InputStream
8bits at a time.
InputStream
给你字节,InputStreamReader
给你已经字符,所以它一次读取 8InputStream
位。
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.
此外,如果您正在阅读大量文本,您甚至可以将InputStreamReader
a包裹起来,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 read
methods:
两种read
方法的区别:
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
.
InputStream::read
读取单个byte
并将其返回为int
whileInputStreamReader::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 建议同时使用BufferedReader和InputStream和InputStreamReader