2015-10-09 90 views
3

我有一个我的想法在1D数组中的例子。它只会输出列。 我的想法是使用2d数组来选择行和列。 这里我的代码:将CSV文件转换为二维数组

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 

输出:

名添汤

+0

神奇的想法,它看起来像你的90%,有什么问题? –

+0

哈哈:)我想用这样的二维数组:String fileData [] [] = new String [ROWS] [COLUMNS]; –

回答

2

我会分裂的结果(String[])只需添加到List那么,如果你真的想把它当作一个二维数组,然后将其转换后的事实。

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); 
+0

感谢这个例子@ug_,但是我怎样才能从一个特定的行和列表中打印呢? –

+1

只需从数组'array [row] [column]'中获取元素即可。记住数组从索引0开始。因此获得第一行和第一列(在你的例子中是“Id”)是'array [0] [0]' –

+0

这正是我想要的! 1000倍感谢 –

1

第一件事,我们不知道多少行在csv文件中。所以不可能确定二维数组的长度。根据这种情况,我们必须增加数组的大小。但是,通常不可能用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 
+0

Thx!例如,如果我只想显示tim作为输出?像out.println(string [?] [?]); –

0

我会使用动态结构来读取所有行。你也应该使用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()][]); 
    } 
} 
0

这是我实现的代码:

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); 
    } 
} 

array2D是你想要的。

0

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

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(";")); 
} 

注意:你也需要在这里处理IOException。