BufferedReader与Console vs Scanner [英] BufferedReader vs Console vs Scanner
本文介绍了BufferedReader与Console vs Scanner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我是 Java
的新手,我想知道在控制台中读取用户输入的最佳选择是什么,据我所知3种方法:
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:
-
控制台控制台= System.console();
-
BufferedReader输入=新的BufferedReader(新的InputStreamReader(System.in));
-
扫描仪阅读器=新扫描仪(System.in);
Console console = System.console();
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
Scanner reader = new Scanner(System.in);
哪一个应该我选择?为什么那个而不是其他?
Which one should I choose? Why that one and not the other ones?
推荐答案
BufferedReader
- 自Java 1.1以来
- 抛出已检查的异常
- 可以读取字符,字符数组和行
- 快速
- Since Java 1.1
- Throws checked exceptions
- Can read chars, char arrays, and lines
- Fast
- 自Java 1.5以来
- 不抛出已检查的异常
- 可以读取行,空格分隔的标记,正则表达式分隔的标记和数字
- 很难阅读单个字符
- Since Java 1.5
- Doesn't throw checked exceptions
- Can read lines, whitespace-delimited tokens, regex-delimited tokens, and numbers
- Difficult to read single characters
- Since Java 1.6
- Doesn't throw checked exceptions
- Can read lines
- Underlying reader can read chars and char arrays (stops at line bounds)
- Not always available (e.g. Eclipse)
- Can read passwords (i.e. read without displaying the characters)
BufferedReader
方法阅读数字非常有用。例外情况未经检查,因此您不必编写样板try / catch块。
The methods for reading numbers are very useful. The exceptions are unchecked, so you do not have to write boilerplate try/catch blocks.
这篇关于BufferedReader与Console vs Scanner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文