Example usage for org.apache.commons.csv CSVPrinter CSVPrinter

List of usage examples for org.apache.commons.csv CSVPrinter CSVPrinter

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVPrinter CSVPrinter.

Prototype

public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException 

Source Link

Document

Creates a printer that will print values to the given stream following the CSVFormat.

Usage

From source file:com.ibm.watson.app.common.tagEvent.TagRecord.java

public String asCsv() {
    prepareForFormat();//from   w ww  . j  av a 2s  .  c om
    StringBuilder sb = new StringBuilder();
    try {
        CSVPrinter printer = new CSVPrinter(sb, CSVFormat.DEFAULT);
        printer.printRecord(this);
        printer.close();
    } catch (IOException e) {

    }
    return sb.toString();

}

From source file:biz.webgate.dominoext.poi.component.kernel.simpleviewexport.CSVExportProcessor.java

public void process2HTTP(ExportModel expModel, UISimpleViewExport uis, HttpServletResponse hsr,
        DateTimeHelper dth) {/*from   w ww. j  a  va  2s  . c o m*/
    try {
        ByteArrayOutputStream csvBAOS = new ByteArrayOutputStream();
        OutputStreamWriter csvWriter = new OutputStreamWriter(csvBAOS);
        CSVPrinter csvPrinter = new CSVPrinter(csvWriter, CSVFormat.DEFAULT);

        // BUILDING HEADER
        if (uis.isIncludeHeader()) {
            for (ExportColumn expColumn : expModel.getColumns()) {
                csvPrinter.print(expColumn.getColumnName());
            }
            csvPrinter.println();
        }
        // Processing Values
        for (ExportDataRow expRow : expModel.getRows()) {
            for (ExportColumn expColumn : expModel.getColumns()) {
                csvPrinter.print(convertValue(expRow.getValue(expColumn.getPosition()), expColumn, dth));
            }
            csvPrinter.println();
        }
        csvPrinter.flush();

        hsr.setContentType("text/csv");
        hsr.setHeader("Cache-Control", "no-cache");
        hsr.setDateHeader("Expires", -1);
        hsr.setContentLength(csvBAOS.size());
        hsr.addHeader("Content-disposition", "inline; filename=\"" + uis.getDownloadFileName() + "\"");
        OutputStream os = hsr.getOutputStream();
        csvBAOS.writeTo(os);
        os.close();
    } catch (Exception e) {
        ErrorPageBuilder.getInstance().processError(hsr, "Error during SVE-Generation (CSV Export)", e);
    }
}

From source file:ca.uqac.florentinth.speakerauthentication.Dataset.CSVWriter.java

private void openCSVFile(File location) throws IOException {
    fileWriter = new FileWriter(location, true);
    csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withRecordSeparator(LINE_SEPARATOR));
}

From source file:com.apkcategorychecker.writer.WriterCSV.java

@Override
public void Write(AnalyzerResult result, String _csvPath, int _counter) {

    try {//from   w ww  .j a v a  2 s  .  com

        ArrayList<AnalyzerResult> resultList = new ArrayList<AnalyzerResult>();
        resultList.add(result);

        /*--Create the CSVFormat object--*/

        CSVFormat format = CSVFormat.DEFAULT.withHeader();

        /*--Writing in a CSV file--*/

        FileWriter _out = new FileWriter(_csvPath, true);
        CSVPrinter printer;
        printer = new CSVPrinter(_out, format.withDelimiter('#'));

        /*--Retrieve APKResult and Write in file--*/

        Iterator<AnalyzerResult> it = resultList.iterator();
        while (it.hasNext()) {
            AnalyzerResult _resultElement = it.next();
            List<String> resultData = new ArrayList<>();
            resultData.add(String.valueOf(_counter));
            resultData.add(_resultElement.get_APKName());
            resultData.add(_resultElement.get_APKPath());
            resultData.add(_resultElement.get_Package());
            resultData.add(_resultElement.get_APKMainFramework());
            resultData.add(_resultElement.get_APKBaseFramework());
            resultData.add(String.valueOf(_resultElement.get_html()));
            resultData.add(String.valueOf(_resultElement.get_js()));
            resultData.add(String.valueOf(_resultElement.get_css()));
            resultData.add(_resultElement.get_debuggable());
            resultData.add(_resultElement.get_permissions());
            resultData.add(_resultElement.get_minSdkVersion());
            resultData.add(_resultElement.get_maxSdkVersion());
            resultData.add(_resultElement.get_targetSdkVersion());
            resultData.add(_resultElement.get_fileSize());
            resultData.add(String.valueOf(_resultElement.get_startAnalysis()));
            resultData.add(String.valueOf(_resultElement.get_durationAnalysis()));
            resultData.add(String.valueOf(_resultElement.get_decodeSuccess()));
            printer.printRecord(resultData);
        }

        /*--Close the printer--*/
        printer.close();
        System.out.println("Record added to CSV file");
        this.removeBlankLines(_csvPath);

    } catch (IOException ex) {
        Logger.getLogger(WriterCSV.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.github.jferard.pgloaderutils.loader.CSVCleanerFileReader.java

public CSVCleanerFileReader(CSVParser parser, CSVRecordCleaner recordCleaner) throws IOException {
    this.recordCleaner = recordCleaner;
    PipedWriter pipedWriter = new PipedWriter();
    this.modifiedStreamReader = new PipedReader(pipedWriter, BUFFER_SIZE);

    this.parser = parser;
    this.printer = new CSVPrinter(pipedWriter, CSVFormat.RFC4180);
    this.logger = Logger.getLogger("Cleaner");
}

From source file:be.roots.taconic.pricingguide.service.ReportServiceImpl.java

@Override
public void report(Contact contact, List<String> modelIds) throws IOException {

    final CSVFormat csvFileFormat = CSVFormat.DEFAULT;
    final FileWriter fileWriter = new FileWriter(getFileNameFor(new DateTime()), true);
    final CSVPrinter csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

    final List<String> record = new ArrayList<>();

    record.add(new DateTime().toString(DefaultUtil.FORMAT_TIMESTAMP));

    record.add(contact.getHsId());// w  ww. ja va2s. com
    record.add(contact.getSalutation());
    record.add(contact.getFirstName());
    record.add(contact.getLastName());
    record.add(contact.getEmail());
    record.add(contact.getCompany());
    record.add(contact.getCountry());
    record.add(contact.getPersona());
    if (contact.getJobRole() != null) {
        record.add(contact.getJobRole().getDescription());
    } else {
        record.add(null);
    }
    if (contact.getCurrency() != null) {
        record.add(contact.getCurrency().name());
        record.add(contact.getCurrency().getDescription());
    } else {
        record.add(null);
        record.add(null);
    }

    record.addAll(modelIds);

    csvFilePrinter.printRecord(record);
    csvFilePrinter.close();

}

From source file:edu.harvard.hms.dbmi.bd2k.irct.ws.rs.resultconverter.CSVTabularDataConverter.java

@Override
public StreamingOutput createStream(final Result result) {
    StreamingOutput stream = new StreamingOutput() {
        @Override/*from ww w  .  j a v a 2s  .  co m*/
        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            ResultSet rs = null;
            CSVPrinter printer = null;
            try {
                rs = (ResultSet) result.getData();
                rs.load(result.getResultSetLocation());

                printer = new CSVPrinter(new OutputStreamWriter(outputStream), CSVFormat.DEFAULT);

                String[] columnHeaders = new String[rs.getColumnSize()];
                for (int i = 0; i < rs.getColumnSize(); i++) {
                    columnHeaders[i] = rs.getColumn(i).getName();
                }
                printer.printRecord((Object[]) columnHeaders);

                rs.beforeFirst();
                while (rs.next()) {
                    String[] row = new String[rs.getColumnSize()];
                    for (int i = 0; i < rs.getColumnSize(); i++) {
                        row[i] = rs.getString(i);
                    }
                    printer.printRecord((Object[]) row);
                }
                printer.flush();

            } catch (ResultSetException | PersistableException e) {
                log.info("Error creating CSV Stream: " + e.getMessage());
            } finally {
                if (printer != null) {
                    printer.close();
                }

                if (rs != null && !rs.isClosed()) {
                    try {
                        rs.close();
                    } catch (ResultSetException e) {
                        e.printStackTrace();
                    }
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            }

        }
    };
    return stream;
}

From source file:com.leadscope.commanda.maps.CSVLineMap.java

@Override
public String apply(List<String> element) {
    StringWriter sw = new StringWriter();
    try {//from   www .j ava 2  s.c o  m
        CSVPrinter printer = new CSVPrinter(sw, noBreakFormat);
        printer.printRecord(element);
        printer.close();
        return sw.toString();
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.streamsets.pipeline.lib.generator.delimited.DelimitedCharDataGenerator.java

public DelimitedCharDataGenerator(Writer writer, CSVFormat format, CsvHeader header, String headerKey,
        String valueKey, String replaceNewLines) throws IOException {
    format = format.withHeader((String[]) null);
    this.format = format;
    this.headerKey = headerKey;
    this.valueKey = valueKey;
    printer = new CSVPrinter(writer, format);
    this.header = header;
    firstRecord = true;/*from   ww  w  .  j  av  a  2 s .co m*/
    this.replaceNewLines = replaceNewLines;
}

From source file:ijfx.ui.datadisplay.table.SaveCSV.java

@Override
public void run() {
    if (displayService.getActiveDisplay() instanceof TableDisplay) {

        try {/*from w ww  .j a v a2  s .com*/
            csvFileFormat = csvFileFormat.withDelimiter(delimiter.charAt(0));
            tableDisplay = (TableDisplay) displayService.getActiveDisplay();
            fileWriter = new FileWriter(file.getAbsolutePath());
            csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
            String[] headers = header.split(delimiter);

            if (headers.length != tableDisplay.get(0).size() && !header.equals("")) {
                String message = "The number of header doesn't match with the number of columns";
                uIService.showDialog(message, DialogPrompt.MessageType.ERROR_MESSAGE);
                return;
            } else {
                csvFilePrinter.printRecord(Arrays.stream(headers).collect(Collectors.toList()));
            }

            for (int i = 0; i < tableDisplay.get(0).get(0).size(); i++) {
                List line = new ArrayList();
                for (int j = 0; j < tableDisplay.get(0).size(); j++) {
                    line.add(tableDisplay.get(0).get(j, i).toString());
                }
                csvFilePrinter.printRecord(line);
            }

            fileWriter.flush();
            fileWriter.close();
            csvFilePrinter.close();

        } catch (IOException ex) {
            Logger.getLogger(SaveCSV.class.getName()).log(Level.SEVERE, null, ex);

        }

    }
}