java 关闭链接到 System.in 的扫描仪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14142853/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:13:48  来源:igfitidea点击:

Close a Scanner linked to System.in

javajava.util.scanner

提问by JavaNewbie_M107

I have a Scannerlinked to System.in. Now, after using the Scanner, I should close it, as it is bad coding practice to leave it open. But, if I close the Scanner, I will also be closing System.in! Can anyone tell me how I can close the Scannerwithout closing System.in(if there is any way).

我有一个Scanner链接到System.in. 现在,在使用 之后Scanner,我应该关闭它,因为让它保持打开状态是不好的编码习惯。但是,如果我关闭Scanner,我也将关闭System.in!谁能告诉我如何关闭Scanner而不关闭System.in(如果有任何方法)。

采纳答案by paul jerman

One option is to wrap your System.instream in a CloseShieldInputStreamthat prevents it from being closed. Your reader would then use the CloseShieldInputStreamrather than the raw System.instream.

一种选择是将您的System.in流包装在一个 中CloseShieldInputStream,以防止它被关闭。然后,您的读者将使用CloseShieldInputStream而非原始System.in流。

Here is the API for the class: http://commons.apache.org/io/apidocs/org/apache/commons/io/input/CloseShieldInputStream.html

这是该类的 API:http: //commons.apache.org/io/apidocs/org/apache/commons/io/input/CloseShieldInputStream.html

回答by Peter Lawrey

The simplest thing is to not close Scanner if you don't want to close the underlying stream.

如果您不想关闭底层流,最简单的事情就是不要关闭 Scanner。

Ideally you should create just one Scanner which you use for the life of the program. In any case, it appears you don't have a good reason to close it.

理想情况下,您应该只创建一个在程序生命周期中使用的扫描仪。无论如何,您似乎没有充分的理由关闭它。

回答by mist

Instead of adding shield classes and stuff like that, just put a nice comment and a

而不是添加屏蔽类和类似的东西,只需添加一个很好的评论和

        @SuppressWarnings("resource")

That's good enough. And I don't seem to see a lot of drawbacks to this approach. Don't forget the comment.

这已经足够了。而且我似乎没有看到这种方法有很多缺点。不要忘记评论。

回答by Blrp

I have vague memories of strange, undiagnosable problems long ago with using the same Scannerof System.intwice, so this is what I use (even though you should probably just use one scanner for the duration of the program):

我对很久以前使用相同ScannerSystem.in两次的奇怪的,无法诊断的问题有模糊的记忆,所以这就是我使用的(即使你应该在程序期间只使用一个扫描仪):

static String input() {
    try {
        return new Scanner(System.in).nextLine();
    } catch (NoSuchElementException e) {
        throw e;
    }
}

For some reason this works without warnings, whereas if I don't do the catch-throw, Eclipse will complain Resource leak: '<unassigned Closeable value>' is never closed.

出于某种原因,这可以在没有警告的情况下工作,而如果我不进行接球投掷,Eclipse 会抱怨Resource leak: '<unassigned Closeable value>' is never closed.

回答by Ted McLeod

According to the API for InputSteam"The close method of InputStream does nothing.", so since System.in is an instance of InputStream, you don't need to worry about close() being called on it.

根据InputSteamAPI“InputStream 的 close 方法什么都不做。”,因此由于 System.in 是 InputStream 的实例,因此您无需担心 close() 会被调用。