BufferedReader vs Console vs Scanner

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)