Scanner vs. BufferedReader

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

    1. BufferedReader has significantly larger buffer memory than Scanner. Use BufferedReader if you want to get long strings from a stream, and use Scanner if you want to parse specific type of token from a stream.

    2. Scanner can use tokenize using custom delimiter and parse the stream into primitive types of data, while BufferedReader can only read and store String.

    3. BufferedReader is synchronous while Scanner is not. Use BufferedReader if you're working with multiple threads.

    4. Scanner hides IOException while BufferedReader throws it immediately.

提交回复
热议问题