java scanner.close() 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26245468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does scanner.close() do?
提问by Kootling
Say I have the following example code:
假设我有以下示例代码:
Scanner scan1 = new Scanner(System.in); // declaring new Scanner called scan1
int x = scan1.nextInt(); // scan for user input and set it to x
System.out.println(x); // print the value of x
scan1.close(); // closes the scanner (I don't know exactly what this does)
Scanner scan2 = new Scanner(System.in); // declaring new Scanner called scan1
int y = scan2.nextInt(); // scan for user input and set it to y
System.out.println(y); // print the value of y
I read the Oracle documentation on the Scanner
classand came across this:
我阅读了有关该Scanner
课程的Oracle 文档并发现了这一点:
When a Scanner is closed, it will close its input source if the source implements the Closeable interface.
当 Scanner 关闭时,如果源实现 Closeable 接口,它将关闭其输入源。
Does this mean that once a Scanner
(of System.in
) is closed, I will no longer be able to use System.in
throughout the entire Java program? Or does it mean I will no longer be able to use it throughout the class? Or only the method? Or only its scope?
这是否意味着一旦 a Scanner
(of System.in
) 关闭,我将不再能够System.in
在整个 Java 程序中使用?或者这是否意味着我将无法在整个课程中使用它?还是只有方法?还是只有它的范围?
Another question I have is, is a Scanner restricted to the scope it was declared in (similar to the primitive data types)?
我的另一个问题是,扫描器是否仅限于它声明的范围(类似于原始数据类型)?
采纳答案by FatalError
Yes, it does mean that System.in
will be closed. Test case:
是的,这确实意味着System.in
它将被关闭。测试用例:
import java.util.*;
public class CloseScanner {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
scanner.close();
System.in.read();
}
}
This code terminates with
这段代码以
$ java CloseScanner
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
at CloseScanner.main(CloseScanner.java:7)
Once closed, you won't be able to use System.in
for the rest of your program. The fact that close()
is passed through is nice because it means you don't have to maintain a separate reference to the input stream so that you can close it later, for example:
关闭后,您将无法使用System.in
程序的其余部分。close()
传递的事实很好,因为这意味着您不必维护对输入流的单独引用,以便稍后可以关闭它,例如:
scanner = new Scanner(foo.somethingThatMakesAnInputStream());
You can do that and call .close()
on the scanner to close the underlying stream.
您可以这样做并调用.close()
扫描器来关闭底层流。
In most cases you won't want to close System.in
, so you won't want to call .close()
in that case.
在大多数情况下,您不想 close System.in
,因此.close()
在这种情况下您不想调用。