Scanner的一个构造方法,因为是接受键盘输入,System.in,所以是这个方法。- public Scanner(InputStream source) {
- this(new InputStreamReader(source), WHITESPACE_PATTERN);
- }
复制代码 Scanner.close内部代码。- public void close() {
- if (closed)
- return;
- if (source instanceof Closeable) {
- try {
- ((Closeable)source).close();
- } catch (IOException ioe) {
- lastException = ioe;
- }
- }
- sourceClosed = true;
- source = null;
- closed = true;
- }
复制代码 输入流需要关闭资源这下就明白了吧。
还有就是熟悉了输入输出流。就可以使用
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
br.close();
|