24

Here is the line i am using currently

File booleanTopicFile;
// booleanTopicFile is csv file uploaded from form
CSVReader csvReader = new CSVReader(new InputStreamReader(new FileInputStream(booleanTopicFile), "UTF-8"));

Want to skip the first line of the csv which contains headings. I dont want to use any separator as except the default one comma(,) which is already available in default constructor. In parameterized constructor there is a option to skip no. of lines but how to deal with the 2nd and 3rd param of the constructor.

CSVReader csvReader = new CSVReader(new InputStreamReader(Reader reader, char c, char c1, int index);

-- Thanks

Sangram Anand
  • 10,526
  • 23
  • 70
  • 103

6 Answers6

35

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

CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1);
Chris
  • 7,675
  • 8
  • 51
  • 101
Abhijeet Doshi
  • 374
  • 4
  • 5
  • 9
    +1 Correct answer but don't most CSV files use double quoting ? So it would be `new CSVReader(new StringReader(csvText), CSVReader.DEFAULT_SEPARATOR, CSVReader.DEFAULT_QUOTE_CHARACTER, 1);` instead. – Christophe Roussy Oct 30 '13 at 12:25
  • 5
    In version 2.3 the *DEFAULT_SEPARATOR* and *DEFAULT_QUOTE_CHARACTER* are now in *CSVParser* class. – nyxz Sep 09 '15 at 14:50
  • There is one issue with this. If a csv is having values containing single quote, it will skip that record. e.g Adrian,D'sousa,Agent – Vikas May 02 '18 at 11:54
  • 2
    Just fyi, but this constructor is deprecated in Version 4.0. Not sure how I can achieve the same functionality without using deprecated methods. – wheeleruniverse May 30 '18 at 05:03
  • 3
    this is deprecated you can use => `CSVReader csvReader = new CSVReaderBuilder(new FileReader(csvFileName)).withSkipLines(1).build()` – Vishnu Atrai Sep 02 '19 at 11:23
28

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();
dazito
  • 7,740
  • 15
  • 75
  • 117
14

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.

Community
  • 1
  • 1
Sinsanator
  • 381
  • 4
  • 16
  • Very helpful. I was looking for a way to use a null char for the quote parameter, when I saw your answer. – Stephane Sep 30 '14 at 10:06
5

with latest version opencsv version use -

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

Vishnu Atrai
  • 2,370
  • 22
  • 24
-1

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();
Maroun
  • 94,125
  • 30
  • 188
  • 241
-1
watFileCsvBeans = new CsvToBeanBuilder<ClassType>(isr)
  .withType(ClassType.class)
  .withIgnoreLeadingWhiteSpace(true)
  // CsvToBeanFilter with a custom allowLine implementation
  .withFilter(line -> !line[0].equals("skipme"))
  .build()
  .parse(); 

It's useful in my case. Instead, "withSkipLines" is not working for me. opencsv version: 5.5.2