java永远不可能到达的地方_java - 资源泄漏:'in'永远不会关闭

java - 资源泄漏:'in'永远不会关闭

为什么Eclipse会让我变暖“资源泄漏:'in'永远不会关闭”以下代码?

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();

13个解决方案

47 votes

因为您没有关闭扫描仪

in.close();

nogard answered 2019-06-13T12:46:40Z

42 votes

正如其他人所说,你需要在IO类上调用'close'。 我将补充一点,这是一个使用try - finally块而没有捕获的好地方,像这样:

public void readShapeData() throws IOException {

Scanner in = new Scanner(System.in);

try {

System.out.println("Enter the width of the Rectangle: ");

width = in.nextDouble();

System.out.println("Enter the height of the Rectangle: ");

height = in.nextDouble();

} finally {

in.close();

}

}

这可确保您的扫描仪始终处于关闭状态,从而确保正确的资源清理。

同样,在Java 7或更高版本中,您可以使用“try-with-resources”语法:

try (Scanner in = new Scanner(System.in)) {

...

}

Eric Lindauer answered 2019-06-13T12:47:23Z

8 votes

您需要致电in.close(),在finally块中确保它发生。

从Eclipse文档中,这就是为什么它标记这个特定问题(强调我的):

实现接口java.io.Closeable的类(自JDK 1.5起)   和java.lang.AutoCloseable(因为JDK 1.7)被认为是   表示外部资源,应使用方法关闭   close(),当它们不再需要时。

Eclipse Java编译器能够分析代码是否使用此类代码   类型遵守此政策。

...

编译器将使用“资源泄漏标记[违规]:'流'永远不会关闭”。

这里有完整的解释

Will answered 2019-06-13T12:48:29Z

6 votes

它告诉您需要关闭您在Console上实例化的扫描仪Scanner.close().通常应关闭每个读卡器。

请注意,如果您关闭Console,您将无法再次阅读它。 您也可以查看Console课程。

public void readShapeData() {

Console console = System.console();

double width = Double.parseDouble(console.readLine("Enter the width of the Rectangle: "));

double height = Double.parseDouble(console.readLine("Enter the height of the Rectangle: "));

...

}

Alex answered 2019-06-13T12:49:04Z

4 votes

如果您使用的是JDK7或8,则可以将try-catch与资源一起使用。这将自动关闭扫描程序。

try ( Scanner scanner = new Scanner(System.in); )

{

System.out.println("Enter the width of the Rectangle: ");

width = scanner.nextDouble();

System.out.println("Enter the height of the Rectangle: ");

height = scanner.nextDouble();

}

catch(Exception ex)

{

//exception handling...do something (e.g., print the error message)

ex.printStackTrace();

}

agyeya answered 2019-06-13T12:49:30Z

3 votes

完成后应关闭扫描仪:

in.close();

Dan W answered 2019-06-13T12:50:00Z

3 votes

// An InputStream which is typically connected to keyboard input of console programs

Scanner in= new Scanner(System.in);

上面的行将使用参数System.in调用Scanner类的构造函数,并将返回对新构造的对象的引用。

它连接到连接到键盘的输入流,因此现在在运行时您可以使用用户输入来执行所需的操作。

//Write piece of code

要消除内存泄漏 -

in.close();//write at end of code.

Aashi answered 2019-06-13T12:50:44Z

2 votes

通常,处理I / O的类的实例应在完成后关闭。 因此,在代码的最后,您可以添加in.close()。

arshajii answered 2019-06-13T12:51:10Z

1 votes

扫描仪应该关闭。 关闭读者,流......以及这类对象以释放资源和消除内存泄漏是一种很好的做法。 并在finally块中执行此操作以确保即使在处理这些对象时发生异常也将其关闭。

Carlos Cambón answered 2019-06-13T12:51:38Z

1 votes

private static Scanner in;

我通过声明为私有静态Scanner类变量来修复它。 不知道为什么修复它,但这是eclipse推荐我做的。

zachdyer answered 2019-06-13T12:52:03Z

1 votes

添加private static Scanner in;并没有真正解决问题,它只清除警告。使扫描仪静止意味着它永远保持打开状态(或直到课程被卸载,这几乎是“永远”)。编译器不再给你任何警告,因为你告诉他“永远保持打开”。 但这不是你真正想要的,因为一旦你不再需要它们就应该关闭资源。

HTH,曼弗雷德。

user2393042 answered 2019-06-13T12:52:38Z

1 votes

Scanner sc = new Scanner(System.in);

//do stuff with sc

sc.close();//write at end of code.

tejas answered 2019-06-13T12:52:57Z

0 votes

in.close();

scannerObject.close();

它将关闭Scanner并关闭警告。

HRUTU.Z answered 2019-06-13T12:53:23Z

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值