简体   繁体   English

如果我不关闭扫描仪怎么办?

[英]What if I do not close the Scanner?

I have a question about the Java's Scanner. 我对Java的扫描仪有疑问。 I am writing a tic-tac-toe program, and this function gets called every time a player should make its move. 我正在编写一个井字游戏程序,每次玩家进行移动时都会调用此函数。

public void mossa(){
    int riga, colonna;
    int errore;
    Scanner input = new Scanner(System.in);
    if(player=='O')
        System.out.println("GIOCATORE O\n");
    else
        System.out.println("GIOCATORE X\n");

    do{
        errore = 0;
        System.out.println("Riga: ");
        riga = input.nextInt();

        System.out.println("Colonna: ");
        colonna = input.nextInt();

        if(board[riga][colonna]!=' '){
            errore=1;
            System.out.println("Hai scelto una casella gia occupata!\n");
        }
        if(riga>3 || colonna>3 || riga<0 ||colonna<0){
            errore=1;
            System.out.println("Hai scelto una casella che non esiste!\n");
        }

    }while(errore==1);

    //----->input.close();<------


    if(player == 'X')
        board[riga][colonna] = 'X';
    else
        board[riga][colonna] = 'O';

    togglePlayer();
}

At first I thought that it was better to close the Scanner after having captured the player's move, and then opening it again when the function gets called another time (see the input.close() commented). 起初我认为最好在捕获了玩家的移动后关闭扫描仪,然后在另一次调用该函数时再次打开它(参见input.close()注释)。 The problem is that if I close the scanner, I have a runtime error saying that the scanner is empty, and putting the nextInt() in an if driven by hasNextInt() doesn't help. 问题是如果我关闭扫描程序,我有一个运行时错误说扫描程序是空的,并将nextInt()放在一个由hasNextInt()驱动的情况下没有帮助。 So, is it wrong leaving the input Scanner not closed (I have only the warning of resource leak)? 那么,输入扫描仪没有关闭是不对的(我只有资源泄漏的警告)?

Thank you in advance. 先感谢您。

You don't have to close the Scanner. 您不必关闭扫描仪。 It's a wrapper around standard input, the JVM will take care of closing stdin for you when the program is terminated. 它是标准输入的包装器,JVM将在程序终止时为您关闭stdin。

Static code analysis only detects that you have something closeable that you didn't close, so it complains. 静态代码分析只检测到你有一些你没有关闭的东西,所以它会抱怨。 It's not paying attention to the thing that the Scanner is wrapping. 它没有注意扫描仪包装的东西。

Things like files, network sockets, and jdbc objects need closing. 文件,网络套接字和jdbc对象之类的东西需要关闭。 stdin, stdout, and stderr get taken care of for you by the jvm, byteArray streams have nothing that needs closing. stdin,stdout和stderr由jvm为你处理,byteArray流没有什么需要关闭。 But Java uses a decorator pattern to wrap streams and the static analysis tool doesn't differentiate. 但Java使用装饰器模式来包装流,静态分析工具无法区分。

I've never heard of anything saying that you should close a scanner while not expecting input, only that you should close them when your program is finished (and in theory, Java will close it for you anyway). 我从来没有听说过任何关于你应该在不期待输入的情况下关闭扫描仪的事情,只是你应该在你的程序完成时关闭它们(理论上,Java无论如何都会关闭它)。 I very much doubt that having a single scanner open will cause any kind of resource leakage. 我非常怀疑打开一个扫描仪会导致任何类型的资源泄漏。

Only close the scanner when you are finished using it. 只有在使用完毕后才能关闭扫描仪。 If you do not explicitly close the scanner eventually the garbage collector will destroy it and remove it from memory after your program is terminated. 如果您没有显式关闭扫描程序,垃圾收集器将终止它并在程序终止后将其从内存中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM