Java Code Examples for org.apache.commons.csv.CSVFormat#print()

The following examples show how to use org.apache.commons.csv.CSVFormat#print() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonsCsvWriter.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
@Override
public void open(final Serializable checkpoint) throws Exception {
    final CsvWriterMapper defaultMapper = mapping != null ? new DefaultMapper(Thread.currentThread().getContextClassLoader().loadClass(mapping)) : null;
    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);
}
 
Example 2
Source File: CsvLogEventLayout.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public String toSerializable(final LogEvent event) {
    final StringBuilder buffer = getStringBuilder();
    final CSVFormat format = getFormat();
    try {
        format.print(event.getNanoTime(), buffer, true);
        format.print(event.getTimeMillis(), buffer, false);
        format.print(event.getLevel(), buffer, false);
        format.print(event.getThreadId(), buffer, false);
        format.print(event.getThreadName(), buffer, false);
        format.print(event.getThreadPriority(), buffer, false);
        format.print(event.getMessage().getFormattedMessage(), buffer, false);
        format.print(event.getLoggerFqcn(), buffer, false);
        format.print(event.getLoggerName(), buffer, false);
        format.print(event.getMarker(), buffer, false);
        format.print(event.getThrownProxy(), buffer, false);
        format.print(event.getSource(), buffer, false);
        format.print(event.getContextData(), buffer, false);
        format.print(event.getContextStack(), buffer, false);
        format.println(buffer);
        return buffer.toString();
    } catch (final IOException e) {
        StatusLogger.getLogger().error(event.toString(), e);
        return format.getCommentMarker() + " " + e;
    }
}
 
Example 3
Source File: BeamTableUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
public static String beamRow2CsvLine(Row row, CSVFormat csvFormat) {
  StringWriter writer = new StringWriter();
  try (CSVPrinter printer = csvFormat.print(writer)) {
    for (int i = 0; i < row.getFieldCount(); i++) {
      printer.print(row.getBaseValue(i, Object.class).toString());
    }
    printer.println();
  } catch (IOException e) {
    throw new IllegalArgumentException("encodeRecord failed!", e);
  }
  return writer.toString();
}