如何将txt文件读入Java的二维数组?

3

简单的问题,我该怎么把一个txt文件读入到一个二维数组中呢?我需要将每一行放入一个数组中以便以后用于测量长度和宽度的总和,在“生命游戏”中使用。

代码

public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int matrix[][] = new int[size][size];
            while (s.hasNext()) {
                for (int i = 0; i < matrix.length; i++) {
                       for (int col = 0; col < matrix.length; col++) {
                      //      matrix[i][col] = ;
                        }
                }
            }
            s.close();

        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }

文本文档

20 20
    0    2
    0    6
    0    7
    0   10
    0   11
    0   16
    1    0
    1    4
    1    7
    2    6
    2   12
    2   15
    2   19
    3    0
    3    4
    3   14
    3   16
    3   17
    4    0
    4    8
    4   12
    4   14
    4   15
    5    0
    5    2
    5    4
    5    7
    5   13
    5   17
    6   11
    6   14
    6   15
    6   18
    7    4
    7   10
    7   13
    7   14
    8    6
    8   11
    8   12
    8   14
    8   17
    8   19
    9    6
    9    8
    9   13
    9   14
   10    3
   10   10
   10   19
   11    6
   11    7
   11    9
   11   10
   11   17
   12    0
   12    2
   12   12
   13    0
   13    1
   13    7
   13    8
   13   14
   13   15
   14    7
   14    9
   14   12
   14   13
   14   16
   14   18
   15    0
   15    2
   15    6
   15    8
   15   10
   15   11
   15   16
   15   17
   16    2
   16    3
   16   18
   17    2
   17    7
   17   11
   17   13
   17   19
   18    0
   18    5
   18    6
   18   11
   18   12
   18   13
   18   14
   19    3
   19    4
   19    6
   19    8
   19   11
   19   16
   19   17

1
你尝试过 scanner.nextInt() 吗? - Limit
@Limit,不,我还没有尝试过。 - user5129558
4个回答

2
public int[][] P4_Icel_Murad_Life(String fileName) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));

        // n = Rownum ; m = Colnum
        int array[][] = new int[n][m];
        String line = reader.readLine();

        int i = 0, j = 0;
        while (line != null) {
            String strArray[] = line.split(" ");

            if (!line.trim().isEmpty()) {
                for (String s : strArray) {
                    if (!s.trim().isEmpty()) {
                        array[i][j++] = Integer.parseInt(s);
                    }
                }
                line = reader.readLine();
                i++;
                j = 0;
            }
        }
        reader.close();
        return array;
    } catch (IOException ex) {
        System.out.println("Problems..");
    }
    return null;
}

2

看起来文本文件包含了生命游戏中所有存在生命的位置对。

你所做的是每次读取时增加“i”值,但这并不保证正确。

相反,下面的协议似乎是文本文件的协议。

  1. 行包含棋盘的X和Y大小。

其余的行包含存在生命的位置。

因此,你的代码应该像这样(除了你已经提供的异常处理之外):

 public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int rows = s.nextInt();
            int columns = s.nextInt();
            int matrix[][] = new int[rows][columns];
            while (s.hasNextInt()) {
                int row = s.nextInt();
                int col = s.nextInt(); // If this fails the text documnet is incorrect.
                matrix[row][col] = 1;
            }
            s.close();
            // At this point all dead cells are 0 and all live are 1
        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }

0

你对 int 值感兴趣,所以使用它。

此外,hasNextLine 条件已经移动到 rows 循环中,并且在 columns 循环中添加了一个 hasNextInt 条件。

public P4_Icel_Murad_Life(String fileName) {
        Scanner in;
        try {
            Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
            int matrix[][] = new int[size][size];

                for (int i = 0; i < matrix.length && s.hasNextLine(); i++) {
                       for (int col = 0; col < matrix.length && s.hasNextInt(); col++) {
                           matrix[i][col] = s.nextInt() ;

                        }
                       s.nextLine(); // col values populated for this row, time to go to the next line
                }

            s.close();

        } catch (IOException i) {
            System.out.println("Problems..");

        }
    }

有没有办法让我根据文件初始化数组? - user5129558
是的,但是您需要读取文件一次才能知道其尺寸。 - Arnaud

0

您可以使用opencsv来读写这种类型的文件

 CSVReader reader = new CSVReader(
                new FileReader("C:/Users/muro0/Desktop/life100.txt"), ' ');
 // Read all rows at once
 List<String[]> allRows = readAll(reader);

它将文件读入字符串中,但你当然可以将其转换为整数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接