Reader和InputStream有什么区别?

发布于 2024-10-06 14:10:47 字数 90 浏览 0 评论 0原文

Reader和InputStream有什么区别? 以及何时使用什么? 如果我可以使用 Reader 来读取字符,为什么我会使用 inputstream,我想读取对象?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水晶透心 2024-10-13 14:10:47

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

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

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

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

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.

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.

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

Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
傾旎 2024-10-13 14:10:47

输入流用于从流中读取字节。因此它们对于图像、视频和序列化对象等二进制数据很有用。

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

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

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

说谎友 2024-10-13 14:10:47

我想混乱的根源在于 < code>InputStream.read() 返回一个 intReader.read() 也返回一个 int

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

InputStream 允许您逐字节读取内容,例如内容“a‡a”有 3 个字符,但以 5 个字节编码以 UTF-8 格式。因此,使用 Inputstream,您可以将其读取为 5 个字节的流(每个字节表示为 0 到 255 之间的 int),结果为 9722612816197,其中

a -> U+0061 -> 0x61 (hex) -> 97 (dec)
‡ -> U+2021 -> 0xE280A1 (utf-8 encoding of 0x2021) -> 226 128 161 (1 int per byte)
a -> U+0061 -> 0x61 (hex) -> 97 (dec)

Reader 可让您阅读内容逐个字符,因此内容“a‡a”被读取为 3 个字符 97822597,其中

a -> U+0061 -> 0x61 -> 97
‡ -> U+2021 -> 0x2021 -> 8225 (single int, not 3)
a -> U+0061 -> 0x61 -> 97

字符 ‡ 被称为 < a href="https://www.fileformat.info/info/unicode/char/2021/index.htm" rel="noreferrer">Unicode 格式的 U+2021

I guess the source of confusion is that InputStream.read() returns an int and Reader.read() also returns an 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)

An InputStream lets you read the contents byte by byte, for example the contents "a‡a" has 3 characters but it's encoded at 5 bytes in UTF-8. So with Inputstream you can read it as a stream of 5 bytes (each one represented as an int between 0 and 255) resulting in 97, 226, 128, 161 and 97 where

a -> U+0061 -> 0x61 (hex) -> 97 (dec)
‡ -> U+2021 -> 0xE280A1 (utf-8 encoding of 0x2021) -> 226 128 161 (1 int per byte)
a -> U+0061 -> 0x61 (hex) -> 97 (dec)

A Reader lets you read the contents character by character so the contents "a‡a" are read as 3 characters 97, 8225 and 97 where

a -> U+0061 -> 0x61 -> 97
‡ -> U+2021 -> 0x2021 -> 8225 (single int, not 3)
a -> U+0061 -> 0x61 -> 97

The character ‡ is referred as U+2021 in Unicode

留一抹残留的笑 2024-10-13 14:10:47

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

One accepts bytes and the other accepts characters.

以往的大感动 2024-10-13 14:10:47

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文