When working with CSV files in Java, most developers assume that the default delimiter will always be a comma. But in real-world applications, CSV files may use semicolons (;
), pipes (|
), tabs (\t
), or other custom delimiters. Fortunately, OpenCSV—a powerful CSV parsing library—makes it easy to read files with custom delimiters.
In this tutorial, we’ll walk through how to read CSV files with custom delimiters using OpenCSV, complete with a Maven setup and code examples. This guide is ideal for beginners who are looking to understand how to deal with non-standard CSV formats.

Why Use OpenCSV?
OpenCSV simplifies working with CSV files in Java. It offers features like:
- Annotation-based mapping to Java Beans
- Support for different character encodings
- Easy handling of custom delimiters
- Built-in handling of quotes and escape characters
When your CSV files use delimiters other than commas, OpenCSV makes it easy to configure and parse them.
Maven Dependency
Add the following dependency in your pom.xml
file:
1 2 3 4 5 6 7 8 |
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>5.9</version> </dependency> |
CSV Sample (custom_delimited.csv)
1 2 3 4 5 6 |
id|name|email 1|John Doe|john@example.com 2|Jane Smith|jane@example.com |
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 |
package com.kscodes.opencsv; import com.opencsv.CSVReaderBuilder; import com.opencsv.CSVParserBuilder; import com.opencsv.CSVReader; import java.io.FileReader; import java.util.List; public class CustomDelimiterReader { public static void main(String[] args) { String filePath = "custom_delimited.csv"; try (CSVReader reader = new CSVReaderBuilder(new FileReader(filePath)) .withCSVParser(new CSVParserBuilder().withSeparator('|').build()) .build()) { List[String[]> records = reader.readAll(); for (String[] row : records) { System.out.println("ID: " + row[0] + ", Name: " + row[1] + ", Email: " + row[2]); } } catch (Exception e) { e.printStackTrace(); } } } |
Explanation
- CSVParserBuilder allows you to define the custom delimiter (
|
in this case). - CSVReaderBuilder uses that parser to read the file line by line.
readAll()
collects all rows into a list of string arrays.
This approach ensures that your application can correctly parse files regardless of the delimiter used.
Best Practices
- Always validate the delimiter before processing unknown CSV files.
- Handle exceptions gracefully to avoid crashes when the format changes.
- Consider using CsvToBeanBuilder if you’re mapping data directly into Java Beans.
External References
Conclusion
Reading CSV files with custom delimiters doesn’t have to be complicated. Thanks to OpenCSV’s powerful configuration options, parsing files with |
, ;
, or even tab delimiters is simple and clean. If you’re working on enterprise applications or importing legacy data formats, mastering this technique will save you time and avoid common parsing errors.
Stay tuned for more OpenCSV tutorials on kscodes.com!