将CSV文件转换为2D数组

我在一维数组中有一个我的想法的例子。 它只会输出列。 我的想法是使用2d数组来选择行和列。 这是我的代码:

String fName = "c:\\csv\\myfile.csv"; String thisLine; int count=0; FileInputStream fis = new FileInputStream(fName); DataInputStream myInput = new DataInputStream(fis); int i=0; while ((thisLine = myInput.readLine()) != null) { String strar[] = thisLine.split(";"); out.println(strar[1]); // Here column 2 } 

myfile.csv

 Id;name E1;Tim A1;Tom 

输出:

蒂姆汤姆

我只是将拆分结果( String[] )添加到List然后如果你真的想要它作为2d数组然后在事后转换它。

 List lines = new ArrayList(); while ((thisLine = myInput.readLine()) != null) { lines.add(thisLine.split(";")); } // convert our list to a String array. String[][] array = new String[lines.size()][0]; lines.toArray(array); 

首先我们不知道csv文件中有多少行。 因此无法确定2darrays的长度。 我们必须根据这种情况增加数组的大小。 但是,通常用java重新调整数组的大小是不可能的。 因此,当我们需要重新调整数组大小时,我们会创建源数组的新数组和副本内容。

解决方案:

 int i = 0;//line count of csv String[][] data = new String[0][];//csv data line count=0 initially while ((thisLine = myInput.readLine()) != null) { ++i;//increment the line count when new line found String[][] newdata = new String[i][2];//create new array for data String strar[] = thisLine.split(";");//get contents of line as an array newdata[i - 1] = strar;//add new line to the array System.arraycopy(data, 0, newdata, 0, i - 1);//copy previously read values to new array data = newdata;//set new array as csv data } 

创建测试以查看csv数据:

 for (String[] strings : data) { for (String string : strings) { System.out.print("\t" + string); } System.out.println(); } 

输出:

 Id name E1 Tim A1 Tom 

我会使用动态结构来读取所有行。 您还应该使用try-resource块自动关闭流。

 public static String[][] readCSV(String path) throws FileNotFoundException, IOException { try (FileReader fr = new FileReader(path); BufferedReader br = new BufferedReader(fr)) { Collection lines = new ArrayList<>(); for (String line = br.readLine(); line != null; line = br.readLine()) { lines.add(line.split(";")); } return lines.toArray(new String[lines.size()][]); } } 

最好使用1D String数组的ArrayList,而不是2D String数组。

 String fName = "c:\\csv\\myfile.csv"; String thisLine; FileInputStream fis = new FileInputStream(fName); DataInputStream myInput = new DataInputStream(fis); ArrayList strar = new ArrayList(); while ((thisLine = myInput.readLine()) != null) { strar.add(thisLine.split(";")); } 

注意:您还需要在此处理IOException。

这是我实现的代码:

 String fileName = "myfile.csv"; List> list = new ArrayList>(); BufferedReader br = new BufferedReader(new FileReader(fileName)); String line = br.readLine(); String[] headers = line.split(";"); for(String header: headers) { List subList = new ArrayList(); subList.add(header); list.add(subList); } while((line = br.readLine()) != null) { String[] elems = line.split(";"); for(int i = 0; i < elems.length; i++) { list.get(i).add(elems[i]); } } br.close(); int rows = list.size(); int cols = list.get(0).size(); String[][] array2D = new String[rows][cols]; for(int row = 0; row < rows; row++) { for(int col = 0; col < cols; col++) { array2D[row][col] = list.get(row).get(col); } } 

array2D是你的需要。