[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2. Using SimpleCSV

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.1 Downloading Jar

To get started with SimpleCSV, you will need to download the jar file. The SimpleCSV release page is the default repository but the jars are also available from the central maven repository.

The code works with Java 6 or later.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2 Using With Maven

To use SimpleCSV with maven, include the following dependency in your ‘pom.xml’ file:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3 CsvColumn Annotation

The @CsvColumn annotation is used to mark the fields in your entity that you want to write to and read from CSV files as a column. It also allows you to customize the output format and other details for the particular field instance. The following fields from the annotation can be used:


This allows you to override and set a column name for the field. By default it will use the field name. This column name is used when you are generating and validating the header line.


Set to true if a value in the column is required. This means that it cannot be empty when it is being read in and a parse error or exception will be generated.


Set to true if you want the column read from the line to be trimmed (using String.trim()) before it is converted to Java. This may not be applicable to all field types.


Sets the format for this column. Not all types use the format specifier. Take a look at the particular converter class javadocs for more particulars. The default format tends to be the toString() of the type, and (for example) the java.text.DecimalFormat class is used to override for numbers.


Optional flags for the converter which adjust the output. The flags that are used depend on the converter. See the converter Javadocs for more information. These need to be constants that are added together. For example,

@CsvColumn(converterFlags = XxxConverter.FLAG1 + XxxConverter.FLAG2)
private Xxx dollarAmount;

Sets the converter to use to convert this column if you don’t want to use the default appropriate internal class. This will construct and instance of the class for this particular field. If you want to use a singleton then you should register the type using CsvProcessor.registerConverter(...). This converter class must have a public no-arg constructor.


Set this to a default string for the column. If the column is empty when read, the value will be used instead. Default is the empty string.


Set to false if a column is optional and can be skipped in the input altogether. If this is false then the column doesn’t have to be in the header or the lines at all. Default is true.

WARNING: If you are using optional ordering, the same CsvProcessor cannot be used with multiple files at the same time since the column lists can be dynamic depending on the input file being read.


Used to set the order of the columns by setting the column-name that this column comes after. If this is not specified then the order in which the fields and methods are discovered in the classes will determine their order in the CSV file. If two fields say they come after the same field then you will get an undefined order. If there is an loop in the after columns then an exception will be thrown.

Here’s some examples of how to use the @CsvColumn annotation.

Override the column name:

@CsvColumn(columnName = "Account Number")
private long number;

Change the column input/output format. This will display the amount as $1,231.00 or ($2,000,000.28).

@CsvColumn(columnName = "Amount", format = "$###,##0.00;($###,##0.00)")
private double amount;

Specifying a custom converter class for an object that you have defined.

@CsvColumn(columnName = "Gender", converterClass = GenderConverter.class)
private Gender gender;

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Gray Watson on February 15, 2019 using texi2html 1.82.