有条件地从csv文件中添加行

时间:2013-01-07 10:14:41

标签: java csv

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?
}

我在循环中提到了我的目标。

1 个答案:

答案 0 :(得分: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?
}