Firefox logo

Ant logo

Netbeans logo

Coding your own writer

Writers permits to write data rendered from csv file where you want. csvtosql come with three default implementations of writer:

- StandardOutputWrite, write statements to standard output using System.out.println()
- SqlInsertWriter, write statements to a file specified in configuration.
- JdbcWriter, write statements to a jdbc resource, with options specified in configuration.

You can write your own writer extending class net.sf.csv2sql.writers.AbstractWriter, and this tutorial explain how to extend this class. We using for example SqlFileWriter class.

This is the AbstractWriter class:
public abstract class AbstractWriter {

    private Properties properties;

    public abstract void write(ArrayList statements);

    protected Properties getWriterProperties() {

    public void configure(Properties properties) { = properties;


You simply need to override the write() method, the ArrayList passed to this method contain the statemens already rendered.

This is the extended SqlFileWriter class:
public class SqlFileWriter extends AbstractWriter {

    public void write(ArrayList statements) {
        try {
            File out = new File(getWriterProperties().getProperty("filename"));
            BufferedWriter output = new BufferedWriter(new FileWriter(out));
            Iterator it = statements.iterator();
            while (it.hasNext()) {
                output.write( (String) +"\n" );
        } catch (Exception e) {


The getWriterProperties() method return a java.util.Properties object containing all option for this writer in configuration, so getWriterProperties().getProperty("filename") method return the value for the option names "filename".

copyright 2004 Davide Consonni (