Java Code Examples for org.apache.commons.csv.CSVPrinter#printRecords()

The following examples show how to use org.apache.commons.csv.CSVPrinter#printRecords() . 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: TestForm.java    From zstack with Apache License 2.0 8 votes vote down vote up
private String createCsvContent() {
    String[][] origin = {
            {"aBoolean", "anInt", "aFloat", "aDouble", "aLong", "test"},
            {"true", "2.3", "3.3", "4.3", "5.3"},
            {"True","","","","","a,b,c"},
    };

    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        OutputStreamWriter out =new OutputStreamWriter(os, Charset.forName("UTF-8"));
        CSVPrinter printer = new CSVPrinter(out, CSVFormat.RFC4180);
        printer.printRecords(origin);
        out.close();

        return Base64.getEncoder().encodeToString(os.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 2
Source File: PatientTumorLocation.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
public static void writeRecords(@NotNull String outputPath, @NotNull List<PatientTumorLocation> patientTumorLocations)
        throws IOException {
    CSVFormat format = CSVFormat.DEFAULT.withNullString(Strings.EMPTY).withHeader(Header.class);
    CSVPrinter printer = new CSVPrinter(new FileWriter(outputPath), format);
    printer.printRecords(patientTumorLocations.stream().map(PatientTumorLocation::csvRecord).collect(Collectors.toList()));
    printer.close();
}
 
Example 3
Source File: MetadataResource.java    From newsleak with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Append a list of metadata entries for one document to the temporary metadata
 * file.
 *
 * @param metadata
 *            the metadata
 */
public synchronized void appendMetadata(List<List<String>> metadata) {
	try {
		BufferedWriter writer = new BufferedWriter(new FileWriter(metadataFile, true));
		CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.RFC4180);
		csvPrinter.printRecords(metadata);
		csvPrinter.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: RDBAccess.java    From rmlmapper-java with MIT License 4 votes vote down vote up
/**
 * This method creates an CSV-formatted InputStream from a Result Set.
 * @param rs the Result Set that is used.
 * @return a CSV-formatted InputStream.
 * @throws SQLException
 */
private InputStream getCSVInputStream(ResultSet rs) throws SQLException {
    // Get number of requested columns
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();

    boolean filledInDataTypes = false;
    StringWriter writer = new StringWriter();

    try {
        CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(getCSVHeader(rsmd, columnCount)));
        printer.printRecords();

        // Extract data from result set
        while (rs.next()) {
            String[] csvRow = new String[columnCount];

            // Iterate over column names
            for (int i = 1; i <= columnCount; i++) {
                String columnName = rsmd.getColumnName(i);

                if (!filledInDataTypes) {
                    String dataType = getColumnDataType(rsmd.getColumnTypeName(i));

                    if (dataType != null) {
                        datatypes.put(columnName, dataType);
                    }
                }

                // Add value to CSV row.
                csvRow[i - 1] = rs.getString(columnName);
            }

            // Add CSV row to CSVPrinter.
            // non-varargs call
            printer.printRecord((Object[]) csvRow);
            filledInDataTypes = true;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Get InputStream from StringWriter.
    return new ByteArrayInputStream(writer.toString().getBytes());
}