简体   繁体   English

Reader 和 InputStream 有什么区别?

[英]What is the difference between Reader and InputStream?

What is the difference between Reader and InputStream? Reader 和 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,我猜是读取对象?

An InputStream is the raw method of getting information from a resource. InputStream 是从资源中获取信息的原始方法。 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. Reader 是为字符流设计的。 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.如果您正在阅读的信息都是文本,那么阅读器将为您处理字符解码,并从原始输入流中为您提供 unicode 字符。 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.您可以使用 InputStreamReader 类包装 InputStream 并将其转换为 Reader。

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

InputStreams are used to read bytes from a stream. InputStreams 用于从流中读取字节。 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.另一方面,读取器是字符流,因此它们最适合用于读取字符数据。

I guess the source of confusion is that InputStream.read() returns an int and Reader.read() also returns an int .我想混淆的根源是InputStream.read()返回一个int并且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 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. InputStream允许您逐字节读取内容,例如内容“a‡a”有 3 个字符,但它以 UTF-8 编码为 5 个字节。 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因此,使用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)

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 Reader允许您逐个字符阅读内容,因此内容“a‡a”被读取为 3 个字符97822597 ,其中

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字符 ‡在 Unicode 中被称为U+2021

What is the difference between Reader and InputStream? Reader和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,我想读取对象吗?

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

InputStream accept byte,Reader accept character, In Java, one character = two bytes , and Reader use buffer,InputStream not use. InputStream 接受字节,Reader 接受字符,在 Java 中,一个字符 = 两个字节,Reader 使用缓冲区,InputStream 不使用。 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。

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

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