简体   繁体   English

Java BufferedReader IO错误。 流关闭

[英]Java BufferedReader IO Error. Stream Close

I made a tictactoe project that allow human to play against computer. 我做了一个tictactoeoe项目,该项目可以使人与计算机对抗。 The program will read the list of things like: 该程序将读取诸如以下内容的列表:

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

from the text file and match these with the main chess board to get the move step for the computer. 从文本文件中将其与主棋盘匹配,以获取计算机的移动步骤。 This move step is marked 'D' in the list above. 在上面的列表中,此移动步骤标记为“ D”。

I wrote a simple piece of java code to get input from keyboard, but it keeps printing out IO Error. 我编写了一段简单的Java代码来从键盘获取输入,但是它一直在打印出IO错误。

I run at debug mode. 我在调试模式下运行。 Below is the stacktrace 以下是堆栈跟踪

    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)

I think this a simple mistake, but i can't figure out. 我认为这是一个简单的错误,但我不知道。 Give me some hint please. 请给我一些提示。 Thank you. 谢谢。

You piped the text file in? 您通过管道传输了文本文件? Maybe the multiple BufferedReader instances are causing the problem. 可能是多个BufferedReader实例引起了问题。 They first getConfirmChar() is consuming the file content up into the buffer, but you read just 1 line. 他们首先是getConfirmChar()正在将文件内容消耗到缓冲区中,但是您只读取了1行。 Afterwards in inputMoveStep() you are already at end of file. 之后,在inputMoveStep()中,您已经在文件末尾。 Please try to remove all of the BufferedReader creations and replace the reads by str = System.console().readLine(); 请尝试删除所有BufferedReader创建的内容,并将其替换为str = System.console().readLine();

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

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