27

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

3
  • 3
    Why don't you just read the first line and do nothing with it? Commented Nov 27, 2012 at 8:01
  • 2
    csvReader.readNext(); before the loop, is an alternative solution, but would be optimal if its possible through constructor. Commented Nov 27, 2012 at 8:02
  • Currently, the answer by @dazito is the correct one. It uses the CSVReaderBuilder and its withSkipLines() method to return a CSVReader with the desired number of lines to skip. This design leverages the library’s intended functionality for this purpose rather than manually invoking readLine() and discarding its output. On that note, @svz, ignoring a method's return value is generally seen as an anti-pattern and can hide potential issues. Using CSVReaderBuilder makes the intent clear. Commented Apr 14 at 14:58

6 Answers 6

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);
Sign up to request clarification or add additional context in comments.

5 Comments

+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.
In version 2.3 the DEFAULT_SEPARATOR and DEFAULT_QUOTE_CHARACTER are now in CSVParser class.
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
Just fyi, but this constructor is deprecated in Version 4.0. Not sure how I can achieve the same functionality without using deprecated methods.
this is deprecated you can use => CSVReader csvReader = new CSVReaderBuilder(new FileReader(csvFileName)).withSkipLines(1).build()
31

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();

1 Comment

This is the right way to do it, forget the accepted answer.
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.

2 Comments

Very helpful. I was looking for a way to use a null char for the quote parameter, when I saw your answer.
This constructor is deprecated in Version 4.0. Currently, the answer by @dazito is the correct one.
5

with latest version opencsv version use -

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

1 Comment

That's what I said in my answer from 2016.
0
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

1 Comment

This was helpful. I had the same issue. withSkipLines is inexplicably not working and this 'hack' worked for me. OpenCsv version 5.7.0.
-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();

1 Comment

withSkipLine() worked perfectly, but withFilter() notworking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.