2011-10-27 35 views
1

如何使用openCSV獲取和顯示csv的某些行。 目前,我有以下代碼:使用open csv獲取.csv的內容

CSVReader reader1 = new CSVReader(new FileReader(mydata_csv.getpath())); 
List myDatas = reader1.readAll(); 

如何顯示一個特定的行?

也許我可以用更好的方式來存儲我DATAS(該CSV包含數百個變量的線)。任何建議都會受到歡迎。

回答

6

爲opencsv http://opencsv.sourceforge.net/#how-to-read的文件似乎是說,你的代碼返回一個列表的String []

在這種情況下,我會寫它像這樣:

CSVReader reader1 = new CSVReader(new FileReader(mydata_csv.getpath())); 
List<String[]> myDatas = reader1.readAll(); 
String[] lineI = myDatas.get(i); 
for (String[] line : myDatas) { 
    for (String value : line) { 
     //do stuff with value 
    } 
} 
+0

謝謝。我從來沒有使用List。所以現在,我知道如何以適當的方式使用開放的CSV。 – Delphine

2

你應該使用下面的代碼:

CSVReader reader = new CSVReader(new FileReader(mydata_csv.getpath())); 
String [] nextLine; 
while ((nextLine = reader.readNext()) != null) { 
    // nextLine[] is an array of values from the line 
    System.out.println(nextLine[0] + nextLine[1] + "etc..."); 
}