简体   繁体   中英

inputstream inputstreamreader reader in Java

inputteam每次读取一个字节,inputstreamreader可以将字节转换为characher,然后每次读取一个字符,reader每次也读取一个字符,所以它们之间有什么区别?

The InputStreamReader handles the encoding. A character does not always fit into a byte (8bit) and the byte value does not always map to the same char, the java char 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.

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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