To understand the purpose of this, you need to think hard. In Java, char and String are for "text" expressed as Unicode, and byte or byte[] for binary data. Bytes are not text. Bytes can represent encoded text ... but they must be decoded before you can use the char and String types.
So, when using an InputStreamReader to input characters from a byte stream, why not just use the Reader class (or its subclasses) to read the character directly?
( InputStreamReader is a subclass of Reader , so this is not the case with "either ... or ...".)
The purpose of an InputStreamReader is to adapt an InputStream to a Reader . This adapter will take care of decoding text from bytes to characters that contain Unicode 1 codes.
This way you will use it if you have an existing InputStream (e.g. from a socket) ... or when you need more control over the choice of encoding scheme. (Repeat the last - you can open the file directly using FileReader , but this implicitly uses the standard platform encoding for the file. Using FileInputStream → InputStreamReader, you can explicitly specify the encoding scheme.)
Why not use an OutputStream instead of an OutputStreamWriter to write bytes directly?
Its encoding again. If you want to write text in OUtputStream, you need to encode it according to some encoding scheme; eg.
os.write(str.getBytes("UTF-8"));
Using Writer, you move the encoding to the output pipeline, where it is less intrusive and can usually be performed more efficiently.
1 - or, more strictly, a 16-bit representation of Unicode code points.
source share