线程异常 "main" java.io.IOException:流关闭:BufferedReader class

Exception in thread "main" java.io.IOException: Stream closed : BufferedReader class

void order_particular() throws IOException{
        int order_choice, x;
        //Scanner sc = new Scanner(System.in);
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Do you want to order something ? ");
        System.out.println("Press 1 to order or 0 to go back to main menu");
        //order_choice = sc.next().charAt(0);
        order_choice = Integer.parseInt(reader.readLine());  // Got Error here
        System.out.println("order choice : " + order_choice);
        if(order_choice == 1){
            this.order();
            System.out.println("Do you want to order more ? ");
            System.out.println("Press 1 to order or 0 to get bill");
            x = Integer.parseInt(reader.readLine());
            System.out.println("x value : " + x);
            if(x == 1){
                this.order_more();
                this.bill_more();
            }
            else
                this.bill();
        }
    }

对于上面的代码,我得到了以下错误

Exception in thread "main" 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:270)
        at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:313)
        at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
        at java.base/java.io.InputStreamReader.read(InputStreamReader.java:177)
        at java.base/java.io.BufferedReader.fill(BufferedReader.java:162)
        at java.base/java.io.BufferedReader.readLine(BufferedReader.java:329)
        at java.base/java.io.BufferedReader.readLine(BufferedReader.java:396)
        at Waiter.order_particular(Waiter.java:404)
        at Waiter.main(Waiter.java:439)

我不知道这里发生了什么。我从未关闭 bufferedReader 对象,但它显示错误。如何解决这个错误?

异常表明此时已经关闭了System.in

在调用 order_particular() 之前,您需要查看发生了什么,因为问题就出在这里。也许更早的方法无意中关闭了它;也许你在 try-with-resource 块中围绕它创建了一个 BufferedReader