我有一个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/

10-09 06:22
查看更多