@Override public void flush() throws IOException { printer.flush(); }
private static String doJoinDelimited(Iterable<?> elements, CSVFormat format) { StringWriter out = new StringWriter(); try (CSVPrinter printer = new CSVPrinter(out, format)) { for (Object element : elements) { printer.print(element); } printer.flush(); } catch (IOException e) { throw new IllegalStateException(e); } return out.toString(); }
@Override public synchronized void flush() throws IOException { if (csvPrinter != null) { csvPrinter.flush(); } }
@Override public synchronized void close() throws IOException { if (csvPrinter != null) { csvPrinter.flush(); csvPrinter.close(); isClosed = true; } }
/** * Closes the underlying stream with an optional flush first. * @param flush whether to flush before the actual close. * * @throws IOException * If an I/O error occurs * @since 1.6 */ public void close(final boolean flush) throws IOException { if (flush || format.getAutoFlush()) { flush(); } if (out instanceof Closeable) { ((Closeable) out).close(); } }
@Override public void write(Appendable out, List<Table> tables) throws IOException { try (CSVPrinter printer = new CSVPrinter(out, format)) { for (Table table : tables) { for (List<RectangularTextContainer> row : table.getRows()) { List<String> cells = new ArrayList<>(row.size()); for (RectangularTextContainer<?> tc : row) cells.add(tc.getText()); printer.printRecord(cells); } } printer.flush(); } }
public static byte[] exportAsCSV(QueryResult result) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final CSVPrinter csvPrinter; try { final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC); csvPrinter = new CSVPrinter(new PrintWriter(out), format); csvPrinter.printRecord(result.getMetadata().stream().map(SchemaField::getName) .collect(Collectors.toList())); csvPrinter.printRecords(Iterables.transform(result.getResult(), input -> Iterables.transform(input, input1 -> { if (input1 instanceof List || input1 instanceof Map) { return JsonHelper.encode(input1); } if (input1 instanceof byte[]) { return DatatypeConverter.printBase64Binary((byte[]) input1); } return input1; }))); csvPrinter.flush(); } catch (IOException e) { throw Throwables.propagate(e); } return out.toByteArray(); }
/** * Flushes the printer instance. * * @throws IOException if flushing failed */ @Override public void flush() throws IOException { this.printer.flush(); }
private void doOneRandom(final CSVFormat format) throws Exception { final Random r = new Random(); final int nLines = r.nextInt(4) + 1; final int nCol = r.nextInt(3) + 1; // nLines=1;nCol=2; final String[][] lines = generateLines(nLines, nCol); final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { for (int i = 0; i < nLines; i++) { // for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j])); printer.printRecord((Object[]) lines[i]); } printer.flush(); } final String result = sw.toString(); // System.out.println("### :" + printable(result)); try (final CSVParser parser = CSVParser.parse(result, format)) { final List<CSVRecord> parseResult = parser.getRecords(); final String[][] expected = lines.clone(); for (int i = 0; i < expected.length; i++) { expected[i] = expectNulls(expected[i], format); } Utils.compare("Printer output :" + printable(result), expected, parseResult); } }
@Override protected void finish() throws IOException { csvPrinter.flush(); csvPrinter.close(); }
@Override public void writeEnd() { try { if (csvPrinter != null) { csvPrinter.flush(); } } catch (IOException exception) { throw new OutputException(exception); } }
private static String doJoinDelimited(Iterable<?> elements, CSVFormat format) { StringWriter out = new StringWriter(); try (CSVPrinter printer = new CSVPrinter(out, format)) { for (Object element : elements) { printer.print(element); } printer.flush(); } catch (IOException e) { throw new IllegalStateException(e); } return out.toString(); }
final FileWriter sw = new FileWriter("myfile.csv"); final CSVPrinter printer = new CSVPrinter(sw, format); for (int i = 0; i < nLines; i++) { printer.printRecord(lines[i]); } printer.flush(); printer.close();
final FileWriter sw = new FileWriter("myfile.csv"); final CSVPrinter printer = new CSVPrinter(sw, format); for (int i = 0; i < nLines; i++) { printer.printRecord(lines[i]); } printer.flush(); printer.close()
private File writeCsvFile(DataTable dataTable) throws TaskException { File tmpFile = createTemporaryBuffer(".csv"); LOG.debug("Created output temporary buffer {}, writing csv data", tmpFile); long start = System.currentTimeMillis(); List<List<String>> data = dataTable.getData(); try (CSVPrinter csvPrinter = new CSVPrinter(new BufferedWriter(new FileWriter(tmpFile)), CSVFormat.DEFAULT)) { for (int r = 0; r < data.size(); r++) { List<String> dataRow = data.get(r); // LOG.debug("Writing row " + r + " of " + dataRow.size() + " values"); csvPrinter.printRecord(dataRow); } csvPrinter.flush(); LOG.debug("Done writing data to csv file, took {} seconds", (System.currentTimeMillis() - start) / 1000); } catch (IOException ioe) { throw new TaskException("Could not save .csv file", ioe); } return tmpFile; }
@Override public void collectionProcessComplete() throws AnalysisEngineProcessException { try { csvPrinter = new CSVPrinter(new FileWriter(outputFile), CSVFormat.DEFAULT); for (List<Object> row : exporter.getResult()) { csvPrinter.printRecord(row); } csvPrinter.flush(); } catch (IOException e) { e.printStackTrace(); throw new AnalysisEngineProcessException(e); } IOUtils.closeQuietly(csvPrinter); }
protected abstract void write(T entity, CSVPrinter printer) throws IOException;
@Override public void writeItems(final List<Object> list) throws Exception { for (final Object o : list) { if (CSVRecord.class.isInstance(o)) { writer.printRecord(CSVRecord.class.cast(o)); } else if (Iterable.class.isInstance(o)) { writer.printRecord(Iterable.class.cast(o)); } else if (mapperInstance != null) { writer.printRecord(mapperInstance.getValue().toRecord(o)); } else { throw new IllegalStateException("No way to write " + o + ". Does it implement Iterable<String> or did you set up a mapper?"); } } writer.flush(); }
public String toCsvLine( ) throws IOException { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final CSVPrinter csvPrinter = JavaHelper.makeCsvPrinter( byteArrayOutputStream ); final List<String> dataRow = new ArrayList<>(); dataRow.add( JavaHelper.toIsoDate( getDate() ) ); dataRow.add( getLevel().name() ); dataRow.add( getSource() ); dataRow.add( getLabel() ); dataRow.add( getActor() ); dataRow.add( getTopic() ); dataRow.add( getMessage() ); dataRow.add( getThrowable() == null ? "" : JavaHelper.readHostileExceptionMessage( getThrowable() ) ); csvPrinter.printRecord( dataRow ); csvPrinter.flush(); return byteArrayOutputStream.toString( PwmConstants.DEFAULT_CHARSET.name() ); }
@Override public void process(JCas jcas) throws AnalysisEngineProcessException { DocumentMetaData dmd = DocumentMetaData.get(jcas); try { OutputStreamWriter os = new OutputStreamWriter( getOutputStream(dmd.getDocumentId(), "." + csvVariantName + ".csv")); CSVPrinter p = new CSVPrinter(os, csvFormat); csvVariant.header(p); csvVariant.convert(jcas, p); p.flush(); os.flush(); IOUtils.closeQuietly(os); } catch (IOException e) { throw new AnalysisEngineProcessException(e); } }