OpenCSV Guide Overview
Introduction
OpenCSV is a library used for reading and writing CSV files in Java applications. It offers a wide array of functionalities including error handling, annotations, validators, and processors, among others. Here's a comprehensive guide to its main features and functionalities.
Key Sections
Quick Start
Basic Usage
- Reading: Create a bean for data, annotate fields, and read it using
CsvToBeanBuilder.
List<MyBean> beans = new CsvToBeanBuilder(new FileReader("yourfile.csv"))
.withType(MyBean.class).build().parse();
- Writing: Annotate bean fields and write them using
StatefulBeanToCsv.
Writer writer = new FileWriter("yourfile.csv");
StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
beanToCsv.write(beans);
writer.close();
Advanced Usage
- Using
CSVReaderHeaderAware for reading CSV files with headers as map without creating special classes.
Map<String, String> values = new CSVReaderHeaderAware(new FileReader("yourfile.csv")).readMap();
Upgrading Versions
4.x to 5.x Highlights
- Support for Java 8 Time API and different conversion locales.
- New
FuzzyMappingStrategy for reading into beans.
- Introduction of
CsvRecurse for splitting mappings.
- Deprecated components and breaking changes for better performance.
3.x to 4.x Highlights
- Multi-threaded bean code for faster reading and writing.
- Locale and custom converter support.
- Major removal of deprecated methods and enforced Java 7.
Core Concepts
Configuration
- Customize separators, quote characters, and escape characters for parsers and writers.
Error Handling
- Structured exception handling with
CsvExceptionHandler allowing throwing, queuing, or ignoring exceptions.
- Exceptions can be captured and processed later.
Annotations
- Provides various annotations like
CsvBindByName, CsvBindByPosition, CsvDate, CsvNumber for mapping CSV data to bean fields.
Reading
Parsing
CSVParser and RFC4180Parser are available for parsing CSV files.
- Reading can be done into arrays of strings or directly into beans for easier data manipulation.
Annotations and Beans
- Beans can be annotated to map CSV columns to bean fields using header names or column positions.
- Support for enumerations, dates, numbers, collections, and custom converters.
Writing
From Arrays or Beans
- Write CSV files from arrays of strings or beans.
StatefulBeanToCsv facilitates writing beans to CSV.
- Option to change write order using
Comparator.
- Directly write SQL
ResultSet to CSV.
Validators and Processors
Validators
- Allow creation of custom rules for data validation.
LineValidator, RowValidator, and StringValidator can be used for different validation scopes.
Processors
- Modify data before assignment using
RowProcessor and StringProcessor.
Nuts and Bolts
Flow of Data
- Detailed explanation of how data flows through OpenCSV from reading to writing.
Mapping Strategies
- Different strategies for mapping CSV columns to bean fields including header name, column position, and fuzzy mapping.
This guide provides a thorough understanding of OpenCSV's capabilities and how to utilize them effectively in Java applications.