這是本文件的舊版!
Close the System.in of the Scanner
Problem
如果有寫到CLI的tool,使用Scanner物件是很平常的事情,因此應會遇到是否該close Scanner物件的問題。假如有兩個類別都會使用到使用System.in的Scanner,先close的就會讓後面的發生問題。
Solutions
閱讀幾篇文章後,作法有以下:
- 不理它,反正不會造成memory leak。
- 使用Decorator方式,將System.in包裝在不會真的close的InputStream中。
- 使用Global Scanner。