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

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

Introduction

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

Prototype

public void println() throws IOException 

Source Link

Document

Outputs the record separator.

Usage

From source file:norbert.mynemo.dataimport.scraping.CkRating.java

/**
 * Prints the internal data to the given printer. The printer is usually created by calling the
 * {@link #createPrinter(String)} method.
 *//*from w  w  w. j av  a2  s . c o  m*/
public void printOn(CSVPrinter printer) throws IOException {
    checkNotNull(printer);

    // the write order depends on the order of the headers in the csv format
    printer.print(user);
    printer.print(movie);
    printer.print(value);
    // end of the record
    printer.println();
}

From source file:org.alfresco.repo.web.scripts.person.UserCSVUploadGet.java

@Override
protected void populateBody(Object resource, CSVPrinter csv, List<QName> properties) throws IOException {
    // Just a blank line is needed
    csv.println();
}

From source file:org.apache.beam.sdk.extensions.sql.impl.schema.text.BeamTextCSVTableTest.java

/**
 * Helper that writes the given lines (adding a newline in between) to a stream, then closes the
 * stream./*from  w  w  w  . j a  va2 s  .co m*/
 */
private static void writeToStreamAndClose(List<Object[]> rows, OutputStream outputStream) {
    try (PrintStream writer = new PrintStream(outputStream)) {
        CSVPrinter printer = CSVFormat.DEFAULT.print(writer);
        for (Object[] row : rows) {
            for (Object field : row) {
                printer.print(field);
            }
            printer.println();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.cast.cwm.admin.CSVDownload.java

/**
 * creates a new resource response based on the request attributes
 *
 * @param attributes current request attributes from client
 * @return resource response for answering request
 *//*from   www  .j  a  v a2  s  .  c  o m*/
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    ResourceResponse rr = new ResourceResponse();
    rr.disableCaching();
    rr.setFileName("log.csv");
    rr.setContentDisposition(ContentDisposition.ATTACHMENT);
    rr.setContentType("text/csv");

    if (rr.dataNeedsToBeWritten(attributes)) {
        rr.setWriteCallback(new WriteCallback() {
            @Override
            public void writeData(Attributes attributes) {
                Response response = attributes.getResponse();

                try {
                    CSVPrinter writer = new CSVPrinter(
                            new OutputStreamWriter(response.getOutputStream(), "UTF-8"), CSVFormat.EXCEL);

                    // Write header row
                    for (IDataColumn<E> col : columns) {
                        writer.print(col.getHeaderString());
                    }
                    writer.println();

                    // Write documentation row, if requested
                    if (includeDocumentationRow) {
                        for (IDataColumn<E> col : columns) {
                            if (col instanceof IDocumentedColumn
                                    && ((IDocumentedColumn) col).getDocumentationModel() != null) {
                                writer.print(((IDocumentedColumn) col).getDocumentationModel().getObject());
                            } else {
                                writer.print("");
                            }
                        }
                        writer.println();
                    }

                    // Write Data
                    Iterator<? extends E> it = iteratorProvider.getIterator();
                    while (it.hasNext()) {
                        E e = it.next();
                        for (IDataColumn<E> col : columns) {
                            String columnValue = col.getItemString(new Model<E>(e));
                            if (columnValue == null) {
                                log.warn("Got a null value for {} of item {}", col.getHeaderString(), e);
                                columnValue = "null";
                            }
                            // Clean up text -- CSV file cannot have newlines in it
                            writer.print(columnValue.replaceAll("[\r\n]", " "));
                        }
                        writer.println();
                    }
                    writer.close();

                } catch (UnsupportedEncodingException e) {
                    throw new StringValueConversionException("UTF-8 translation not supported?!", e);
                } catch (IOException e) {
                    throw new WicketRuntimeException("Couldn't write to resource", e);
                }
            }
        });
    }

    return rr;
}

From source file:org.commonvox.hbase_column_manager.ColumnQualifierReport.java

void printReport(CSVPrinter csvPrinter, Set<SchemaEntity> schemaEntities) throws IOException {
    for (SchemaEntity entity : schemaEntities) {
        switch (entity.getSchemaEntityType()) {
        case NAMESPACE:
            namespaceReportString = entity.getNameAsString();
            tableReportString = "";
            colFamilyReportString = "";
            colQualifierReportString = "";
            colMaxLengthReportString = "";
            printReport(csvPrinter, entity.getChildren());
            break;
        case TABLE:
            tableReportString = entity.getNameAsString();
            printReport(csvPrinter, entity.getChildren());
            break;
        case COLUMN_FAMILY:
            colFamilyReportString = entity.getNameAsString();
            if (this.sourceColFamily != null
                    && !colFamilyReportString.equals(Bytes.toString(sourceColFamily))) {
                continue;
            }/*from   ww  w  .  j  av  a 2 s  .c  o  m*/
            printReport(csvPrinter, entity.getChildren());
            break;
        case COLUMN_AUDITOR:
            colQualifierReportString = entity.getNameAsString();
            colMaxLengthReportString = entity.getValue(ColumnAuditor.MAX_VALUE_LENGTH_KEY);
            csvPrinter.print(namespaceReportString);
            csvPrinter.print(tableReportString);
            csvPrinter.print(colFamilyReportString);
            csvPrinter.print(colQualifierReportString);
            csvPrinter.print(colMaxLengthReportString);
            csvPrinter.println();
            recordsWrittenToReport = true;
        }
    }
}

From source file:org.openmrs.projectbuendia.servlet.DataExportServlet.java

private void writeHeaders(CSVPrinter printer, FixedSortedConceptIndexer indexer) throws IOException {
    for (String fixedHeader : FIXED_HEADERS) {
        printer.print(fixedHeader);/*from w  w w  .j ava 2 s.  c o  m*/
    }
    for (int i = 0; i < indexer.size(); i++) {
        // For each observation there are three columns: one for the English
        // name, one for the OpenMRS ID, and one for the UUID of the concept.
        assert COLUMNS_PER_OBS == 3;
        Concept concept = indexer.getConcept(i);
        printer.print(NAMER.getClientName(concept));
        printer.print(concept.getId());
        printer.print(concept.getUuid());
    }
    printer.println();
}

From source file:org.opennms.netmgt.integrations.R.RScriptExecutor.java

/**
 * Convert the table to a CSV string./*  w  w  w . j a va  2 s  .  com*/
 */
protected static StringBuilder toCsv(final RowSortedTable<Long, String, Double> table) throws IOException {
    final String columnNames[] = table.columnKeySet().toArray(new String[] {});

    final StringBuilder sb = new StringBuilder();
    final CSVPrinter printer = CSVFormat.RFC4180.withHeader(columnNames).print(sb);

    for (long rowIndex : table.rowKeySet()) {
        for (String columnName : columnNames) {
            Double value = table.get(rowIndex, columnName);
            if (value == null) {
                value = Double.NaN;
            }
            printer.print(value);
        }
        printer.println();
    }

    return sb;
}

From source file:org.thegalactic.context.io.ContextSerializerCsv.java

/**
 * Write a context to a csv file.//from ww w  . ja  v a  2s  .  co  m
 *
 * The following format is respected:
 *
 * The first line contains the attribute names, the other lines contains the
 * observations identifier followed by boolean values
 *
 * ~~~
 * "",a,b,c,d,e
 * 1,1,0,1,0,0
 * 2,1,1,0,0,0
 * 3,0,1,0,1,1
 * 4,0,0,1,0,1
 * ~~~
 *
 * @param context a context to write
 * @param file    a file
 *
 * @throws IOException When an IOException occurs
 */
public void write(Context context, BufferedWriter file) throws IOException {
    CSVPrinter printer = new CSVPrinter(file, CSVFormat.RFC4180);

    // Get the observations and the attributes
    TreeSet<Comparable> observations = context.getObservations();
    TreeSet<Comparable> attributes = context.getAttributes();

    // Prepare the attribute line
    printer.print("");

    for (Comparable attribute : attributes) {
        // Write each attribute
        printer.print(attribute);
    }

    printer.println();

    for (Comparable observation : observations) {
        // Write the observation
        printer.print(observation);

        // Write the extent/intents
        for (Comparable attribute : attributes) {
            if (context.getIntent(observation).contains(attribute)) {
                printer.print(1);
            } else {
                printer.print(0);
            }
        }

        printer.println();
    }

    printer.close();
}