Apache commons csv skip lines
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
}
}
There is no built-in facility to skip an unknown number of lines.
If you want to skip only the first line (the header line), you can call withSkipHeaderRecord()
while building the parser.
A more general solution would be to call next()
on the iterator:
Iterable<CSVRecord> parser = CSVFormat.DEFAULT.parse(new FileReader("example.csv"));
Iterator<CSVRecord> iterator = parser.iterator();
for (int i = 0; i < amountToSkip; i++) {
if (iterator.hasNext()) {
iterator.next();
}
}
while (iterator.hasNext()) {
CSVRecord record = iterator.next();
System.out.println(record);
}