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

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

问题描述

我正在学习Java,并在一些项目上开展工作。我遇到的一个问题是,当我使用 Scanner 对象时,Eclipse会警告我:


资源泄漏:'scan'永远不会关闭。


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



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



以下是导致错误原因的示例:

  import java.util.Scanner的; 
public class test2 {
public static void main(String [] args){
扫描仪扫描=新的扫描仪(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(){
扫描仪扫描仪=新的扫描仪(System.in);
int blah = scanner.nextInt();
System.out.println(blah);
scanner.close();
}
}

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



我尝试将扫描器对象的创建移动到do循环中,以便每次都创建一个新对象,但错误仍然发生



不知道为什么会发生这种情况,或者我如何确保我的所有I / O对象在没有遇到问题的情况下关闭。



我发现一个帖子提到,当$ code> System.in 关闭时,我无法重新打开。如果是这样,我只需要确保在System.in的扫描仪对象在程序的最后关闭,并且@suppress其他类中的所有其他扫描仪警告?或者是否仍然将所有扫描仪对象打开(坏)?

解决方案

是的,当您关闭扫描仪时,您将关闭底层流(在这种情况下为System.in)。为了避免这种情况,可以创建扫描仪的全局变量,可以由所有类使用,也可以有一个中心点来关闭扫描仪(在程序退出之前就是理想的)


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.

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

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

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

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.

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

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)?

解决方案

Yes, when you close a scanner you will be closing the underlying stream (in this case System.in). To avoid this, either create a global variable of scanner which can be used by all classes or have a central point for shutting down the scanner (just before the program exits would be ideal)

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

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