CSV files are widely used for storing tabular data, but they often come with headers or unnecessary empty lines. If your application reads these files directly, headers and blank rows may cause errors or clutter your output. This is where OpenCSV comes to the rescue.
In this post, we’ll learn how to skip headers and empty lines using OpenCSV in Java with a simple, step-by-step example. By the end, you’ll know exactly how to handle messy CSV files while keeping your data clean and structured.

Why Skip Headers and Empty Lines?
- Headers – Usually, the first line in a CSV contains column names like
id,name,email
. While useful for humans, they can interfere with parsing if not handled properly. - Empty Lines – Sometimes CSV files contain blank rows at the end or between records. These can lead to parsing exceptions or unwanted empty data.
Skipping them ensures your code only processes meaningful data rows.
Maven Dependency
Add OpenCSV to your pom.xml
:
1 2 3 4 5 6 7 8 9 |
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>5.9</version> </dependency> |
Sample CSV (data.csv)
1 2 3 4 5 |
id,name,email 1,John Doe,john@example.com 2,Jane Smith,jane@example.com |
Notice:
- The first row is a header.
- There’s an empty row after the header.
- There’s another blank row at the end.
Notice:
- The first row is a header.
- There’s an empty row after the header.
- There’s another blank row at the end.
Java Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.kscodes.opencsv; import com.opencsv.CSVReaderBuilder; import com.opencsv.CSVReader; import java.io.FileReader; import java.util.List; public class SkipHeadersAndEmptyLines { public static void main(String[] args) { String filePath = "data.csv"; try (CSVReader reader = new CSVReaderBuilder(new FileReader(filePath)) .withSkipLines(1) // Skip the header row .build()) { List<String[]> rows = reader.readAll(); for (String[] row : rows) { if (row.length == 0 || (row.length == 1 && row[0].trim().isEmpty())) { continue; // Skip empty lines } System.out.println("ID: " + row[0] + ", Name: " + row[1] + ", Email: " + row[2]); } } catch (Exception e) { e.printStackTrace(); } } } |
How It Works
withSkipLines(1)
tells OpenCSV to ignore the first line (header).- We manually check
row.length
to skip empty lines. - This ensures only valid rows are printed, leaving out headers and blank entries.
Best Practices
- Always validate the CSV structure before parsing.
- Use CsvToBean for mapping rows directly to objects if you want cleaner code.
- Add logging for skipped lines in production applications to debug malformed files.
External References
Conclusion
By using OpenCSV’s withSkipLines()
method and a few checks for empty lines, you can easily process clean data from even the messiest CSV files. This approach keeps your applications robust and free from common parsing errors caused by headers and blanks.
With this technique, your CSV parsing becomes more reliable, scalable, and production-ready.