BufferedReader 在 Java 中的 For 循环开始时关闭

BufferedReader Closes at the Beginning of a For-Loop in Java

我正在使用 BufferedReader 在 for 循环中接收多个输入;但是,当第一个输入在第一次输入时,它会抛出错误

java.io.Exception: Stream closed

尽管我直到 for 循环结束后才关闭它。代码如下:

        BufferedReader menu = new BufferedReader(new InputStreamReader(System.in));
        for (int i = 0; players.length > i; i++) {
            System.out.println("Choose the " + i + " player");
            System.out.println("1. New Player");
            System.out.println("2. Old Player");
            int choice2 = Integer.parseInt(menu.readLine());
            System.out.println("Which team are you on?");
            int playersTeam = Integer.parseInt(menu.readLine());
            Person player = new Person();
            if (choice2 == 1) {
                player.squadFilling();
                player.playerNaming();
            } else if (choice2 == 2) {
                break;
            }
        }
        menu.close();

导致崩溃的原因是什么,我该如何解决?感谢您的宝贵时间!

堆栈跟踪:

java.io.IOException: Stream closed
    at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:168)
    at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:297)
    at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
    at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
    at java.base/java.io.InputStreamReader.read(InputStreamReader.java:181)
    at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
    at Main.main(Main.java:409)

在 Java 8 中,它没有抛出任何异常(尝试相同的代码)。它工作正常。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

 public static void main(String[] args) throws NumberFormatException, IOException {

    BufferedReader menu = new BufferedReader(new InputStreamReader(System.in));
    for (int i = 0; 2 > i; i++) {
                    System.out.println("Choose the " + i + " player");
                    System.out.println("1. New Player");
                    System.out.println("2. Old Player");
                    int choice2 = Integer.parseInt(menu.readLine());
                    System.out.println("Which team are you on?");
                    int playersTeam = Integer.parseInt(menu.readLine());
                    if (choice2 == 1) {
                        continue;
                    } else if (choice2 == 2) {
                        break;
                    }
                    
    }
    menu.close();
 }
}

我认为您应该尝试将 menu.close(); 移动到最后调用的不同方法。以及将 BufferedReader menu 移到方法之外的更高可见度,并在方法中使用 menu = new BufferedReader(new InputStreamReader(System.in));

BufferedReader menu;
void method(){
    menu = new BufferedReader(new InputStreamReader(System.in));
    for (int i = 0; players.length > i; i++) {
        System.out.println("Choose the " + i + " player");
        System.out.println("1. New Player");
        System.out.println("2. Old Player");
        int choice2 = Integer.parseInt(menu.readLine());
        System.out.println("Which team are you on?");
        int playersTeam = Integer.parseInt(menu.readLine());
        Person player = new Person();
        if (choice2 == 1) {
            player.squadFilling();
            player.playerNaming();
        } else if (choice2 == 2) {
            break;
        }
    }
    cleanup();
}

这允许我们创建一个方法 cleanup()。

private void cleanup(){
    menu.close();
}

我认为这可能有帮助的原因是我相信 menu.close() 是在作者仍在从最后一个循环中完成时被调用的。我认为将 cleanup() 添加为单独的方法会起作用。

我错过了代码中其他地方的一些 .close() 命令。