Example usage for org.apache.commons.csv CSVFormat print

List of usage examples for org.apache.commons.csv CSVFormat print

Introduction

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

Prototype

public CSVPrinter print(final Appendable out) throws IOException 

Source Link

Document

Prints to the specified output.

Usage

From source file:com.ggvaidya.scinames.complexquery.ComplexQueryViewController.java

private void fillCSVFormat(CSVFormat format, Appendable destination, List<List<String>> data)
        throws IOException {
    try (CSVPrinter printer = format.print(destination)) {
        List<List<String>> dataAsTable = data;
        if (dataAsTable.isEmpty())
            return;

        for (int x = 0; x < dataAsTable.get(0).size(); x++) {
            for (int y = 0; y < dataAsTable.size(); y++) {
                String value = dataAsTable.get(y).get(x);
                printer.print(value);/*from w  ww  .  j  a  v a 2 s . com*/
            }
            printer.println();
        }
    }
}

From source file:org.apache.batchee.csv.CommonsCsvWriter.java

@Override
public void open(final Serializable checkpoint) throws Exception {
    final CsvWriterMapper defaultMapper = mapping != null
            ? new DefaultMapper(Thread.currentThread().getContextClassLoader().loadClass(mapping))
            : null;//  w  ww.j  a  v a  2s.c om
    mapperInstance = mapper == null
            ? (defaultMapper != null ? new BeanLocator.LocatorInstance<CsvWriterMapper>(defaultMapper, null)
                    : null)
            : BeanLocator.Finder.get(locator).newInstance(CsvWriterMapper.class, mapper);

    if ((header == null || header.isEmpty()) && Boolean.parseBoolean(writeHeaders)
            && DefaultMapper.class.isInstance(mapperInstance.getValue()) && checkpoint == null) {
        header = toListString(DefaultMapper.class.cast(mapperInstance.getValue()).getHeaders());
    }
    final CSVFormat format = newFormat();

    final File file = new File(output);
    if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
        throw new IllegalStateException("Cant create " + file);
    }
    this.transactionalWriter = new TransactionalWriter(file, encoding, checkpoint);
    this.writer = format.print(transactionalWriter);
}

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

public static String beamSqlRow2CsvLine(BeamRecord row, CSVFormat csvFormat) {
    StringWriter writer = new StringWriter();
    try (CSVPrinter printer = csvFormat.print(writer)) {
        for (int i = 0; i < row.getFieldCount(); i++) {
            printer.print(row.getFieldValue(i).toString());
        }//from w  w w  .  jav a2 s  . c  o  m
        printer.println();
    } catch (IOException e) {
        throw new IllegalArgumentException("encodeRecord failed!", e);
    }
    return writer.toString();
}

From source file:org.tomitribe.tribestream.registryng.resources.ClientResource.java

@GET // more portable way to do a download from a browser
@Path("download")
@Consumes(APPLICATION_JSON)/*ww w .j a  va 2 s  . c  o m*/
@Produces(TEXT_PLAIN)
public Response download(@QueryParam("output-type") @DefaultValue("csv") final String extension,
        @QueryParam("filename") @DefaultValue("responses") final String filename,
        @QueryParam("data") final String base64EncodedResponses,
        @Context final HttpServletRequest httpServletRequest, @Context final Providers providers) {
    final DownloadResponses downloadResponses = loadPayload(DownloadResponses.class, providers,
            base64EncodedResponses);
    final String auth = downloadResponses.getIdentity();
    security.check(auth, httpServletRequest, () -> {
    }, () -> {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    });

    final String contentType;
    final StreamingOutput builder;
    switch (extension) {
    case "csv":
        contentType = TEXT_PLAIN;
        builder = output -> {
            final CSVFormat format = CSVFormat.EXCEL.withHeader("Status", "Duration (ms)", "Error");
            final StringWriter buffer = new StringWriter();
            try (final CSVPrinter print = format.print(buffer)) {
                downloadResponses.getData().forEach(r -> {
                    try {
                        print.printRecord(r.getStatus(), r.getClientExecutionDurationMs(), r.getError());
                    } catch (final IOException e) { // quite unlikely
                        throw new IllegalStateException(e);
                    }
                });
            }
            output.write(buffer.toString().getBytes(StandardCharsets.UTF_8));
        };
        break;
    default:
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }
    return Response.status(Response.Status.OK).header("ContentType", contentType)
            .header("Content-Disposition", "attachment; filename=\"" + filename + '.' + extension + "\"")
            .entity(builder).build();
}