BufferedReader vs Console vs Scanner
Asked Answered
C

3

55

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?

Centro answered 14/7, 2013 at 6:16 Comment(2)
For any trivial program, just use a Scanner. I've never seen Console used before, and use an InputStream/Reader when dealing with something like files.Bisutun
System.console().readLine("Enter input: ") is a bit simpler to use than scanner and it also allows you to specify an input prompt message. But it might not work properly in some IDEsDemarcation
H
61

BufferedReader

Scanner

Console

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.

Harumscarum answered 14/7, 2013 at 10:49 Comment(5)
also read the answers at #2231869Primate
Be careful with scanner, when you have new Scanner(System.in) and you close scanner, then System.in will be closed, too. If you want to use new Scanner(System.in) somewhere again, then trying to read user input will throw exception. Currently I am trying out JLine, what can read password to and prints given mask.Fonz
also with scanner if you mix nextInt() and nextLine(), one needs to be careful as nextLine() will remove the bnewline char from input but nextInt() will not!Powel
Console is not available if standard output/input is redirected || parsing with Scanner is localizedMuscarine
@Muscarine Thanks, I've updated my answer to mention that Console is not available if input/output is redirected. Scanner's localisation is a good point, but I don't think it's worth mentioning in the answer because BufferedReader and Console don't even support reading numbers so localisation isn't really relevant when comparing to them. (Also thanks to @​MangatRaiModi for the warning about mixing Scanner.nextInt() and nextLine(), I've added it to the answer.)Harumscarum
G
4

Console class is implemented in a platform independent way to handle the console input for different Os. All OS has a console/shell but they are quite different in implementation. 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.

Grand answered 14/7, 2013 at 6:22 Comment(0)
C
4

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/

Commute answered 14/7, 2013 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.