Scanner vs. BufferedReader

前端 未结 12 1716
死守一世寂寞
死守一世寂寞 2020-11-22 02:17

As far I know, the two most common methods of reading character-based data from a file in Java is using Scanner or BufferedReader. I also know that

相关标签:
12条回答
  • 2020-11-22 02:46

    In currently latest JDK6 release/build (b27), the Scanner has a smaller buffer (1024 chars) as opposed to the BufferedReader (8192 chars), but it's more than sufficient.

    As to the choice, use the Scanner if you want to parse the file, use the BufferedReader if you want to read the file line by line. Also see the introductory text of their aforelinked API documentations.

    • Parsing = interpreting the given input as tokens (parts). It's able to give back you specific parts directly as int, string, decimal, etc. See also all those nextXxx() methods in Scanner class.
    • Reading = dumb streaming. It keeps giving back you all characters, which you in turn have to manually inspect if you'd like to match or compose something useful. But if you don't need to do that anyway, then reading is sufficient.
    0 讨论(0)
  • 2020-11-22 02:46

    Following are the differences between BufferedReader and Scanner

    1. BufferedReader only read data but scanner also parse data.
    2. you can only read String using BufferedReader, but you can read int, long or float using Scanner.
    3. BufferedReader is older from Scanner,it exists from jdk 1.1 while Scanner was added on JDK 5 release.
    4. The Buffer size of BufferedReader is large(8KB) as compared to 1KB of Scanner.
    5. BufferedReader is more suitable for reading file with long String while Scanner is more suitable for reading small user input from command prompt.
    6. BufferedReader is synchronized but Scanner is not, which means you cannot share Scanner among multiple threads.
    7. BufferedReader is faster than Scanner because it doesn't spent time on parsing
    8. BufferedReader is a bit faster as compared to Scanner
    9. BufferedReader is from java.io package and Scanner is from java.util package on basis of the points we can select our choice.

    Thanks

    0 讨论(0)
  • 2020-11-22 02:47

    There are different ways of taking input in java like:

    1) BufferedReader 2) Scanner 3) Command Line Arguments

    BufferedReader Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

    Where Scanner is a simple text scanner which can parse primitive types and strings using regular expressions.

    if you are writing a simple log reader Buffered reader is adequate. if you are writing an XML parser Scanner is the more natural choice.

    For more information please refer:

    http://java.meritcampus.com/t/240/Bufferedreader?tc=mm69

    0 讨论(0)
  • 2020-11-22 02:53

    I suggest to use BufferedReader for reading text. Scanner hides IOException while BufferedReader throws it immediately.

    0 讨论(0)
  • 2020-11-22 02:53

    Difference between BufferedReader and Scanner are following:

    1. BufferedReader is synchronized but Scanner is not synchronized.
    2. BufferedReader is thread safe but Scanner is not thread safe.
    3. BufferedReader has larger buffer memory but Scanner has smaller buffer memory.
    4. BufferedReader is faster but Scanner is slower in execution.
    5. Code to read a line from console:

      BufferedReader:

       InputStreamReader isr=new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(isr);
       String st= br.readLine();
      

      Scanner:

      Scanner sc= new Scanner(System.in);
      String st= sc.nextLine();
      
    0 讨论(0)
  • 2020-11-22 02:55

    The answer below is taken from Reading from Console: JAVA Scanner vs BufferedReader

    When read an input from console, there are two options exists to achieve that. First using Scanner, another using BufferedReader. Both of them have different characteristics. It means differences how to use it.

    Scanner treated given input as token. BufferedReader just read line by line given input as string. Scanner it self provide parsing capabilities just like nextInt(), nextFloat().

    But, what is others differences between?

    • Scanner treated given input as token. BufferedReader as stream line/String
    • Scanner tokenized given input using regex. Using BufferedReader must write extra code
    • BufferedReader faster than Scanner *point no. 2
    • Scanner isn’t synchronized, BufferedReader synchronized

    Scanner come with since JDK version 1.5 higher.

    When should use Scanner, or Buffered Reader?

    Look at the main differences between both of them, one using tokenized, others using stream line. When you need parsing capabilities, use Scanner instead. But, i am more comfortable with BufferedReader. When you need to read from a File, use BufferedReader, because it’s use buffer when read a file. Or you can use BufferedReader as input to Scanner.

    0 讨论(0)
提交回复
热议问题