如何在 Java 中正确读取 "maze.txt" 文件

How to properly read in a "maze.txt" file in Java

对于我的家庭作业,我们将使用递归遍历虚拟迷宫。我想确保我正在正确读取文件。此时我的代码的目标是读入一个包含迷宫的文本文件,并在迷宫的起点放置一个 'X'。

此外,我在另一个 SO post 上阅读了有关将 BufferedReader 与 FileReader 结合使用的信息,但 post 有点含糊 - 这样做有什么好处?

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class ReadInMaze 
{
private static char[][] maze = null;
private static int rows = 0;
private static int cols = 0;
private static int xStart = 0;
private static int yStart = 0;

public static void Maze(File mazeFile) throws IOException
{
    File mazeFile = new File ("C:/Users/Mark/workspace/18-20_MazeTraversal_Hard/src/MazeForMazeTraversalHW.txt");
    BufferedReader reader = new BufferedReader(new FileReader(mazeFile));

    Scanner lineOfFile = new Scanner(reader.readLine());

    rows = lineOfFile.nextInt(); //get the number of rows of the maze
    cols = lineOfFile.nextInt(); // get the number of columns of the maze
    maze = new char[rows][cols]; //create a char array of the proper size

    //For loops to iterate the rows and col to find the start/enterance of the maze as it pertains to the first char in the row
    for (int y = 0; y < cols; y ++)
    {
        lineOfFile = new Scanner(reader.readLine());
        for(int x = 0; x < rows; x++)
        {
            char start = lineOfFile.next().charAt(0);
            maze[x][y] = start;

            //statement to set the starting coorinates for the maze
            if (start == '.')
            {
                xStart = x;
                yStart = y;
            }

        }
    }


}

我的文本文件中的迷宫如下所示:

# # # # # # # # # # # #
# . . . # . . . . . . #
. . # . # . # # # # . #
# # # . # . . . . # . #
# . . . . # # # . # . .
# # # # . # . # . # . #
# . . # . # . # . # . #
# # . # . # . # . # . #
# . . . . . . . . # . #
# # # # # # . # # # . #
# . . . . . . # . . . #
# # # # # # # # # # # #

我为我的一个 类 做了类似的事情这是我在迷宫中阅读的方式,我使用了一个文件选择器所以你应该用你的文件名替换它,然后一旦你有了迷宫你就可以随心所欲地操纵它

BufferedReader read = new BufferedReader(new FileReader(chooser.getSelectedFile()));
                        String rea = read.readLine();
                        String[] split = rea.split(" ");
                        width =  Integer.valueOf(split[0]);
                        height = Integer.valueOf(split[1]);

                        String readline;
                        int num = 0;
                        maze1 = new char[width][height];
                        while((readline = read.readLine()) != null){
                            char[] ch = readline.toCharArray();
                            for(int i = 0;i < ch.length;i++){
                                maze1[i][num] = ch[i];
                            }
                            num++;
                        }

假设您的迷宫格式如下,迷宫文件顶部的宽度和高度用 space

分隔
12 12
# # # # # # # # # # # #
# . . . # . . . . . . #
. . # . # . # # # # . #
# # # . # . . . . # . #
# . . . . # # # . # . .
# # # # . # . # . # . #
# . . # . # . # . # . #
# # . # . # . # . # . #
# . . . . . . . . # . #
# # # # # # . # # # . #
# . . . . . . # . . . #
# # # # # # # # # # # #