3

I've been searching this everywhere, I'm probably just being thick but is there a way to read a csv file from a particular row onwards? I have a csv file but want to read the data from the 14th row onwards. Below is my csv reader.

  CSVReader reader=new CSVReader(new FileReader(filename1));
  String [] value ;

  while((value=reader.readNext())!=null){

The data inside the csv is something like this

    1   djennings93
    2   27/02/2014
    3   13:31
    ...
    14  26/02/14 14:25:00, www.google.co.uk, 50, Google

I'd like to ignore the data from lines 1-13

2 Answers 2

4

You can skip the first 13 lines by doing:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'', 13);

Have a look here. (question Can I use my own separators and quote characters?)

This will result in reading the data from the 14th line onward.

Sign up to request clarification or add additional context in comments.

3 Comments

cheers mate! It's amazing what a bit of reading can actually do! my bad.
What about reading to a specific line. Also as soon as I define CSVReader reader is the whole file read? Or the file read happens when I iterate over reader?
@User3 check section "How do I read and parse a CSV file?" on opencsv.sourceforge.net
3

Boris's answer still works but deprecated. It is recommended to use builder class:

CSVReader reader = new CSVReaderBuilder(new FileReader("yourfile.csv"))
        .withSkipLines(13)
        .withCSVParser(new CSVParserBuilder().withSeparator('\t').withEscapeChar('\'').build())
        .build();

Comments

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.