Java——关闭扫描器和资源泄漏 [英] Java -- Closing Scanner and Resource Leak

查看:38
本文介绍了Java——关闭扫描器和资源泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Java 并从事一些有趣的项目.我遇到的一个问题是,当我使用 Scanner 对象时,Eclipse 会警告我:

I'm learning Java and working on some projects for fun. One issue that I have run in to is that when I use a Scanner object Eclipse warns me that:

资源泄漏:扫描"从未关闭.

Resource Leak: 'scan' is never closed.

所以,我在代码的末尾添加了一个 scan.close(); 来处理警告.

So, I added a scan.close(); at the end of my code and that takes care of the warning.

出现问题是因为我在同一个包中有其他类也使用扫描仪对象,并且 Eclipse 告诉我分别关闭这些类中的扫描仪.但是,当我这样做时,它似乎关闭了所有扫描仪对象,并且在运行时出现错误.

The problem comes in because I have other classes in the same package that also use scanner objects and and Eclipse tells me to close scanner in those classes respectively. However, when I do that it seems like it closes ALL of the scanner objects and I get errors during run time.

以下是导致错误的示例:

Here is an example of what causes the error:

import java.util.Scanner;
public class test2 {
    public static void main(String [] args) {
        Scanner scan = new Scanner(System.in);
        int test = 0;
        do {    
            //Do stuff
            test = scan.nextInt();
            System.out.println(test);

            scanTest scanTest = new scanTest();
            scanTest.test();
        } while (test != 0);

        scan.close();       
    }
}

import java.util.Scanner;
public class scanTest { 
    public void test() {
        Scanner scanner = new Scanner(System.in);
        int blah = scanner.nextInt();
        System.out.println(blah);
        scanner.close();
    }
}

scanTest 类中关闭扫描仪并再次进入test2 中的do 循环后,在test = scan.nextInt() 行发生错误;

After scanner is closed in the scanTest class and the do loop in test2 is entered again an error occurs at the line test = scan.nextInt();

我尝试将扫描仪对象的创建移动到 do 循环中,只是为了每次都创建一个新对象,但错误仍然发生.

I tried moving the creation of the scanner object into the do loop just to make a new object every time as well but the error still occurs.

不确定为什么会发生这种情况,或者我如何确保关闭所有 I/O 对象而不会遇到问题.

Not sure why this is happening or how I can make sure all my I/O objects are closed out without running into problems.

我遇到的一个帖子提到,当 System.in 关闭时,我无法重新打开.如果是这种情况,我是否只需要确保在程序的最后关闭带有 System.in 的扫描仪对象并@suppress 其他类中的所有其他扫描仪警告?或者这仍然会使所有这些扫描仪对象保持打开状态(坏)?

One post I came across mentioned that when System.in is closed I cannot be re-opened. If this is the case would I just need to make sure a scanner object with System.in is closed at the very end of the program and @suppress all of the other scanner warnings in other classes? Or would that still leave all those scanner objects open (bad)?

推荐答案

首先,这不是内存泄漏.

First, this is no memory leak.

第二,当您关闭流包装器时,默认实现是关闭它包装的流.这意味着当你第一次关闭你的 Scanner(正如它写的那样)时,是的,你关闭了 System.in.

Second, when you close a stream wrapper, the default implementation is for it to close the stream that it wraps. This means that the first time you close your Scanner (as it is written), yes, you close System.in.

一般来说,如果他们想再次从 System.in 中读取数据,人们会希望避免关闭 System.in.解决此问题的最佳方法取决于您的程序.

In general, one would like to avoid closing System.in if they were meaning to read from System.in again. The best way to go about this depends on your program.

人们可能会将 System.in 中的信息复制到某种缓冲区中,然后扫描缓冲区.人们可能不会关闭扫描仪,而是在其他位置重复使用它.甚至可以取消对 Scanner 的引用以进行垃圾收集,并在 System.in 上创建多个新的 Scanner.

One might copy the information from System.in into a buffer of some sort and then scan the buffer. One might not close the Scanner, reusing it in other locations. One might even de-reference the Scanner for garbage collection and create multiple new Scanners on System.in.

这些解决方案并不都是等价的,有些被认为比其他解决方案要好得多;但是,这一切都取决于调用程序.尝试一些,如果遇到问题,请打开一个新的 StackOverflow 问题,在其中显示代码的相关部分、问题的描述、示例输入和错误的输出(以及所需的输出).

These solutions are not all equivalent, some are considered much better than others; but, it all depends on the calling program. Experiment with a few, and if you run into a problem, open a new StackOverflow question where you show the relevant portions of your code, a description of the problem, the example input, and the wrong output (along with the desired output).

祝你好运.

这篇关于Java——关闭扫描器和资源泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆