Scanner vs. BufferedReader

前端 未结 12 1755
死守一世寂寞
死守一世寂寞 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

    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

提交回复
热议问题