本文介绍了将CSV文件转换为2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an example of my idea in a 1d array. It will only output the columns.My idea is to use a 2d array to select the row and column.Here my code:

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

myfile.csv

Id;name
E1;Tim
A1;Tom

输出:

推荐答案

我只将拆分结果(String[])添加到List中,然后,如果您真的希望将其作为2d数组,则在事后将其转换

I would just add the split result (String[]) to a List then if you really want it as a 2d array then convert it after the fact.

List<String[]> lines = new ArrayList<String[]>();
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文件转换为2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:32
查看更多