Java Reader 和 InputStream 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4367539/
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-14 16:36:34  来源:igfitidea点击:

What is the difference between Reader and InputStream?

javafileinputstreamfilereaderfileinputstream

提问by sab

What is the difference between Reader and InputStream? And when to use what? If I can use Reader for reading characters why I will use inputstream, I guess to read objects?

Reader 和 InputStream 有什么区别?什么时候使用什么?如果我可以使用 Reader 读取字符,为什么我会使用 inputstream,我猜是读取对象?

采纳答案by Berin Loritsch

An InputStream is the raw method of getting information from a resource. It grabs the data byte by byte without performing any kind of translation. If you are reading image data, or any binary file, this is the stream to use.

InputStream 是从资源中获取信息的原始方法。它逐字节抓取数据而不执行任何类型的转换。如果您正在读取图像数据或任何二进制文件,这就是要使用的流。

A Reader is designed for character streams. If the information you are reading is all text, then the Reader will take care of the character decoding for you and give you unicode characters from the raw input stream. If you are reading any type of text, this is the stream to use.

Reader 是为字符流设计的。如果您正在阅读的信息都是文本,那么阅读器将为您处理字符解码,并从原始输入流中为您提供 unicode 字符。如果您正在阅读任何类型的文本,这就是要使用的流。

You can wrap an InputStream and turn it into a Reader by using the InputStreamReader class.

您可以使用 InputStreamReader 类包装 InputStream 并将其转换为 Reader。

Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);

回答by Aaron McIver

One accepts bytes and the other accepts characters.

一个接受字节,另一个接受字符。

回答by Vincent Ramdhanie

InputStreams are used to read bytes from a stream. So they are useful for binary data such as images, video and serialized objects.

InputStreams 用于从流中读取字节。因此它们对于二进制数据非常有用,例如图像、视频和序列化对象。

Readers on the other hand are character streams so they are best used to read character data.

另一方面,读取器是字符流,因此它们最适合用于读取字符数据。

回答by RubenLaguna

I guess the source of confusion is that InputStream.read()returns an intand Reader.read()also returns an int.

我想混淆的根源是InputStream.read()返回 anint并且Reader.read()还返回一个int.

The difference is that InputStream.read()return byte values between 0 and 255 corresponding to the raw contents of the byte stream and Reader.read()return the character value which is between 0 and 65357 (because there are 65358 different unicode codepoints)

区别在于InputStream.read()返回字节流原始内容对应的0到255之间的字节值,Reader.read()返回0到65357之间的字符值(因为有65358个不同的unicode码点)

An InputStreamlets you read the contents byte by byte, for example the contents "a?a" are read as a stream of 5 bytes, namely, 97, 226, 128, 161and 97where a -> U+0061 -> 0x61 -> 97and ? -> U+2021 -> 0xE280A1 (utf-8 encoding of 0x2021) -> 226 128 161.

AnInputStream允许您逐字节读取内容,例如内容“a?a”被读取为 5 个字节的流,即97, 226, 128,161以及97wherea -> U+0061 -> 0x61 -> 97? -> U+2021 -> 0xE280A1 (utf-8 encoding of 0x2021) -> 226 128 161

A Readerlets you read the contents character by character so the contents "a?a" are read as 3 characters 97, 8225and 97where a -> U+0061 -> 0x61 -> 97and ? -> U+2021 -> 0x2021 -> 8225.

AReader允许您逐个字符读取内容,因此内容“a?a”被读取为 3 个字符978225以及97wherea -> U+0061 -> 0x61 -> 97? -> U+2021 -> 0x2021 -> 8225

回答by LiLi

InputStream accept byte,Reader accept character, In Java, one character = two bytes , and Reader use buffer,InputStream not use. All file store in disk or transfer based on byte, include image and video, but character is in memory,so InputStream is used frequently.

InputStream 接受字节,Reader 接受字符,在 Java 中,一个字符 = 两个字节,Reader 使用缓冲区,InputStream 不使用。所有文件存储在磁盘或基于字节传输,包括图像和视频,但字符在内存中,因此经常使用InputStream。