2014-11-21 128 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