首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Java中正确读取"maze.txt“文件

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

Stack Overflow用户
提问于 2015-11-19 00:58:49
回答 1查看 5.7K关注 0票数 2

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

此外,我还读到了另一篇关于在BufferedReader中使用FileReader的文章,但这篇文章有点模糊--这有什么好处呢?

代码语言:javascript
运行
复制
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;
            }

        }
    }


}

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

代码语言:javascript
运行
复制
# # # # # # # # # # # #
# . . . # . . . . . . #
. . # . # . # # # # . #
# # # . # . . . . # . #
# . . . . # # # . # . .
# # # # . # . # . # . #
# . . # . # . # . # . #
# # . # . # . # . # . #
# . . . . . . . . # . #
# # # # # # . # # # . #
# . . . . . . # . . . #
# # # # # # # # # # # #
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-19 01:16:15

我在这里的一个班做了这样的事情,我是如何在迷宫中阅读的,我使用了一个文件回收器,所以你应该用你的文件名来代替它,然后一旦你有了迷宫,你就可以随意操作它了。

代码语言:javascript
运行
复制
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++;
                        }

也就是说,假设迷宫的格式是这样的,迷宫文件顶部的宽度和高度用空格隔开。

代码语言:javascript
运行
复制
12 12
# # # # # # # # # # # #
# . . . # . . . . . . #
. . # . # . # # # # . #
# # # . # . . . . # . #
# . . . . # # # . # . .
# # # # . # . # . # . #
# . . # . # . # . # . #
# # . # . # . # . # . #
# . . . . . . . . # . #
# # # # # # . # # # . #
# . . . . . . # . . . #
# # # # # # # # # # # #
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33793443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档