Our machine can only read bytecode, but it is difficult for us to read bytecode. Therefore, encoding and decoding are required when people communicate with the machine. InputStreamReader and its subclass FileReader: (from byte to character) is a decoding process; OutputStreamWrite and its subclass FileWriter: (from character to byte) is a encoding process. In the decoding process of InputStreamReader, the most important thing is to specify the encoding character set for the StreamDecoder class InputStream to Reader. Otherwise, the default Character Set of the operating system will be used, which may cause garbled characters. (Check the constructor of the InputStreamReader class in JDK. Specify the constructor other than the first constructor.) If you do not understand this figure, first read the article Association, Aggregation and Composition. There is still a problem to solve in this figure: What is StreamDecoder? This is a bit complicated, and there is little information on the Internet. Let's look at the source code by myself: [java] public class InputStreamReader extends Reader {private final StreamDecoder sd; // It is known that there must be a StreamDecoder object public InputStreamReader (InputStream in) in InputStreamReader {// InputStreamReader has multiple constructor methods. I suppose it uses this super (in ); try {// create a StreamDecoder object sd = StreamDecoder. forInputStreamReader (in, this, (String) null); // use the system default encoding} catch (UnsupportedEncodingException e ){ // The default encoding shoshould always be available throw new Error (e) ;}} public int read () throws IOException {// It's tricky to see, it turns out that StreamDecoder is in read return sd. read ();}/** other methods. Check the relevant methods. **/}. Let's take a look at StreamDecoder in JDK 7 (not displayed in eclipse, I don't know why. I can view the source code on this website) how to implement it: public class StreamDecoder extends Reader {private static final int MIN_BYTE_BUFFER_SIZE = 32; private static final int DEFAULT_BYTE_BUFF ER_SIZE = 8192; private Charset cs; private CharsetDecoder decoder; private ByteBuffer bb; // The following method is public static StreamDecoder forInputStreamReader (InputStream in, object lock, String charsetName) throws UnsupportedEncodingException {String csn = charsetName; if (csn = null) // because the default encoding is used, this csn = Charset is executed. defaultCharset (). name (); try {if (Charset. isSupported (Csn) // checks whether JVM supports return new StreamDecoder (in, lock, Charset. forName (csn);} catch (IllegalCharsetNameException x) {} throw new UnsupportedEncodingException (csn);} StreamDecoder (InputStream in, Object lock, Charset cs) {this (in, lock, cs. newDecoder (). onMalformedInput (CodingErrorAction. REPLACE ). onUnmappableCharacter (CodingErrorAction. REPLACE); // The amount, indicating that it is using the Charset object to generate the CharsetDecoder object. The purpose is To execute another constructor} StreamDecoder (InputStream in, Object lock, CharsetDecoder dec) {// CharsetDecoder: is an engine, A byte sequence can be converted into a 16-bit Unicode sequence super (lock) according to a specific character set; this. cs = dec. charset (); this. decoder = dec; // the following code is unnecessary. We cannot use it here. // This path disabled until direct buffers are faster if (false & in instanceof FileInputStream) {ch = getChannel (FileInputStream) in); if (ch! = Null) bb = ByteBuffer. allocateDirect (DEFAULT_BYTE_BUFFER_SIZE);} if (ch = null) {this. in = in; this. ch = null; bb = ByteBuffer. allocate (DEFAULT_BYTE_BUFFER_SIZE);} bb. flip (); // So that bb is initially empty} // call this function. public int read () throws IOException {return read0 (); // amount, it is false again; Continue reading} private int read0 () throws IOException {synchronized (lock) {// Return the leftover char, if there is one if (haveLeftoverChar) {haveLeftoverChar = false; return leftoverChar;} // Convert more bytessz char cb [] = new char [2]; // read two bytes at a time int n = read (cb, 0, 2 ); switch (n) {case-1: return-1; case 2: leftoverChar = cb [1]; haveLeftoverChar = true; // fall through case 1: return cb [0]; default: assert false: n; return-1;} // end of catch} // end of synchronized} understands who is responsible for conversion. Haha