Java 中的 inputstream inputstreamreader 阅读器
inputteam每次读取一个字节,而inputstreamreader可以将byte转换为characher,然后每次读取一个字符,而reader每次也读取一个字符,那么它们之间有什么区别呢?
inputsteam reads a byte each time, and inputstreamreader can convert byte to characher, and then reads a character each time, and reader also reads a character each time, so what is the difference between them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
InputStreamReader
处理编码。字符并不总是适合byte
(8 位),并且字节值并不总是映射到相同的字符,例如 javachar
使用 16 位来编码字符这使得可以表示更多数量的不同字符。根据输入流的源,字符可以使用 ASCII(1 字节)、UTF-8(1 或更多字节)、UTF-16(2 或 4 字节)、utf-32(4 字节)或任何其他现有的编码编码。给定正确的字符集,读者可以将原始字节转换为相应的 java 字符。
The
InputStreamReader
handles the encoding. A character does not always fit into abyte
(8bit) and the byte value does not always map to the same char, the javachar
for example uses 16bit to encode a character which makes it possible to represent a greater number of different characters.Depending on the source of the InputStream a character may be encoded with ASCII(1 byte), UTF-8(1 or more byte), UTF-16(2 or 4 byte), utf-32(4 byte) or any other existing encoding. Given the right Charset a Reader can convert the raw bytes into the corresponding java character.
来自 JavaDocs:
输入流:
这个抽象类是表示字节输入流的所有类的超类
Input Stream Reader:
从字节流到字符流的桥梁:它读取字节并使用指定的字符集将它们解码为字符。
流只是为您提供原始字节,读取器可以将原始字节转换为不同编码(ASCII / ISO / UTF)的字符。
http://download.oracle.com/javase/6 /docs/api/java/io/InputStream.html
http://download.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html
http://download.oracle.com/javase /6/docs/api/java/nio/charset/Charset.html
From the JavaDocs:
Input Stream:
This abstract class is the superclass of all classes representing an input stream of bytes
Input Stream Reader:
a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset
The stream just gives you the raw bytes, the reader can convert the raw bytes into characters for different encodings (ASCII/ISO/UTF).
http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html
http://download.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html
http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html
InputStreamReader 是抽象类 Reader 的实现,它从 InputStream 读取字符,根据给定的字符集转换字节。 Reader 还有其他实现,例如 StringReader,它从字符串返回字符,不需要任何字符集转换。
InputStreamReader is an implementation of the abstract class Reader that reads character from an InputStream, converting bytes according to a given charset. There are other implementations of Reader too, for example StringReader which return character from a string and does not need any charset conversion.