printRecords(Arrays.asList(values));
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(); }
@Test public void testExcelPrintAllArrayOfLists() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) { printer.printRecords( (Object[]) new List[] { Arrays.asList("r1c1", "r1c2"), Arrays.asList("r2c1", "r2c2") }); assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString()); } }
@Test public void testExcelPrintAllIterableOfLists() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) { printer.printRecords( Arrays.asList(new List[] { Arrays.asList("r1c1", "r1c2"), Arrays.asList("r2c1", "r2c2") })); assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString()); } }
@Test public void testExcelPrintAllIterableOfArrays() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) { printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } })); assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString()); } }
@Test public void testExcelPrintAllArrayOfArrays() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL)) { printer.printRecords((Object[]) new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }); assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString()); } }
@Test public void testPrintRecordsWithObjectArray() throws IOException { final CharArrayWriter charArrayWriter = new CharArrayWriter(0); try (CSVPrinter csvPrinter = CSVFormat.INFORMIX_UNLOAD.print(charArrayWriter)) { final HashSet<BatchUpdateException> hashSet = new HashSet<>(); final Object[] objectArray = new Object[6]; objectArray[3] = hashSet; csvPrinter.printRecords(objectArray); } assertEquals(6, charArrayWriter.size()); assertEquals("\n\n\n\n\n\n", charArrayWriter.toString()); }
@Test public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); try (final Connection connection = geH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { printer.printRecords(stmt.executeQuery("select ID, NAME from TEST")); } } assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString()); }
@Test public void testPrintRecordsWithEmptyVector() throws IOException { try (CSVPrinter csvPrinter = CSVFormat.POSTGRESQL_TEXT.printer()) { final Vector<CSVFormatTest.EmptyEnum> vector = new Vector<>(); final int expectedCapacity = 23; vector.setSize(expectedCapacity); csvPrinter.printRecords(vector); assertEquals(expectedCapacity, vector.capacity()); } }
@Test public void testJdbcPrinterWithResultSetMetaData() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); Class.forName("org.h2.Driver"); try (final Connection connection = geH2Connection()) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST"); final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet.getMetaData()).print(sw)) { printer.printRecords(resultSet); assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString()); } } }
@Test public void testJdbcPrinterWithResultSet() throws IOException, ClassNotFoundException, SQLException { final StringWriter sw = new StringWriter(); Class.forName("org.h2.Driver"); try (final Connection connection = geH2Connection();) { setUpTable(connection); try (final Statement stmt = connection.createStatement(); final ResultSet resultSet = stmt.executeQuery("select ID, NAME from TEST"); final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet).print(sw)) { printer.printRecords(resultSet); } } assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString()); }
@Test public void testPrintRecordsWithResultSetOneRow() throws IOException, SQLException { try (CSVPrinter csvPrinter = CSVFormat.MYSQL.printer()) { final Value[] valueArray = new Value[0]; final ValueArray valueArrayTwo = ValueArray.get(valueArray); try (ResultSet resultSet = valueArrayTwo.getResultSet()) { csvPrinter.printRecords(resultSet); assertEquals(0, resultSet.getRow()); } } }
// Now we have complete List of CSVRecord objects in double-sorted order (first by minute-of-hour, then by date-time). // Now let's dump those back to a text file in CSV format. try ( PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( "/Users/brainydeveloper/output.csv" ) ) ) ) { final CSVPrinter printer = CSVFormat.DEFAULT.print( out ); printer.printRecords( outputList ); } } catch ( FileNotFoundException ex ) { System.out.println( "ERROR - Exception needs to be handled." ); } catch ( IOException ex ) { System.out.println( "ERROR - Exception needs to be handled." ); }
String outputFile = savePath+".csv"; CSVPrinter csvFilePrinter = null; CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader(); FileWriter fileWriter = new FileWriter(outputFile); csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat); csvFilePrinter.printRecords(excelParser.getRecords()); fileWriter.flush(); fileWriter.close(); csvFilePrinter.close();
final RowSortedTable<String, String, Double> graph = TreeBasedTable.create(); graph.put("A", "0", 0.0); graph.put("A", "1", 1.0); graph.put("B", "0", 0.1); graph.put("B", "1", 1.1); final Appendable out = new StringBuilder(); try { final CSVPrinter printer = CSVFormat.DEFAULT.print(out); printer.printRecords(// graph.rowMap().values()// .stream()// .map(x -> x.values())// .collect(Collectors.toList())); } catch (final IOException e) { e.printStackTrace(); } System.out.println(out); // 0.0,1.0 // 0.1,1.1
@SafeVarargs public final void add(T... values) { final List<T> loadedValues = loadValues(); final List<T> toAdd = Arrays.asList(values); loadedValues.removeAll(toAdd); List<T> result = new ArrayList<T>(toAdd); result.addAll(loadedValues); LinkedHashSet<T> r = new LinkedHashSet<T>(result); final List<String> collect = r.stream() .map(serializer) .map(StringEscapeUtils::escapeCsv) .collect(toList()); try { final StringWriter out = new StringWriter(); CSVFormat.DEFAULT.print(out).printRecords(collect); final String content = out.toString(); persistService.persist(persistKey, content, (s) -> s); } catch (Exception e) { LOGGER.error("Can't store suggestions for " + persistKey, e); } }
private void processAIP(ModelService model, StorageService storage, JobPluginInfo jobPluginInfo, CSVPrinter csvFilePrinter, AIP aip) { if (csvFilePrinter == null) { LOGGER.warn("CSVPrinter is NULL! Skipping..."); return; } try { if (outputDataInformation && aip.getRepresentations() != null) { List<List<String>> dataInformation = InventoryReportPluginUtils.getDataInformation(fields, aip, model, storage); csvFilePrinter.printRecords(dataInformation); } if (outputDescriptiveMetadataInformation && aip.getDescriptiveMetadata() != null) { List<List<String>> dataInformation = InventoryReportPluginUtils.getDescriptiveMetadataInformation(fields, aip, model, storage); csvFilePrinter.printRecords(dataInformation); } if (otherMetadataTypes != null && !otherMetadataTypes.isEmpty()) { for (String otherMetadataType : otherMetadataTypes) { List<List<String>> otherMetadataInformation = InventoryReportPluginUtils.getOtherMetadataInformation(fields, otherMetadataType, aip, model, storage); csvFilePrinter.printRecords(otherMetadataInformation); } } jobPluginInfo.incrementObjectsProcessedWithSuccess(); } catch (IOException e) { LOGGER.error("Error writing CSV file", e); jobPluginInfo.incrementObjectsProcessedWithFailure(); } }