Reading from Console: JAVA Scanner vs BufferedReader

When read an input from console, there are two options exists to achive that. First using Scanner, last 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?

  1. Scanner treated given input as token. BufferedReader as stream line/String
  2. Scanner tokenized given input using regex. Using BufferedReader must write extra code
  3. BufferedReader faster than Scanner *point no. 2
  4. 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.

Want to know deeper about Scanner or BufferedReader?

Advertisements

About wirasihombing

At present, I was a man who has graduated from my college in January 2011. Still looking for a job, trying to go abroad. In my body, flowing the Batak's blood.

Posted on August 27, 2012, in Tutorial and tagged , , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: