2014-11-21 130 views
0
List<First> listfirst(String dir){  
     try {   
      List<First> list=new ArrayList<First>();      
      CSVReader reader = new CSVReader(new FileReader(dir)); 
      String [] nextLine; 
      while ((nextLine = reader.readNext()) != null) { 
      // nextLine[] is an array of values from the line 
      list.add(new First(nextLine[0], nextLine[1], nextLine[2],nextLine[3], nextLine[4],nextLine[5], nextLine[6], nextLine[7])); 
      }   
      return list; 
     } catch (Exception e) {    
      e.printStackTrace(); 
      return null; 
     } 
    } 

有1994行。但是這個代碼只打印了388行。我該怎麼辦?用opencsv讀取csv

回答

1

這就是我建議讀應該是這樣的:

讀取文件

import au.com.bytecode.opencsv.CSVReadProc; 
import java.util.Arrays; 

csv.read("example.csv", new CSVReadProc() { 
    public void procRow(int rowIndex, String... values) { 
     System.out.println(rowIndex + ": " + Arrays.asList(values)); 
    } 
}); 
0

如果你想閱讀整個CSV,一旦閱讀並遍歷它,

List<String[]> allRows = reader.readAll(); 

     //Read CSV line by line and use the string array as you want 
    for(String[] row : allRows){ 
     System.out.println(Arrays.toString(row)); 
    } 
    } 

而且還嘗試使用CSVReader的構造函數,

CSVReader reader = new CSVReader(new FileReader("input.csv"), ',', '"', 0); 
// 0 refers the 1st line