java 将 CSV 文件转换为二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33034833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting CSV File Into 2D Array
提问by Mees van Z
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
我的文件.csv
Id;name
E1;Tim
A1;Tom
Output:
输出:
name Tim Tom
姓名蒂姆·汤姆
采纳答案by ug_
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.
如果您真的希望将其作为二维数组,我会将拆分结果 ( String[]
)添加到List
then 中,然后在事后将其转换。
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);
回答by Channa Jayamuni
First thing we don't know how many lines are there in the csv file. So it's impossible to determine the length of the 2d array. We have to increment the size of the array according to this case. But, normally it's impossible to re-size the array with java. So we create new array and copy contents of source array when we need to re-size the array.
首先我们不知道 csv 文件中有多少行。所以无法确定二维数组的长度。我们必须根据这种情况增加数组的大小。但是,通常不可能用 java 重新调整数组的大小。因此,当我们需要重新调整数组大小时,我们创建新数组并复制源数组的内容。
Solution for you:
为您解决:
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
}
Create test to view csv data:
创建测试以查看 csv 数据:
for (String[] strings : data) {
for (String string : strings) {
System.out.print("\t" + string);
}
System.out.println();
}
Output:
输出:
Id name
E1 Tim
A1 Tom
回答by Flown
I would use a dynamic structure to read all lines. Also you should use a try-resource block to close the streams automatically.
我会使用动态结构来读取所有行。此外,您应该使用 try-resource 块来自动关闭流。
public static String[][] readCSV(String path) throws FileNotFoundException, IOException {
try (FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr)) {
Collection<String[]> lines = new ArrayList<>();
for (String line = br.readLine(); line != null; line = br.readLine()) {
lines.add(line.split(";"));
}
return lines.toArray(new String[lines.size()][]);
}
}
回答by Flown
This is my implemented code:
这是我实现的代码:
String fileName = "myfile.csv";
List<List<String>> list = new ArrayList<List<String>>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
String[] headers = line.split(";");
for(String header: headers) {
List<String> subList = new ArrayList<String>();
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);
}
}
The array2D
is your wants.
这array2D
是你的愿望。
回答by IsuruJ
It's better to use an ArrayList of 1D String arrays, instead of a 2D String array.
最好使用一维字符串数组的 ArrayList,而不是二维字符串数组。
String fName = "c:\csv\myfile.csv";
String thisLine;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
ArrayList<String[]> strar = new ArrayList<String[]>();
while ((thisLine = myInput.readLine()) != null) {
strar.add(thisLine.split(";"));
}
Note: Also you need to handle the IOException here.
注意:您还需要在此处处理 IOException。