【问题标题】:Java BufferedReader IO Error. Stream CloseJava BufferedReader IO 错误。流关闭
【发布时间】:2019-09-01 03:25:54
【问题描述】:

我做了一个 tictactoe 项目,让人类可以与计算机对战。 该程序将读取以下内容列表:

xxxd
----
----
----

从文本文件中匹配这些与主棋盘以获得计算机的移动步骤。此移动步骤在上面的列表中标记为“D”。

我编写了一段简单的 java 代码来获取键盘输入,但它不断打印出 IO 错误。

我在调试模式下运行。下面是堆栈跟踪

    package net.luan.controller;

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

    import net.luan.bean.MainBoard;
    import net.luan.bean.Position;
    import net.luan.util.ProjectProperties;
    import net.luan.util.StepValidation;

    public class HumanController {

    private char humanName = ProjectProperties.HUMAN_NAME;

    public HumanController() {

    }

    public void makeMoveStep(MainBoard mainBoard) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StepValidation validator = new StepValidation();
        try {
            Position moveStep = this.inputMoveStep(br);
            boolean true_flag = validator.isStepValid(moveStep, mainBoard);
            while (!true_flag) {
                this.inputMoveStep(br);
            }
            mainBoard.setPosition(moveStep);
            System.out.println("Human: (" + moveStep.getX() + ", " + moveStep.getY() + ")");
            br.close();
        } catch (NumberFormatException e) {
            System.out.println("Hay nhap vao so nguyen.");
        } catch (IOException e) {
            System.out.println("IO Error.");
            e.printStackTrace();
        }
    }

    private Position inputMoveStep(BufferedReader br) throws IOException {
        Position movePosition = new Position();
        String str;
        System.out.print("Input X: ");

/*It stops right here and turns out an IO Erro*/

        str = br.readLine();        int x = Integer.parseInt(str); 
        System.out.print("Input Y: ");
        str = br.readLine();
        int y = Integer.parseInt(str);
        movePosition.setName(humanName);
        movePosition.setX(x);
        movePosition.setY(y);
        return movePosition;
    }

    }


    /*And it is called by the MainController.*/

    /**
     * MainController
     */
    package net.luan.controller;

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

    import net.luan.bean.MainBoard;
    import net.luan.logic.WinCheckingLogic;


    public class MainController {
    public static void main(String[] args) {

        MainBoard mainBoard = new MainBoard();

        ComputerController computerController = new ComputerController();
        HumanController humanController = new HumanController();

        WinCheckingLogic winCheck = new WinCheckingLogic();

        boolean isContinue = true;
        boolean isWinMove = false;

        char inputChar;

        mainBoard.boardInitialize();
        mainBoard.drawBoard();

        try {
            System.out.println("who play first? Human? Y: ");
            inputChar = getConfirmChar();
            while (isContinue == true) {
                if (inputChar == 'Y' || inputChar == 'y') {
                    while (isWinMove == false) {
                        humanController.makeMoveStep(mainBoard);
                        mainBoard.drawBoard();
                        isWinMove = winCheck.isWinMoveStep(mainBoard);
                        if (isWinMove == true) {
                            System.out.println("You Won.");
                            isContinue = false;
                        }

                        computerController.makeMoveStep(mainBoard);
                        mainBoard.drawBoard();
                        isWinMove = winCheck.isWinMoveStep(mainBoard);
                        if (isWinMove) {
                            System.out.println("Computer Won.");
                            isContinue = false;
                        }
                    }
                } else {
                    while (isWinMove == false) {
                        computerController.makeMoveStep(mainBoard);
                        mainBoard.drawBoard();
                        isWinMove = winCheck.isWinMoveStep(mainBoard);
                        if (isWinMove) {
                            System.out.println("Computer Won.");
                            isContinue = false;
                        }

                        humanController.makeMoveStep(mainBoard);
                        mainBoard.drawBoard();
                        isWinMove = winCheck.isWinMoveStep(mainBoard);
                        if (isWinMove == true) {
                            System.out.println("You Won.");
                            isContinue = false;
                        }
                    }
                }

                System.out.println("Do you want to continue? Y: ");
                inputChar = getConfirmChar();
                if (inputChar == 'Y' || inputChar == 'y') {
                    isContinue = true;
                } else {
                    isContinue = false;
                }
            }
        } catch (IOException e) {
            System.out.println("OI Error");
        }
    }

    public static char getConfirmChar() throws IOException {
        char ch = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        boolean again = true;
        while (again) {
            str = br.readLine();
            if (str.length() == 1) {
                again = false;
                ch = str.charAt(0);
            } else {
                again = true;
                System.out.println("Khong hop le, nhap lai!");
            }
        }
        br.close();
        return ch;
    }
    }

StackTrace

Input X: IO Error.
    java.io.IOException: Stream closed
    at java.io.BufferedInputStream.getBufIfOpen(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at net.luan.controller.HumanController.inputMoveStep(HumanController.java:52)
    at net.luan.controller.HumanController.makeMoveStep(HumanController.java:32)
    at net.luan.controller.MainController.main(MainController.java:67)

我认为这是一个简单的错误,但我无法弄清楚。请给我一些提示。谢谢。

【问题讨论】:

    标签: java io bufferedreader


    【解决方案1】:

    您通过管道输入了文本文件?可能是多个 BufferedReader 实例导致了问题。他们首先 getConfirmChar() 将文件内容消耗到缓冲区中,但您只读取了 1 行。之后在 inputMoveStep() 中,您已经在文件末尾。请尝试删除所有 BufferedReader 创建并将读取替换为str = System.console().readLine();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 2014-05-18
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多