【发布时间】:2012-09-13 05:10:16
【问题描述】:
为什么 Eclipse 在下面的代码中给了我温暖的“资源泄漏:'in' is never closed”?
public void readShapeData() {
Scanner in = new Scanner(System.in);
System.out.println("Enter the width of the Rectangle: ");
width = in.nextDouble();
System.out.println("Enter the height of the Rectangle: ");
height = in.nextDouble();
【问题讨论】:
-
致未来的读者:许多答案表明您必须关闭扫描仪才能关闭底层资源。虽然这通常是正确的,但 standard in 是一个例外。关闭它会阻止您再次阅读它,这通常是不希望的。作为一个经验法则:不要关闭你没有打开的东西。
标签: java eclipse input resources memory-leaks