The class InputStream
does the data reading binaries, does not matter the source (ex.: FileInputStream
to read files, ByteArrayInputStream
to read from an array of bytes, socket.getInputStram()
to read from a socket, System.in
to read from the console, etc). Already the class Reader
does the data reading textual, i.e., strings composed of Unicode characters (code Units 16-bit, including surrogate pairs).
The function of InputStreamReader
is serve as an adapter (Adapter) between the two classes - reads bytes on one side, converts into characters on the other, through the use of a character encoding (encoding). I mean, he’s a Reader
who receives a InputStream
in the construction, consuming data of this stream and presenting them as characters for the consumer.
As a Reader
is a "lower level" class (its function is to read characters, no more, no less) the Scanner
is a more specialized class, aimed at interpret an underlying text in several forms (e.g.: a sequence of digits may be a number, true
can be a Boolean), even using regular expressions. Its function is not to treat streams, including her delegate this responsibility to a specialized class - such as the InputStream
or the Reader
.
Ultimately, you use the most appropriate class(s) (s) for your purpose. You can even use all 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
In essence, the applicability of Scanner would be to read data on console, while Inputstreamreader would be for reading and handling external files? But nothing would stop me from reading a given console with the right Inputstreamreader?
– Dan Lucio Prada
Not necessarily, it can be used for other data sources, including it can receive streams several, I only mentioned the console because it is widely used for this since data on the console needs to be interpreted before it is considered manipulated by the application. Both can be used for the same thing but operate in different ways so one is more used than the other in certain situations. If you use the
InputStreamReader
to read what comes from the console and does not come what you expect, has little to do.– Maniero