2015-10-09 89 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。