Java 如何检查二维文件

Java 如何检查二维文件,java,file,Java,File,所以我正在尝试创建一个连接4游戏。 游戏是(6行)x(7列)的2维 当我使用重置GUI的框架、面板和标签的方法创建新游戏时,我创建了一个空文件,并用0填充它。 范例 当我的游戏正在玩,每个玩家都在移动时,我想去找到玩过的确切位置,并将0更改为玩家棋盘格符号,其中玩家1为“1”,玩家2为“-1”。 例如: 每次经过不同的时间段后,我都想制作一个保存这些更改的按钮,以便能够重新启动游戏并将上一个重新加载到我的GUI中 public static void file_write(int row,int

所以我正在尝试创建一个连接4游戏。 游戏是(6行)x(7列)的2维 当我使用重置GUI的框架、面板和标签的方法创建新游戏时,我创建了一个空文件,并用0填充它。 范例

当我的游戏正在玩,每个玩家都在移动时,我想去找到玩过的确切位置,并将0更改为玩家棋盘格符号,其中玩家1为“1”,玩家2为“-1”。 例如:

每次经过不同的时间段后,我都想制作一个保存这些更改的按钮,以便能够重新启动游戏并将上一个重新加载到我的GUI中

public static void file_write(int row,int col,int letter) throws IOException{
        int counter1 = 0; // a counter that goes +1 until reach the row 
        int counter2=0; // a counter that goes +1 until reach the col
        FileWriter writehandle;
        writehandle = new FileWriter("file.txt");
        BufferedWriter bw = new BufferedWriter(writehandle);
        FileReader readhandle = new FileReader("file.txt");
        BufferedReader br = new BufferedReader(readhandle);

        if(Constants.once){ // boolean from class Constants that fills the file with 0. First example
        for(int i=0; i<7; i++){
            for(int j=0; j < 8; j++){
                Constants.line="0";
                bw.write(Constants.line + " "); // filling the file with 0
                }
        bw.newLine();
        }
    }
        if(letter == Constants.Player1) // player 1 symbol is 1
        for(int i=0; i<row;i++)
            for(int j=0; j<col; j++)

                if (br.readLine() == "0"  && counter2 != col){ //if statement in order to not change 
                    bw.write("0");                             //anything else except the player symbol i 
                    counter2++;                                //am looking for
                }
                else if(br.readLine() == " "   && counter2 != col){
                    bw.write(" ");
                    counter2++;
                }
                else if (br.readLine() == "-1" &&  counter2 != col){
                    bw.write("-1");
                    counter2++;
                }

                if(letter == Constants.Player1)
                bw.write("-1");
                else if (letter == Constants.Player2)
                    bw.write("1");
        bw.close();
        writehandle.close();
        Constants.once =false;
        file_read(0,0);


    }
public static void file\u write(int行、int列、int字母)引发IOException{
int counter1=0;//在到达行之前一直为+1的计数器
int counter2=0;//在到达列之前一直为+1的计数器
文件编写器writehandle;
writehandle=newfilewriter(“file.txt”);
BufferedWriter bw=新的BufferedWriter(writehandle);
FileReader readhandle=newFileReader(“file.txt”);
BufferedReader br=新的BufferedReader(readhandle);
if(Constants.once){//boolean来自于用0填充文件的类常量。第一个示例

对于(inti=0;i哦,上帝——原地重写会变得非常可怕,而且非常容易出错

我意识到这并不是严格地回答你的问题,但是有什么原因不能将整个文件加载到内存中,进行更改,然后再次将其写回磁盘

    static void write(Path outputFile, int[][] values) throws IOException {
        List<String> lines = Arrays.stream(values)
                .map(row -> Joiner.on("  ")
                        .join(Arrays.stream(row)
                                .mapToObj(Integer::toString)
                                .collect(Collectors.toList())))
                .collect(Collectors.toList());
        Files.write(outputFile, lines, StandardCharsets.UTF_8);
    }

    static int[][] read(Path inputFile) throws IOException {
        int[][] result = new int[6][7];
        List<String> lines = Files.readAllLines(inputFile);
        Preconditions.checkState(lines.size() == 6);
        for (int lineIdx = 0; lineIdx < lines.size(); lineIdx ++) {
            String[] lineValues = lines.get(lineIdx).split("\\s\\s");
            Preconditions.checkState(lineValues.length == 7);
            for (int columnIndex = 0; columnIndex < lineValues.length; columnIndex++) {
                result[lineIdx][columnIndex] = Integer.parseInt(lineValues[columnIndex]);
            }
        }
        return result;
    }

我不愿意编写文件的原因是,按字节方式操作流非常脆弱,如果希望代码可维护,这几乎不是一个好主意。

要求创建一个负责保存文件的按钮,然后创建另一个按钮将其加载回GUI并从那里继续播放我不确定我是不是把事情搞得太复杂了。当然-所以你可以把“写”功能和“保存”按钮联系起来,把“读”功能和“加载”按钮联系起来,对吗?还是我遗漏了什么?我会试试让你知道的。谢谢。
public static void file_write(int row,int col,int letter) throws IOException{
        int counter1 = 0; // a counter that goes +1 until reach the row 
        int counter2=0; // a counter that goes +1 until reach the col
        FileWriter writehandle;
        writehandle = new FileWriter("file.txt");
        BufferedWriter bw = new BufferedWriter(writehandle);
        FileReader readhandle = new FileReader("file.txt");
        BufferedReader br = new BufferedReader(readhandle);

        if(Constants.once){ // boolean from class Constants that fills the file with 0. First example
        for(int i=0; i<7; i++){
            for(int j=0; j < 8; j++){
                Constants.line="0";
                bw.write(Constants.line + " "); // filling the file with 0
                }
        bw.newLine();
        }
    }
        if(letter == Constants.Player1) // player 1 symbol is 1
        for(int i=0; i<row;i++)
            for(int j=0; j<col; j++)

                if (br.readLine() == "0"  && counter2 != col){ //if statement in order to not change 
                    bw.write("0");                             //anything else except the player symbol i 
                    counter2++;                                //am looking for
                }
                else if(br.readLine() == " "   && counter2 != col){
                    bw.write(" ");
                    counter2++;
                }
                else if (br.readLine() == "-1" &&  counter2 != col){
                    bw.write("-1");
                    counter2++;
                }

                if(letter == Constants.Player1)
                bw.write("-1");
                else if (letter == Constants.Player2)
                    bw.write("1");
        bw.close();
        writehandle.close();
        Constants.once =false;
        file_read(0,0);


    }
    static void write(Path outputFile, int[][] values) throws IOException {
        List<String> lines = Arrays.stream(values)
                .map(row -> Joiner.on("  ")
                        .join(Arrays.stream(row)
                                .mapToObj(Integer::toString)
                                .collect(Collectors.toList())))
                .collect(Collectors.toList());
        Files.write(outputFile, lines, StandardCharsets.UTF_8);
    }

    static int[][] read(Path inputFile) throws IOException {
        int[][] result = new int[6][7];
        List<String> lines = Files.readAllLines(inputFile);
        Preconditions.checkState(lines.size() == 6);
        for (int lineIdx = 0; lineIdx < lines.size(); lineIdx ++) {
            String[] lineValues = lines.get(lineIdx).split("\\s\\s");
            Preconditions.checkState(lineValues.length == 7);
            for (int columnIndex = 0; columnIndex < lineValues.length; columnIndex++) {
                result[lineIdx][columnIndex] = Integer.parseInt(lineValues[columnIndex]);
            }
        }
        return result;
    }

static void examplePlayerOneMoves(int row, int col) {
    int[][] game = read(GAME_FILE);
    Preconditions.checkState(game[row, col] == 0);
    game[row][col] = 1;
    write(GAME_FILE, game);
}