What are the differences between InputStreamReader and Scanner in Java

I was aware of reading data by Scanner, i know superficially how it works, however I came across some examples using a class called InputStreamReader for reading data.

What are the differences between these approaches and if they are used for the same purpose, what is best and why.

Author: Maniero, 2015-03-03

2 answers

A Class InputStream it reads binary data , no matter the source (e.g. 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 it reads textual data , i.e. strings composed of 16-bit Unicode characters ( code units, including surrogate pairs).

The function of InputStreamReader it is to serve as a adapter (adapt) between the two classes - reads bytes on one side, converts to characters on the other, through the use of a character encoding (encoding ). That is, it is a Reader that receives a InputStream in the build, consuming data from that stream and presenting it as characters to the consumer.

While a Reader is a "lowest level" class (its function is to read characters, no more, no less) the Scanner it is a more specialized class, intended for to interpret an underlying text in a variety of ways (e.g., a sequence of digits can be a number, true can be a boolean), including using regular expressions. Its function is not to handle streams, including it delegates this responsibility to a specialized class - such asInputStream or Reader.

Ultimately, you use the most appropriate class (s) for your purpose. Including being able 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
 10
Author: mgibsonbr, 2015-03-03 03:17:54

In Addition to the size of the buffer the pattern for the first one to be much larger than the second, and the InputStreamReader it was designed for a view streams in a general way, and with a lot of control over how you do the reading, though it did not need to worry about the content, while the Scanner it has the most specific, but it has the best tools for the control of the content you read.

Normally Scanner is used for console reading, although it can be used to read files, it can be useful in poorly structured files as in the case of XML. This preference is given because it reads tokens from data, it understands what is being read - doing a parsing - and this facilitates data entry where there are no guarantees of what can be received. Because it is a class to be used in a more specific and simplified way it is not thread-safe .

 4
Author: Maniero, 2020-09-21 14:56:48