2013-01-07 26 views
0
String csvFilename = "C:\\ClothingItem.csv"; 
CSVReader csvReader = new CSVReader(new FileReader(csvFilename)); 
String[] row = null; 
while ((row = csvReader.readNext()) != null) { 
    // Here my objective is to check if row[0] contains some 
    // string"abc" then 
    // I want to add the next 15 lines from csv file to a list. 
    // Then again check the same condition after those lines means 
    // from the 16th line and than add again to the list. 
    // How could I approach? 
} 

我已經在while循環中提到了我的目標。有條件地從csv文件中添加行

+1

你爲什麼不具有可變'當你遇到「ABC」,它遞減,而讀下一行,當計數器命中0,再次檢查「ABC」條件counter'設置爲15。 – Srinivas

+1

@Srinivas聽起來像一個合適的回答對我來說,不只是一個評論:) –

+0

@ user1788115如果你的邏輯你的麻煩提供更多的信息,如果你要處理的CSV文件使用的lib像[HTTP://supercsv.sourceforge。淨/] –

回答

0
int counter=0;  
while ((row = csvReader.readNext()) != null) { 
//Here my objective is to check if row[0] contains some string"abc" then 
     if(counter==0 && row.contains("abc")) { 
      counter=15; //Might be off by one depending upon the condition you have, would you include "abc" line or not? 
     } 
     counter--; 
     if(counter>0) { 
      //Add this line to a List. 
     } 
     // I want to add the next 15 lines from csv file to a list. Then again check  the same condition after those lines means from the 16th line and than add again to the list.How could I approach? 
}