Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader vs Console vs Scanner

Tags:

java

java-io

Hi I'm new to Java and I would like to know what is the best choice to read a user Input in the console, as far as I know there are 3 ways to do it:

  1. Console console = System.console();
  2. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  3. Scanner reader = new Scanner(System.in);

Which one should I choose? Why that one and not the other ones?

like image 681
Victor Castillo Torres Avatar asked Jul 14 '13 06:07

Victor Castillo Torres


People also ask

Is it better to use Scanner or BufferedReader?

BufferedReader is a bit faster as compared to scanner because scanner does parsing of input data and BufferedReader simply reads sequence of characters.

What can I use instead of BufferedReader?

BufferedReader vs Scanner in JavaA scanner is a much more powerful utility than BufferedReader. It can parse the user input and read an int, short, byte, float, long, and double apart from String. On the other hand, BufferedReader can only read String in Java. 2.

How would you compare Scanner and Console classes in Java?

So Console class gives you a Java platform independent runtime class to access things like password input, etc. Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and does not do any special parsing.

What is difference between BufferedReader and InputStreamReader?

BufferedReader reads a couple of characters from the Input Stream and stores them in a buffer. InputStreamReader reads only one character from the input stream and the remaining characters still remain in the streams hence There is no buffer in this case.


2 Answers

BufferedReader

  • Since Java 1.1
  • Throws checked exceptions
  • Can read single chars, char arrays, and lines
  • Fast

Scanner

  • Since Java 1.5
  • Throws unchecked exceptions
  • Can read lines, numbers, whitespace-delimited tokens, regex-delimited tokens
  • Difficult to read single characters

Console

  • Since Java 1.6
  • Throws unchecked exceptions
  • Not always available (e.g. if input/output is redirected, and in Eclipse)
  • Can read lines
  • Underlying reader can read single chars and char arrays (but stops at line bounds)
  • Can read passwords (i.e. read without displaying the characters)

Recommendation: Scanner

The methods for reading numbers are very useful (though beware when using nextInt() etc. followed by nextLine()). The exceptions are unchecked, so you do not have to write boilerplate try/catch blocks.

like image 87
tom Avatar answered Sep 20 '22 00:09

tom


beside these you can also use datainputstream etc.

Now BufferedReader Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

Where Scanner is a simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and does not do any special parsing.

also check the below link it will surely help you.......

http://www.javawebtips.com/50474/

like image 44
roanjain Avatar answered Sep 24 '22 00:09

roanjain