
本文详解如何从文件中读取迷宫尺寸与布局,安全、准确地将每行字符载入二维 char 数组,避免重复调用 `readline()` 导致跳行或数组越界等常见错误。
在处理类似迷宫解析这类结构化文本文件时,一个典型格式是:首行为两个整数(行数 row 和列数 col),随后紧接 row 行字符数据(每行长度应为 col,含空格、# 等符号)。目标是将这些字符精确填充至 char[row][col] 二维数组中。但初学者常因对 BufferedReader.readLine() 的副作用理解不足而引入逻辑漏洞——例如在 while 条件中提前消费一行,又在循环体内再次调用,导致实际读取行数减少、数组末行缺失或索引错位。
核心问题在于原代码中的双重 readLine() 调用:
while(read.readLine() != null) { // ❌ 第一次调用:跳过第一行迷宫!
for(int i = 0; i < row-1; i++) { // ❌ 循环上限错误:应为 row,非 row-1
String line = read.readLine(); // ❌ 第二次调用:继续跳行,i 与 line 错配
// ... 填充逻辑
}
}这不仅造成首行迷宫被忽略,还因 i
✅ 正确做法是:用单一、受控的 readLine() 驱动循环,确保每调用一次即对应填充一行。推荐采用 for 循环配合显式行读取,如下所示:
立即学习“Java免费学习笔记(深入)”;
import java.io.*;
import java.util.*;
public class MazeLoader {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print("Enter the filename of the maze: ");
String fileName = inp.next();
File fileObj = new File(fileName);
try (BufferedReader reader = new BufferedReader(new FileReader(fileObj))) {
// 读取首行:解析 row 和 col
String[] dims = reader.readLine().split("\s+");
int row = Integer.parseInt(dims[0]);
int col = Integer.parseInt(dims[1]);
char[][] maze = new char[row][col];
System.out.printf("Loading %d×%d maze...
", row, col);
// 关键修正:逐行读取并填充,共执行 row 次
for (int i = 0; i < row; i++) {
String line = reader.readLine();
if (line == null) {
throw new IOException("Unexpected end of file: expected " + row + " rows, but only " + i + " lines found.");
}
// 截取前 col 个字符(防御性处理:防止超长行)
String trimmed = line.length() >= col ? line.substring(0, col) : String.format("%-" + col + "s", line);
for (int j = 0; j < col; j++) {
maze[i][j] = trimmed.charAt(j);
}
}
// 验证输出(可选)
System.out.println("
Loaded maze:");
for (char[] rowArr : maze) {
System.out.println(Arrays.toString(rowArr));
}
} catch (IOException e) {
System.err.println("Error reading file '" + fileName + "': " + e.getMessage());
e.printStackTrace();
} finally {
inp.close();
}
}
}? 关键要点总结:
- 杜绝隐式 readLine():不在循环条件中调用 readLine(),所有读取必须显式赋值给变量并在循环体内使用;
- 严格匹配行数:for (int i = 0; i
- 健壮性增强:检查 line == null 并抛出明确异常,避免静默失败;对超长/过短行做截断或补空处理(如 String.format("%-Ns", line));
- 资源管理:使用 try-with-resources 自动关闭 BufferedReader,无需手动 close();
- 输入验证:Scanner 也应在 finally 中关闭,防止资源泄漏。
按此方案实现后,输入示例 mazeA.txt 将被精准映射为 7×9 的二维字符数组,每一行字符位置与原始文件完全对齐,为后续迷宫求解(如 BFS、DFS)提供可靠的数据基础。









