关闭链接到 System.in 的扫描仪 [英] Close a Scanner linked to System.in

查看:25
本文介绍了关闭链接到 System.in 的扫描仪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Scanner linked 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 Scanner without closing System.in (if there is any way).

推荐答案

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

One option is to wrap your System.in stream in a CloseShieldInputStream that prevents it from being closed. Your reader would then use the CloseShieldInputStream rather than the raw System.in stream.

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

这篇关于关闭链接到 System.in 的扫描仪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆