我有一个Scanner
链接到System.in
。现在,在使用了Scanner
之后,我应该将其关闭,因为将其保持打开状态是不好的编码习惯。但是,如果我关闭Scanner
,我也将关闭System.in
!谁能告诉我如何在不关闭Scanner
的情况下关闭System.in
(如果可以的话)。
最佳答案
一种选择是将System.in
流包装在CloseShieldInputStream
中,以防止其被关闭。然后,您的读者将使用CloseShieldInputStream
而不是原始的System.in
流。
这是该类的API:
http://commons.apache.org/io/apidocs/org/apache/commons/io/input/CloseShieldInputStream.html
关于java - 关闭链接到System.in的扫描仪,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14142853/