Skip first line using Open CSV reader

前端 未结 5 1509
离开以前
离开以前 2021-01-04 00:49

Here is the line i am using currently

File booleanTopicFile;
// booleanTopicFile is csv file uploaded from form
CSVReader csvReader = new CSVReader(new Input         


        
相关标签:
5条回答
  • 2021-01-04 00:54

    with latest version opencsv version use -

    CSVReader csvReader = new CSVReaderBuilder(new FileReader("book.csv")).withSkipLines(1).build()

    0 讨论(0)
  • 2021-01-04 00:56

    You can also use withFilter:

    watFileCsvBeans = new CsvToBeanBuilder<ClassType>(isr)
      .withType(ClassType.class)
      .withIgnoreLeadingWhiteSpace(true)
      // CsvToBeanFilter with a custom allowLine implementation
      .withFilter(line -> !line[0].equals("skipme"))
      .build()
      .parse();
    
    0 讨论(0)
  • 2021-01-04 01:00

    I found this question and response helpful, I'd like to expand on Christophe Roussy's comment. In the latest opencsv (2.3 as of this writing) The actual line of code is:

    new CSVReader( new StringReader(csvText), CSVParser.DEFAULT_SEPARATOR,
                   CSVParser.DEFAULT_QUOTE_CHARACTER, 1);
    

    Note it uses CSVParser instead of CSVReader.

    0 讨论(0)
  • 2021-01-04 01:06

    This constructor of CSVReader class will skip 1st line of the csv while reading the file.

    CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1);
    
    0 讨论(0)
  • 2021-01-04 01:06

    At least since version 3.8 you can use the CSVReaderBuilder and set it to skip the first line.

    Example:

    CSVReader reader = new CSVReaderBuilder(inputStreamReader)
                    .withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS)
                    // Skip the header
                    .withSkipLines(1)
                    .build();
    
    0 讨论(0)
提交回复
热议问题