Example usage for org.apache.commons.csv.writer CSVWriter setWriter

List of usage examples for org.apache.commons.csv.writer CSVWriter setWriter

Introduction

In this page you can find the example usage for org.apache.commons.csv.writer CSVWriter setWriter.

Prototype

public void setWriter(Writer writer) 

Source Link

Document

Set the writer to write the CSV file to.

Usage

From source file:org.apache.camel.dataformat.csv.CsvDataFormat.java

public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws Exception {
    if (delimiter != null) {
        config.setDelimiter(delimiter.charAt(0));
    }/*from  ww  w .  j ava 2 s . c o  m*/

    OutputStreamWriter out = new OutputStreamWriter(outputStream, IOHelper.getCharsetName(exchange));
    CSVWriter csv = new CSVWriter(config);
    csv.setWriter(out);

    try {
        List<?> list = ExchangeHelper.convertToType(exchange, List.class, object);
        if (list != null) {
            for (Object child : list) {
                Map<?, ?> row = ExchangeHelper.convertToMandatoryType(exchange, Map.class, child);
                doMarshalRecord(exchange, row, out, csv);
            }
        } else {
            Map<?, ?> row = ExchangeHelper.convertToMandatoryType(exchange, Map.class, object);
            doMarshalRecord(exchange, row, out, csv);
        }
    } finally {
        IOHelper.close(out);
    }
}