The class InputStream
read data < (eg, FileInputStream
to read files, ByteArrayInputStream
to read from a byte array, socket.getInputStram()
to read from a socket, System.in
to read from the console , etc). The Reader
class reads 16-bit Unicode characters, including surrogate pairs ).
The function of InputStreamReader
is to serve as an adapter ( Adapter ) between the two classes - reads bytes on one side, converts characters from the other , by using a character encoding ( encoding ). That is, it is a Reader
that receives InputStream
in the build, consuming data from that stream and displaying it as characters for the consumer.
While a Reader
is a "lower-level" class (its function is to read characters, not more, not less) o Scanner
is a more specialized class, intended to interpret underlying text in several ways (eg a sequence of digits can be a number, true
can be a boolean), including using regular expressions. Its function is not to treat streams , including it delegates that responsibility to a specialized class - such as InputStream
or Reader
.
In the end, you use the most appropriate class (es) for your purpose. You can even use the 3 at the same time:
InputStream is = new FileInputStream("arquivo.txt"); // Lê bytes do arquivo
Reader r = new InputStreamReader(is, "UTF-8"); // Transforma em caracteres
Scanner s = new Scanner(r); // Prepara-se para interpretar esses caracteres de modo semântico