Reading CSV Files with Custom Delimiters in OpenCSV

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.

read CSV files with custom delimiters using OpenCSV

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:

CSV Sample (custom_delimited.csv)

Java Code Example

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!