
本文详解如何用java准确解析含行列声明的迷宫文本文件,逐行读取并填充至二维char数组,避免重复调用readline导致跳行、索引越界等常见错误,并提供健壮、可复用的实现方案。
在处理类似迷宫(maze)这类结构化文本数据时,一个典型格式是:首行为两个整数(行数 row 和列数 col),随后紧跟 row 行、每行恰好 col 个(或经空格分隔后逻辑上对应 col 个)字符的网格数据。目标是将这些字符无损、对齐地载入 char[row][col] 二维数组中。但初学者常因误用 BufferedReader.readLine() 而引发跳行、数组未完全填充或 NullPointerException 等问题——正如示例代码中在 while(read.readLine() != null) 和循环体内再次调用 read.readLine(),导致第一行迷宫被直接丢弃。
核心问题在于:readLine() 是“消耗性”操作,每次调用即前进到下一行;不可为同一行反复读取,也不应在未校验返回值的情况下盲目使用。
✅ 正确做法是:显式、顺序、单次读取每一行,并严格控制读取次数为 row 次。以下是优化后的完整实现:
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();
inp.close(); // 及时关闭控制台输入,避免资源泄漏
File fileObj = new File(fileName);
System.out.println("File " + fileName + " has been entered. Solving:
");
try (BufferedReader read = new BufferedReader(new FileReader(fileObj))) {
// Step 1: 读取首行,解析行列数
String rcLine = read.readLine();
if (rcLine == null) {
throw new IOException("File is empty or missing dimension line.");
}
String[] rc = rcLine.trim().split("\s+");
if (rc.length < 2) {
throw new IOException("Invalid dimension format: expected 'rows cols'");
}
int row = Integer.parseInt(rc[0]);
int col = Integer.parseInt(rc[1]);
char[][] maze = new char[row][col];
// Step 2: 逐行读取迷宫数据(共 row 行)
for (int i = 0; i < row; i++) {
String line = read.readLine();
if (line == null) {
throw new IOException("Unexpected end of file: expected " + row + " maze lines, got only " + i);
}
// 去除首尾空格,按任意空白符分割,取前 col 个非空 token(适配题目中带空格的格式)
String[] tokens = line.trim().split("\s+");
if (tokens.length < col) {
throw new IOException("Line " + (i + 1) + " has insufficient tokens (" + tokens.length + " < " + col + ")");
}
// 将每个 token 的首字符(如 "#" 或 " ")填入数组 —— 精准匹配题目示例格式
for (int j = 0; j < col; j++) {
maze[i][j] = tokens[j].charAt(0);
}
}
// Step 3: 验证并打印结果(可选)
System.out.println("Loaded maze (" + row + "x" + col + "):");
for (int i = 0; i < row; i++) {
System.out.println(Arrays.toString(maze[i]));
}
} catch (NumberFormatException e) {
System.err.println("Error: Invalid number in dimension line.");
e.printStackTrace();
} catch (IOException e) {
System.err.println("I/O Error: " + e.getMessage());
e.printStackTrace();
}
}
}? 关键改进与注意事项:
立即学习“Java免费学习笔记(深入)”;
- 杜绝重复 readLine():不再使用 while(read.readLine() != null) 这类不可控循环,而是用确定次数的 for (int i = 0; i
- 空行/空格鲁棒性:使用 trim().split("\s+") 处理题目中 "# # #####" 这类含多个空格的格式,自动提取有效字符 token,避免 charAt(j) 直接访问空格或越界。
- 前置空值检查:每次 read.readLine() 后立即判断 line == null,防止 NullPointerException 并给出清晰错误提示。
- 资源安全:采用 try-with-resources 自动关闭 BufferedReader,无需手动 close()。
- 输入清理:Scanner 在读取文件名后及时关闭,避免后续标准输入干扰。
? 扩展建议:若需支持更灵活格式(如无空格的紧凑型 ######),只需将 tokens[j].charAt(0) 替换为 line.charAt(j),并确保 line.length() >= col 即可。但当前实现已完美契合题目所给 mazeA.txt 格式。
通过以上方法,你将获得一个稳定、可调试、符合生产规范的迷宫加载模块——它不仅是解决一道编程题,更是掌握 Java 文件 I/O 严谨实践的坚实一步。










