what are the benefits of BufferedReader over Scanner

折月煮酒 提交于 2019-12-01 11:25:32
korefn

It's an issue of how you intend to use the stream. A buffered reader exists for simple and threaded applications. This is due to scanner's lack of thread safety.

I think you'll get more on this from this question Scanner vs. BufferedReader

Probably, when this code was written, the Scanner class didn't exist (in fact java 1.4 did not have the Scanner class), or maybe who written this code simply preferred using BufferedReader's readLine method instead of using Scanner.nextLine() method, i can't see other explainations about your question

BufferedReader is simpler (which makes it slightly more efficient) but it is also a clearer choice showing you intend to do is to use the functionality BufferdReader provides. i.e readLine() is the main one.

In short, if you have BufferedReader you know it is just going to read lines. If you use Scanner it implies you may or many not be reading something more complicated.

BTW:

  Integer.parseInt(br.readLine())

and

  scanner.nextInt();

are not the same although the distinction is usually lost on noob developers. For this reason I prefer the first example. The difference is how new lines are handled.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!