将csv文件读入二维数组Java

时间:2013-07-24 04:28:17

标签: arrays csv type-2-dimension

我正在尝试编写一个方法来获取多行csv文件,并将该文件的内容作为arraylist返回。我的问题是,当我打印数组行时,它们似乎只有文件最后一行的内容。我怀疑它可能是我不知道的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

3 个答案:

答案 0 :(得分: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++,不要为循环嵌套另一个)。

答案 1 :(得分: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++;
}

答案 2 :(得分: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("");
             }
        }