What are the differences between Inputstreamreader and Java Scanner

Asked

Viewed 2,516 times

4

I was aware of the data reading by Scanner, know superficially its functioning, however I came across some examples using a class called InputStreamReader for data reading.

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

2 answers

10


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

4

Besides the size of buffer standard of the first being much larger than the second, the InputStreamReader was designed to read streams generally and with a lot of control on how to read although not worry about the content, while the Scanner has more specific function but has better tools to control read content.

Usually the Scanner is used for console reading, although it can be used to read files, it can be useful in unstructured files as in the case of XML. This preference is given because he reads tokens of data, it understands what is being read - making 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.

  • 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?

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.