
在Java开发中,经常需要从外部文件加载数据。当数据以矩阵形式存储在文本文件中时,例如每行代表矩阵的一行,每个字符代表一个元素,我们需要一种高效且健壮的方法来将其读取并转换为程序可操作的二维数组。
在填充矩阵之前,首先需要确定其准确的行数(m)和列数(n)。这可以通过对文件进行一次预扫描来完成:
以下代码片段展示了如何实现这一逻辑:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MatrixReader {
/**
* 预扫描文件以确定矩阵的行数和列数。
* 假设文件格式为每行一个矩阵行,每个字符一个元素。
*
* @param filePath 文本文件的路径。
* @return 包含行数和列数的 int 数组,例如 {rows, cols}。
* @throws FileNotFoundException 如果文件不存在。
* @throws IllegalArgumentException 如果文件为空或格式不正确。
*/
private static int[] getMatrixDimensions(String filePath) throws FileNotFoundException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("文件未找到: " + filePath);
}
int rows = 0;
int cols = 0;
try (Scanner scanner = new Scanner(file)) {
if (scanner.hasNextLine()) {
String firstLine = scanner.nextLine();
cols = firstLine.length(); // 列数等于第一行字符长度
rows++; // 第一行已经计数
} else {
throw new IllegalArgumentException("文件为空,无法确定矩阵维度。");
}
// 遍历剩余行以确定总行数
while (scanner.hasNextLine()) {
scanner.nextLine();
rows++;
}
}
return new int[]{rows, cols};
}
}在获取到矩阵的维度 (rows, cols) 后,就可以创建相应大小的二维数组,并再次遍历文件,将数据逐个填充进去。
立即学习“Java免费学习笔记(深入)”;
填充过程通常遵循以下步骤:
以下是实现这一过程的核心代码片段:
// ... (接上文的 MatrixReader 类)
/**
* 从文本文件读取数据并填充到二维整型矩阵中。
* 文件格式假设为每行一个矩阵行,每个字符一个元素。
*
* @param filePath 文本文件的路径。
* @return 填充好的二维整型矩阵。
* @throws FileNotFoundException 如果文件不存在。
* @throws IllegalArgumentException 如果文件为空或格式不正确。
*/
public static int[][] readMatrixFromFile(String filePath) throws FileNotFoundException {
int[] dimensions = getMatrixDimensions(filePath);
int rows = dimensions[0];
int cols = dimensions[1];
if (rows == 0 || cols == 0) {
throw new IllegalArgumentException("矩阵维度为零,请检查文件内容。");
}
int[][] matrix = new int[rows][cols];
File file = new File(filePath);
try (Scanner scanner = new Scanner(file)) {
int currentRowIdx = 0;
while (scanner.hasNextLine() && currentRowIdx < rows) {
String line = scanner.nextLine();
// 确保当前行的长度与预期的列数匹配
if (line.length() != cols) {
throw new IllegalArgumentException("文件格式错误:第 " + (currentRowIdx + 1) + " 行长度不一致。");
}
char[] charArray = line.toCharArray();
for (int colIdx = 0; colIdx < cols; colIdx++) {
// 将字符转换为数字值
// 注意:Character.getNumericValue() 适用于单个数字字符 '0'-'9'
// 对于非数字字符或多位数,需要更复杂的解析逻辑
matrix[currentRowIdx][colIdx] = Character.getNumericValue(charArray[colIdx]);
}
currentRowIdx++;
}
}
return matrix;
}
// 示例:打印矩阵以验证
public static void printMatrix(int[][] matrix) {
if (matrix == null || matrix.length == 0) {
System.out.println("矩阵为空或维度为零。");
return;
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
String filePath = "./src/User/url.txt"; // 请替换为你的文件路径
try {
int[][] myMatrix = readMatrixFromFile(filePath);
System.out.println("成功从文件读取矩阵:");
printMatrix(myMatrix);
} catch (FileNotFoundException e) {
System.err.println("错误: 文件未找到 - " + e.getMessage());
} catch (IllegalArgumentException e) {
System.err.println("错误: 文件内容格式不正确 - " + e.getMessage());
} catch (Exception e) {
System.err.println("发生未知错误: " + e.getMessage());
e.printStackTrace();
}
}
}假设 url.txt 文件内容如下:
123 456 789
运行 main 方法将输出:
成功从文件读取矩阵: 1 2 3 4 5 6 7 8 9
// 如果每行是 "1 2 3" 这种格式
String[] numStrs = line.split(" ");
for (int colIdx = 0; colIdx < numStrs.length; colIdx++) {
matrix[currentRowIdx][colIdx] = Integer.parseInt(numStrs[colIdx]);
}通过本教程,您应该能够掌握在Java中从特定格式的文本文件读取数据并填充到二维数组的方法。关键在于分两步走:首先确定矩阵的精确维度,然后逐行读取并解析数据以填充矩阵。同时,注意文件路径、资源管理、字符转换的局限性以及健壮的错误处理,是构建可靠数据加载功能的重要方面。
以上就是Java:高效地从文本文件读取并填充二维数组的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号