/** * Formats the specified values. * * @param values * the values to format * @return the formatted values */ public String format(final Object... values) { final StringWriter out = new StringWriter(); try { new CSVPrinter(out, this).printRecord(values); return out.toString().trim(); } catch (final IOException e) { // should not happen because a StringWriter does not do IO. throw new IllegalStateException(e); } }
/** * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine * characters will be escaped. * * @param values * values to output. * @throws IOException * If an I/O error occurs */ public void printRecord(final Object... values) throws IOException { for (final Object value : values) { print(value); } println(); }
private void print(final Object object, final CharSequence value, final int offset, final int len) throws IOException { if (!newRecord) { out.append(format.getDelimiter()); } if (format.isQuoting()) { // the original object is needed so can check for Number printAndQuote(object, value, offset, len); } else if (format.isEscaping()) { printAndEscape(value, offset, len); } else { out.append(value, offset, offset + len); } newRecord = false; }
/** * Prints all the objects in the given array. * * @param values * the values to print. * @throws IOException * If an I/O error occurs */ public void printRecords(final Object[] values) throws IOException { for (final Object value : values) { if (value instanceof Object[]) { this.printRecord((Object[]) value); } else if (value instanceof Iterable) { this.printRecord((Iterable<?>) value); } else { this.printRecord(value); } } }
/** * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed. * * @param value * value to be output. * @throws IOException * If an I/O error occurs */ public void print(final Object value) throws IOException { // null values are considered empty String strValue; if (value == null) { final String nullString = format.getNullString(); strValue = nullString == null ? Constants.EMPTY : nullString; } else { strValue = value.toString(); } this.print(value, strValue, 0, strValue.length()); }
case NONE: printAndEscape(value, offset, len); return; case MINIMAL:
/** * Prints all the objects in the given collection. * * @param values * the values to print. * @throws IOException * If an I/O error occurs */ public void printRecords(final Iterable<?> values) throws IOException { for (final Object value : values) { if (value instanceof Object[]) { this.printRecord((Object[]) value); } else if (value instanceof Iterable) { this.printRecord((Iterable<?>) value); } else { this.printRecord(value); } } }
/** * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine * characters will be escaped. * * @param values * values to output. * @throws IOException * If an I/O error occurs */ public void printRecord(final Iterable<?> values) throws IOException { for (final Object value : values) { print(value); } println(); }
public static void write(Writer writer,List<Object[]> rows) throws NestedIOException { CSVPrinter printer = null; try{ printer = new CSVPrinter(writer, CSVFormat.DEFAULT); for(Object[] row : rows){ printer.printRecord(row); } }catch(IOException e){ throw Exceptions.wrap(e); }finally{ IO.close(writer); IO.close(printer); } }
/** * Prints all the objects in the given JDBC result set. * * @param resultSet result set * the values to print. * @throws IOException * If an I/O error occurs * @throws SQLException if a database access error occurs */ public void printRecords(final ResultSet resultSet) throws SQLException, IOException { final int columnCount = resultSet.getMetaData().getColumnCount(); while (resultSet.next()) { for (int i = 1; i <= columnCount; i++) { print(resultSet.getString(i)); } println(); } }