3

Possible Duplicate:
Scanner vs. BufferedReader

Is there any situation in which it's apropriate to use java.util.Scanner in order to read input of some sort? In my small test I've found it to be incredibly slow compared to java.util.Bufferedreader or implementing your own reader from java.util.InputStreamReader.

So is there any reason as to why I would want to use a Scanner?

Community
  • 1
  • 1
Rovanion
  • 4,382
  • 3
  • 29
  • 49

5 Answers5

6

From the docs:

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

That won´t do a BufferedReader.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • Just to add further Scanner is not thread safe but is more powerful then BufferReader. It has more cheese built into in. – amod Sep 12 '12 at 12:40
4

The Scanner class main purpose is for parsing text for primitive types and strings using regular expressions. You can provide several resource types to read from.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
3

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 less than a couple of micro-seconds each.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Scanner- Many of its methods is the idea of parsing the input stream into tokens. BufferedReader doesn't rely on breaking up its input into tokens. It allows you to read character by character if you want. It can read an entire line and let you do what you want. A scanner into it; it can do all that a BufferedReader can do and at the same level of efficiency as well. However, in addition a Scanner can parse the underlying stream for primitive types and strings using regular expressions. It can also tokenize the underlying stream with the delimiter of your choice. It can also do forward scanning of the underlying stream disregarding the delimiter!

EDIT Just Forget to mention... "A scanner however is not thread safe, it has to be externally synchronized."

amod
  • 4,190
  • 10
  • 49
  • 75
0

Here is the difference between java.lang.util.scanner and java.lang.util buffered reader. While both are useful in taking input from the user in a java class, there is a significant difference that one needs to understand.

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

While buffered reader is a buffered input system. It takes chunks (stream) of data and then feeds into the data type that the user wants to store it in. So until you flush or the buffer is full, the reader stream wont give you data..

athspk
  • 6,722
  • 7
  • 37
  • 51