Java OpenCSV tutorial to work with CSV files

BeanIO is an open source Java framework for marshaling and marshaling Java beans from a flat file, stream, or simple String object. It is very powerful with support for XML, CSV, delimited and fixed length stream formats, Object binding, filed validation, integration with spring-batch, etc. But there are scenarios where you need to simply read or write CSV data, OpenCSV framework is quite useful. Here are simple step by step guide to convert Java objects to CSV data.

Step 1: Define in the maven pom.xml file to bring in the OpenCSV jar file.


Step 2: Write the Person model class.

Step 3: Write the interface.

Step 4: Write the implementation using the above interface.

If you want to write to a file, you can use a FileWriter instead of the StringWriter.

Step 5: Write a test class to test it.In practice, a unit test class must be written.


Step 6: The output is


800+ Java Interview Q&As Menu

Learn by categories on the go...
Learn by categories such as FAQs – Core Java, Key Area – Low Latency, Core Java – Java 8, JEE – Microservices, Big Data – NoSQL, Architecture – Distributed, Big Data – Spark, etc. Some posts belong to multiple categories.
Top