Close a Scanner linked to System.in

后端 未结 5 1383
抹茶落季
抹茶落季 2020-11-22 02:57

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.

5条回答
  •  心在旅途
    2020-11-22 03:05

    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.

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

提交回复
热议问题