0

我正在尝试编写一种方法来获取多行 csv 文件并将该文件的内容作为数组列表返回。我的问题是,当我打印数组行时,它们似乎只有文件最后一行的内容。我怀疑这可能与我不知道的 FileReader 或 BufferedReader 有关。无论如何,这是代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public int[][] readCSV(String pFilename) throws NumberFormatException, IOException {




int[][] arr = new int[19][19];



    BufferedReader br = new BufferedReader(new FileReader(pFilename));   
    String line = " ";
    String [] temp;

    while ((line = br.readLine())!= null){ 
        temp = line.split(","); //split spaces

        for(int i = 0; i<arr.length; i++) {
            for (int j = 0; j<arr.length; j++) {    
                arr[i][j] = Integer.parseInt(temp[j]);
            }
        }

    }

    printArray(arr);
}

public static void printArray (int[][] arr) { 
         for (int i =0; i <arr.length; i++) {
          for (int j = 0; j < arr.length; j++) {
            System.out.print(arr[i][j]);
          }
          System.out.println("");
         }
    }   



    }

输入

1,1,0,0,1,1,1,1,1,0,0,1,1,0,0,0,1,1,1
1,0,0,1,1,1,1 ,0,1,1,1,0,1,1,1,0,1,0,1
1,0,0,1,0,0,1,1,1,1,1,1,1, 1,1,0,1,1,0
1,1,0,1,0,1,1,0,1,0,1,0,0,1,1,1,0,1,1
1, 1,0,1,1,1,0,1,0,1,0,0,1,1,1,0,1,0,0
1,1,1,1,1,1,1,1 ,1,1,1,1,1,0,1,0,1,1,1
1,1,1,0,0,0,1,1,1,0,1,1,1,1, 1,0,1,0,1
0,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,1,1,1
1,1, 1,1,1,0,0,1,1,1,1,0,1,0,0,1,1,0,1
1,1,1,0,1,1,1,0,1 ,0,1,1,1,1,0,1,1,1,1
0,0,1,1,1,1,1,0,0,1,0,1,1,0,1, 1,0,1,0
1,0,0,0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0
1,1,1, 1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1
1,0,1,0,1,1,1,1,1,1 ,0,1,1,1,1,1,0,1,1
1,1,1,0,1,1,0,1,1,0,1,1,1,0,1,1, 0,1,0
1,0,0,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,0
1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1
0,1,0,0,1,1,0 ,1,0,1,1,1,0,1,1,0,1,1,1
1,1,0,0,1,1,1,1,1,1,0,0,1, 1,0,1,0,1,0
0,1,1,1,0,0,1,1,1,1,1,1,0,1,0,1,1,1,0

打印输出

0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110

4

3 回答 3

0
while ((line = br.readLine())!= null){ 
        temp = line.split(","); //split spaces

        for(int i = 0; i<arr.length; i++) {
            for (int j = 0; j<arr.length; j++) {    
                arr[i][j] = Integer.parseInt(temp[j]);
            }
        }

    }

那是因为您正在为每个输入行重写整个数组。所以只有最后一个“幸存下来”。

您希望保持i索引与当前行号相关联(只需i++为外循环的每次迭代执行一次,不要嵌套另一个 for 循环)。

于 2013-07-24T04:35:33.937 回答
0

我假设您的 CSV 行和列是固定的,因为您使用的是固定数组,如果它是动态的,您应该首先计算行和列,然后初始化数组或使用更好的动态数据结构。

当您为数组分配重复值时,代码中有错误。

将其更改为:

BufferedReader br = new BufferedReader(new FileReader(pFilename));   
String line = " ";
String [] temp;

while ((line = br.readLine())!= null){ 
    temp = line.split(","); //split spaces

       for (int j = 0; j<temp.length; j++) {    
            arr[i][j] = Integer.parseInt(temp[j]);
        }
    i++;
}
于 2013-07-26T00:25:46.190 回答
0

这将起作用:

    public static int[][]  readCSV(String pFilename) throws NumberFormatException, IOException {




    int[][] arr = new int[20][19];



        BufferedReader br = new BufferedReader(new FileReader(pFilename));   
        String line = " ";

        int i = -1;
        while ((line = br.readLine())!= null && i < arr.length){ 
            String [] temp = line.split(","); //split spaces
            ++i;   

                for (int j = 0; j<arr[i].length; j++) {    
                    arr[i][j] = Integer.parseInt(temp[j]);
                }

        }

        printArray(arr);
        return arr;
    }

    public static void printArray (int[][] arr) { 
             for (int i =0; i <arr.length; i++) {
              for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]);
              }
              System.out.println("");
             }
        }   
于 2013-07-26T07:52:45.730 回答