Skip to content
Advertisement

Apache commons csv skip lines

How to skip lines in input file with apache commons csv. In my file first few lines are garbage useful meta-information like date, etc. Can’t find any options for this.

private void parse() throws Exception {
    Iterable<CSVRecord> records = CSVFormat.EXCEL
            .withQuote('"').withDelimiter(';').parse(new FileReader("example.csv"));
    for (CSVRecord csvRecord : records) {
        //do something            
    }
}

Advertisement

Answer

Use FileReader.readLine() before starting the for-loop.

Your example:

private void parse() throws Exception {
  FileReader reader = new FileReader("example.csv");
  reader.readLine(); // Read the first/current line.

  Iterable <CSVRecord> records = CSVFormat.EXCEL.withQuote('"').withDelimiter(';').parse(reader);
  for (CSVRecord csvRecord: records) {
    // do something
  }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement