Why does java.util.Scanner exist?

Possible duplicate:
Scanner vs. Bufferedreader

Is there any situation in which it is suitable for using java.util.Scanner to read any data? In my little test, I found it to be incredibly slow compared to java.util.Bufferedreader or implementing your own reader from java.util.InputStreamReader.

So is there a reason why I would like to use a scanner?

+4
source share
5 answers

The main objective of the Scanner class is to parse text for primitive types and strings using regular expressions. You can provide several types of reading resources.

+4
source

From docs :

A simple text scanner that can parse primitive types and strings using regular expressions.

It will not be a BufferedReader.

+6
source

While Scanner is relatively slower, it is often more than fast enough, and it is much more powerful than BufferedReader.

 public static void main(String... args) throws IOException { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10000; i++) sb.append("line: ").append(i).append("\n"); String lines = sb.toString(); for (int i = 0; i < 6; i++) { testBufferedReader(lines); testScanner(lines); } } private static void testBufferedReader(String text) throws IOException { int count = 0; BufferedReader br = new BufferedReader(new StringReader(text)); long start = System.nanoTime(); while (br.readLine() != null) count++; long time = System.nanoTime() - start; System.out.printf("BufferedReader.readLine() took an average of %,d ns count=%,d%n", time / count, count); } private static void testScanner(String text) throws IOException { int count = 0; Scanner sc = new Scanner(new StringReader(text)); long start = System.nanoTime(); while (sc.hasNextLine()) { sc.nextLine(); count++; } long time = System.nanoTime() - start; System.out.printf("Scanner nextLine took an average of %,d ns count=%,d%n", time / count, count); } 

finally prints

 BufferedReader.readLine() took an average of 124 ns count=10,000 Scanner nextLine took an average of 1,549 ns count=10,000 

While the relative difference is large, the scanner is smaller than a few microseconds each.

+3
source

Scanner. Many of his methods are the idea of โ€‹โ€‹parsing the input stream into tokens. BufferedReader does not rely on breaking its input into tokens. This allows you to read character by character if you want. It can read a whole line and let you do what you want. Scanner in it; it can do whatever BufferedReader can do, and at the same level of efficiency. However, in addition, the scanner can parse the underlying stream for primitive types and strings using regular expressions. It can also lexify the underlying stream using the delimiter of your choice. It can also perform a direct scan of the underlying stream without regard to the delimiter!

EDIT Just forget to mention ... "However, the scanner is not thread safe, it must be synchronized from the outside."

0
source

Here is the difference between java.lang.util.scanner and java.lang.util buffered reader . Although both of them are useful when entering user data into the java class, there is a significant difference that needs to be understood.

The scanner is a single token input system that uses spaces as the default separator. Although you can change this to other formats using various other methods.

While a buffered reader is a buffer input system. It takes fragments (stream) of data and then passes it to the data type that the user wants to store in it. This way, until you close or the buffer is full, the read stream will not give you data.

0
source

Source: https://habr.com/ru/post/1433797/


All Articles